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