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.
This commit is contained in:
zarzet committed 2026-10-25 14:13:37 +07:00
1 parent 12f04d239e
commit d41731fb69
5 files changed
+83 -5

No files matched your search

+4
View File
@@ -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(
@@ -211,6 +211,10 @@ class LinkClientController extends Notifier<LinkClientState> {
Future<void>? _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<LinkClientState> {
}
_disposed = false;
_lifecycle = AppLifecycleListener(onStateChange: _onLifecycle);
ref.listen(
linkKeepConnectedProvider,
(_, keep) => _updateConnected(() => _keepConnected = keep),
fireImmediately: true,
);
ref.onDispose(_dispose);
_loading = Future<void>.microtask(
() => _serial(_load).catchError(
@@ -335,7 +344,7 @@ class LinkClientController extends Notifier<LinkClientState> {
);
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<LinkClientState> {
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
+6
View File
@@ -99,3 +99,9 @@ class LinkActiveClient extends Notifier<LinkClientApi?> {
final linkActiveClientProvider =
NotifierProvider<LinkActiveClient, LinkClientApi?>(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<bool>((ref) => false);
+29
View File
@@ -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');
+19
View File
@@ -226,6 +226,17 @@ class FakeLinkPairing implements LinkPairingApi {
}
}
class _KeepConnected extends Notifier<bool> {
@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);