1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 21:09:21 +00:00
comunicmobile/lib/ui/routes/settings/application_settings.dart

122 lines
3.4 KiB
Dart
Raw Normal View History

2020-05-12 15:50:05 +00:00
import 'package:comunic/helpers/preferences_helper.dart';
import 'package:comunic/ui/widgets/async_screen_widget.dart';
2021-02-12 21:36:22 +00:00
import 'package:comunic/ui/widgets/settings/header_spacer_section.dart';
2021-04-27 07:44:11 +00:00
import 'package:comunic/utils/flutter_utils.dart';
2020-05-12 15:50:05 +00:00
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart';
2021-12-28 14:23:08 +00:00
import 'package:flutter_settings_ui/flutter_settings_ui.dart';
2020-05-12 15:50:05 +00:00
/// Application settings
///
/// @author Pierre Hubert
2020-05-13 16:12:45 +00:00
class ApplicationSettings extends StatefulWidget {
2020-05-12 15:50:05 +00:00
@override
2020-05-13 16:21:30 +00:00
_ApplicationSettingsState createState() => _ApplicationSettingsState();
2020-05-12 15:50:05 +00:00
}
2020-05-13 16:21:30 +00:00
class _ApplicationSettingsState extends State<ApplicationSettings> {
PreferencesHelper? _preferencesHelper;
2020-05-12 15:50:05 +00:00
Future<void> _refresh() async {
_preferencesHelper = await PreferencesHelper.getInstance();
}
@override
Widget build(BuildContext context) => AsyncScreenWidget(
onReload: _refresh,
onBuild: _buildSections,
errorMessage: tr("Could not load settings!")!);
2020-05-12 15:50:05 +00:00
Widget _buildSections() {
return SettingsList(
2020-05-13 16:21:30 +00:00
sections: [
2021-02-12 21:36:22 +00:00
HeadSpacerSection(),
2020-05-13 16:21:30 +00:00
_buildAppearanceSection(),
_buildDebugSection()
],
2020-05-12 15:50:05 +00:00
);
}
/// Appearance section
SettingsSection _buildAppearanceSection() => SettingsSection(
title: tr("Appearance"),
tiles: [
_PreferencesSettingsTile(
preferencesKey: PreferencesKeyList.ENABLE_DARK_THEME,
title: tr("Enable dark theme"),
subtitle: null,
onChange: _updatedSettings,
helper: _preferencesHelper,
),
],
);
2020-05-13 16:21:30 +00:00
/// Debug section
SettingsSection _buildDebugSection() => SettingsSection(
title: tr("Debug features"),
tiles: [
2020-05-13 16:26:21 +00:00
// Force mobile mode
2020-05-13 16:21:30 +00:00
_PreferencesSettingsTile(
preferencesKey: PreferencesKeyList.FORCE_MOBILE_MODE,
title: tr("Force mobile mode"),
2021-04-27 07:44:11 +00:00
subtitle: isIOS
? null
: tr(
"Force the smartphone mode of the application to be used, even when tablet mode could be used."),
2020-05-13 16:21:30 +00:00
onChange: _updatedSettings,
helper: _preferencesHelper,
),
2020-05-13 16:26:21 +00:00
// Show performances overlay
_PreferencesSettingsTile(
preferencesKey: PreferencesKeyList.SHOW_PERFORMANCE_OVERLAY,
title: tr("Show performances overlay"),
subtitle: null,
onChange: _updatedSettings,
helper: _preferencesHelper,
),
2020-05-13 16:21:30 +00:00
],
);
2020-05-12 15:50:05 +00:00
/// Apply new settings
_updatedSettings() {
setState(() {});
applyNewThemeSettings(context);
2020-05-12 15:50:05 +00:00
}
}
class _PreferencesSettingsTile extends SettingsTile {
final PreferencesKeyList preferencesKey;
final String? title;
final String? subtitle;
2020-05-12 15:50:05 +00:00
final Function onChange;
final PreferencesHelper? helper;
2020-05-12 15:50:05 +00:00
2021-12-28 14:23:08 +00:00
_PreferencesSettingsTile({
required this.preferencesKey,
required this.title,
required this.subtitle,
required this.onChange,
required this.helper,
2020-05-12 15:50:05 +00:00
});
@override
Widget build(BuildContext context) {
return SettingsTile.switchTile(
title: title,
subtitle: subtitle,
onToggle: _doChange,
switchValue: helper!.getBool(preferencesKey),
2021-04-17 06:40:33 +00:00
titleMaxLines: 2,
2021-04-27 07:44:11 +00:00
subtitleMaxLines: 4,
2020-05-12 15:50:05 +00:00
);
}
void _doChange(bool value) async {
await helper!.setBool(preferencesKey, value);
2020-05-12 15:50:05 +00:00
onChange();
}
}