Extract wakelock as disposable

This commit is contained in:
Ming Ming 2021-09-15 04:39:34 +08:00
parent 88ba985a1a
commit 26da1ec582
2 changed files with 26 additions and 4 deletions

View file

@ -13,8 +13,9 @@ import 'package:nc_photos/snack_bar_manager.dart';
import 'package:nc_photos/theme.dart'; import 'package:nc_photos/theme.dart';
import 'package:nc_photos/use_case/request_public_link.dart'; import 'package:nc_photos/use_case/request_public_link.dart';
import 'package:nc_photos/widget/animated_visibility.dart'; import 'package:nc_photos/widget/animated_visibility.dart';
import 'package:nc_photos/widget/disposable.dart';
import 'package:nc_photos/widget/wakelock_util.dart';
import 'package:video_player/video_player.dart'; import 'package:video_player/video_player.dart';
import 'package:wakelock/wakelock.dart';
class VideoViewer extends StatefulWidget { class VideoViewer extends StatefulWidget {
VideoViewer({ VideoViewer({
@ -41,7 +42,8 @@ class VideoViewer extends StatefulWidget {
final bool canPlay; final bool canPlay;
} }
class _VideoViewerState extends State<VideoViewer> { class _VideoViewerState extends State<VideoViewer>
with DisposableManagerMixin<VideoViewer> {
@override @override
initState() { initState() {
super.initState(); super.initState();
@ -56,8 +58,14 @@ class _VideoViewerState extends State<VideoViewer> {
duration: k.snackBarDurationNormal, duration: k.snackBarDurationNormal,
)); ));
}); });
}
Wakelock.enable(); @override
initDisposables() {
return [
...super.initDisposables(),
WakelockControllerDisposable(),
];
} }
@override @override
@ -81,7 +89,6 @@ class _VideoViewerState extends State<VideoViewer> {
dispose() { dispose() {
super.dispose(); super.dispose();
_controller.dispose(); _controller.dispose();
Wakelock.disable();
} }
void _initController(String url) async { void _initController(String url) async {

View file

@ -0,0 +1,15 @@
import 'package:flutter/widgets.dart';
import 'package:nc_photos/widget/disposable.dart';
import 'package:wakelock/wakelock.dart';
class WakelockControllerDisposable implements Disposable {
@override
init(State state) {
Wakelock.enable();
}
@override
dispose(State state) {
Wakelock.disable();
}
}