diff --git a/lib/model/source/album.dart b/lib/model/source/album.dart index 65a5eb908..6099639a6 100644 --- a/lib/model/source/album.dart +++ b/lib/model/source/album.dart @@ -10,7 +10,7 @@ import 'package:collection/collection.dart'; import 'package:flutter/widgets.dart'; mixin AlbumMixin on SourceBase { - final Set _directories = {}; + final Set _directories = {}; final Set _newAlbums = {}; List get rawAlbums => List.unmodifiable(_directories); @@ -68,7 +68,7 @@ mixin AlbumMixin on SourceBase { void addDirectories({required Set albums, bool notify = true}) { if (!_directories.containsAll(albums)) { - _directories.addAll(albums); + _directories.addAll(albums.whereNotNull()); _onAlbumChanged(notify: notify); } } diff --git a/lib/widgets/aves_app.dart b/lib/widgets/aves_app.dart index 602be743f..86510c002 100644 --- a/lib/widgets/aves_app.dart +++ b/lib/widgets/aves_app.dart @@ -104,11 +104,12 @@ class AvesApp extends StatefulWidget { class _AvesAppState extends State with WidgetsBindingObserver { final ValueNotifier appModeNotifier = ValueNotifier(AppMode.main); - late Future _appSetup; - late Future _dynamicColorPaletteLoader; - final _mediaStoreSource = MediaStoreSource(); + late final Future _appSetup; + late final Size _screenSize; + late final Future _dynamicColorPaletteLoader; + final CollectionSource _mediaStoreSource = MediaStoreSource(); final Debouncer _mediaStoreChangeDebouncer = Debouncer(delay: Durations.mediaContentChangeDebounceDelay); - final Set changedUris = {}; + final Set _changedUris = {}; // observers are not registered when using the same list object with different items // the list itself needs to be reassigned @@ -125,6 +126,8 @@ class _AvesAppState extends State with WidgetsBindingObserver { super.initState(); EquatableConfig.stringify = true; _appSetup = _setup(); + // remember screen size to use it later, when `context` and `window` are no longer reliable + _screenSize = window.physicalSize / window.devicePixelRatio; _dynamicColorPaletteLoader = DynamicColorPlugin.getCorePalette(); _mediaStoreChangeChannel.receiveBroadcastStream().listen((event) => _onMediaStoreChange(event as String?)); _newIntentChannel.receiveBroadcastStream().listen((event) => _onNewIntent(event as Map?)); @@ -293,20 +296,13 @@ class _AvesAppState extends State with WidgetsBindingObserver { if (!settings.initialized) return; final stopwatch = Stopwatch()..start(); - final Size screenSize; - try { - screenSize = window.physicalSize / window.devicePixelRatio; - } catch (error) { - // view may no longer be usable - return; - } var tileExtent = settings.getTileExtent(CollectionPage.routeName); if (tileExtent == 0) { - tileExtent = screenSize.shortestSide / CollectionGrid.columnCountDefault; + tileExtent = _screenSize.shortestSide / CollectionGrid.columnCountDefault; } - final rows = (screenSize.height / tileExtent).ceil(); - final columns = (screenSize.width / tileExtent).ceil(); + final rows = (_screenSize.height / tileExtent).ceil(); + final columns = (_screenSize.width / tileExtent).ceil(); final count = rows * columns; final collection = CollectionLens(source: _mediaStoreSource, listenToSource: false); settings.topEntryIds = collection.sortedEntries.take(count).map((entry) => entry.id).toList(); @@ -405,14 +401,14 @@ class _AvesAppState extends State with WidgetsBindingObserver { } void _onMediaStoreChange(String? uri) { - if (uri != null) changedUris.add(uri); - if (changedUris.isNotEmpty) { + if (uri != null) _changedUris.add(uri); + if (_changedUris.isNotEmpty) { _mediaStoreChangeDebouncer(() async { - final todo = changedUris.toSet(); - changedUris.clear(); + final todo = _changedUris.toSet(); + _changedUris.clear(); final tempUris = await _mediaStoreSource.refreshUris(todo); if (tempUris.isNotEmpty) { - changedUris.addAll(tempUris); + _changedUris.addAll(tempUris); _onMediaStoreChange(null); } });