You've already forked immich
mirror of
https://github.com/immich-app/immich.git
synced 2025-07-14 07:04:24 +02:00
Switches to memoized
This commit is contained in:
@ -11,7 +11,7 @@ import 'package:immich_mobile/shared/models/store.dart' as store;
|
|||||||
/// Provides the initialized video player controller
|
/// Provides the initialized video player controller
|
||||||
/// If the asset is local, use the local file
|
/// If the asset is local, use the local file
|
||||||
/// Otherwise, use a video player with a URL
|
/// Otherwise, use a video player with a URL
|
||||||
ChewieController? useChewieController(
|
AsyncSnapshot<ChewieController> useChewieController(
|
||||||
Asset asset, {
|
Asset asset, {
|
||||||
EdgeInsets controlsSafeAreaMinimum = const EdgeInsets.only(
|
EdgeInsets controlsSafeAreaMinimum = const EdgeInsets.only(
|
||||||
bottom: 100,
|
bottom: 100,
|
||||||
@ -29,96 +29,12 @@ ChewieController? useChewieController(
|
|||||||
VoidCallback? onPaused,
|
VoidCallback? onPaused,
|
||||||
VoidCallback? onVideoEnded,
|
VoidCallback? onVideoEnded,
|
||||||
}) {
|
}) {
|
||||||
return use(
|
|
||||||
_ChewieControllerHook(
|
|
||||||
keys: [asset],
|
|
||||||
asset: asset,
|
|
||||||
placeholder: placeholder,
|
|
||||||
showOptions: showOptions,
|
|
||||||
controlsSafeAreaMinimum: controlsSafeAreaMinimum,
|
|
||||||
autoPlay: autoPlay,
|
|
||||||
allowFullScreen: allowFullScreen,
|
|
||||||
customControls: customControls,
|
|
||||||
hideControlsTimer: hideControlsTimer,
|
|
||||||
showControlsOnInitialize: showControlsOnInitialize,
|
|
||||||
showControls: showControls,
|
|
||||||
allowedScreenSleep: allowedScreenSleep,
|
|
||||||
onPlaying: onPlaying,
|
|
||||||
onPaused: onPaused,
|
|
||||||
onVideoEnded: onVideoEnded,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
class _ChewieControllerHook extends Hook<ChewieController?> {
|
|
||||||
final Asset asset;
|
|
||||||
final EdgeInsets controlsSafeAreaMinimum;
|
|
||||||
final bool showOptions;
|
|
||||||
final bool showControlsOnInitialize;
|
|
||||||
final bool autoPlay;
|
|
||||||
final bool allowFullScreen;
|
|
||||||
final bool allowedScreenSleep;
|
|
||||||
final bool showControls;
|
|
||||||
final Widget? customControls;
|
|
||||||
final Widget? placeholder;
|
|
||||||
final Duration hideControlsTimer;
|
|
||||||
final VoidCallback? onPlaying;
|
|
||||||
final VoidCallback? onPaused;
|
|
||||||
final VoidCallback? onVideoEnded;
|
|
||||||
|
|
||||||
const _ChewieControllerHook({
|
|
||||||
super.keys,
|
|
||||||
required this.asset,
|
|
||||||
this.controlsSafeAreaMinimum = const EdgeInsets.only(
|
|
||||||
bottom: 100,
|
|
||||||
),
|
|
||||||
this.showOptions = true,
|
|
||||||
this.showControlsOnInitialize = false,
|
|
||||||
this.autoPlay = true,
|
|
||||||
this.allowFullScreen = false,
|
|
||||||
this.allowedScreenSleep = false,
|
|
||||||
this.showControls = true,
|
|
||||||
this.customControls,
|
|
||||||
this.placeholder,
|
|
||||||
this.hideControlsTimer = const Duration(seconds: 3),
|
|
||||||
this.onPlaying,
|
|
||||||
this.onPaused,
|
|
||||||
this.onVideoEnded,
|
|
||||||
});
|
|
||||||
|
|
||||||
@override
|
|
||||||
createState() => _ChewieControllerHookState();
|
|
||||||
}
|
|
||||||
|
|
||||||
class _ChewieControllerHookState
|
|
||||||
extends HookState<ChewieController?, _ChewieControllerHook> {
|
|
||||||
ChewieController? chewieController;
|
|
||||||
VideoPlayerController? videoPlayerController;
|
|
||||||
|
|
||||||
@override
|
|
||||||
void initHook() {
|
|
||||||
super.initHook();
|
|
||||||
_initialize().whenComplete(() => setState(() {}));
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
void dispose() {
|
|
||||||
videoPlayerController?.pause();
|
|
||||||
videoPlayerController?.dispose();
|
|
||||||
chewieController?.dispose();
|
|
||||||
super.dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
ChewieController? build(BuildContext context) {
|
|
||||||
return chewieController;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Initializes the chewie controller and video player controller
|
/// Initializes the chewie controller and video player controller
|
||||||
Future<void> _initialize() async {
|
Future<ChewieController> initializeChewie(Asset asset) async {
|
||||||
if (hook.asset.isLocal && hook.asset.livePhotoVideoId == null) {
|
late VideoPlayerController videoPlayerController;
|
||||||
|
if (asset.isLocal && asset.livePhotoVideoId == null) {
|
||||||
// Use a local file for the video player controller
|
// Use a local file for the video player controller
|
||||||
final file = await hook.asset.local!.file;
|
final file = await asset.local!.file;
|
||||||
if (file == null) {
|
if (file == null) {
|
||||||
throw Exception('No file found for the video');
|
throw Exception('No file found for the video');
|
||||||
}
|
}
|
||||||
@ -126,9 +42,9 @@ class _ChewieControllerHookState
|
|||||||
} else {
|
} else {
|
||||||
// Use a network URL for the video player controller
|
// Use a network URL for the video player controller
|
||||||
final serverEndpoint = store.Store.get(store.StoreKey.serverEndpoint);
|
final serverEndpoint = store.Store.get(store.StoreKey.serverEndpoint);
|
||||||
final String videoUrl = hook.asset.livePhotoVideoId != null
|
final String videoUrl = asset.livePhotoVideoId != null
|
||||||
? '$serverEndpoint/asset/file/${hook.asset.livePhotoVideoId}'
|
? '$serverEndpoint/asset/file/${asset.livePhotoVideoId}'
|
||||||
: '$serverEndpoint/asset/file/${hook.asset.remoteId}';
|
: '$serverEndpoint/asset/file/${asset.remoteId}';
|
||||||
|
|
||||||
final url = Uri.parse(videoUrl);
|
final url = Uri.parse(videoUrl);
|
||||||
final accessToken = store.Store.get(StoreKey.accessToken);
|
final accessToken = store.Store.get(StoreKey.accessToken);
|
||||||
@ -139,20 +55,23 @@ class _ChewieControllerHookState
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await videoPlayerController!.initialize();
|
await videoPlayerController.initialize();
|
||||||
|
|
||||||
chewieController = ChewieController(
|
return ChewieController(
|
||||||
videoPlayerController: videoPlayerController!,
|
videoPlayerController: videoPlayerController,
|
||||||
controlsSafeAreaMinimum: hook.controlsSafeAreaMinimum,
|
controlsSafeAreaMinimum: controlsSafeAreaMinimum,
|
||||||
showOptions: hook.showOptions,
|
showOptions: showOptions,
|
||||||
showControlsOnInitialize: hook.showControlsOnInitialize,
|
showControlsOnInitialize: showControlsOnInitialize,
|
||||||
autoPlay: hook.autoPlay,
|
autoPlay: autoPlay,
|
||||||
allowFullScreen: hook.allowFullScreen,
|
allowFullScreen: allowFullScreen,
|
||||||
allowedScreenSleep: hook.allowedScreenSleep,
|
allowedScreenSleep: allowedScreenSleep,
|
||||||
showControls: hook.showControls,
|
showControls: showControls,
|
||||||
customControls: hook.customControls,
|
customControls: customControls,
|
||||||
placeholder: hook.placeholder,
|
placeholder: placeholder,
|
||||||
hideControlsTimer: hook.hideControlsTimer,
|
hideControlsTimer: hideControlsTimer,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final controller = useMemoized(() => initializeChewie(asset));
|
||||||
|
return useFuture(controller);
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ class VideoViewerPage extends HookConsumerWidget {
|
|||||||
),
|
),
|
||||||
showControls: showControls && !isMotionVideo,
|
showControls: showControls && !isMotionVideo,
|
||||||
hideControlsTimer: hideControlsTimer,
|
hideControlsTimer: hideControlsTimer,
|
||||||
);
|
).data;
|
||||||
|
|
||||||
// The last volume of the video used when mute is toggled
|
// The last volume of the video used when mute is toggled
|
||||||
final lastVolume = useState(0.0);
|
final lastVolume = useState(0.0);
|
||||||
|
Reference in New Issue
Block a user