feat: new settings to configure when to report playback and when to mark item complete (#32)

This commit is contained in:
Dr.Blank 2024-09-26 01:39:43 -04:00 committed by GitHub
parent 7279fa0bd6
commit 8049a660e6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 129 additions and 7 deletions

View file

@ -39,7 +39,15 @@ class PlaybackReporter {
/// the minimum duration to report /// the minimum duration to report
final Duration reportingDurationThreshold; 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 /// timer to report every 10 seconds
/// tracking the time since the last report
Timer? _reportTimer; Timer? _reportTimer;
/// metadata to report /// metadata to report
@ -61,6 +69,8 @@ class PlaybackReporter {
this.deviceManufacturer, this.deviceManufacturer,
this.reportingDurationThreshold = const Duration(seconds: 1), this.reportingDurationThreshold = const Duration(seconds: 1),
Duration reportingInterval = const Duration(seconds: 10), Duration reportingInterval = const Duration(seconds: 10),
this.minimumPositionForReporting,
this.markCompleteWhenTimeLeft = const Duration(seconds: 5),
}) : _reportingInterval = reportingInterval { }) : _reportingInterval = reportingInterval {
// initial conditions // initial conditions
if (player.playing) { if (player.playing) {
@ -97,23 +107,35 @@ class PlaybackReporter {
_logger.fine( _logger.fine(
'player state observed, stopping stopwatch at ${_stopwatch.elapsed}', 'player state observed, stopping stopwatch at ${_stopwatch.elapsed}',
); );
await syncCurrentPosition(); await tryReportPlayback(null);
} }
}), }),
); );
_logger.fine( _logger.fine(
'initialized with interval: $reportingInterval, threshold: $reportingDurationThreshold', 'initialized with reportingInterval: $reportingInterval, reportingDurationThreshold: $reportingDurationThreshold',
);
_logger.fine(
'initialized with minimumPositionForReporting: $minimumPositionForReporting, markCompleteWhenTimeLeft: $markCompleteWhenTimeLeft',
); );
_logger.fine( _logger.fine(
'initialized with deviceModel: $deviceModel, deviceSdkVersion: $deviceSdkVersion, deviceClientName: $deviceClientName, deviceClientVersion: $deviceClientVersion, deviceManufacturer: $deviceManufacturer', 'initialized with deviceModel: $deviceModel, deviceSdkVersion: $deviceSdkVersion, deviceClientName: $deviceClientName, deviceClientVersion: $deviceClientVersion, deviceManufacturer: $deviceManufacturer',
); );
} }
void tryReportPlayback(_) async { Future<void> tryReportPlayback(_) async {
_logger.fine( _logger.fine(
'callback called when elapsed ${_stopwatch.elapsed}', '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) { if (_stopwatch.elapsed > reportingDurationThreshold) {
_logger.fine( _logger.fine(
'reporting now with elapsed ${_stopwatch.elapsed} > threshold $reportingDurationThreshold', 'reporting now with elapsed ${_stopwatch.elapsed} > threshold $reportingDurationThreshold',
@ -159,6 +181,22 @@ class PlaybackReporter {
return _session; 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 { Future<void> syncCurrentPosition() async {
final data = _getSyncData(); final data = _getSyncData();
if (data == null) { if (data == null) {
@ -229,6 +267,23 @@ class PlaybackReporter {
); );
return null; 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( return SyncSessionReqParams(
currentTime: player.positionInBook, currentTime: player.positionInBook,
timeListened: _stopwatch.elapsed, timeListened: _stopwatch.elapsed,

View file

@ -13,7 +13,7 @@ part 'playback_reporter_provider.g.dart';
class PlaybackReporter extends _$PlaybackReporter { class PlaybackReporter extends _$PlaybackReporter {
@override @override
Future<core.PlaybackReporter> build() async { Future<core.PlaybackReporter> build() async {
final appSettings = ref.watch(appSettingsProvider); final playerSettings = ref.watch(appSettingsProvider).playerSettings;
final player = ref.watch(simpleAudiobookPlayerProvider); final player = ref.watch(simpleAudiobookPlayerProvider);
final packageInfo = await PackageInfo.fromPlatform(); final packageInfo = await PackageInfo.fromPlatform();
final api = ref.watch(authenticatedApiProvider); final api = ref.watch(authenticatedApiProvider);
@ -26,7 +26,9 @@ class PlaybackReporter extends _$PlaybackReporter {
final reporter = core.PlaybackReporter( final reporter = core.PlaybackReporter(
player, player,
api, api,
reportingInterval: appSettings.playerSettings.playbackReportInterval, reportingInterval: playerSettings.playbackReportInterval,
markCompleteWhenTimeLeft: playerSettings.markCompleteWhenTimeLeft,
minimumPositionForReporting: playerSettings.minimumPositionForReporting,
deviceName: deviceName, deviceName: deviceName,
deviceModel: deviceModel, deviceModel: deviceModel,
deviceSdkVersion: deviceSdkVersion, deviceSdkVersion: deviceSdkVersion,

View file

@ -6,7 +6,7 @@ part of 'playback_reporter_provider.dart';
// RiverpodGenerator // RiverpodGenerator
// ************************************************************************** // **************************************************************************
String _$playbackReporterHash() => r'c210b7286d9c151fd59a9ead9eb4a28d1cffdc7c'; String _$playbackReporterHash() => r'f5436d652e51c37bcc684acdaec94e17a97e68e5';
/// See also [PlaybackReporter]. /// See also [PlaybackReporter].
@ProviderFor(PlaybackReporter) @ProviderFor(PlaybackReporter)

View file

@ -45,7 +45,9 @@ class PlayerSettings with _$PlayerSettings {
@Default(1) double preferredDefaultSpeed, @Default(1) double preferredDefaultSpeed,
@Default([0.75, 1, 1.25, 1.5, 1.75, 2]) List<double> speedOptions, @Default([0.75, 1, 1.25, 1.5, 1.75, 2]) List<double> speedOptions,
@Default(SleepTimerSettings()) SleepTimerSettings sleepTimerSettings, @Default(SleepTimerSettings()) SleepTimerSettings sleepTimerSettings,
@Default(Duration(seconds: 10)) Duration minimumPositionForReporting,
@Default(Duration(seconds: 10)) Duration playbackReportInterval, @Default(Duration(seconds: 10)) Duration playbackReportInterval,
@Default(Duration(seconds: 15)) Duration markCompleteWhenTimeLeft,
@Default(true) bool configurePlayerForEveryBook, @Default(true) bool configurePlayerForEveryBook,
}) = _PlayerSettings; }) = _PlayerSettings;

View file

@ -514,7 +514,10 @@ mixin _$PlayerSettings {
List<double> get speedOptions => throw _privateConstructorUsedError; List<double> get speedOptions => throw _privateConstructorUsedError;
SleepTimerSettings get sleepTimerSettings => SleepTimerSettings get sleepTimerSettings =>
throw _privateConstructorUsedError; throw _privateConstructorUsedError;
Duration get minimumPositionForReporting =>
throw _privateConstructorUsedError;
Duration get playbackReportInterval => throw _privateConstructorUsedError; Duration get playbackReportInterval => throw _privateConstructorUsedError;
Duration get markCompleteWhenTimeLeft => throw _privateConstructorUsedError;
bool get configurePlayerForEveryBook => throw _privateConstructorUsedError; bool get configurePlayerForEveryBook => throw _privateConstructorUsedError;
/// Serializes this PlayerSettings to a JSON map. /// Serializes this PlayerSettings to a JSON map.
@ -540,7 +543,9 @@ abstract class $PlayerSettingsCopyWith<$Res> {
double preferredDefaultSpeed, double preferredDefaultSpeed,
List<double> speedOptions, List<double> speedOptions,
SleepTimerSettings sleepTimerSettings, SleepTimerSettings sleepTimerSettings,
Duration minimumPositionForReporting,
Duration playbackReportInterval, Duration playbackReportInterval,
Duration markCompleteWhenTimeLeft,
bool configurePlayerForEveryBook}); bool configurePlayerForEveryBook});
$MinimizedPlayerSettingsCopyWith<$Res> get miniPlayerSettings; $MinimizedPlayerSettingsCopyWith<$Res> get miniPlayerSettings;
@ -569,7 +574,9 @@ class _$PlayerSettingsCopyWithImpl<$Res, $Val extends PlayerSettings>
Object? preferredDefaultSpeed = null, Object? preferredDefaultSpeed = null,
Object? speedOptions = null, Object? speedOptions = null,
Object? sleepTimerSettings = null, Object? sleepTimerSettings = null,
Object? minimumPositionForReporting = null,
Object? playbackReportInterval = null, Object? playbackReportInterval = null,
Object? markCompleteWhenTimeLeft = null,
Object? configurePlayerForEveryBook = null, Object? configurePlayerForEveryBook = null,
}) { }) {
return _then(_value.copyWith( return _then(_value.copyWith(
@ -597,10 +604,18 @@ class _$PlayerSettingsCopyWithImpl<$Res, $Val extends PlayerSettings>
? _value.sleepTimerSettings ? _value.sleepTimerSettings
: sleepTimerSettings // ignore: cast_nullable_to_non_nullable : sleepTimerSettings // ignore: cast_nullable_to_non_nullable
as SleepTimerSettings, as SleepTimerSettings,
minimumPositionForReporting: null == minimumPositionForReporting
? _value.minimumPositionForReporting
: minimumPositionForReporting // ignore: cast_nullable_to_non_nullable
as Duration,
playbackReportInterval: null == playbackReportInterval playbackReportInterval: null == playbackReportInterval
? _value.playbackReportInterval ? _value.playbackReportInterval
: playbackReportInterval // ignore: cast_nullable_to_non_nullable : playbackReportInterval // ignore: cast_nullable_to_non_nullable
as Duration, as Duration,
markCompleteWhenTimeLeft: null == markCompleteWhenTimeLeft
? _value.markCompleteWhenTimeLeft
: markCompleteWhenTimeLeft // ignore: cast_nullable_to_non_nullable
as Duration,
configurePlayerForEveryBook: null == configurePlayerForEveryBook configurePlayerForEveryBook: null == configurePlayerForEveryBook
? _value.configurePlayerForEveryBook ? _value.configurePlayerForEveryBook
: configurePlayerForEveryBook // ignore: cast_nullable_to_non_nullable : configurePlayerForEveryBook // ignore: cast_nullable_to_non_nullable
@ -657,7 +672,9 @@ abstract class _$$PlayerSettingsImplCopyWith<$Res>
double preferredDefaultSpeed, double preferredDefaultSpeed,
List<double> speedOptions, List<double> speedOptions,
SleepTimerSettings sleepTimerSettings, SleepTimerSettings sleepTimerSettings,
Duration minimumPositionForReporting,
Duration playbackReportInterval, Duration playbackReportInterval,
Duration markCompleteWhenTimeLeft,
bool configurePlayerForEveryBook}); bool configurePlayerForEveryBook});
@override @override
@ -687,7 +704,9 @@ class __$$PlayerSettingsImplCopyWithImpl<$Res>
Object? preferredDefaultSpeed = null, Object? preferredDefaultSpeed = null,
Object? speedOptions = null, Object? speedOptions = null,
Object? sleepTimerSettings = null, Object? sleepTimerSettings = null,
Object? minimumPositionForReporting = null,
Object? playbackReportInterval = null, Object? playbackReportInterval = null,
Object? markCompleteWhenTimeLeft = null,
Object? configurePlayerForEveryBook = null, Object? configurePlayerForEveryBook = null,
}) { }) {
return _then(_$PlayerSettingsImpl( return _then(_$PlayerSettingsImpl(
@ -715,10 +734,18 @@ class __$$PlayerSettingsImplCopyWithImpl<$Res>
? _value.sleepTimerSettings ? _value.sleepTimerSettings
: sleepTimerSettings // ignore: cast_nullable_to_non_nullable : sleepTimerSettings // ignore: cast_nullable_to_non_nullable
as SleepTimerSettings, as SleepTimerSettings,
minimumPositionForReporting: null == minimumPositionForReporting
? _value.minimumPositionForReporting
: minimumPositionForReporting // ignore: cast_nullable_to_non_nullable
as Duration,
playbackReportInterval: null == playbackReportInterval playbackReportInterval: null == playbackReportInterval
? _value.playbackReportInterval ? _value.playbackReportInterval
: playbackReportInterval // ignore: cast_nullable_to_non_nullable : playbackReportInterval // ignore: cast_nullable_to_non_nullable
as Duration, as Duration,
markCompleteWhenTimeLeft: null == markCompleteWhenTimeLeft
? _value.markCompleteWhenTimeLeft
: markCompleteWhenTimeLeft // ignore: cast_nullable_to_non_nullable
as Duration,
configurePlayerForEveryBook: null == configurePlayerForEveryBook configurePlayerForEveryBook: null == configurePlayerForEveryBook
? _value.configurePlayerForEveryBook ? _value.configurePlayerForEveryBook
: configurePlayerForEveryBook // ignore: cast_nullable_to_non_nullable : configurePlayerForEveryBook // ignore: cast_nullable_to_non_nullable
@ -737,7 +764,9 @@ class _$PlayerSettingsImpl implements _PlayerSettings {
this.preferredDefaultSpeed = 1, this.preferredDefaultSpeed = 1,
final List<double> speedOptions = const [0.75, 1, 1.25, 1.5, 1.75, 2], final List<double> speedOptions = const [0.75, 1, 1.25, 1.5, 1.75, 2],
this.sleepTimerSettings = const SleepTimerSettings(), this.sleepTimerSettings = const SleepTimerSettings(),
this.minimumPositionForReporting = const Duration(seconds: 10),
this.playbackReportInterval = const Duration(seconds: 10), this.playbackReportInterval = const Duration(seconds: 10),
this.markCompleteWhenTimeLeft = const Duration(seconds: 15),
this.configurePlayerForEveryBook = true}) this.configurePlayerForEveryBook = true})
: _speedOptions = speedOptions; : _speedOptions = speedOptions;
@ -770,14 +799,20 @@ class _$PlayerSettingsImpl implements _PlayerSettings {
final SleepTimerSettings sleepTimerSettings; final SleepTimerSettings sleepTimerSettings;
@override @override
@JsonKey() @JsonKey()
final Duration minimumPositionForReporting;
@override
@JsonKey()
final Duration playbackReportInterval; final Duration playbackReportInterval;
@override @override
@JsonKey() @JsonKey()
final Duration markCompleteWhenTimeLeft;
@override
@JsonKey()
final bool configurePlayerForEveryBook; final bool configurePlayerForEveryBook;
@override @override
String toString() { String toString() {
return 'PlayerSettings(miniPlayerSettings: $miniPlayerSettings, expandedPlayerSettings: $expandedPlayerSettings, preferredDefaultVolume: $preferredDefaultVolume, preferredDefaultSpeed: $preferredDefaultSpeed, speedOptions: $speedOptions, sleepTimerSettings: $sleepTimerSettings, playbackReportInterval: $playbackReportInterval, configurePlayerForEveryBook: $configurePlayerForEveryBook)'; return 'PlayerSettings(miniPlayerSettings: $miniPlayerSettings, expandedPlayerSettings: $expandedPlayerSettings, preferredDefaultVolume: $preferredDefaultVolume, preferredDefaultSpeed: $preferredDefaultSpeed, speedOptions: $speedOptions, sleepTimerSettings: $sleepTimerSettings, minimumPositionForReporting: $minimumPositionForReporting, playbackReportInterval: $playbackReportInterval, markCompleteWhenTimeLeft: $markCompleteWhenTimeLeft, configurePlayerForEveryBook: $configurePlayerForEveryBook)';
} }
@override @override
@ -797,8 +832,15 @@ class _$PlayerSettingsImpl implements _PlayerSettings {
.equals(other._speedOptions, _speedOptions) && .equals(other._speedOptions, _speedOptions) &&
(identical(other.sleepTimerSettings, sleepTimerSettings) || (identical(other.sleepTimerSettings, sleepTimerSettings) ||
other.sleepTimerSettings == sleepTimerSettings) && other.sleepTimerSettings == sleepTimerSettings) &&
(identical(other.minimumPositionForReporting,
minimumPositionForReporting) ||
other.minimumPositionForReporting ==
minimumPositionForReporting) &&
(identical(other.playbackReportInterval, playbackReportInterval) || (identical(other.playbackReportInterval, playbackReportInterval) ||
other.playbackReportInterval == playbackReportInterval) && other.playbackReportInterval == playbackReportInterval) &&
(identical(
other.markCompleteWhenTimeLeft, markCompleteWhenTimeLeft) ||
other.markCompleteWhenTimeLeft == markCompleteWhenTimeLeft) &&
(identical(other.configurePlayerForEveryBook, (identical(other.configurePlayerForEveryBook,
configurePlayerForEveryBook) || configurePlayerForEveryBook) ||
other.configurePlayerForEveryBook == other.configurePlayerForEveryBook ==
@ -815,7 +857,9 @@ class _$PlayerSettingsImpl implements _PlayerSettings {
preferredDefaultSpeed, preferredDefaultSpeed,
const DeepCollectionEquality().hash(_speedOptions), const DeepCollectionEquality().hash(_speedOptions),
sleepTimerSettings, sleepTimerSettings,
minimumPositionForReporting,
playbackReportInterval, playbackReportInterval,
markCompleteWhenTimeLeft,
configurePlayerForEveryBook); configurePlayerForEveryBook);
/// Create a copy of PlayerSettings /// Create a copy of PlayerSettings
@ -843,7 +887,9 @@ abstract class _PlayerSettings implements PlayerSettings {
final double preferredDefaultSpeed, final double preferredDefaultSpeed,
final List<double> speedOptions, final List<double> speedOptions,
final SleepTimerSettings sleepTimerSettings, final SleepTimerSettings sleepTimerSettings,
final Duration minimumPositionForReporting,
final Duration playbackReportInterval, final Duration playbackReportInterval,
final Duration markCompleteWhenTimeLeft,
final bool configurePlayerForEveryBook}) = _$PlayerSettingsImpl; final bool configurePlayerForEveryBook}) = _$PlayerSettingsImpl;
factory _PlayerSettings.fromJson(Map<String, dynamic> json) = factory _PlayerSettings.fromJson(Map<String, dynamic> json) =
@ -862,8 +908,12 @@ abstract class _PlayerSettings implements PlayerSettings {
@override @override
SleepTimerSettings get sleepTimerSettings; SleepTimerSettings get sleepTimerSettings;
@override @override
Duration get minimumPositionForReporting;
@override
Duration get playbackReportInterval; Duration get playbackReportInterval;
@override @override
Duration get markCompleteWhenTimeLeft;
@override
bool get configurePlayerForEveryBook; bool get configurePlayerForEveryBook;
/// Create a copy of PlayerSettings /// Create a copy of PlayerSettings

View file

@ -73,10 +73,19 @@ _$PlayerSettingsImpl _$$PlayerSettingsImplFromJson(Map<String, dynamic> json) =>
? const SleepTimerSettings() ? const SleepTimerSettings()
: SleepTimerSettings.fromJson( : SleepTimerSettings.fromJson(
json['sleepTimerSettings'] as Map<String, dynamic>), json['sleepTimerSettings'] as Map<String, dynamic>),
minimumPositionForReporting: json['minimumPositionForReporting'] == null
? const Duration(seconds: 10)
: Duration(
microseconds:
(json['minimumPositionForReporting'] as num).toInt()),
playbackReportInterval: json['playbackReportInterval'] == null playbackReportInterval: json['playbackReportInterval'] == null
? const Duration(seconds: 10) ? const Duration(seconds: 10)
: Duration( : Duration(
microseconds: (json['playbackReportInterval'] as num).toInt()), microseconds: (json['playbackReportInterval'] as num).toInt()),
markCompleteWhenTimeLeft: json['markCompleteWhenTimeLeft'] == null
? const Duration(seconds: 15)
: Duration(
microseconds: (json['markCompleteWhenTimeLeft'] as num).toInt()),
configurePlayerForEveryBook: configurePlayerForEveryBook:
json['configurePlayerForEveryBook'] as bool? ?? true, json['configurePlayerForEveryBook'] as bool? ?? true,
); );
@ -90,7 +99,11 @@ Map<String, dynamic> _$$PlayerSettingsImplToJson(
'preferredDefaultSpeed': instance.preferredDefaultSpeed, 'preferredDefaultSpeed': instance.preferredDefaultSpeed,
'speedOptions': instance.speedOptions, 'speedOptions': instance.speedOptions,
'sleepTimerSettings': instance.sleepTimerSettings, 'sleepTimerSettings': instance.sleepTimerSettings,
'minimumPositionForReporting':
instance.minimumPositionForReporting.inMicroseconds,
'playbackReportInterval': instance.playbackReportInterval.inMicroseconds, 'playbackReportInterval': instance.playbackReportInterval.inMicroseconds,
'markCompleteWhenTimeLeft':
instance.markCompleteWhenTimeLeft.inMicroseconds,
'configurePlayerForEveryBook': instance.configurePlayerForEveryBook, 'configurePlayerForEveryBook': instance.configurePlayerForEveryBook,
}; };