nc-photos/app/lib/widget/viewer_mixin.dart

125 lines
3.4 KiB
Dart
Raw Normal View History

2021-09-14 23:00:24 +02:00
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:logging/logging.dart';
import 'package:native_device_orientation/native_device_orientation.dart';
2023-07-17 09:35:45 +02:00
import 'package:nc_photos/entity/pref.dart';
2021-12-10 10:02:56 +01:00
import 'package:nc_photos/throttler.dart';
2021-09-14 23:00:24 +02:00
import 'package:nc_photos/widget/disposable.dart';
2022-12-16 16:01:04 +01:00
import 'package:np_codegen/np_codegen.dart';
2023-08-27 12:58:05 +02:00
import 'package:np_platform_util/np_platform_util.dart';
2021-09-14 23:00:24 +02:00
import 'package:screen_brightness/screen_brightness.dart';
2022-12-16 16:01:04 +01:00
part 'viewer_mixin.g.dart';
2021-09-14 23:00:24 +02:00
mixin ViewerControllersMixin<T extends StatefulWidget>
on DisposableManagerMixin<T> {
@override
initDisposables() {
return [
...super.initDisposables(),
2023-08-27 12:58:05 +02:00
if (getRawPlatform().isMobile) _ViewerBrightnessController(),
2021-09-14 23:00:24 +02:00
_ViewerSystemUiResetter(),
2023-08-27 12:58:05 +02:00
if (getRawPlatform().isMobile && Pref().isViewerForceRotationOr(false))
2021-09-14 23:00:24 +02:00
_ViewerOrientationController(
onChanged: _onOrientationChanged,
),
];
}
void _onOrientationChanged(NativeDeviceOrientation orientation) {
_$__NpLog.log.info("[_onOrientationChanged] $orientation");
2021-09-14 23:00:24 +02:00
if (!mounted) {
return;
}
final List<DeviceOrientation> prefer;
switch (orientation) {
case NativeDeviceOrientation.portraitDown:
prefer = [DeviceOrientation.portraitDown];
break;
case NativeDeviceOrientation.landscapeLeft:
prefer = [DeviceOrientation.landscapeLeft];
break;
case NativeDeviceOrientation.landscapeRight:
prefer = [DeviceOrientation.landscapeRight];
break;
case NativeDeviceOrientation.portraitUp:
default:
prefer = [DeviceOrientation.portraitUp];
break;
}
SystemChrome.setPreferredOrientations(prefer);
}
}
/// Control the screen brightness according to the settings
class _ViewerBrightnessController implements Disposable {
@override
init(State state) {
2021-10-27 22:40:54 +02:00
final brightness = Pref().getViewerScreenBrightness();
2021-09-14 23:00:24 +02:00
if (brightness != null && brightness >= 0) {
2021-10-31 11:18:43 +01:00
ScreenBrightness().setScreenBrightness(brightness / 100.0);
2021-09-14 23:00:24 +02:00
}
}
@override
dispose(State state) {
2021-10-31 11:18:43 +01:00
ScreenBrightness().resetScreenBrightness();
2021-09-14 23:00:24 +02:00
}
}
/// Make sure the system UI overlay is reset on dispose
class _ViewerSystemUiResetter implements Disposable {
@override
init(State state) {}
@override
dispose(State state) {
2021-09-15 17:13:38 +02:00
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
overlays: SystemUiOverlay.values);
2021-09-14 23:00:24 +02:00
}
}
class _ViewerOrientationController implements Disposable {
_ViewerOrientationController({
this.onChanged,
});
@override
init(State state) {
_subscription = NativeDeviceOrientationCommunicator()
.onOrientationChanged(useSensor: true)
.listen((orientation) {
2021-12-10 10:02:56 +01:00
_triggerThrottler.trigger(
data: orientation,
maxResponceTime: const Duration(seconds: 1),
);
2021-09-14 23:00:24 +02:00
});
}
@override
dispose(State state) {
_subscription.cancel();
SystemChrome.setPreferredOrientations([]);
}
2021-12-10 10:02:56 +01:00
void _onTriggered(List<NativeDeviceOrientation> values) {
onChanged?.call(values.last);
}
2021-09-14 23:00:24 +02:00
ValueChanged<NativeDeviceOrientation>? onChanged;
late final StreamSubscription<NativeDeviceOrientation> _subscription;
2021-12-10 10:02:56 +01:00
late final _triggerThrottler = Throttler<NativeDeviceOrientation>(
onTriggered: _onTriggered,
logTag: "_ViewerOrientationController.trigger",
);
2021-09-14 23:00:24 +02:00
}
@npLog
// ignore: camel_case_types
class __ {}