Refactor: Use fn as getter for sortAsc value

Signed-off-by: Neeraj Gupta <254676+ua741@users.noreply.github.com>
This commit is contained in:
Neeraj Gupta 2023-05-26 10:04:32 +05:30
parent 4560f99574
commit 0dc5279f8b
2 changed files with 15 additions and 7 deletions

View file

@ -74,7 +74,7 @@ class CollectionPage extends StatelessWidget {
selectedFiles: _selectedFiles,
initialFiles: initialFiles,
albumName: c.collection.name,
sortOrderAsc: c.collection.pubMagicMetadata.asc ?? false,
sortAsyncFn: () => c.collection.pubMagicMetadata.asc ?? false,
);
return Scaffold(
appBar: PreferredSize(

View file

@ -25,6 +25,8 @@ typedef GalleryLoader = Future<FileLoadResult> Function(
bool? asc,
});
typedef SortAscFn = bool Function();
class Gallery extends StatefulWidget {
final GalleryLoader asyncLoader;
final List<File>? initialFiles;
@ -42,7 +44,9 @@ class Gallery extends StatefulWidget {
final Widget loadingWidget;
final bool disableScroll;
final bool limitSelectionToOne;
final bool sortOrderAsc;
// add a Function variable to get sort value in bool
final SortAscFn? sortAsyncFn;
const Gallery({
required this.asyncLoader,
@ -61,7 +65,7 @@ class Gallery extends StatefulWidget {
this.loadingWidget = const EnteLoadingWidget(),
this.disableScroll = false,
this.limitSelectionToOne = false,
this.sortOrderAsc = false,
this.sortAsyncFn,
Key? key,
}) : super(key: key);
@ -84,6 +88,7 @@ class _GalleryState extends State<Gallery> {
StreamSubscription<TabDoubleTapEvent>? _tabDoubleTapEvent;
final _forceReloadEventSubscriptions = <StreamSubscription<Event>>[];
late String _logTag;
bool _sortOrderAsc = false;
@override
void initState() {
@ -91,6 +96,7 @@ class _GalleryState extends State<Gallery> {
"Gallery_${widget.tagPrefix}${kDebugMode ? "_" + widget.albumName! : ""}";
_logger = Logger(_logTag);
_logger.finest("init Gallery");
_sortOrderAsc = widget.sortAsyncFn != null ? widget.sortAsyncFn!() : false;
_itemScroller = ItemScrollController();
if (widget.reloadEvent != null) {
_reloadEventSubscription = widget.reloadEvent!.listen((event) async {
@ -122,13 +128,15 @@ class _GalleryState extends State<Gallery> {
_forceReloadEventSubscriptions.add(
event.listen((event) async {
_logger.finest("Force refresh all files on ${event.reason}");
_sortOrderAsc =
widget.sortAsyncFn != null ? widget.sortAsyncFn!() : false;
final result = await _loadFiles();
_setFilesAndReload(result.files);
}),
);
}
}
if (widget.initialFiles != null && !widget.sortOrderAsc) {
if (widget.initialFiles != null && !_sortOrderAsc) {
_onFilesLoaded(widget.initialFiles!);
}
_loadFiles(limit: kInitialLoadLimit).then((result) async {
@ -156,7 +164,7 @@ class _GalleryState extends State<Gallery> {
galleryLoadStartTime,
galleryLoadEndTime,
limit: limit,
asc: widget.sortOrderAsc,
asc: _sortOrderAsc,
);
final endTime = DateTime.now().microsecondsSinceEpoch;
final duration = Duration(microseconds: endTime - startTime);
@ -216,7 +224,7 @@ class _GalleryState extends State<Gallery> {
disableScroll: widget.disableScroll,
emptyState: widget.emptyState,
asyncLoader: widget.asyncLoader,
sortOrderAsc: widget.sortOrderAsc,
sortOrderAsc: _sortOrderAsc,
removalEventTypes: widget.removalEventTypes,
tagPrefix: widget.tagPrefix,
scrollBottomSafeArea: widget.scrollBottomSafeArea,
@ -248,7 +256,7 @@ class _GalleryState extends State<Gallery> {
if (dailyFiles.isNotEmpty) {
collatedFiles.add(dailyFiles);
}
if (widget.sortOrderAsc) {
if (_sortOrderAsc) {
collatedFiles
.sort((a, b) => a[0].creationTime!.compareTo(b[0].creationTime!));
} else {