From 40e55df0d446ad9a1a32bc66965447aec384bc3e Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Fri, 30 Oct 2026 18:41:55 +0700 Subject: [PATCH] fix(player): look for the cover provider again after a failure The browser kept the first cover provider lookup for the life of the process. BrowserCoverProvider.load caught only platform errors, so any other error stayed in the cached future and made every later children and search call throw; a lookup that answered null left local covers out until the process ended. load now answers null on any error, and the browser keeps only a found provider: a call without one shows no local covers and the next browse looks again (concurrent calls still share one lookup). The root tabs no longer wait for the lookup and the store warm-up, which they do not need; they start it so the list opened next finds it ready. --- lib/services/media_library_browser.dart | 39 ++++++++++++++------ test/media_library_browser_test.dart | 47 +++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 11 deletions(-) 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';