Skip to content

Commit

Permalink
fix: scrobbling not working for first track or single track
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Nov 13, 2023
1 parent e29a38d commit 0a6b54d
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions lib/provider/proxy_playlist/proxy_playlist_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,26 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>
Catcher2.reportCheckedError(e, stackTrace);
}
});

String? lastScrobbled;
audioPlayer.positionStream.listen((position) {
try {
final uid = state.activeTrack is LocalTrack
? (state.activeTrack as LocalTrack).path
: state.activeTrack?.id;

if (state.activeTrack == null ||
lastScrobbled == uid ||
position.inSeconds < 30) {
return;
}

scrobbler.scrobble(state.activeTrack!);
lastScrobbled = uid;
} catch (e, stack) {
Catcher2.reportCheckedError(e, stack);
}
});
}();
}

Expand Down Expand Up @@ -609,30 +629,12 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>

@override
set state(state) {
final hasActiveTrackChanged = super.state.activeTrack is SpotubeTrack
? state.activeTrack?.id != super.state.activeTrack?.id
: super.state.activeTrack is LocalTrack &&
state.activeTrack is LocalTrack
? (super.state.activeTrack as LocalTrack).path !=
(state.activeTrack as LocalTrack).path
: super.state.activeTrack?.id != state.activeTrack?.id;

final oldTrack = super.state.activeTrack;

super.state = state;
if (state.tracks.isEmpty && ref.read(paletteProvider) != null) {
ref.read(paletteProvider.notifier).state = null;
} else {
updatePalette();
}
audioPlayer.position.then((position) {
final isMoreThan30secs = position != null &&
(position == Duration.zero || position.inSeconds > 30);

if (hasActiveTrackChanged && oldTrack != null && isMoreThan30secs) {
scrobbler.scrobble(oldTrack);
}
});
}

@override
Expand Down

0 comments on commit 0a6b54d

Please sign in to comment.