From f9d014c0476f301177e0c1e5fff0acd3a9475d4d Mon Sep 17 00:00:00 2001 From: Nutcake Date: Wed, 1 Nov 2023 15:10:43 +0100 Subject: [PATCH] Fix Resonite Essentials folder failing to open Closes #5 --- lib/apis/record_api.dart | 9 +++++- lib/clients/inventory_client.dart | 30 ++++++++++++++++---- lib/models/records/record.dart | 8 ++++-- lib/widgets/inventory/inventory_browser.dart | 18 ------------ 4 files changed, 38 insertions(+), 27 deletions(-) diff --git a/lib/apis/record_api.dart b/lib/apis/record_api.dart index 210d18c..35c5110 100644 --- a/lib/apis/record_api.dart +++ b/lib/apis/record_api.dart @@ -24,6 +24,13 @@ class RecordApi { return Record.fromMap(body); } + static Future getGroupRecordByPath(ApiClient client, {required String path, required String groupId}) async { + final response = await client.get("/groups/$groupId/records/$path"); + client.checkResponse(response); + final body = jsonDecode(response.body) as Map; + return Record.fromMap(body); + } + static Future> getUserRecordsAt(ApiClient client, {required String path, String? user}) async { final response = await client.get(Uri.encodeFull("/users/${user ?? client.userId}/records?path=$path")); client.checkResponse(response); @@ -32,7 +39,7 @@ class RecordApi { } static Future> getGroupRecordsAt(ApiClient client, {required String path, required String groupId}) async { - final response = await client.get("/users/$groupId/records?path=$path"); + final response = await client.get("/groups/$groupId/records?path=$path"); client.checkResponse(response); final body = jsonDecode(response.body) as List; return body.map((e) => Record.fromMap(e)).toList(); diff --git a/lib/clients/inventory_client.dart b/lib/clients/inventory_client.dart index dcf7528..c6176e1 100644 --- a/lib/clients/inventory_client.dart +++ b/lib/clients/inventory_client.dart @@ -63,7 +63,8 @@ class InventoryClient extends ChangeNotifier { Future? get directoryFuture => _currentDirectory?.then( (ResoniteDirectory value) { value.children.sort( - (ResoniteDirectory a, ResoniteDirectory b) => _sortMode.sortFunction(a.record, b.record, reverse: _sortReverse), + (ResoniteDirectory a, ResoniteDirectory b) => + _sortMode.sortFunction(a.record, b.record, reverse: _sortReverse), ); return value; }, @@ -113,10 +114,29 @@ class InventoryClient extends ChangeNotifier { ); } else { if (record.recordType == RecordType.link) { - final linkRecord = - await RecordApi.getUserRecord(apiClient, recordId: record.linkRecordId, user: record.linkOwnerId); - records = await RecordApi.getUserRecordsAt(apiClient, - path: "${linkRecord.path}\\${linkRecord.name}", user: linkRecord.ownerId); + if (record.isGroupRecord) { + final linkRecord = await RecordApi.getGroupRecordByPath( + apiClient, + path: "root/${record.path}/${record.name}", + groupId: record.linkOwnerId, + ); + records = await RecordApi.getGroupRecordsAt( + apiClient, + path: "${linkRecord.path}\\${linkRecord.name}", + groupId: linkRecord.ownerId, + ); + } else { + final linkRecord = await RecordApi.getUserRecord( + apiClient, + recordId: record.linkRecordId, + user: record.linkOwnerId, + ); + records = await RecordApi.getUserRecordsAt( + apiClient, + path: "${linkRecord.path}\\${linkRecord.name}", + user: linkRecord.ownerId, + ); + } } else { records = await RecordApi.getUserRecordsAt(apiClient, path: "${record.path}\\${record.name}", user: record.ownerId); diff --git a/lib/models/records/record.dart b/lib/models/records/record.dart index d0f15c6..174efc1 100644 --- a/lib/models/records/record.dart +++ b/lib/models/records/record.dart @@ -222,6 +222,8 @@ class Record { bool get isItem => assetUri.startsWith("resdb"); + bool get isGroupRecord => linkOwnerId.startsWith("G-"); + String get linkRecordId { if (!isLink) { throw "Record is not a link."; @@ -242,12 +244,12 @@ class Record { String ownerId = assetUri.replaceFirst("resrec:///", ""); - final lastSlashIdx = ownerId.lastIndexOf("/"); - if (lastSlashIdx == -1) { + final nextSlashIdx = ownerId.indexOf("/"); + if (nextSlashIdx == -1) { throw "Record has invalid assetUri"; } - return ownerId.substring(0, lastSlashIdx); + return ownerId.substring(0, nextSlashIdx); } Record copyWith({ diff --git a/lib/widgets/inventory/inventory_browser.dart b/lib/widgets/inventory/inventory_browser.dart index 7b03575..0a318cb 100644 --- a/lib/widgets/inventory/inventory_browser.dart +++ b/lib/widgets/inventory/inventory_browser.dart @@ -187,24 +187,6 @@ class _InventoryBrowserState extends State with AutomaticKeepA final record = objects[index]; return ObjectInventoryTile( record: record, - selected: iClient.isRecordSelected(record), - onTap: iClient.isAnyRecordSelected - ? () async { - iClient.toggleRecordSelected(record); - } - : () async { - await Navigator.push( - context, - MaterialPageRoute( - builder: (context) => PhotoView( - minScale: PhotoViewComputedScale.contained, - imageProvider: - CachedNetworkImageProvider(Aux.resdbToHttp(record.thumbnailUri)), - heroAttributes: PhotoViewHeroAttributes(tag: record.id), - ), - ), - ); - }, onLongPress: () async { iClient.toggleRecordSelected(record); },