nc-photos/lib/widget/settings.dart

341 lines
11 KiB
Dart
Raw Normal View History

2021-06-23 10:15:25 +02:00
import 'package:event_bus/event_bus.dart';
2021-04-10 06:28:12 +02:00
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:kiwi/kiwi.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
2021-07-25 07:00:38 +02:00
import 'package:nc_photos/app_localizations.dart';
2021-06-23 10:15:25 +02:00
import 'package:nc_photos/event/event.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/k.dart' as k;
2021-06-23 10:15:25 +02:00
import 'package:nc_photos/language_util.dart' as language_util;
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/metadata_task_manager.dart';
import 'package:nc_photos/platform/k.dart' as platform_k;
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/pref.dart';
import 'package:nc_photos/snack_bar_manager.dart';
import 'package:nc_photos/theme.dart';
import 'package:nc_photos/widget/fancy_option_picker.dart';
2021-08-08 20:46:16 +02:00
import 'package:nc_photos/widget/lab_settings.dart';
import 'package:nc_photos/widget/stateful_slider.dart';
import 'package:screen_brightness/screen_brightness.dart';
2021-04-10 06:28:12 +02:00
import 'package:url_launcher/url_launcher.dart';
class SettingsArguments {
SettingsArguments(this.account);
final Account account;
}
class Settings extends StatefulWidget {
static const routeName = "/settings";
2021-07-23 22:05:57 +02:00
static Route buildRoute(SettingsArguments args) => MaterialPageRoute(
builder: (context) => Settings.fromArgs(args),
);
2021-04-10 06:28:12 +02:00
Settings({
2021-07-23 22:05:57 +02:00
Key? key,
required this.account,
2021-04-10 06:28:12 +02:00
}) : super(key: key);
2021-07-23 22:05:57 +02:00
Settings.fromArgs(SettingsArguments args, {Key? key})
2021-04-10 06:28:12 +02:00
: this(
account: args.account,
);
@override
createState() => _SettingsState();
final Account account;
}
class _SettingsState extends State<Settings> {
@override
initState() {
super.initState();
2021-07-23 22:05:57 +02:00
_isEnableExif = Pref.inst().isEnableExifOr();
_screenBrightness = Pref.inst().getViewerScreenBrightnessOr(-1);
2021-04-10 06:28:12 +02:00
}
@override
build(context) {
return AppTheme(
child: Scaffold(
body: Builder(
builder: (context) => _buildContent(context),
),
),
);
}
Widget _buildContent(BuildContext context) {
2021-07-25 07:00:38 +02:00
final translator = L10n.of(context).translator;
2021-04-10 06:28:12 +02:00
return CustomScrollView(
slivers: [
SliverAppBar(
pinned: true,
2021-07-25 07:00:38 +02:00
title: Text(L10n.of(context).settingsWidgetTitle),
2021-04-10 06:28:12 +02:00
),
SliverList(
delegate: SliverChildListDelegate(
[
2021-06-23 10:15:25 +02:00
ListTile(
2021-07-25 07:00:38 +02:00
title: Text(L10n.of(context).settingsLanguageTitle),
2021-06-23 10:15:25 +02:00
subtitle: Text(language_util.getSelectedLanguageName(context)),
onTap: () => _onLanguageTap(context),
),
2021-04-10 06:28:12 +02:00
SwitchListTile(
2021-07-25 07:00:38 +02:00
title: Text(L10n.of(context).settingsExifSupportTitle),
2021-04-10 06:28:12 +02:00
subtitle: _isEnableExif
2021-07-25 07:00:38 +02:00
? Text(L10n.of(context).settingsExifSupportTrueSubtitle)
2021-04-10 06:28:12 +02:00
: null,
value: _isEnableExif,
onChanged: (value) => _onExifSupportChanged(context, value),
),
if (platform_k.isMobile)
SwitchListTile(
title: Text(L10n.of(context).settingsScreenBrightnessTitle),
subtitle: Text(
L10n.of(context).settingsScreenBrightnessDescription),
value: _screenBrightness >= 0,
onChanged: (value) =>
_onScreenBrightnessChanged(context, value),
),
2021-07-25 07:00:38 +02:00
_buildCaption(
context, L10n.of(context).settingsAboutSectionTitle),
2021-04-10 06:28:12 +02:00
ListTile(
2021-07-25 07:00:38 +02:00
title: Text(L10n.of(context).settingsVersionTitle),
2021-04-16 10:45:17 +02:00
subtitle: const Text(k.versionStr),
2021-08-08 20:46:16 +02:00
onTap: () => _onVersionTap(context),
2021-04-10 06:28:12 +02:00
),
ListTile(
2021-07-25 07:00:38 +02:00
title: Text(L10n.of(context).settingsSourceCodeTitle),
2021-04-10 06:28:12 +02:00
subtitle: Text(_sourceRepo),
onTap: () async {
await launch(_sourceRepo);
},
),
2021-06-10 15:49:17 +02:00
ListTile(
2021-07-25 07:00:38 +02:00
title: Text(L10n.of(context).settingsBugReportTitle),
2021-06-10 15:49:17 +02:00
onTap: () {
launch(_bugReportUrl);
},
),
if (translator.isNotEmpty)
ListTile(
2021-07-25 07:00:38 +02:00
title: Text(L10n.of(context).settingsTranslatorTitle),
subtitle: Text(translator),
onTap: () {
launch(_translationUrl);
},
)
else
ListTile(
title: Text("Improve translation"),
subtitle: Text("Help translating to your language"),
onTap: () {
launch(_translationUrl);
},
),
2021-04-10 06:28:12 +02:00
],
),
),
],
);
}
Widget _buildCaption(BuildContext context, String label) {
return Padding(
padding: const EdgeInsets.fromLTRB(16, 16, 16, 8),
child: Text(
label,
style: TextStyle(
color: Theme.of(context).accentColor,
),
),
);
}
2021-06-23 10:15:25 +02:00
void _onLanguageTap(BuildContext context) {
final selected =
Pref.inst().getLanguageOr(language_util.supportedLanguages[0]!.langId);
2021-06-23 10:15:25 +02:00
showDialog(
context: context,
builder: (context) => FancyOptionPicker(
items: language_util.supportedLanguages.values
.map((lang) => FancyOptionPickerItem(
label: lang.nativeName,
isSelected: lang.langId == selected,
onSelect: () {
2021-06-23 10:15:25 +02:00
_log.info(
"[_onLanguageTap] Set language: ${lang.nativeName}");
Navigator.of(context).pop(lang.langId);
},
dense: true,
2021-06-23 10:15:25 +02:00
))
.toList(),
),
).then((value) {
if (value != null) {
Pref.inst().setLanguage(value).then((_) {
KiwiContainer().resolve<EventBus>().fire(LanguageChangedEvent());
});
}
});
}
2021-04-10 06:28:12 +02:00
void _onExifSupportChanged(BuildContext context, bool value) {
if (value) {
showDialog(
context: context,
builder: (context) => AlertDialog(
2021-07-25 07:00:38 +02:00
title: Text(L10n.of(context).exifSupportConfirmationDialogTitle),
content: Text(L10n.of(context).exifSupportDetails),
2021-04-10 06:28:12 +02:00
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(MaterialLocalizations.of(context).cancelButtonLabel),
),
TextButton(
onPressed: () {
Navigator.of(context).pop(true);
},
2021-07-25 07:00:38 +02:00
child: Text(L10n.of(context).enableButtonLabel),
2021-04-10 06:28:12 +02:00
),
],
),
).then((value) {
if (value == true) {
_setExifSupport(true);
}
});
} else {
_setExifSupport(false);
}
}
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: (_) => AppTheme(
child: AlertDialog(
title: Text(L10n.of(context).settingsScreenBrightnessTitle),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(L10n.of(context).settingsScreenBrightnessDescription),
const SizedBox(height: 8),
Row(
mainAxisSize: MainAxisSize.max,
children: [
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);
}
},
),
),
Icon(Icons.brightness_high),
],
),
],
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop((brightness * 100).round());
},
child: Text(MaterialLocalizations.of(context).okButtonLabel),
),
],
),
),
);
if (value != null) {
_setScreenBrightness(value);
}
} finally {
ScreenBrightness.resetScreenBrightness();
}
} else {
_setScreenBrightness(-1);
}
}
2021-08-08 20:46:16 +02:00
void _onVersionTap(BuildContext context) {
if (++_labUnlockCount >= 10) {
Navigator.of(context).pushNamed(LabSettings.routeName);
_labUnlockCount = 0;
}
}
2021-04-10 06:28:12 +02:00
void _setExifSupport(bool value) {
final oldValue = _isEnableExif;
setState(() {
_isEnableExif = value;
});
Pref.inst().setEnableExif(value).then((result) {
if (result) {
if (value) {
MetadataTaskManager().addTask(MetadataTask(widget.account));
2021-04-10 06:28:12 +02:00
}
} else {
_log.severe("[_setExifSupport] Failed writing pref");
SnackBarManager().showSnackBar(SnackBar(
2021-07-25 07:00:38 +02:00
content: Text(L10n.of(context).writePreferenceFailureNotification),
2021-04-10 06:28:12 +02:00
duration: k.snackBarDurationNormal,
));
setState(() {
_isEnableExif = oldValue;
});
}
});
}
void _setScreenBrightness(int value) {
final oldValue = _screenBrightness;
setState(() {
_screenBrightness = value;
});
Pref.inst().setViewerScreenBrightness(value).then((result) {
if (!result) {
_log.severe("[_setScreenBrightness] Failed writing pref");
SnackBarManager().showSnackBar(SnackBar(
content: Text(L10n.of(context).writePreferenceFailureNotification),
duration: k.snackBarDurationNormal,
));
setState(() {
_screenBrightness = oldValue;
});
}
});
}
2021-04-10 06:28:12 +02:00
static const String _sourceRepo = "https://gitlab.com/nkming2/nc-photos";
2021-06-10 15:49:17 +02:00
static const String _bugReportUrl =
"https://gitlab.com/nkming2/nc-photos/-/issues";
2021-04-10 06:28:12 +02:00
static const String _translationUrl =
2021-04-10 09:23:22 +02:00
"https://gitlab.com/nkming2/nc-photos/-/tree/master/lib/l10n";
2021-04-10 06:28:12 +02:00
2021-07-23 22:05:57 +02:00
late bool _isEnableExif;
late int _screenBrightness;
2021-08-08 20:46:16 +02:00
int _labUnlockCount = 0;
2021-04-10 06:28:12 +02:00
static final _log = Logger("widget.settings._SettingsState");
}