Cache collectionFileCount to avoid UI flicker post heroAnimation

This commit is contained in:
Neeraj Gupta 2023-07-06 13:26:38 +05:30
parent 1831ed4bdd
commit baa2138d1f
3 changed files with 30 additions and 6 deletions

View file

@ -58,6 +58,7 @@ class CollectionsService {
Future<Map<int, int>>? _collectionIDToNewestFileTime;
Collection? cachedUncategorizedCollection;
final Map<String, File> _coverCache = <String, File>{};
final Map<int, int> _countCache = <int, int>{};
CollectionsService._privateConstructor() {
_db = CollectionsDB.instance;
@ -83,6 +84,7 @@ class CollectionsService {
_coverCache.removeWhere(
(key, value) => key.startsWith(event.collectionID!.toString()),
);
_countCache.remove(event.collectionID);
}
});
}
@ -252,6 +254,20 @@ class CollectionsService {
return null;
}
Future<int> getFileCount(Collection c) async {
if (_countCache.containsKey(c.id)) {
return _countCache[c.id]!;
} else {
final count = await _filesDB.collectionFileCount(c.id);
_countCache[c.id] = count;
return count;
}
}
int? getCachedFileCount(Collection c) {
return _countCache[c.id];
}
Future<bool> setCollectionSyncTime(int collectionID, int? time) async {
final key = _collectionSyncTimeKeyPrefix + collectionID.toString();
if (time == null) {

View file

@ -65,9 +65,8 @@ class AlbumColumnItemWidget extends StatelessWidget {
children: [
Text(collection.displayName),
FutureBuilder<int>(
future: FilesDB.instance.collectionFileCount(
collection.id,
),
future: CollectionsService.instance.getFileCount
(collection),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(

View file

@ -128,12 +128,21 @@ class AlbumRowItemWidget extends StatelessWidget {
width: sideOfThumbnail,
child: FutureBuilder<int>(
future: showFileCount
? FilesDB.instance.collectionFileCount(c.id)
? CollectionsService.instance.getFileCount(c)
: Future.value(0),
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data! > 0) {
int? cachedCount;
if (showFileCount) {
if (snapshot.hasData) {
cachedCount = snapshot.data;
} else {
cachedCount =
CollectionsService.instance.getCachedFileCount(c);
}
}
if (cachedCount != null && cachedCount > 0) {
final String textCount =
NumberFormat().format(snapshot.data);
NumberFormat().format(cachedCount);
return Row(
children: [
Container(