Save lastPageRead before system kills app

If the app is going to background or is about to be killed, the lastPageRead value is going to be saved, so the progress is not lost if the user leaves the app and the system kills it.
This commit is contained in:
NBA2K1 2025-07-12 20:31:19 +02:00
parent 47c9b79ce9
commit f3ef7791a6
2 changed files with 39 additions and 30 deletions

View file

@ -169,7 +169,7 @@ enum _AniSkipPhase { none, opening, ending }
bool _firstTime = true;
class _AnimeStreamPageState extends riv.ConsumerState<AnimeStreamPage>
with TickerProviderStateMixin {
with TickerProviderStateMixin, WidgetsBindingObserver {
late final GlobalKey<VideoState> _key = GlobalKey<VideoState>();
late final useLibass = ref.read(useLibassStateProvider);
late final Player _player = Player(
@ -327,29 +327,36 @@ class _AnimeStreamPageState extends riv.ConsumerState<AnimeStreamPage>
_completed;
_currentTotalDurationSub;
_loadAndroidFont().then((_) {
_player.open(
Media(
_video.value!.videoTrack!.id,
httpHeaders: _video.value!.headers,
start: _streamController.geTCurrentPosition(),
),
);
_openMedia(_video.value!, _streamController.geTCurrentPosition());
if (widget.isTorrent) {
Future.delayed(const Duration(seconds: 10)).then((_) {
if (mounted) {
_player.open(
Media(
_video.value!.videoTrack!.id,
httpHeaders: _video.value!.headers,
start: _streamController.geTCurrentPosition(),
),
);
_openMedia(_video.value!, _streamController.geTCurrentPosition());
}
});
}
_setPlaybackSpeed(ref.read(defaultPlayBackSpeedStateProvider));
if (ref.read(enableAniSkipStateProvider)) _initAniSkip();
});
WidgetsBinding.instance.addObserver(this);
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.paused ||
state == AppLifecycleState.detached) {
_setCurrentPosition(true);
}
}
Future<void> _openMedia(VideoPrefs prefs, [Duration? position]) {
return _player.open(
Media(
prefs.videoTrack!.id,
httpHeaders: prefs.headers,
start: position ?? _currentPosition.value,
),
);
}
Future<void> _loadAndroidFont() async {
@ -397,6 +404,7 @@ class _AnimeStreamPageState extends riv.ConsumerState<AnimeStreamPage>
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_setCurrentPosition(true);
_player.dispose();
_currentPositionSub.cancel();
@ -495,22 +503,10 @@ class _AnimeStreamPageState extends riv.ConsumerState<AnimeStreamPage>
if (widget.isLocal) {
_player.setVideoTrack(quality.videoTrack!);
} else {
_player.open(
Media(
quality.videoTrack!.id,
httpHeaders: quality.headers,
start: _currentPosition.value,
),
);
_openMedia(quality);
}
} else {
_player.open(
Media(
quality.videoTrack!.id,
httpHeaders: quality.headers,
start: _currentPosition.value,
),
);
_openMedia(quality);
}
_initSubtitleAndAudio = true;
Navigator.pop(context);

View file

@ -126,7 +126,7 @@ class MangaChapterPageGallery extends ConsumerStatefulWidget {
class _MangaChapterPageGalleryState
extends ConsumerState<MangaChapterPageGallery>
with TickerProviderStateMixin {
with TickerProviderStateMixin, WidgetsBindingObserver {
late AnimationController _scaleAnimationController;
late Animation<double> _animation;
late ReaderController _readerController = ref.read(
@ -136,6 +136,7 @@ class _MangaChapterPageGalleryState
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_readerController.setMangaHistoryUpdate();
final index = _uChapDataPreload[_currentIndex!].index;
if (index != null) {
@ -157,6 +158,17 @@ class _MangaChapterPageGalleryState
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.paused ||
state == AppLifecycleState.detached) {
final index = _uChapDataPreload[_currentIndex!].index;
if (index != null) {
_readerController.setPageIndex(_geCurrentIndex(index), true);
}
}
}
late final _autoScroll = ValueNotifier(
_readerController.autoScrollValues().$1,
);
@ -201,6 +213,7 @@ class _MangaChapterPageGalleryState
_animation.addListener(() => _photoViewController.scale = _animation.value);
_itemPositionsListener.itemPositions.addListener(_readProgressListener);
_initCurrentIndex();
WidgetsBinding.instance.addObserver(this);
}
final double _horizontalScaleValue = 1.0;