Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Tighten memory music implementation

ashilkn3792c50d97f6
diff --git a/mobile/apps/photos/lib/services/memories/just_audio_memory_music_player.dart b/mobile/apps/photos/lib/services/memories/just_audio_memory_music_player.dart
index 1960f7ea78..2ac672556d 100644
--- a/mobile/apps/photos/lib/services/memories/just_audio_memory_music_player.dart
+++ b/mobile/apps/photos/lib/services/memories/just_audio_memory_music_player.dart
@@ -51,11 +51,9 @@ class JustAudioMemoryMusicPlayer implements MemoryMusicPlayer {
}
rethrow;
}
+ await _player.setLoopMode(LoopMode.one);
}
- @override
- Future<void> setLooping() => _player.setLoopMode(LoopMode.one);
-
@override
Future<void> play() {
final generation = ++_fadeGeneration;
@@ -96,8 +94,7 @@ class JustAudioMemoryMusicPlayer implements MemoryMusicPlayer {
@override
Future<void> dispose() async {
await pauseImmediately();
- final generation = ++_fadeGeneration;
- await _enqueueFade(generation, _player.dispose);
+ await _enqueueFade(++_fadeGeneration, _player.dispose);
}
Future<void> _fadeTo(
diff --git a/mobile/apps/photos/lib/services/memories/memory_music_controller.dart b/mobile/apps/photos/lib/services/memories/memory_music_controller.dart
index c600d8469c..d21bca33e0 100644
--- a/mobile/apps/photos/lib/services/memories/memory_music_controller.dart
+++ b/mobile/apps/photos/lib/services/memories/memory_music_controller.dart
@@ -53,9 +53,7 @@ class MemoryMusicController extends ChangeNotifier {
_MemoryMusicPauseReason.videoItem,
currentItemIsVideo,
);
- if (sameMemory &&
- (_status == _MemoryMusicPlaybackStatus.ready ||
- _status == _MemoryMusicPlaybackStatus.loading)) {
+ if (sameMemory && _status != _MemoryMusicPlaybackStatus.idle) {
if (itemPauseChanged) await _synchronizePlayback();
return;
}
@@ -155,8 +153,6 @@ class MemoryMusicController extends ChangeNotifier {
if (!_isCurrentLoad(generation)) return;
await _player.load(track);
if (!_isCurrentLoad(generation)) return;
- await _player.setLooping();
- if (!_isCurrentLoad(generation)) return;
_status = _MemoryMusicPlaybackStatus.ready;
await _synchronizePlayback();
}
diff --git a/mobile/apps/photos/lib/services/memories/memory_music_player.dart b/mobile/apps/photos/lib/services/memories/memory_music_player.dart
index 2b0b31196e..10bbe486d2 100644
--- a/mobile/apps/photos/lib/services/memories/memory_music_player.dart
+++ b/mobile/apps/photos/lib/services/memories/memory_music_player.dart
@@ -5,8 +5,6 @@ abstract interface class MemoryMusicPlayer {
Future<void> load(MemoryMusicTrack track);
- Future<void> setLooping();
-
Future<void> play();
Future<void> playImmediately();
diff --git a/mobile/apps/photos/lib/services/memories/memory_music_service.dart b/mobile/apps/photos/lib/services/memories/memory_music_service.dart
index 3b2658d153..85e248f269 100644
--- a/mobile/apps/photos/lib/services/memories/memory_music_service.dart
+++ b/mobile/apps/photos/lib/services/memories/memory_music_service.dart
@@ -16,7 +16,29 @@ String memoryMusicCacheFileName(String url) =>
class MemoryMusicService {
static final _logger = Logger("MemoryMusicService");
- static MemoryMusicService? _instance;
+ static final instance = MemoryMusicService(
+ preferences: ServiceLocator.instance.prefs,
+ fetchManifest: () async {
+ final response = await ServiceLocator.instance.nonEnteDio.get<Object?>(
+ _manifestURL,
+ );
+ return response.data;
+ },
+ hasAsset: (url) => RemoteAssetsService.instance.hasAsset(
+ url,
+ cacheFileName: memoryMusicCacheFileName(url),
+ ),
+ cacheAsset: (url) async {
+ await RemoteAssetsService.instance.getAsset(
+ url,
+ cacheFileName: memoryMusicCacheFileName(url),
+ );
+ },
+ deleteAsset: (url) => RemoteAssetsService.instance.deleteAsset(
+ url,
+ cacheFileName: memoryMusicCacheFileName(url),
+ ),
+ );
final SharedPreferences _preferences;
final Future<Object?> Function() _fetchManifest;
@@ -42,41 +64,8 @@ class MemoryMusicService {
_cacheAsset = cacheAsset,
_deleteAsset = deleteAsset;
- static MemoryMusicService get instance => _instance ??= MemoryMusicService(
- preferences: ServiceLocator.instance.prefs,
- fetchManifest: () async {
- final response = await ServiceLocator.instance.nonEnteDio.get<Object?>(
- _manifestURL,
- );
- return response.data;
- },
- hasAsset: (url) => RemoteAssetsService.instance.hasAsset(
- url,
- cacheFileName: memoryMusicCacheFileName(url),
- ),
- cacheAsset: (url) async {
- await RemoteAssetsService.instance.getAsset(
- url,
- cacheFileName: memoryMusicCacheFileName(url),
- );
- },
- deleteAsset: (url) => RemoteAssetsService.instance.deleteAsset(
- url,
- cacheFileName: memoryMusicCacheFileName(url),
- ),
- );
-
- Future<List<MemoryMusicTrack>> prepare() {
- final preparation = _preparation;
- if (preparation != null) return preparation;
-
- late final Future<List<MemoryMusicTrack>> nextPreparation;
- nextPreparation = _prepare().whenComplete(() {
- if (identical(_preparation, nextPreparation)) _preparation = null;
- });
- _preparation = nextPreparation;
- return nextPreparation;
- }
+ Future<List<MemoryMusicTrack>> prepare() =>
+ _preparation ??= _prepare().whenComplete(() => _preparation = null);
Future<List<MemoryMusicTrack>> _prepare() async {
_tracks ??= _readCachedManifest();
@@ -179,7 +168,9 @@ class MemoryMusicService {
if (!_availableURLs.contains(url)) _downloadAsset(url),
];
if (_availableURLs.isEmpty && downloads.isNotEmpty) {
- await _waitForAvailableAsset(downloads);
+ await Stream.fromFutures(
+ downloads,
+ ).firstWhere((didDownload) => didDownload, orElse: () => false);
}
return tracks
@@ -199,19 +190,10 @@ class MemoryMusicService {
}
}
- Future<bool> _downloadAsset(String url) {
- final download = _assetDownloads[url];
- if (download != null) return download;
-
- late final Future<bool> nextDownload;
- nextDownload = _cacheAssetURL(url).whenComplete(() {
- if (identical(_assetDownloads[url], nextDownload)) {
+ Future<bool> _downloadAsset(String url) =>
+ _assetDownloads[url] ??= _cacheAssetURL(url).whenComplete(() {
_assetDownloads.remove(url);
- }
- });
- _assetDownloads[url] = nextDownload;
- return nextDownload;
- }
+ });
Future<bool> _cacheAssetURL(String url) async {
try {
@@ -232,22 +214,6 @@ class MemoryMusicService {
return true;
}
- Future<void> _waitForAvailableAsset(List<Future<bool>> downloads) {
- final available = Completer<void>();
- var remaining = downloads.length;
- for (final download in downloads) {
- unawaited(
- download.then((didDownload) {
- remaining--;
- if (!available.isCompleted && (didDownload || remaining == 0)) {
- available.complete();
- }
- }),
- );
- }
- return available.future;
- }
-
List<MemoryMusicTrack> _parseManifest(Object? manifest) {
if (manifest is! Map<String, dynamic>) {
throw const FormatException("Memory music manifest must be an object");
diff --git a/mobile/apps/photos/lib/services/remote_assets_service.dart b/mobile/apps/photos/lib/services/remote_assets_service.dart
index 759d2a8932..5f84e146ba 100644
--- a/mobile/apps/photos/lib/services/remote_assets_service.dart
+++ b/mobile/apps/photos/lib/services/remote_assets_service.dart
@@ -124,7 +124,7 @@ class RemoteAssetsService {
});
}
- Future<bool> hasAsset(String remotePath, {String? cacheFileName}) async {
+ Future<bool> hasAsset(String remotePath, {String? cacheFileName}) {
return _lockFor(remotePath).synchronized(() async {
final path = await _getLocalPath(
remotePath,
diff --git a/mobile/apps/photos/lib/ui/home/memories/memory_music_session.dart b/mobile/apps/photos/lib/ui/home/memories/memory_music_session.dart
index e7ba022837..ef4dbd214e 100644
--- a/mobile/apps/photos/lib/ui/home/memories/memory_music_session.dart
+++ b/mobile/apps/photos/lib/ui/home/memories/memory_music_session.dart
@@ -27,7 +27,6 @@ class _MemoryMusicSessionState extends State<MemoryMusicSession>
static final _logger = Logger("MemoryMusicSession");
MemoryMusicController? _controller;
- bool _isAppActive = true;
late bool _isMuted;
@override
@@ -35,9 +34,6 @@ class _MemoryMusicSessionState extends State<MemoryMusicSession>
super.initState();
_isMuted = localSettings.isMemoriesAudioMuted();
WidgetsBinding.instance.addObserver(this);
- final lifecycleState = WidgetsBinding.instance.lifecycleState;
- _isAppActive =
- lifecycleState == null || lifecycleState == AppLifecycleState.resumed;
unawaited(_initialize());
}
@@ -55,7 +51,10 @@ class _MemoryMusicSessionState extends State<MemoryMusicSession>
player: JustAudioMemoryMusicPlayer(),
tracks: tracks,
);
- if (!_isAppActive) unawaited(controller.setAppActive(false));
+ final lifecycleState = WidgetsBinding.instance.lifecycleState;
+ if (lifecycleState != null && lifecycleState != AppLifecycleState.resumed) {
+ unawaited(controller.setAppActive(false));
+ }
setState(() => _controller = controller);
}
@@ -81,10 +80,9 @@ class _MemoryMusicSessionState extends State<MemoryMusicSession>
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
- _isAppActive = state == AppLifecycleState.resumed;
final controller = _controller;
if (controller != null) {
- unawaited(controller.setAppActive(_isAppActive));
+ unawaited(controller.setAppActive(state == AppLifecycleState.resumed));
}
}
@@ -108,24 +106,21 @@ class _MemoryMusicSessionState extends State<MemoryMusicSession>
class MemoryMusicScope extends InheritedNotifier<MemoryMusicController> {
final bool _isMuted;
- final Future<void> Function() _toggleMuted;
+ final Future<void> Function() toggleMuted;
const MemoryMusicScope({
required MemoryMusicController? controller,
required bool isMuted,
- required Future<void> Function() toggleMuted,
+ required this.toggleMuted,
required super.child,
super.key,
}) : _isMuted = isMuted,
- _toggleMuted = toggleMuted,
super(notifier: controller);
MemoryMusicController? get controller => notifier;
bool get isMuted => notifier?.isMuted ?? _isMuted;
- Future<void> toggleMuted() => _toggleMuted();
-
static MemoryMusicScope? maybeOf(BuildContext context, {bool listen = true}) {
return listen
? context.dependOnInheritedWidgetOfExactType<MemoryMusicScope>()
diff --git a/mobile/apps/photos/test/services/memories/memory_music_controller_test.dart b/mobile/apps/photos/test/services/memories/memory_music_controller_test.dart
index a034758b81..c5a9616517 100644
--- a/mobile/apps/photos/test/services/memories/memory_music_controller_test.dart
+++ b/mobile/apps/photos/test/services/memories/memory_music_controller_test.dart
@@ -41,12 +41,18 @@ void main() {
controller.dispose();
});
- test("activating a photo memory loads, loops, and plays its track", () async {
+ test("photo memories load, loop, and play their assigned tracks", () async {
await controller.activateMemory("memory-1", currentItemIsVideo: false);
- expect(player.loadedTracks.map((track) => track.id), <String>["track-1"]);
expect(player.looping, isTrue);
expect(player.playing, isTrue);
+
+ await controller.activateMemory("memory-2", currentItemIsVideo: false);
+
+ expect(player.loadedTracks.map((track) => track.id), <String>[
+ "track-1",
+ "track-2",
+ ]);
});
test("video items pause music and photos resume it", () async {
@@ -99,16 +105,6 @@ void main() {
expect(player.playing, isTrue);
});
- test("changing memories loads the assigned track", () async {
- await controller.activateMemory("memory-1", currentItemIsVideo: false);
- await controller.activateMemory("memory-2", currentItemIsVideo: false);
-
- expect(player.loadedTracks.map((track) => track.id), <String>[
- "track-1",
- "track-2",
- ]);
- });
-
test("newer memory wins when activations overlap", () async {
final firstLoadStarted = Completer<void>();
final releaseFirstLoad = Completer<void>();
@@ -190,10 +186,6 @@ class _FakeMemoryMusicPlayer implements MemoryMusicPlayer {
final beforeCompleting = beforeNextLoadCompletes;
beforeNextLoadCompletes = null;
await beforeCompleting?.call();
- }
-
- @override
- Future<void> setLooping() async {
looping = true;
}
diff --git a/mobile/apps/photos/test/services/memories/memory_music_selector_test.dart b/mobile/apps/photos/test/services/memories/memory_music_selector_test.dart
index abe1d9ff18..13cb56dadb 100644
--- a/mobile/apps/photos/test/services/memories/memory_music_selector_test.dart
+++ b/mobile/apps/photos/test/services/memories/memory_music_selector_test.dart
@@ -7,7 +7,7 @@ void main() {
MemoryMusicTrack(id: "track-1", url: "https://example.com/track-1.mp3"),
MemoryMusicTrack(id: "track-2", url: "https://example.com/track-2.mp3"),
];
- const memoryIDs = <String>["memory-a", "memory-b", "memory-c", "memory-d"];
+ const memoryIDs = <String>["memory-d", "memory-e"];
test("assigns stable tracks without adjacent repeats", () {
final assignments = assignMemoryMusicTracks(
@@ -16,10 +16,8 @@ void main() {
);
expect(memoryIDs.map((memoryID) => assignments[memoryID]!.id), <String>[
- "track-2",
"track-1",
"track-2",
- "track-1",
]);
});
diff --git a/mobile/apps/photos/test/services/memories/memory_music_service_test.dart b/mobile/apps/photos/test/services/memories/memory_music_service_test.dart
index b6195a960b..863c42b9f7 100644
--- a/mobile/apps/photos/test/services/memories/memory_music_service_test.dart
+++ b/mobile/apps/photos/test/services/memories/memory_music_service_test.dart
@@ -44,7 +44,6 @@ void main() {
expect(tracks.first.artist, "Artist");
expect(tracks.last.title, "Zed");
expect(checkedURLs, unorderedEquals([_sharedURL, _newURL]));
- expect(checkedURLs.where((url) => url == _sharedURL), hasLength(1));
expect(
jsonDecode(preferences.getString(_manifestPreferenceKey)!),
manifest,
@@ -130,25 +129,18 @@ void main() {
]),
),
);
- final availableURLs = <String>[];
final deletedURLs = <String>[];
final emptyManifest = _manifest([]);
final service = _service(
preferences,
fetchManifest: () async => emptyManifest,
- hasAsset: (url) async {
- availableURLs.add(url);
- return true;
- },
deleteAsset: (url) async => deletedURLs.add(url),
);
final tracks = await service.prepare();
expect(tracks, isEmpty);
- expect(availableURLs, isEmpty);
expect(deletedURLs, unorderedEquals([_cachedURL, _sharedURL]));
- expect(deletedURLs.where((url) => url == _cachedURL), hasLength(1));
expect(
jsonDecode(preferences.getString(_manifestPreferenceKey)!),
emptyManifest,
@@ -165,7 +157,6 @@ void main() {
]),
),
);
- final availableURLs = <String>[];
final deletedURLs = <String>[];
final service = _service(
preferences,
@@ -173,17 +164,12 @@ void main() {
_track("new-shared-id", _sharedURL),
_track("new", _newURL),
]),
- hasAsset: (url) async {
- availableURLs.add(url);
- return true;
- },
deleteAsset: (url) async => deletedURLs.add(url),
);
final tracks = await service.prepare();
expect(tracks.map((track) => track.id), ["new", "new-shared-id"]);
- expect(availableURLs, unorderedEquals([_newURL, _sharedURL]));
expect(deletedURLs, [_cachedURL]);
});
@@ -216,7 +202,6 @@ void main() {
);
finishDownload.complete();
- await Future<void>.delayed(Duration.zero);
});
test("returns after the first download succeeds", () async {
@@ -250,7 +235,6 @@ void main() {
);
finishSlowDownload.complete();
- await Future<void>.delayed(Duration.zero);
});
test("retries failed downloads without refetching the manifest", () async {
@@ -296,7 +280,6 @@ void main() {
await fetchStarted.future;
final secondPreparation = service.prepare();
- expect(identical(firstPreparation, secondPreparation), isTrue);
expect(fetchCalls, 1);
finishFetch.complete();