From d41731fb69b61c552f06a2fcdbca64e8b7c8d9e6 Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Sun, 25 Oct 2026 14:13:37 +0700 Subject: [PATCH] fix(link): keep the notification on the desktop's song in the background The phone closed its session with the desktop in the background, so Next and Previous from the notification reached the desktop but the song it moved to never came back, and the notification kept the old one (Pause looked right only because it is shown before the desktop answers). The session now stays connected in the background while the desktop plays in the phone's place. --- lib/main.dart | 4 +++ .../link/client/link_client_controller.dart | 30 +++++++++++++++---- lib/services/link/link_client_api.dart | 6 ++++ test/link_client_controller_test.dart | 29 ++++++++++++++++++ test/support/link_client_fakes.dart | 19 ++++++++++++ 5 files changed, 83 insertions(+), 5 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 5334fcd71..2f73800f1 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -38,6 +38,7 @@ import 'package:mornye/services/desktop_diagnostics.dart'; import 'package:mornye/services/desktop_library_watcher.dart'; import 'package:mornye/services/link/desktop/link_desktop_host.dart'; import 'package:mornye/services/link/features/link_remote_playback.dart'; +import 'package:mornye/services/link/link_client_api.dart'; import 'package:mornye/services/music_player_service.dart'; import 'package:mornye/services/playback_notification.dart'; import 'package:mornye/services/player_widget_service.dart'; @@ -188,6 +189,9 @@ void main() { activeRemotePlaybackProvider.overrideWith( (ref) => ref.watch(linkRemotePlaybackProvider), ), + linkKeepConnectedProvider.overrideWith( + (ref) => ref.watch(linkRemotePlaybackProvider) != null, + ), ], child: EagerInitialization( child: MornyeApp( diff --git a/lib/services/link/client/link_client_controller.dart b/lib/services/link/client/link_client_controller.dart index 30f5f136f..3314c5b46 100644 --- a/lib/services/link/client/link_client_controller.dart +++ b/lib/services/link/client/link_client_controller.dart @@ -211,6 +211,10 @@ class LinkClientController extends Notifier { Future? _loading; LinkDeviceIdentity? _device; var _foreground = true; + + /// [linkKeepConnectedProvider]: the session stays connected in the + /// background too. + var _keepConnected = false; var _discoveryHolds = 0; var _discoveryBlocked = false; @@ -236,6 +240,11 @@ class LinkClientController extends Notifier { } _disposed = false; _lifecycle = AppLifecycleListener(onStateChange: _onLifecycle); + ref.listen( + linkKeepConnectedProvider, + (_, keep) => _updateConnected(() => _keepConnected = keep), + fireImmediately: true, + ); ref.onDispose(_dispose); _loading = Future.microtask( () => _serial(_load).catchError( @@ -335,7 +344,7 @@ class LinkClientController extends Notifier { ); await previous?.dispose(); if (next != null) { - if (!_foreground) next.pause(); + if (!_connected) next.pause(); next.start(); } _updateDiscovery(); @@ -378,14 +387,25 @@ class LinkClientController extends Notifier { if (lifecycle == AppLifecycleState.inactive) return; final foreground = lifecycle == AppLifecycleState.resumed; if (foreground == _foreground || _disposed) return; - _foreground = foreground; - if (foreground) { + _updateConnected(() => _foreground = foreground); + if (foreground) unawaited(refreshLocalNetwork()); + _updateDiscovery(); + } + + /// Whether the session is connected rather than paused. + bool get _connected => _foreground || _keepConnected; + + /// Applies [change], then pauses or resumes the session when that changed + /// [_connected]. + void _updateConnected(void Function() change) { + final was = _connected; + change(); + if (_connected == was || _disposed) return; + if (_connected) { _session?.resume(); - unawaited(refreshLocalNetwork()); } else { _session?.pause(); } - _updateDiscovery(); } /// Keeps discovery running while the returned callback is not called, for diff --git a/lib/services/link/link_client_api.dart b/lib/services/link/link_client_api.dart index 9899a017a..1feca0a24 100644 --- a/lib/services/link/link_client_api.dart +++ b/lib/services/link/link_client_api.dart @@ -99,3 +99,9 @@ class LinkActiveClient extends Notifier { final linkActiveClientProvider = NotifierProvider(LinkActiveClient.new); + +/// Whether the session stays connected while the app is in the background, +/// which it otherwise leaves. The app root sets it while the desktop plays +/// in the phone's place: the notification shows the desktop's song, and +/// only the session hears when the desktop moves to another one. +final linkKeepConnectedProvider = Provider((ref) => false); diff --git a/test/link_client_controller_test.dart b/test/link_client_controller_test.dart index 2dc58c060..b94d21076 100644 --- a/test/link_client_controller_test.dart +++ b/test/link_client_controller_test.dart @@ -289,6 +289,35 @@ void main() { expect(harness.browser.started, 2); }); + test('the session stays connected in the background when asked', () async { + final harness = await _started(); + await harness.controller.pair(linkTestTarget('a'), '123456'); + await pumpEventQueue(); + final session = harness.sessions.single; + + harness.keepConnected(true); + await pumpEventQueue(); + await _lifecycle([AppLifecycleState.hidden, AppLifecycleState.paused]); + expect(session.calls, ['start']); + + harness.keepConnected(false); + await pumpEventQueue(); + expect(session.calls, ['start', 'pause']); + + harness.keepConnected(true); + await pumpEventQueue(); + expect(session.calls, ['start', 'pause', 'resume']); + + await _lifecycle([ + AppLifecycleState.hidden, + AppLifecycleState.inactive, + AppLifecycleState.resumed, + ]); + harness.keepConnected(false); + await pumpEventQueue(); + expect(session.calls, ['start', 'pause', 'resume']); + }); + test('catalog.changed rescans the desktop once bursts settle', () async { final harness = await _started(); final record = await harness.controller.pair(linkTestTarget('a'), '123456'); diff --git a/test/support/link_client_fakes.dart b/test/support/link_client_fakes.dart index 22f8a687c..5bd0ee494 100644 --- a/test/support/link_client_fakes.dart +++ b/test/support/link_client_fakes.dart @@ -226,6 +226,17 @@ class FakeLinkPairing implements LinkPairingApi { } } +class _KeepConnected extends Notifier { + @override + bool build() => false; + + void set(bool keep) => state = keep; +} + +final _keepConnectedProvider = NotifierProvider<_KeepConnected, bool>( + _KeepConnected.new, +); + /// The client's providers wired to fakes. Tests set /// `SharedPreferences.setMockInitialValues` first. class LinkClientHarness { @@ -263,10 +274,18 @@ class LinkClientHarness { linkClientStoreProvider.overrideWithValue(LinkClientStore()), linkPairingApiProvider.overrideWithValue(pairing), linkLibraryBridgeProvider.overrideWithValue(bridge), + linkKeepConnectedProvider.overrideWith( + (ref) => ref.watch(_keepConnectedProvider), + ), ]; late final container = ProviderContainer(overrides: overrides); + /// Sets [linkKeepConnectedProvider], as the app root does while the + /// desktop plays for the phone. + void keepConnected(bool keep) => + container.read(_keepConnectedProvider.notifier).set(keep); + LinkClientController get controller => container.read(linkClientControllerProvider.notifier); LinkClientState get state => container.read(linkClientControllerProvider);