Discord RPC now shows correct position / duration

This commit is contained in:
Schnitzel5 2025-08-06 00:03:05 +02:00
parent 2eca6576a2
commit 608fc525fb
2 changed files with 16 additions and 32 deletions

View file

@ -314,10 +314,7 @@ class _AnimeStreamPageState extends riv.ConsumerState<AnimeStreamPage>
.duration
.listen((duration) {
_currentTotalDuration.value = duration;
discordRpc?.startChapterTimestamp(
_currentPosition.value.inMilliseconds,
duration.inMilliseconds,
);
discordRpc?.updateChapterTimestamp(_currentPosition.value, duration);
});
bool get hasNextEpisode => _streamController.getEpisodeIndex().$1 != 0;
@ -785,8 +782,13 @@ mp.register_script_message('call_button_${button.id}_long', button${button.id}lo
void _updateRpcTimestamp() {
final now = DateTime.now().millisecondsSinceEpoch;
if (lastRpcTimestampUpdate + 10000 < now) {
discordRpc?.updateChapterTimestamp(_currentPosition.value.inMilliseconds);
if (lastRpcTimestampUpdate + 5000 < now) {
if (_currentTotalDuration.value != null) {
discordRpc?.updateChapterTimestamp(
_currentPosition.value,
_currentTotalDuration.value!,
);
}
lastRpcTimestampUpdate = now;
}
}

View file

@ -18,12 +18,6 @@ class DiscordRPC {
/// Start timestamp in millis
final int startAt = DateTime.timestamp().millisecondsSinceEpoch;
/// Start timestamp in millis for the current chapter/episode
int chapterStartAt = 0;
/// End timestamp in millis for the current chapter/episode
int chapterEndAt = 0;
/// Temp var
late bool rpcShowReadingWatchingProgress;
@ -132,32 +126,20 @@ class DiscordRPC {
await updateActivity(timestamps: RPCTimestamps(start: startAt));
}
Future<void> startChapterTimestamp(
int offsetInMillis,
int durationInMillis,
Future<void> updateChapterTimestamp(
Duration position,
Duration duration,
) async {
if (!rpcShowReadingWatchingProgress) {
return;
}
chapterStartAt = DateTime.timestamp().millisecondsSinceEpoch;
chapterEndAt =
DateTime.timestamp().millisecondsSinceEpoch + durationInMillis;
await updateActivity(
timestamps: RPCTimestamps(
start: chapterStartAt,
end: chapterEndAt - offsetInMillis,
),
);
}
Future<void> updateChapterTimestamp(int newOffsetInMillis) async {
if (!rpcShowReadingWatchingProgress) {
return;
}
await updateActivity(
timestamps: RPCTimestamps(
start: chapterStartAt,
end: chapterEndAt - newOffsetInMillis,
start: DateTime.timestamp().subtract(position).millisecondsSinceEpoch,
end: DateTime.timestamp()
.subtract(position)
.add(duration)
.millisecondsSinceEpoch,
),
);
}