2023-06-26 18:27:47 +03:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2024-12-05 02:33:46 +05:30
|
|
|
import 'package:immich_mobile/providers/asset_viewer/video_player_value_provider.dart';
|
2023-06-26 18:27:47 +03:00
|
|
|
|
|
|
|
|
class VideoPlaybackControls {
|
2025-07-29 00:34:03 +05:30
|
|
|
const VideoPlaybackControls({required this.position, required this.pause, this.restarted = false});
|
2023-06-26 18:27:47 +03:00
|
|
|
|
2025-09-23 17:24:57 -04:00
|
|
|
final Duration position;
|
2024-03-05 22:42:22 -05:00
|
|
|
final bool pause;
|
2024-12-05 02:33:46 +05:30
|
|
|
final bool restarted;
|
2023-06-26 18:27:47 +03:00
|
|
|
}
|
|
|
|
|
|
2025-07-25 08:07:22 +05:30
|
|
|
final videoPlayerControlsProvider = StateNotifierProvider<VideoPlayerControls, VideoPlaybackControls>((ref) {
|
2023-06-26 18:27:47 +03:00
|
|
|
return VideoPlayerControls(ref);
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-23 17:24:57 -04:00
|
|
|
const videoPlayerControlsDefault = VideoPlaybackControls(position: Duration.zero, pause: false);
|
2024-12-05 02:33:46 +05:30
|
|
|
|
2023-06-26 18:27:47 +03:00
|
|
|
class VideoPlayerControls extends StateNotifier<VideoPlaybackControls> {
|
2024-12-05 02:33:46 +05:30
|
|
|
VideoPlayerControls(this.ref) : super(videoPlayerControlsDefault);
|
2023-06-26 18:27:47 +03:00
|
|
|
|
|
|
|
|
final Ref ref;
|
|
|
|
|
|
|
|
|
|
VideoPlaybackControls get value => state;
|
|
|
|
|
|
|
|
|
|
set value(VideoPlaybackControls value) {
|
|
|
|
|
state = value;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 22:42:22 -05:00
|
|
|
void reset() {
|
2024-12-05 02:33:46 +05:30
|
|
|
state = videoPlayerControlsDefault;
|
2024-03-05 22:42:22 -05:00
|
|
|
}
|
|
|
|
|
|
2025-09-23 17:24:57 -04:00
|
|
|
Duration get position => state.position;
|
2024-12-05 02:33:46 +05:30
|
|
|
bool get paused => state.pause;
|
2023-06-26 18:27:47 +03:00
|
|
|
|
2025-09-23 17:24:57 -04:00
|
|
|
set position(Duration value) {
|
2024-12-05 02:33:46 +05:30
|
|
|
if (state.position == value) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-06-26 18:27:47 +03:00
|
|
|
|
2024-12-05 02:33:46 +05:30
|
|
|
state = VideoPlaybackControls(position: value, pause: state.pause);
|
2024-03-05 22:42:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pause() {
|
2024-12-05 02:33:46 +05:30
|
|
|
if (state.pause) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state = VideoPlaybackControls(position: state.position, pause: true);
|
2024-03-05 22:42:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void play() {
|
2024-12-05 02:33:46 +05:30
|
|
|
if (!state.pause) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state = VideoPlaybackControls(position: state.position, pause: false);
|
2024-03-05 22:42:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void togglePlay() {
|
2025-07-25 08:07:22 +05:30
|
|
|
state = VideoPlaybackControls(position: state.position, pause: !state.pause);
|
2023-06-26 18:27:47 +03:00
|
|
|
}
|
2024-06-12 12:43:01 -05:00
|
|
|
|
|
|
|
|
void restart() {
|
2025-09-23 17:24:57 -04:00
|
|
|
state = const VideoPlaybackControls(position: Duration.zero, pause: false, restarted: true);
|
2025-07-29 00:34:03 +05:30
|
|
|
ref.read(videoPlaybackValueProvider.notifier).value = ref
|
|
|
|
|
.read(videoPlaybackValueProvider.notifier)
|
|
|
|
|
.value
|
|
|
|
|
.copyWith(state: VideoPlaybackState.playing, position: Duration.zero);
|
2024-06-12 12:43:01 -05:00
|
|
|
}
|
2023-06-26 18:27:47 +03:00
|
|
|
}
|