From b1386b8f574cbb13085a39f4ab94919812784362 Mon Sep 17 00:00:00 2001 From: ashilkn Date: Thu, 3 Jul 2025 10:37:59 +0530 Subject: [PATCH] Extract group building code to a function for better readability --- .../lib/models/gallery/gallery_sections.dart | 94 +++++++++++-------- 1 file changed, 53 insertions(+), 41 deletions(-) diff --git a/mobile/lib/models/gallery/gallery_sections.dart b/mobile/lib/models/gallery/gallery_sections.dart index 214b90ebf9..ddfee9da01 100644 --- a/mobile/lib/models/gallery/gallery_sections.dart +++ b/mobile/lib/models/gallery/gallery_sections.dart @@ -38,61 +38,36 @@ class GallerySections { init(); } - final List _groupIDs = []; - final Map> _groupIDToFilesMap = {}; - final Map _groupIdToheaderDataMap = {}; + final List _groupIds = []; + final Map> _groupIdToFilesMap = {}; + final Map _groupIdToHeaderDataMap = {}; late final int crossAxisCount; final currentUserID = Configuration.instance.getUserID(); + static const double spacing = 2.0; - List get groupIDs => _groupIDs; - Map> get groupIDToFilesMap => _groupIDToFilesMap; + List get groupIDs => _groupIds; + Map> get groupIDToFilesMap => _groupIdToFilesMap; Map get groupIdToheaderDataMap => - _groupIdToheaderDataMap; + _groupIdToHeaderDataMap; final _uuid = const Uuid(); void init() { - List dailyFiles = []; - for (int index = 0; index < allFiles.length; index++) { - if (index > 0 && - !groupType.areFromSameGroup(allFiles[index - 1], allFiles[index])) { - _createNewGroup(dailyFiles); - dailyFiles = []; - } - dailyFiles.add(allFiles[index]); - } - if (dailyFiles.isNotEmpty) { - _createNewGroup(dailyFiles); - } - + _buildGroups(); crossAxisCount = localSettings.getPhotoGridSize(); } - void _createNewGroup( - List dailyFiles, - ) { - final uuid = _uuid.v1(); - _groupIDs.add(uuid); - _groupIDToFilesMap[uuid] = dailyFiles; - _groupIdToheaderDataMap[uuid] = GroupHeaderData( - title: groupType.getTitle( - context, - dailyFiles.first, - lastFile: dailyFiles.last, - ), - groupType: groupType, - ); - } - List getSectionLayouts() { int currentIndex = 0; double currentOffset = 0.0; final tileHeight = widthAvailable / crossAxisCount; final sectionLayouts = []; + final groupIDs = _groupIdToFilesMap.keys; + // TODO: spacing - for (final key in _groupIDToFilesMap.keys) { - final filesInGroup = _groupIDToFilesMap[key]!; + for (final groupID in groupIDs) { + final filesInGroup = _groupIdToFilesMap[groupID]!; final numberOfGridRows = (filesInGroup.length / crossAxisCount).ceil(); final firstIndex = currentIndex == 0 ? currentIndex : currentIndex + 1; final lastIndex = firstIndex + numberOfGridRows; @@ -109,11 +84,11 @@ class GallerySections { maxOffset: maxOffset, headerExtent: headerExtent, tileHeight: tileHeight, - spacing: 0, + spacing: spacing, builder: (context, index) { if (index == firstIndex) { return GroupHeaderWidget( - title: _groupIdToheaderDataMap[key]!.title, + title: _groupIdToHeaderDataMap[groupID]!.title, gridSize: crossAxisCount, ); } else { @@ -138,8 +113,7 @@ class GallerySections { return FixedExtentGridRow( width: tileHeight, height: tileHeight, - //TODO: spacing - spacing: 0, + spacing: spacing, textDirection: TextDirection.ltr, children: gridRowChildren, ); @@ -148,11 +122,49 @@ class GallerySections { ), ); currentIndex = lastIndex; + + // Adding this crashes the app?????? + + // if (groupID != groupIDs.last) { + // // lastIndex - (firstIndex + 1) - 1 + // currentOffset = maxOffset + (lastIndex - firstIndex) * spacing; + // } currentOffset = maxOffset; } return sectionLayouts; } + + void _buildGroups() { + List dailyFiles = []; + for (int index = 0; index < allFiles.length; index++) { + if (index > 0 && + !groupType.areFromSameGroup(allFiles[index - 1], allFiles[index])) { + _createNewGroup(dailyFiles); + dailyFiles = []; + } + dailyFiles.add(allFiles[index]); + } + if (dailyFiles.isNotEmpty) { + _createNewGroup(dailyFiles); + } + } + + void _createNewGroup( + List dailyFiles, + ) { + final uuid = _uuid.v1(); + _groupIds.add(uuid); + _groupIdToFilesMap[uuid] = dailyFiles; + _groupIdToHeaderDataMap[uuid] = GroupHeaderData( + title: groupType.getTitle( + context, + dailyFiles.first, + lastFile: dailyFiles.last, + ), + groupType: groupType, + ); + } } class GroupHeaderData {