From 78ffc38534a0911fb716c7615eb391e0b1ca8e37 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 12 May 2020 17:50:05 +0200 Subject: [PATCH] Create application settings section --- .../account_settings_route.dart | 9 ++ .../application_settings.dart | 109 ++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 lib/ui/routes/account_settings/application_settings.dart diff --git a/lib/ui/routes/account_settings/account_settings_route.dart b/lib/ui/routes/account_settings/account_settings_route.dart index 93010db..b88b9ac 100644 --- a/lib/ui/routes/account_settings/account_settings_route.dart +++ b/lib/ui/routes/account_settings/account_settings_route.dart @@ -1,6 +1,7 @@ import 'package:comunic/ui/routes/account_settings/account_image_settings.dart'; import 'package:comunic/ui/routes/account_settings/account_privacy_settings.dart'; import 'package:comunic/ui/routes/account_settings/account_security_settings.dart'; +import 'package:comunic/ui/routes/account_settings/application_settings.dart'; import 'package:comunic/ui/routes/account_settings/custom_emojies_account_settings.dart'; import 'package:comunic/ui/routes/account_settings/general_account_settings.dart'; import 'package:comunic/utils/intl_utils.dart'; @@ -75,6 +76,14 @@ class __AccountSettingsBodyState extends State<_AccountSettingsBody> { leading: Icon(Icons.security), onTap: () => _openSection(AccountPrivacySettings()), ), + + // Privacy settings + SettingsTile( + title: tr("Application settings"), + subtitle: tr("Manage local application settings"), + leading: Icon(Icons.smartphone), + onTap: () => _openSection(ApplicationSettings()), + ), ], ) ], diff --git a/lib/ui/routes/account_settings/application_settings.dart b/lib/ui/routes/account_settings/application_settings.dart new file mode 100644 index 0000000..3ee1fe7 --- /dev/null +++ b/lib/ui/routes/account_settings/application_settings.dart @@ -0,0 +1,109 @@ +import 'package:comunic/helpers/preferences_helper.dart'; +import 'package:comunic/ui/widgets/async_screen_widget.dart'; +import 'package:comunic/utils/intl_utils.dart'; +import 'package:comunic/utils/ui_utils.dart'; +import 'package:flutter/material.dart'; +import 'package:settings_ui/settings_ui.dart'; + +/// Application settings +/// +/// @author Pierre Hubert + +class ApplicationSettings extends StatelessWidget { + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar(title: Text(tr("Application settings"))), + body: _ApplicationSettingsScreen(), + ); + } +} + +class _ApplicationSettingsScreen extends StatefulWidget { + @override + __ApplicationSettingsScreenState createState() => + __ApplicationSettingsScreenState(); +} + +class __ApplicationSettingsScreenState + extends State<_ApplicationSettingsScreen> { + PreferencesHelper _preferencesHelper; + + Future _refresh() async { + _preferencesHelper = await PreferencesHelper.getInstance(); + } + + @override + Widget build(BuildContext context) => AsyncScreenWidget( + onReload: _refresh, + onBuild: _buildSections, + errorMessage: tr("Could not load settings!")); + + Widget _buildSections() { + return SettingsList( + sections: [_buildAppearanceSection(), _buildGeneralSection()], + ); + } + + /// 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, + ), + ], + ); + + /// General section + SettingsSection _buildGeneralSection() => SettingsSection( + title: tr("General"), + tiles: [ + SettingsTile( + title: tr("About this application"), + subtitle: tr("Learn more about us"), + onTap: () => showAboutAppDialog(context), + ) + ], + ); + + /// Apply new settings + _updatedSettings() { + setState(() {}); + } +} + +class _PreferencesSettingsTile extends SettingsTile { + final PreferencesKeyList preferencesKey; + final String title; + final String subtitle; + final Function onChange; + final PreferencesHelper helper; + + const _PreferencesSettingsTile({ + @required this.preferencesKey, + @required this.title, + @required this.subtitle, + @required this.onChange, + @required this.helper, + }); + + @override + Widget build(BuildContext context) { + return SettingsTile.switchTile( + title: title, + subtitle: subtitle, + onToggle: _doChange, + switchValue: helper.getBool(preferencesKey), + ); + } + + void _doChange(bool value) async { + await helper.setBool(preferencesKey, value); + onChange(); + } +}