diff --git a/lib/services/media_library_browser.dart b/lib/services/media_library_browser.dart index 471d633ef..09cc64048 100644 --- a/lib/services/media_library_browser.dart +++ b/lib/services/media_library_browser.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:io'; import 'dart:math'; @@ -36,7 +37,8 @@ class BrowserCoverProvider { static const _channel = MethodChannel('com.zarz.mornye/covers'); - /// This app's cover provider, or null where there is none. + /// This app's cover provider, or null where there is none or it could + /// not be reached. static Future load() async { if (!Platform.isAndroid) return null; try { @@ -51,9 +53,7 @@ class BrowserCoverProvider { if (key is String && value is String) key: value, }, ); - } on PlatformException { - return null; - } on MissingPluginException { + } on Object { return null; } } @@ -120,7 +120,7 @@ class MediaLibraryBrowser { final CoverStore? _coverStore; final Future Function() _loadCoverProvider; - Future? _coverProviderLoad; + Future? _coverProviderLoad; BrowserCoverProvider? _coverProvider; CoverStore get _covers => _coverStore ?? CoverStore.instance; @@ -167,8 +167,12 @@ class MediaLibraryBrowser { String parentId, [ Map? options, ]) async { + if (parentId == AudioService.browsableRootId) { + // The tabs have no artwork; the lists opened from them next do. + unawaited(_prepareCovers()); + return _tabs(); + } await _prepareCovers(); - if (parentId == AudioService.browsableRootId) return _tabs(); final uri = _parse(parentId); if (uri == null) return const []; final query = uri.queryParameters; @@ -222,12 +226,25 @@ class MediaLibraryBrowser { MediaItem _shuffleItem() => MediaItem(id: shuffleId, title: strings().nowPlayingShuffleLibrary); - /// Finds the cover provider once and reads which network covers are - /// stored, so the items built next can show the app's cover files. + /// Finds the cover provider and reads which network covers are stored, + /// so the items built next can show the app's cover files. Only a found + /// provider is kept: without one, as after an error, those items show no + /// local covers and the next browse looks again. Future _prepareCovers() async { - final provider = _coverProvider = await (_coverProviderLoad ??= - _loadCoverProvider()); - if (provider != null) await _covers.warmUp(); + if (_coverProvider == null) { + final load = _coverProviderLoad ??= _findCoverProvider(); + await load; + if (identical(_coverProviderLoad, load)) _coverProviderLoad = null; + } + if (_coverProvider != null) await _covers.warmUp(); + } + + Future _findCoverProvider() async { + try { + _coverProvider = await _loadCoverProvider(); + } on Object { + _coverProvider = null; + } } /// Artwork a car can load: a web cover as it is, and a cover file of the diff --git a/test/media_library_browser_test.dart b/test/media_library_browser_test.dart index 1b7c3cef6..3e389fbfb 100644 --- a/test/media_library_browser_test.dart +++ b/test/media_library_browser_test.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:io'; import 'package:audio_service/audio_service.dart'; @@ -514,6 +515,52 @@ void main() { ); }); + test('looks for the provider again after it was not found', () async { + final answers = [StateError('channel'), null, provider()]; + var loads = 0; + final browser = _browser( + library: _Library(albums: [album('Path', '$library/a.jpg')]), + covers: covers, + coverProvider: () async { + final answer = answers[loads++]; + if (answer is Error) throw answer; + return answer as BrowserCoverProvider?; + }, + ); + Future shown() async => (await browser.children( + MediaLibraryBrowser.albumsId, + )).single.artUri?.toString(); + + expect(await shown(), isNull); + expect(await shown(), isNull); + expect(await shown(), cover('library_covers', 'a.jpg')); + expect(await shown(), cover('library_covers', 'a.jpg')); + expect(loads, 3); + }); + + test('the tabs do not wait for the provider', () async { + final found = Completer(); + var loads = 0; + final browser = _browser( + library: _Library(albums: [album('Path', '$library/a.jpg')]), + covers: covers, + coverProvider: () { + loads++; + return found.future; + }, + ); + + final tabs = await browser.children(AudioService.browsableRootId); + expect(_titles(tabs), ['Songs', 'Albums', 'Artists', 'Playlists']); + final albums = browser.children(MediaLibraryBrowser.albumsId); + found.complete(provider()); + expect( + (await albums).single.artUri.toString(), + cover('library_covers', 'a.jpg'), + ); + expect(loads, 1); + }); + test('shows the file the store keeps for a network cover', () async { const stored = 'network://connection/covers/a.jpg'; const song = 'network://connection/music/song.flac';