1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-19 18:26:46 +01:00

await things

This commit is contained in:
mertalev 2024-11-08 22:04:43 -05:00
parent dbca16e352
commit 190dbb0042
No known key found for this signature in database
GPG key ID: CA85EF6600C9E8AD
2 changed files with 50 additions and 40 deletions

View file

@ -170,7 +170,7 @@ class NativeVideoViewerPage extends HookConsumerWidget {
// When the volume changes, set the volume // When the volume changes, set the volume
ref.listen(videoPlayerControlsProvider.select((value) => value.mute), ref.listen(videoPlayerControlsProvider.select((value) => value.mute),
(_, mute) { (_, mute) async {
final playerController = controller.value; final playerController = controller.value;
if (playerController == null) { if (playerController == null) {
return; return;
@ -183,9 +183,9 @@ class NativeVideoViewerPage extends HookConsumerWidget {
try { try {
if (mute && playbackInfo.volume != 0.0) { if (mute && playbackInfo.volume != 0.0) {
playerController.setVolume(0.0); await playerController.setVolume(0.0);
} else if (!mute && playbackInfo.volume != 0.7) { } else if (!mute && playbackInfo.volume != 0.7) {
playerController.setVolume(0.7); await playerController.setVolume(0.7);
} }
} catch (error) { } catch (error) {
log.severe('Error setting volume: $error'); log.severe('Error setting volume: $error');
@ -196,7 +196,7 @@ class NativeVideoViewerPage extends HookConsumerWidget {
final seekThrottler = final seekThrottler =
useThrottler(interval: const Duration(milliseconds: 200)); useThrottler(interval: const Duration(milliseconds: 200));
ref.listen(videoPlayerControlsProvider.select((value) => value.position), ref.listen(videoPlayerControlsProvider.select((value) => value.position),
(_, position) { (_, position) async {
final playerController = controller.value; final playerController = controller.value;
if (playerController == null) { if (playerController == null) {
return; return;
@ -211,7 +211,11 @@ class NativeVideoViewerPage extends HookConsumerWidget {
final int seek = (asset.duration * (position / 100.0)).inSeconds; final int seek = (asset.duration * (position / 100.0)).inSeconds;
if (seek != playbackInfo.position) { if (seek != playbackInfo.position) {
try { try {
final maybeSeek =
seekThrottler.run(() => playerController.seekTo(seek)); seekThrottler.run(() => playerController.seekTo(seek));
if (maybeSeek != null) {
await maybeSeek;
}
} catch (error) { } catch (error) {
log.severe('Error seeking to position $position: $error'); log.severe('Error seeking to position $position: $error');
} }
@ -223,7 +227,7 @@ class NativeVideoViewerPage extends HookConsumerWidget {
// // When the custom video controls pause or play // // When the custom video controls pause or play
ref.listen(videoPlayerControlsProvider.select((value) => value.pause), ref.listen(videoPlayerControlsProvider.select((value) => value.pause),
(_, pause) { (_, pause) async {
final videoController = controller.value; final videoController = controller.value;
if (videoController == null || !context.mounted) { if (videoController == null || !context.mounted) {
return; return;
@ -231,43 +235,29 @@ class NativeVideoViewerPage extends HookConsumerWidget {
try { try {
if (pause) { if (pause) {
videoController.pause(); await videoController.pause();
} else { } else {
videoController.play(); await videoController.play();
} }
} catch (error) { } catch (error) {
log.severe('Error pausing or playing video: $error'); log.severe('Error pausing or playing video: $error');
} }
}); });
void onPlaybackReady() { void onPlaybackReady() async {
final videoController = controller.value; final videoController = controller.value;
if (videoController == null || !isCurrent || !context.mounted) { if (videoController == null || !isCurrent || !context.mounted) {
return; return;
} }
try { try {
videoController.play(); await videoController.play();
videoController.setVolume(0.9); await videoController.setVolume(0.9);
} catch (error) { } catch (error) {
log.severe('Error playing video: $error'); log.severe('Error playing video: $error');
} }
} }
ref.listen(currentAssetProvider, (_, value) {
// Delay the video playback to avoid a stutter in the swipe animation
Timer(const Duration(milliseconds: 300), () {
if (!context.mounted) {
return;
}
currentAsset.value = value;
if (currentAsset.value == asset) {
onPlaybackReady();
}
});
});
void onPlaybackStatusChanged() { void onPlaybackStatusChanged() {
final videoController = controller.value; final videoController = controller.value;
if (videoController == null || !context.mounted) { if (videoController == null || !context.mounted) {
@ -331,6 +321,15 @@ class NativeVideoViewerPage extends HookConsumerWidget {
} }
} }
void removeListeners(NativeVideoPlayerController controller) {
controller.onPlaybackPositionChanged
.removeListener(onPlaybackPositionChanged);
controller.onPlaybackStatusChanged
.removeListener(onPlaybackStatusChanged);
controller.onPlaybackReady.removeListener(onPlaybackReady);
controller.onPlaybackEnded.removeListener(onPlaybackEnded);
}
void initController(NativeVideoPlayerController nc) { void initController(NativeVideoPlayerController nc) {
if (controller.value != null) { if (controller.value != null) {
return; return;
@ -349,6 +348,25 @@ class NativeVideoViewerPage extends HookConsumerWidget {
Timer(const Duration(milliseconds: 200), checkIfBuffering); Timer(const Duration(milliseconds: 200), checkIfBuffering);
} }
ref.listen(currentAssetProvider, (_, value) {
final playerController = controller.value;
if (playerController != null && value != asset) {
removeListeners(playerController);
}
// Delay the video playback to avoid a stutter in the swipe animation
Timer(const Duration(milliseconds: 300), () {
if (!context.mounted) {
return;
}
currentAsset.value = value;
if (currentAsset.value == asset) {
onPlaybackReady();
}
});
});
useEffect( useEffect(
() { () {
return () { return () {
@ -356,19 +374,10 @@ class NativeVideoViewerPage extends HookConsumerWidget {
if (playerController == null) { if (playerController == null) {
return; return;
} }
removeListeners(playerController);
try { playerController.stop().catchError((error) {
playerController.stop(); log.severe('Error stopping video: $error');
});
playerController.onPlaybackPositionChanged
.removeListener(onPlaybackPositionChanged);
playerController.onPlaybackStatusChanged
.removeListener(onPlaybackStatusChanged);
playerController.onPlaybackReady.removeListener(onPlaybackReady);
playerController.onPlaybackEnded.removeListener(onPlaybackEnded);
} catch (error) {
log.severe('Error during useEffect cleanup: $error');
}
controller.value = null; controller.value = null;
WakelockPlus.disable(); WakelockPlus.disable();

View file

@ -10,11 +10,12 @@ class Throttler {
Throttler({required this.interval}); Throttler({required this.interval});
void run(FutureOr<void> Function() action) { T? run<T>(T Function() action) {
if (_lastActionTime == null || if (_lastActionTime == null ||
(DateTime.now().difference(_lastActionTime!) > interval)) { (DateTime.now().difference(_lastActionTime!) > interval)) {
action(); final response = action();
_lastActionTime = DateTime.now(); _lastActionTime = DateTime.now();
return response;
} }
} }