-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: get notified about cover store changes (#1011)
- Loading branch information
1 parent
5b35fe3
commit 316adf5
Showing
17 changed files
with
324 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import 'local_cover_service.dart'; | ||
import 'dart:async'; | ||
import 'dart:typed_data'; | ||
import 'package:safe_change_notifier/safe_change_notifier.dart'; | ||
|
||
class LocalCoverModel extends SafeChangeNotifier { | ||
LocalCoverModel({ | ||
required LocalCoverService localCoverService, | ||
}) : _localCoverService = localCoverService; | ||
|
||
final LocalCoverService _localCoverService; | ||
StreamSubscription<bool>? _propertiesChangedSub; | ||
|
||
int get storeLength => _localCoverService.storeLength; | ||
Uint8List? get(String? albumId) => _localCoverService.get(albumId); | ||
|
||
Future<Uint8List?> getCover({ | ||
required String albumId, | ||
required String path, | ||
}) async => | ||
_localCoverService.getCover(albumId: albumId, path: path); | ||
|
||
void init() => _propertiesChangedSub ??= | ||
_localCoverService.propertiesChanged.listen((_) => notifyListeners()); | ||
|
||
@override | ||
Future<void> dispose() async { | ||
await _propertiesChangedSub?.cancel(); | ||
super.dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import 'dart:async'; | ||
import 'dart:io'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:audio_metadata_reader/audio_metadata_reader.dart'; | ||
import 'package:collection/collection.dart'; | ||
|
||
import '../compute_isolate.dart'; | ||
import '../constants.dart'; | ||
import '../persistence_utils.dart'; | ||
|
||
class LocalCoverService { | ||
final _propertiesChangedController = StreamController<bool>.broadcast(); | ||
Stream<bool> get propertiesChanged => _propertiesChangedController.stream; | ||
|
||
var _store = <String, Uint8List?>{}; | ||
int get storeLength => _store.length; | ||
|
||
Future<Uint8List?> getCover({ | ||
required String albumId, | ||
required String path, | ||
}) async { | ||
if (albumId.isNotEmpty == true) { | ||
final metadata = await readMetadata(File(path), getImage: true); | ||
final cover = _put( | ||
albumId: albumId, | ||
data: metadata.pictures | ||
.firstWhereOrNull((e) => e.bytes.isNotEmpty) | ||
?.bytes, | ||
); | ||
if (cover != null) { | ||
_propertiesChangedController.add(true); | ||
} | ||
return cover; | ||
} | ||
return null; | ||
} | ||
|
||
Uint8List? _put({required String albumId, Uint8List? data}) { | ||
return _store.containsKey(albumId) | ||
? _store.update(albumId, (value) => data) | ||
: _store.putIfAbsent(albumId, () => data); | ||
} | ||
|
||
Uint8List? get(String? albumId) => albumId == null ? null : _store[albumId]; | ||
|
||
Future<void> write() async => writeUint8ListMap(_store, kCoverStore); | ||
|
||
// This does not make much sense except for small libs, where the store is filled | ||
// fast anyways. Let's keep it for eventual use... | ||
Future<void> read() async => | ||
_store = await computeIsolate(() => readUint8ListMap(kCoverStore)) ?? []; | ||
|
||
Future<void> dispose() async => _propertiesChangedController.close(); | ||
} |
Oops, something went wrong.