mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-11-21 20:00:49 +01:00
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.
This commit is contained in:
1 parent
7d5bbd306b
commit
40e55df0d4
2 files changed
+75
-11
No files matched your search
@@ -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<BrowserCoverProvider?> 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<BrowserCoverProvider?> Function() _loadCoverProvider;
|
||||
Future<BrowserCoverProvider?>? _coverProviderLoad;
|
||||
Future<void>? _coverProviderLoad;
|
||||
BrowserCoverProvider? _coverProvider;
|
||||
|
||||
CoverStore get _covers => _coverStore ?? CoverStore.instance;
|
||||
@@ -167,8 +167,12 @@ class MediaLibraryBrowser {
|
||||
String parentId, [
|
||||
Map<String, dynamic>? 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<void> _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<void> _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
|
||||
|
||||
@@ -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 = <Object?>[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<String?> 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<BrowserCoverProvider?>();
|
||||
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';
|
||||
|
||||
Reference in new issue
Block a user