Skip to content

Commit

Permalink
Make some AppWidgetControl variables private
Browse files Browse the repository at this point in the history
  • Loading branch information
Abestanis committed Oct 21, 2024
1 parent e2efeda commit 38efc38
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions lib/logic/app_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,45 @@ class AppWidgetControl extends Control {
static AppWidgetControl instance = AppWidgetControl();
@visibleForTesting
static const appWidgetName = 'MusicPlayerAppWidget';
static const _songUriKey = 'song';
static const _playingKey = 'playing';

StreamSubscription<Song>? _currentSongListener;
StreamSubscription<bool>? _playingStateListener;

StreamSubscription<Song>? currentSongListener;
StreamSubscription<bool>? playingStateListener;
/// The last song content uri sent to the widget.
String? lastSongContentUri;
String? _lastSongContentUri;

/// The last playing state sent to the widget.
bool? lastPlayingState;
bool? _lastPlayingState;

@override
void init() {
super.init();
lastSongContentUri = null;
lastPlayingState = null;
currentSongListener = PlaybackControl.instance.onSongChange
.listen((song) => update(song, MusicPlayer.instance.playing));
playingStateListener = MusicPlayer.instance.playingStream.listen(
(playing) => update(PlaybackControl.instance.currentSong, playing));
_lastSongContentUri = null;
_lastPlayingState = null;
_currentSongListener =
PlaybackControl.instance.onSongChange.listen((song) => update(song, MusicPlayer.instance.playing));
_playingStateListener =
MusicPlayer.instance.playingStream.listen((playing) => update(PlaybackControl.instance.currentSong, playing));
}

@override
void dispose() {
currentSongListener?.cancel();
playingStateListener?.cancel();
_currentSongListener?.cancel();
_playingStateListener?.cancel();
super.dispose();
}

/// Update the widgets with the current [song] and [playing] state.
Future<void> update(Song song, bool playing) async {
if (playing == lastPlayingState && song.contentUri == lastSongContentUri) {
if (playing == _lastPlayingState && song.contentUri == _lastSongContentUri) {
return;
}
lastSongContentUri = song.contentUri;
lastPlayingState = playing;
await HomeWidget.saveWidgetData('song', song.contentUri);
await HomeWidget.saveWidgetData('playing', playing);
_lastSongContentUri = song.contentUri;
_lastPlayingState = playing;
await HomeWidget.saveWidgetData(_songUriKey, song.contentUri);
await HomeWidget.saveWidgetData(_playingKey, playing);
await HomeWidget.updateWidget(name: appWidgetName);
}
}

0 comments on commit 38efc38

Please sign in to comment.