Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions flutter_readium_platform_interface/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ Format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Unreleased

### Fixed

- `ReadiumTimebasedState.fromJson()` now tolerates audiobook terminal locators
that omit `progression` / `totalProgression`, preserving the locator while
normalizing ended-state progress to `1.0` instead of crashing on Android.

## [0.3.3] - 2026-08-04

## [0.3.2] - 2026-08-03
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ import 'link.dart';
const int _emptyIntValue = -1;
const double _emptyDoubleValue = -1;

extension IntCheck on int? {
int? check(int? defaultValue) => (this == _emptyIntValue) ? defaultValue : this;
}

extension DoubleNullableCheck on double? {
double? check(double? defaultValue) => (this == _emptyDoubleValue) ? defaultValue : this;

extension DoubleNullableRound on double? {
/// Ensure that this double? is within [epsilon] of [defaultValue], otherwise return this double? (or defaultValue if this is null).
double roundToIfCloseTo(
double defaultValue, {
Expand Down Expand Up @@ -183,9 +177,9 @@ class Locator extends AdditionalProperties with Equatable implements JSONable {
}) => copyWith(
locations: (locations ?? Locations()).copyWith(
fragments: fragments ?? locations?.fragments,
progression: progression.check(locations?.progression),
position: position.check(locations?.position),
totalProgression: totalProgression.check(locations?.totalProgression),
progression: progression == _emptyDoubleValue ? locations?.progression : progression,
position: position == _emptyIntValue ? locations?.position : position,
totalProgression: totalProgression == _emptyDoubleValue ? locations?.totalProgression : totalProgression,
additionalProperties: otherLocations ?? locations?.additionalProperties,
),
);
Expand Down Expand Up @@ -326,9 +320,9 @@ class Locations extends AdditionalProperties with Equatable implements JSONable
..removeWhere((key, value) => value == null);

return Locations(
progression: progression.check(this.progression),
position: position.check(this.position),
totalProgression: totalProgression.check(this.totalProgression),
progression: progression == _emptyDoubleValue ? this.progression : progression,
position: position == _emptyIntValue ? this.position : position,
totalProgression: totalProgression == _emptyDoubleValue ? this.totalProgression : totalProgression,
fragments: fragments ?? this.fragments,
cssSelector: cssSelector ?? this.cssSelector,
domRange: domRange ?? this.domRange,
Expand Down
23 changes: 23 additions & 0 deletions flutter_readium_platform_interface/test/models_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,29 @@ void main() {
});
});

group('ReadiumTimebasedState', () {
test('fromJson normalizes an ended locator with partial location data', () {
final state = ReadiumTimebasedState.fromJson({
'state': 'ended',
'currentLocator': {
'href': 'last.mp3',
'type': 'audio/mpeg',
'locations': {
'position': 3,
'fragments': ['t=123.45'],
},
},
});

expect(state.state, TimebasedState.ended);
expect(state.currentLocator?.href, 'last.mp3');
expect(state.currentLocator?.locations?.position, 3);
expect(state.currentLocator?.locations?.fragments, ['t=123.45']);
expect(state.currentLocator?.locations?.progression, 1.0);
expect(state.currentLocator?.locations?.totalProgression, 1.0);
});
});

// ---------------------------------------------------------------------------
// ReadiumReaderStatus enum
// ---------------------------------------------------------------------------
Expand Down