Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mob][photos] Ente wakelock utility

laurenspriem7b0f5909b564
diff --git a/mobile/lib/core/configuration.dart b/mobile/lib/core/configuration.dart
index 4809ba8634..8019e2a73c 100644
--- a/mobile/lib/core/configuration.dart
+++ b/mobile/lib/core/configuration.dart
@@ -35,10 +35,10 @@ import 'package:photos/services/sync_service.dart';
import 'package:photos/utils/crypto_util.dart';
import 'package:photos/utils/file_uploader.dart';
import 'package:photos/utils/validator_util.dart';
+import "package:photos/utils/wakelock_util.dart";
import 'package:shared_preferences/shared_preferences.dart';
import "package:tuple/tuple.dart";
import 'package:uuid/uuid.dart';
-import 'package:wakelock_plus/wakelock_plus.dart';
class Configuration {
Configuration._privateConstructor();
@@ -585,7 +585,7 @@ class Configuration {
Future<void> setShouldKeepDeviceAwake(bool value) async {
await _preferences.setBool(keyShouldKeepDeviceAwake, value);
- await WakelockPlus.toggle(enable: value);
+ await EnteWakeLock.toggle(enable: value);
}
Future<void> setShouldBackupVideos(bool value) async {
diff --git a/mobile/lib/ui/viewer/file/video_widget.dart b/mobile/lib/ui/viewer/file/video_widget.dart
index 7f9218e9af..ed772df4fa 100644
--- a/mobile/lib/ui/viewer/file/video_widget.dart
+++ b/mobile/lib/ui/viewer/file/video_widget.dart
@@ -17,9 +17,9 @@ import 'package:photos/ui/viewer/file/video_controls.dart';
import "package:photos/utils/dialog_util.dart";
import 'package:photos/utils/file_util.dart';
import 'package:photos/utils/toast_util.dart';
+import "package:photos/utils/wakelock_util.dart";
import 'package:video_player/video_player.dart';
import 'package:visibility_detector/visibility_detector.dart';
-import 'package:wakelock_plus/wakelock_plus.dart';
class VideoWidget extends StatefulWidget {
final EnteFile file;
@@ -45,7 +45,7 @@ class _VideoWidgetState extends State<VideoWidget> {
ChewieController? _chewieController;
final _progressNotifier = ValueNotifier<double?>(null);
bool _isPlaying = false;
- bool _wakeLockEnabledHere = false;
+ final EnteWakeLock _wakeLock = EnteWakeLock();
@override
void initState() {
@@ -126,13 +126,7 @@ class _VideoWidgetState extends State<VideoWidget> {
_chewieController?.dispose();
_progressNotifier.dispose();
- if (_wakeLockEnabledHere) {
- unawaited(
- WakelockPlus.enabled.then((isEnabled) {
- isEnabled ? WakelockPlus.disable() : null;
- }),
- );
- }
+ _wakeLock.dispose();
super.dispose();
}
@@ -257,17 +251,10 @@ class _VideoWidgetState extends State<VideoWidget> {
Future<void> _keepScreenAliveOnPlaying(bool isPlaying) async {
if (isPlaying) {
- return WakelockPlus.enabled.then((value) {
- if (value == false) {
- WakelockPlus.enable();
- //wakeLockEnabledHere will not be set to true if wakeLock is already enabled from settings on iOS.
- //We shouldn't disable when video is not playing if it was enabled manually by the user from ente settings by user.
- _wakeLockEnabledHere = true;
- }
- });
+ _wakeLock.enable();
}
- if (_wakeLockEnabledHere && !isPlaying) {
- return WakelockPlus.disable();
+ if (!isPlaying) {
+ _wakeLock.disable();
}
}
diff --git a/mobile/lib/utils/wakelock_util.dart b/mobile/lib/utils/wakelock_util.dart
new file mode 100644
index 0000000000..de3de62bb3
--- /dev/null
+++ b/mobile/lib/utils/wakelock_util.dart
@@ -0,0 +1,34 @@
+import "dart:async" show unawaited;
+
+import "package:wakelock_plus/wakelock_plus.dart";
+
+class EnteWakeLock {
+ bool _wakeLockEnabledHere = false;
+
+ void enable() {
+ WakelockPlus.enabled.then((value) {
+ if (value == false) {
+ WakelockPlus.enable();
+ //wakeLockEnabledHere will not be set to true if wakeLock is already enabled from settings on iOS.
+ //We shouldn't disable when video is not playing if it was enabled manually by the user from ente settings by user.
+ _wakeLockEnabledHere = true;
+ }
+ });
+ }
+
+ void disable() {
+ if (_wakeLockEnabledHere) {
+ WakelockPlus.disable();
+ }
+ }
+
+ void dispose() {
+ if (_wakeLockEnabledHere) {
+ unawaited(
+ WakelockPlus.enabled.then((isEnabled) {
+ isEnabled ? WakelockPlus.disable() : null;
+ }),
+ );
+ }
+ }
+}