mirror of
https://github.com/Dr-Blank/Vaani.git
synced 2025-12-15 15:39:29 +00:00
feat: new settings to configure when to report playback and when to mark item complete (#32)
This commit is contained in:
parent
7279fa0bd6
commit
8049a660e6
6 changed files with 129 additions and 7 deletions
|
|
@ -39,7 +39,15 @@ class PlaybackReporter {
|
|||
/// the minimum duration to report
|
||||
final Duration reportingDurationThreshold;
|
||||
|
||||
/// the duration to wait before starting the reporting
|
||||
/// this is to ignore the initial duration in case user is browsing
|
||||
final Duration? minimumPositionForReporting;
|
||||
|
||||
/// the duration to mark the book as complete when the time left is less than this
|
||||
final Duration markCompleteWhenTimeLeft;
|
||||
|
||||
/// timer to report every 10 seconds
|
||||
/// tracking the time since the last report
|
||||
Timer? _reportTimer;
|
||||
|
||||
/// metadata to report
|
||||
|
|
@ -61,6 +69,8 @@ class PlaybackReporter {
|
|||
this.deviceManufacturer,
|
||||
this.reportingDurationThreshold = const Duration(seconds: 1),
|
||||
Duration reportingInterval = const Duration(seconds: 10),
|
||||
this.minimumPositionForReporting,
|
||||
this.markCompleteWhenTimeLeft = const Duration(seconds: 5),
|
||||
}) : _reportingInterval = reportingInterval {
|
||||
// initial conditions
|
||||
if (player.playing) {
|
||||
|
|
@ -97,23 +107,35 @@ class PlaybackReporter {
|
|||
_logger.fine(
|
||||
'player state observed, stopping stopwatch at ${_stopwatch.elapsed}',
|
||||
);
|
||||
await syncCurrentPosition();
|
||||
await tryReportPlayback(null);
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
_logger.fine(
|
||||
'initialized with interval: $reportingInterval, threshold: $reportingDurationThreshold',
|
||||
'initialized with reportingInterval: $reportingInterval, reportingDurationThreshold: $reportingDurationThreshold',
|
||||
);
|
||||
_logger.fine(
|
||||
'initialized with minimumPositionForReporting: $minimumPositionForReporting, markCompleteWhenTimeLeft: $markCompleteWhenTimeLeft',
|
||||
);
|
||||
_logger.fine(
|
||||
'initialized with deviceModel: $deviceModel, deviceSdkVersion: $deviceSdkVersion, deviceClientName: $deviceClientName, deviceClientVersion: $deviceClientVersion, deviceManufacturer: $deviceManufacturer',
|
||||
);
|
||||
}
|
||||
|
||||
void tryReportPlayback(_) async {
|
||||
Future<void> tryReportPlayback(_) async {
|
||||
_logger.fine(
|
||||
'callback called when elapsed ${_stopwatch.elapsed}',
|
||||
);
|
||||
if (player.book != null &&
|
||||
player.positionInBook >=
|
||||
player.book!.duration - markCompleteWhenTimeLeft) {
|
||||
_logger.info(
|
||||
'marking complete as time left is less than $markCompleteWhenTimeLeft',
|
||||
);
|
||||
await markComplete();
|
||||
return;
|
||||
}
|
||||
if (_stopwatch.elapsed > reportingDurationThreshold) {
|
||||
_logger.fine(
|
||||
'reporting now with elapsed ${_stopwatch.elapsed} > threshold $reportingDurationThreshold',
|
||||
|
|
@ -159,6 +181,22 @@ class PlaybackReporter {
|
|||
return _session;
|
||||
}
|
||||
|
||||
Future<void> markComplete() async {
|
||||
if (player.book == null) {
|
||||
throw NoAudiobookPlayingError();
|
||||
}
|
||||
await authenticatedApi.me.createUpdateMediaProgress(
|
||||
libraryItemId: player.book!.libraryItemId,
|
||||
parameters: CreateUpdateProgressReqParams(
|
||||
isFinished: true,
|
||||
currentTime: player.positionInBook,
|
||||
duration: player.book!.duration,
|
||||
),
|
||||
responseErrorHandler: _responseErrorHandler,
|
||||
);
|
||||
_logger.info('Marked complete for book: ${player.book!.libraryItemId}');
|
||||
}
|
||||
|
||||
Future<void> syncCurrentPosition() async {
|
||||
final data = _getSyncData();
|
||||
if (data == null) {
|
||||
|
|
@ -229,6 +267,23 @@ class PlaybackReporter {
|
|||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
// if in the ignore duration, don't sync
|
||||
if (minimumPositionForReporting != null &&
|
||||
player.positionInBook < minimumPositionForReporting!) {
|
||||
// but if elapsed time is more than the minimumPositionForReporting, sync
|
||||
if (_stopwatch.elapsed > minimumPositionForReporting!) {
|
||||
_logger.info(
|
||||
'Syncing position despite being less than minimumPositionForReporting as elapsed time is more: ${_stopwatch.elapsed}',
|
||||
);
|
||||
} else {
|
||||
_logger.info(
|
||||
'Ignoring sync for position: ${player.positionInBook} < $minimumPositionForReporting',
|
||||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return SyncSessionReqParams(
|
||||
currentTime: player.positionInBook,
|
||||
timeListened: _stopwatch.elapsed,
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ part 'playback_reporter_provider.g.dart';
|
|||
class PlaybackReporter extends _$PlaybackReporter {
|
||||
@override
|
||||
Future<core.PlaybackReporter> build() async {
|
||||
final appSettings = ref.watch(appSettingsProvider);
|
||||
final playerSettings = ref.watch(appSettingsProvider).playerSettings;
|
||||
final player = ref.watch(simpleAudiobookPlayerProvider);
|
||||
final packageInfo = await PackageInfo.fromPlatform();
|
||||
final api = ref.watch(authenticatedApiProvider);
|
||||
|
|
@ -26,7 +26,9 @@ class PlaybackReporter extends _$PlaybackReporter {
|
|||
final reporter = core.PlaybackReporter(
|
||||
player,
|
||||
api,
|
||||
reportingInterval: appSettings.playerSettings.playbackReportInterval,
|
||||
reportingInterval: playerSettings.playbackReportInterval,
|
||||
markCompleteWhenTimeLeft: playerSettings.markCompleteWhenTimeLeft,
|
||||
minimumPositionForReporting: playerSettings.minimumPositionForReporting,
|
||||
deviceName: deviceName,
|
||||
deviceModel: deviceModel,
|
||||
deviceSdkVersion: deviceSdkVersion,
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ part of 'playback_reporter_provider.dart';
|
|||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$playbackReporterHash() => r'c210b7286d9c151fd59a9ead9eb4a28d1cffdc7c';
|
||||
String _$playbackReporterHash() => r'f5436d652e51c37bcc684acdaec94e17a97e68e5';
|
||||
|
||||
/// See also [PlaybackReporter].
|
||||
@ProviderFor(PlaybackReporter)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue