mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-03-13 18:58:53 +01:00
Refactor: extract view settings
This commit is contained in:
parent
e724df1b45
commit
cad6ef6bf6
8 changed files with 536 additions and 219 deletions
|
@ -3,6 +3,7 @@ import 'package:nc_photos/di_container.dart';
|
|||
import 'package:nc_photos/entity/pref.dart';
|
||||
import 'package:nc_photos/language_util.dart' as language_util;
|
||||
import 'package:nc_photos/lazy.dart';
|
||||
import 'package:nc_photos/widget/gps_map.dart';
|
||||
import 'package:np_codegen/np_codegen.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
|
||||
|
@ -80,6 +81,33 @@ class PrefController {
|
|||
value: value,
|
||||
);
|
||||
|
||||
ValueStream<int> get viewerScreenBrightness =>
|
||||
_viewerScreenBrightnessController.stream;
|
||||
|
||||
Future<void> setViewerScreenBrightness(int value) => _set<int>(
|
||||
controller: _viewerScreenBrightnessController,
|
||||
setter: (pref, value) => pref.setViewerScreenBrightness(value),
|
||||
value: value,
|
||||
);
|
||||
|
||||
ValueStream<bool> get isViewerForceRotation =>
|
||||
_isViewerForceRotationController.stream;
|
||||
|
||||
Future<void> setViewerForceRotation(bool value) => _set<bool>(
|
||||
controller: _isViewerForceRotationController,
|
||||
setter: (pref, value) => pref.setViewerForceRotation(value),
|
||||
value: value,
|
||||
);
|
||||
|
||||
ValueStream<GpsMapProvider> get gpsMapProvider =>
|
||||
_gpsMapProviderController.stream;
|
||||
|
||||
Future<void> setGpsMapProvider(GpsMapProvider value) => _set<GpsMapProvider>(
|
||||
controller: _gpsMapProviderController,
|
||||
setter: (pref, value) => pref.setGpsMapProvider(value.index),
|
||||
value: value,
|
||||
);
|
||||
|
||||
Future<void> _set<T>({
|
||||
required BehaviorSubject<T> controller,
|
||||
required Future<bool> Function(Pref pref, T value) setter,
|
||||
|
@ -128,4 +156,10 @@ class PrefController {
|
|||
BehaviorSubject.seeded(_c.pref.getMemoriesRangeOr(2));
|
||||
late final _isPhotosTabSortByNameController =
|
||||
BehaviorSubject.seeded(_c.pref.isPhotosTabSortByNameOr(false));
|
||||
late final _viewerScreenBrightnessController =
|
||||
BehaviorSubject.seeded(_c.pref.getViewerScreenBrightnessOr(-1));
|
||||
late final _isViewerForceRotationController =
|
||||
BehaviorSubject.seeded(_c.pref.isViewerForceRotationOr(false));
|
||||
late final _gpsMapProviderController = BehaviorSubject.seeded(
|
||||
GpsMapProvider.fromValue(_c.pref.getGpsMapProviderOr(0)));
|
||||
}
|
||||
|
|
|
@ -13,14 +13,14 @@ enum GpsMapProvider {
|
|||
// the order must not be changed
|
||||
google,
|
||||
osm,
|
||||
}
|
||||
;
|
||||
|
||||
static GpsMapProvider fromValue(int value) => GpsMapProvider.values[value];
|
||||
|
||||
extension GpsMapProviderExtension on GpsMapProvider {
|
||||
String toUserString() {
|
||||
switch (this) {
|
||||
case GpsMapProvider.google:
|
||||
return "Google Maps";
|
||||
|
||||
case GpsMapProvider.osm:
|
||||
return "OpenStreetMap";
|
||||
}
|
||||
|
|
|
@ -13,13 +13,10 @@ import 'package:nc_photos/language_util.dart' as language_util;
|
|||
import 'package:nc_photos/mobile/platform.dart'
|
||||
if (dart.library.html) 'package:nc_photos/web/platform.dart' as platform;
|
||||
import 'package:nc_photos/platform/features.dart' as features;
|
||||
import 'package:nc_photos/platform/k.dart' as platform_k;
|
||||
import 'package:nc_photos/platform/notification.dart';
|
||||
import 'package:nc_photos/snack_bar_manager.dart';
|
||||
import 'package:nc_photos/stream_util.dart';
|
||||
import 'package:nc_photos/url_launcher_util.dart';
|
||||
import 'package:nc_photos/widget/fancy_option_picker.dart';
|
||||
import 'package:nc_photos/widget/gps_map.dart';
|
||||
import 'package:nc_photos/widget/list_tile_center_leading.dart';
|
||||
import 'package:nc_photos/widget/settings/developer_settings.dart';
|
||||
import 'package:nc_photos/widget/settings/expert_settings.dart';
|
||||
|
@ -28,9 +25,9 @@ import 'package:nc_photos/widget/settings/metadata_settings.dart';
|
|||
import 'package:nc_photos/widget/settings/photos_settings.dart';
|
||||
import 'package:nc_photos/widget/settings/settings_list_caption.dart';
|
||||
import 'package:nc_photos/widget/settings/theme_settings.dart';
|
||||
import 'package:nc_photos/widget/settings/viewer_settings.dart';
|
||||
import 'package:nc_photos/widget/stateful_slider.dart';
|
||||
import 'package:np_codegen/np_codegen.dart';
|
||||
import 'package:screen_brightness/screen_brightness.dart';
|
||||
import 'package:tuple/tuple.dart';
|
||||
|
||||
part 'settings.g.dart';
|
||||
|
@ -126,7 +123,7 @@ class _SettingsState extends State<Settings> {
|
|||
leading: const Icon(Icons.view_carousel_outlined),
|
||||
label: L10n.global().settingsViewerTitle,
|
||||
description: L10n.global().settingsViewerDescription,
|
||||
pageBuilder: () => _ViewerSettings(),
|
||||
pageBuilder: () => const ViewerSettings(),
|
||||
),
|
||||
if (features.isSupportEnhancement)
|
||||
_SubPageItem(
|
||||
|
@ -293,210 +290,6 @@ class _SubPageItem extends StatelessWidget {
|
|||
final Widget Function() pageBuilder;
|
||||
}
|
||||
|
||||
class _ViewerSettings extends StatefulWidget {
|
||||
@override
|
||||
createState() => _ViewerSettingsState();
|
||||
}
|
||||
|
||||
@npLog
|
||||
class _ViewerSettingsState extends State<_ViewerSettings> {
|
||||
@override
|
||||
initState() {
|
||||
super.initState();
|
||||
_screenBrightness = Pref().getViewerScreenBrightnessOr(-1);
|
||||
_isForceRotation = Pref().isViewerForceRotationOr(false);
|
||||
_gpsMapProvider = GpsMapProvider.values[Pref().getGpsMapProviderOr(0)];
|
||||
}
|
||||
|
||||
@override
|
||||
build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Builder(
|
||||
builder: (context) => _buildContent(context),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildContent(BuildContext context) {
|
||||
return CustomScrollView(
|
||||
slivers: [
|
||||
SliverAppBar(
|
||||
pinned: true,
|
||||
title: Text(L10n.global().settingsViewerTitle),
|
||||
),
|
||||
SliverList(
|
||||
delegate: SliverChildListDelegate(
|
||||
[
|
||||
if (platform_k.isMobile)
|
||||
SwitchListTile(
|
||||
title: Text(L10n.global().settingsScreenBrightnessTitle),
|
||||
subtitle:
|
||||
Text(L10n.global().settingsScreenBrightnessDescription),
|
||||
value: _screenBrightness >= 0,
|
||||
onChanged: (value) =>
|
||||
_onScreenBrightnessChanged(context, value),
|
||||
),
|
||||
if (platform_k.isMobile)
|
||||
SwitchListTile(
|
||||
title: Text(L10n.global().settingsForceRotationTitle),
|
||||
subtitle:
|
||||
Text(L10n.global().settingsForceRotationDescription),
|
||||
value: _isForceRotation,
|
||||
onChanged: (value) => _onForceRotationChanged(value),
|
||||
),
|
||||
ListTile(
|
||||
title: Text(L10n.global().settingsMapProviderTitle),
|
||||
subtitle: Text(_gpsMapProvider.toUserString()),
|
||||
onTap: () => _onMapProviderTap(context),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _onScreenBrightnessChanged(
|
||||
BuildContext context, bool value) async {
|
||||
if (value) {
|
||||
var brightness = 0.5;
|
||||
try {
|
||||
await ScreenBrightness().setScreenBrightness(brightness);
|
||||
final value = await showDialog<int>(
|
||||
context: context,
|
||||
builder: (_) => AlertDialog(
|
||||
title: Text(L10n.global().settingsScreenBrightnessTitle),
|
||||
content: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(L10n.global().settingsScreenBrightnessDescription),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.max,
|
||||
children: [
|
||||
const Icon(Icons.brightness_low),
|
||||
Expanded(
|
||||
child: StatefulSlider(
|
||||
initialValue: brightness,
|
||||
min: 0.01,
|
||||
onChangeEnd: (value) async {
|
||||
brightness = value;
|
||||
try {
|
||||
await ScreenBrightness().setScreenBrightness(value);
|
||||
} catch (e, stackTrace) {
|
||||
_log.severe("Failed while setScreenBrightness", e,
|
||||
stackTrace);
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
const Icon(Icons.brightness_high),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: <Widget>[
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop((brightness * 100).round());
|
||||
},
|
||||
child: Text(MaterialLocalizations.of(context).okButtonLabel),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (value != null) {
|
||||
unawaited(_setScreenBrightness(value));
|
||||
}
|
||||
} finally {
|
||||
unawaited(ScreenBrightness().resetScreenBrightness());
|
||||
}
|
||||
} else {
|
||||
unawaited(_setScreenBrightness(-1));
|
||||
}
|
||||
}
|
||||
|
||||
void _onForceRotationChanged(bool value) => _setForceRotation(value);
|
||||
|
||||
Future<void> _onMapProviderTap(BuildContext context) async {
|
||||
final oldValue = _gpsMapProvider;
|
||||
final newValue = await showDialog<GpsMapProvider>(
|
||||
context: context,
|
||||
builder: (context) => FancyOptionPicker(
|
||||
items: GpsMapProvider.values
|
||||
.map((provider) => FancyOptionPickerItem(
|
||||
label: provider.toUserString(),
|
||||
isSelected: provider == oldValue,
|
||||
onSelect: () {
|
||||
_log.info(
|
||||
"[_onMapProviderTap] Set map provider: ${provider.toUserString()}");
|
||||
Navigator.of(context).pop(provider);
|
||||
},
|
||||
))
|
||||
.toList(),
|
||||
),
|
||||
);
|
||||
if (newValue == null || newValue == oldValue) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_gpsMapProvider = newValue;
|
||||
});
|
||||
try {
|
||||
await Pref().setGpsMapProvider(newValue.index);
|
||||
} catch (e, stackTrace) {
|
||||
_log.severe("[_onMapProviderTap] Failed writing pref", e, stackTrace);
|
||||
SnackBarManager().showSnackBar(SnackBar(
|
||||
content: Text(L10n.global().writePreferenceFailureNotification),
|
||||
duration: k.snackBarDurationNormal,
|
||||
));
|
||||
setState(() {
|
||||
_gpsMapProvider = oldValue;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _setScreenBrightness(int value) async {
|
||||
final oldValue = _screenBrightness;
|
||||
setState(() {
|
||||
_screenBrightness = value;
|
||||
});
|
||||
if (!await Pref().setViewerScreenBrightness(value)) {
|
||||
_log.severe("[_setScreenBrightness] Failed writing pref");
|
||||
SnackBarManager().showSnackBar(SnackBar(
|
||||
content: Text(L10n.global().writePreferenceFailureNotification),
|
||||
duration: k.snackBarDurationNormal,
|
||||
));
|
||||
setState(() {
|
||||
_screenBrightness = oldValue;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _setForceRotation(bool value) async {
|
||||
final oldValue = _isForceRotation;
|
||||
setState(() {
|
||||
_isForceRotation = value;
|
||||
});
|
||||
if (!await Pref().setViewerForceRotation(value)) {
|
||||
_log.severe("[_setForceRotation] Failed writing pref");
|
||||
SnackBarManager().showSnackBar(SnackBar(
|
||||
content: Text(L10n.global().writePreferenceFailureNotification),
|
||||
duration: k.snackBarDurationNormal,
|
||||
));
|
||||
setState(() {
|
||||
_isForceRotation = oldValue;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
late int _screenBrightness;
|
||||
late bool _isForceRotation;
|
||||
late GpsMapProvider _gpsMapProvider;
|
||||
}
|
||||
|
||||
class _AlbumSettings extends StatefulWidget {
|
||||
@override
|
||||
createState() => _AlbumSettingsState();
|
||||
|
|
|
@ -13,13 +13,6 @@ extension _$_SettingsStateNpLog on _SettingsState {
|
|||
static final log = Logger("widget.settings._SettingsState");
|
||||
}
|
||||
|
||||
extension _$_ViewerSettingsStateNpLog on _ViewerSettingsState {
|
||||
// ignore: unused_element
|
||||
Logger get _log => log;
|
||||
|
||||
static final log = Logger("widget.settings._ViewerSettingsState");
|
||||
}
|
||||
|
||||
extension _$_AlbumSettingsStateNpLog on _AlbumSettingsState {
|
||||
// ignore: unused_element
|
||||
Logger get _log => log;
|
||||
|
|
71
app/lib/widget/settings/viewer/bloc.dart
Normal file
71
app/lib/widget/settings/viewer/bloc.dart
Normal file
|
@ -0,0 +1,71 @@
|
|||
part of '../viewer_settings.dart';
|
||||
|
||||
@npLog
|
||||
class _Bloc extends Bloc<_Event, _State> with BlocLogger {
|
||||
_Bloc({
|
||||
required this.prefController,
|
||||
}) : super(_State(
|
||||
screenBrightness: prefController.viewerScreenBrightness.value,
|
||||
isForceRotation: prefController.isViewerForceRotation.value,
|
||||
gpsMapProvider: prefController.gpsMapProvider.value,
|
||||
)) {
|
||||
on<_Init>(_onInit);
|
||||
on<_SetScreenBrightness>(_onSetScreenBrightness);
|
||||
on<_SetForceRotation>(_onSetForceRotation);
|
||||
on<_SetGpsMapProvider>(_onSetGpsMapProvider);
|
||||
}
|
||||
|
||||
@override
|
||||
String get tag => _log.fullName;
|
||||
|
||||
Future<void> _onInit(_Init ev, Emitter<_State> emit) async {
|
||||
_log.info(ev);
|
||||
await Future.wait([
|
||||
emit.forEach<int>(
|
||||
prefController.viewerScreenBrightness,
|
||||
onData: (data) => state.copyWith(screenBrightness: data),
|
||||
onError: (e, stackTrace) {
|
||||
_log.severe("[_onInit] Uncaught exception", e, stackTrace);
|
||||
return state.copyWith(error: ExceptionEvent(e, stackTrace));
|
||||
},
|
||||
),
|
||||
emit.forEach<bool>(
|
||||
prefController.isViewerForceRotation,
|
||||
onData: (data) => state.copyWith(isForceRotation: data),
|
||||
onError: (e, stackTrace) {
|
||||
_log.severe("[_onInit] Uncaught exception", e, stackTrace);
|
||||
return state.copyWith(error: ExceptionEvent(e, stackTrace));
|
||||
},
|
||||
),
|
||||
emit.forEach<GpsMapProvider>(
|
||||
prefController.gpsMapProvider,
|
||||
onData: (data) => state.copyWith(gpsMapProvider: data),
|
||||
onError: (e, stackTrace) {
|
||||
_log.severe("[_onInit] Uncaught exception", e, stackTrace);
|
||||
return state.copyWith(error: ExceptionEvent(e, stackTrace));
|
||||
},
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
void _onSetScreenBrightness(_SetScreenBrightness ev, Emitter<_State> emit) {
|
||||
_log.info(ev);
|
||||
if (ev.value < 0) {
|
||||
prefController.setViewerScreenBrightness(-1);
|
||||
} else {
|
||||
prefController.setViewerScreenBrightness((ev.value * 100).round());
|
||||
}
|
||||
}
|
||||
|
||||
void _onSetForceRotation(_SetForceRotation ev, Emitter<_State> emit) {
|
||||
_log.info(ev);
|
||||
prefController.setViewerForceRotation(ev.value);
|
||||
}
|
||||
|
||||
void _onSetGpsMapProvider(_SetGpsMapProvider ev, Emitter<_State> emit) {
|
||||
_log.info(ev);
|
||||
prefController.setGpsMapProvider(ev.value);
|
||||
}
|
||||
|
||||
final PrefController prefController;
|
||||
}
|
63
app/lib/widget/settings/viewer/state_event.dart
Normal file
63
app/lib/widget/settings/viewer/state_event.dart
Normal file
|
@ -0,0 +1,63 @@
|
|||
part of '../viewer_settings.dart';
|
||||
|
||||
@genCopyWith
|
||||
@toString
|
||||
class _State {
|
||||
const _State({
|
||||
required this.screenBrightness,
|
||||
required this.isForceRotation,
|
||||
required this.gpsMapProvider,
|
||||
this.error,
|
||||
});
|
||||
|
||||
@override
|
||||
String toString() => _$toString();
|
||||
|
||||
final int screenBrightness;
|
||||
final bool isForceRotation;
|
||||
final GpsMapProvider gpsMapProvider;
|
||||
|
||||
final ExceptionEvent? error;
|
||||
}
|
||||
|
||||
abstract class _Event {
|
||||
const _Event();
|
||||
}
|
||||
|
||||
@toString
|
||||
class _Init implements _Event {
|
||||
const _Init();
|
||||
|
||||
@override
|
||||
String toString() => _$toString();
|
||||
}
|
||||
|
||||
@toString
|
||||
class _SetScreenBrightness implements _Event {
|
||||
const _SetScreenBrightness(this.value);
|
||||
|
||||
@override
|
||||
String toString() => _$toString();
|
||||
|
||||
final double value;
|
||||
}
|
||||
|
||||
@toString
|
||||
class _SetForceRotation implements _Event {
|
||||
const _SetForceRotation(this.value);
|
||||
|
||||
@override
|
||||
String toString() => _$toString();
|
||||
|
||||
final bool value;
|
||||
}
|
||||
|
||||
@toString
|
||||
class _SetGpsMapProvider implements _Event {
|
||||
const _SetGpsMapProvider(this.value);
|
||||
|
||||
@override
|
||||
String toString() => _$toString();
|
||||
|
||||
final GpsMapProvider value;
|
||||
}
|
251
app/lib/widget/settings/viewer_settings.dart
Normal file
251
app/lib/widget/settings/viewer_settings.dart
Normal file
|
@ -0,0 +1,251 @@
|
|||
import 'package:copy_with/copy_with.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:nc_photos/app_localizations.dart';
|
||||
import 'package:nc_photos/bloc_util.dart';
|
||||
import 'package:nc_photos/controller/pref_controller.dart';
|
||||
import 'package:nc_photos/exception_event.dart';
|
||||
import 'package:nc_photos/exception_util.dart' as exception_util;
|
||||
import 'package:nc_photos/k.dart' as k;
|
||||
import 'package:nc_photos/platform/k.dart' as platform_k;
|
||||
import 'package:nc_photos/snack_bar_manager.dart';
|
||||
import 'package:nc_photos/widget/fancy_option_picker.dart';
|
||||
import 'package:nc_photos/widget/gps_map.dart';
|
||||
import 'package:nc_photos/widget/page_visibility_mixin.dart';
|
||||
import 'package:nc_photos/widget/stateful_slider.dart';
|
||||
import 'package:np_codegen/np_codegen.dart';
|
||||
import 'package:screen_brightness/screen_brightness.dart';
|
||||
import 'package:to_string/to_string.dart';
|
||||
|
||||
part 'viewer/bloc.dart';
|
||||
part 'viewer/state_event.dart';
|
||||
part 'viewer_settings.g.dart';
|
||||
|
||||
typedef _BlocListener = BlocListener<_Bloc, _State>;
|
||||
typedef _BlocSelector<T> = BlocSelector<_Bloc, _State, T>;
|
||||
|
||||
class ViewerSettings extends StatelessWidget {
|
||||
const ViewerSettings({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (_) => _Bloc(
|
||||
prefController: context.read(),
|
||||
),
|
||||
child: const _WrappedViewerSettings(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _WrappedViewerSettings extends StatefulWidget {
|
||||
const _WrappedViewerSettings();
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _WrappedViewerSettingsState();
|
||||
}
|
||||
|
||||
@npLog
|
||||
class _WrappedViewerSettingsState extends State<_WrappedViewerSettings>
|
||||
with RouteAware, PageVisibilityMixin {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_bloc.add(const _Init());
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: MultiBlocListener(
|
||||
listeners: [
|
||||
_BlocListener(
|
||||
listenWhen: (previous, current) => previous.error != current.error,
|
||||
listener: (context, state) {
|
||||
if (state.error != null && isPageVisible()) {
|
||||
SnackBarManager().showSnackBar(SnackBar(
|
||||
content:
|
||||
Text(exception_util.toUserString(state.error!.error)),
|
||||
duration: k.snackBarDurationNormal,
|
||||
));
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
child: CustomScrollView(
|
||||
slivers: [
|
||||
SliverAppBar(
|
||||
pinned: true,
|
||||
title: Text(L10n.global().settingsViewerTitle),
|
||||
),
|
||||
SliverList(
|
||||
delegate: SliverChildListDelegate(
|
||||
[
|
||||
if (platform_k.isMobile)
|
||||
_BlocSelector<int>(
|
||||
selector: (state) => state.screenBrightness,
|
||||
builder: (context, state) {
|
||||
return SwitchListTile(
|
||||
title:
|
||||
Text(L10n.global().settingsScreenBrightnessTitle),
|
||||
subtitle: Text(L10n.global()
|
||||
.settingsScreenBrightnessDescription),
|
||||
value: state >= 0,
|
||||
onChanged: (value) =>
|
||||
_onScreenBrightnessChanged(context, value),
|
||||
);
|
||||
},
|
||||
),
|
||||
if (platform_k.isMobile)
|
||||
_BlocSelector<bool>(
|
||||
selector: (state) => state.isForceRotation,
|
||||
builder: (context, state) {
|
||||
return SwitchListTile(
|
||||
title: Text(L10n.global().settingsForceRotationTitle),
|
||||
subtitle: Text(
|
||||
L10n.global().settingsForceRotationDescription),
|
||||
value: state,
|
||||
onChanged: (value) {
|
||||
_bloc.add(_SetForceRotation(value));
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
_BlocSelector<GpsMapProvider>(
|
||||
selector: (state) => state.gpsMapProvider,
|
||||
builder: (context, state) {
|
||||
return ListTile(
|
||||
title: Text(L10n.global().settingsMapProviderTitle),
|
||||
subtitle: Text(state.toUserString()),
|
||||
onTap: () => _onMapProviderTap(context),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _onScreenBrightnessChanged(
|
||||
BuildContext context, bool value) async {
|
||||
if (!value) {
|
||||
_bloc.add(const _SetScreenBrightness(-1));
|
||||
return;
|
||||
}
|
||||
|
||||
final result = await showDialog<double>(
|
||||
context: context,
|
||||
builder: (_) => const _BrightnessDialog(initialValue: 0.5),
|
||||
);
|
||||
if (!context.mounted || result == null) {
|
||||
return;
|
||||
}
|
||||
_bloc.add(_SetScreenBrightness(result));
|
||||
}
|
||||
|
||||
Future<void> _onMapProviderTap(BuildContext context) async {
|
||||
final result = await showDialog<GpsMapProvider>(
|
||||
context: context,
|
||||
builder: (context) => FancyOptionPicker(
|
||||
items: GpsMapProvider.values
|
||||
.map((provider) => FancyOptionPickerItem(
|
||||
label: provider.toUserString(),
|
||||
isSelected: provider == _bloc.state.gpsMapProvider,
|
||||
onSelect: () {
|
||||
_log.info(
|
||||
"[_onMapProviderTap] Set map provider: ${provider.toUserString()}");
|
||||
Navigator.of(context).pop(provider);
|
||||
},
|
||||
))
|
||||
.toList(),
|
||||
),
|
||||
);
|
||||
if (!context.mounted ||
|
||||
result == null ||
|
||||
result == _bloc.state.gpsMapProvider) {
|
||||
return;
|
||||
}
|
||||
_bloc.add(_SetGpsMapProvider(result));
|
||||
}
|
||||
|
||||
late final _bloc = context.read<_Bloc>();
|
||||
}
|
||||
|
||||
class _BrightnessDialog extends StatefulWidget {
|
||||
const _BrightnessDialog({
|
||||
required this.initialValue,
|
||||
});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _BrightnessDialogState();
|
||||
|
||||
final double initialValue;
|
||||
}
|
||||
|
||||
@npLog
|
||||
class _BrightnessDialogState extends State<_BrightnessDialog> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
ScreenBrightness().setScreenBrightness(widget.initialValue);
|
||||
_value = widget.initialValue;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
ScreenBrightness().resetScreenBrightness();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text(L10n.global().settingsScreenBrightnessTitle),
|
||||
content: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(L10n.global().settingsScreenBrightnessDescription),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.max,
|
||||
children: [
|
||||
const Icon(Icons.brightness_low),
|
||||
Expanded(
|
||||
child: StatefulSlider(
|
||||
initialValue: widget.initialValue,
|
||||
min: 0.01,
|
||||
onChangeEnd: (value) async {
|
||||
_value = value;
|
||||
try {
|
||||
await ScreenBrightness().setScreenBrightness(value);
|
||||
} catch (e, stackTrace) {
|
||||
_log.severe(
|
||||
"Failed while setScreenBrightness", e, stackTrace);
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
const Icon(Icons.brightness_high),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop(_value);
|
||||
},
|
||||
child: Text(MaterialLocalizations.of(context).okButtonLabel),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
late double _value;
|
||||
}
|
112
app/lib/widget/settings/viewer_settings.g.dart
Normal file
112
app/lib/widget/settings/viewer_settings.g.dart
Normal file
|
@ -0,0 +1,112 @@
|
|||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'viewer_settings.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// CopyWithLintRuleGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// ignore_for_file: library_private_types_in_public_api, duplicate_ignore
|
||||
|
||||
// **************************************************************************
|
||||
// CopyWithGenerator
|
||||
// **************************************************************************
|
||||
|
||||
abstract class $_StateCopyWithWorker {
|
||||
_State call(
|
||||
{int? screenBrightness,
|
||||
bool? isForceRotation,
|
||||
GpsMapProvider? gpsMapProvider,
|
||||
ExceptionEvent? error});
|
||||
}
|
||||
|
||||
class _$_StateCopyWithWorkerImpl implements $_StateCopyWithWorker {
|
||||
_$_StateCopyWithWorkerImpl(this.that);
|
||||
|
||||
@override
|
||||
_State call(
|
||||
{dynamic screenBrightness,
|
||||
dynamic isForceRotation,
|
||||
dynamic gpsMapProvider,
|
||||
dynamic error = copyWithNull}) {
|
||||
return _State(
|
||||
screenBrightness: screenBrightness as int? ?? that.screenBrightness,
|
||||
isForceRotation: isForceRotation as bool? ?? that.isForceRotation,
|
||||
gpsMapProvider:
|
||||
gpsMapProvider as GpsMapProvider? ?? that.gpsMapProvider,
|
||||
error: error == copyWithNull ? that.error : error as ExceptionEvent?);
|
||||
}
|
||||
|
||||
final _State that;
|
||||
}
|
||||
|
||||
extension $_StateCopyWith on _State {
|
||||
$_StateCopyWithWorker get copyWith => _$copyWith;
|
||||
$_StateCopyWithWorker get _$copyWith => _$_StateCopyWithWorkerImpl(this);
|
||||
}
|
||||
|
||||
// **************************************************************************
|
||||
// NpLogGenerator
|
||||
// **************************************************************************
|
||||
|
||||
extension _$_WrappedViewerSettingsStateNpLog on _WrappedViewerSettingsState {
|
||||
// ignore: unused_element
|
||||
Logger get _log => log;
|
||||
|
||||
static final log =
|
||||
Logger("widget.settings.viewer_settings._WrappedViewerSettingsState");
|
||||
}
|
||||
|
||||
extension _$_BrightnessDialogStateNpLog on _BrightnessDialogState {
|
||||
// ignore: unused_element
|
||||
Logger get _log => log;
|
||||
|
||||
static final log =
|
||||
Logger("widget.settings.viewer_settings._BrightnessDialogState");
|
||||
}
|
||||
|
||||
extension _$_BlocNpLog on _Bloc {
|
||||
// ignore: unused_element
|
||||
Logger get _log => log;
|
||||
|
||||
static final log = Logger("widget.settings.viewer_settings._Bloc");
|
||||
}
|
||||
|
||||
// **************************************************************************
|
||||
// ToStringGenerator
|
||||
// **************************************************************************
|
||||
|
||||
extension _$_StateToString on _State {
|
||||
String _$toString() {
|
||||
// ignore: unnecessary_string_interpolations
|
||||
return "_State {screenBrightness: $screenBrightness, isForceRotation: $isForceRotation, gpsMapProvider: ${gpsMapProvider.name}, error: $error}";
|
||||
}
|
||||
}
|
||||
|
||||
extension _$_InitToString on _Init {
|
||||
String _$toString() {
|
||||
// ignore: unnecessary_string_interpolations
|
||||
return "_Init {}";
|
||||
}
|
||||
}
|
||||
|
||||
extension _$_SetScreenBrightnessToString on _SetScreenBrightness {
|
||||
String _$toString() {
|
||||
// ignore: unnecessary_string_interpolations
|
||||
return "_SetScreenBrightness {value: ${value.toStringAsFixed(3)}}";
|
||||
}
|
||||
}
|
||||
|
||||
extension _$_SetForceRotationToString on _SetForceRotation {
|
||||
String _$toString() {
|
||||
// ignore: unnecessary_string_interpolations
|
||||
return "_SetForceRotation {value: $value}";
|
||||
}
|
||||
}
|
||||
|
||||
extension _$_SetGpsMapProviderToString on _SetGpsMapProvider {
|
||||
String _$toString() {
|
||||
// ignore: unnecessary_string_interpolations
|
||||
return "_SetGpsMapProvider {value: ${value.name}}";
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue