From 03b00b7d2617eaabd7096a7b61f7951c2d040338 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Tue, 19 Jan 2021 16:36:21 +0100 Subject: [PATCH] fix: Persistent settings --- lib/components/settings_switch_list_tile.dart | 41 +++++++++++++++++ lib/views/settings.dart | 44 +++++++------------ 2 files changed, 57 insertions(+), 28 deletions(-) create mode 100644 lib/components/settings_switch_list_tile.dart diff --git a/lib/components/settings_switch_list_tile.dart b/lib/components/settings_switch_list_tile.dart new file mode 100644 index 00000000..bd960f86 --- /dev/null +++ b/lib/components/settings_switch_list_tile.dart @@ -0,0 +1,41 @@ +import 'package:flutter/material.dart'; + +import 'matrix.dart'; + +class SettingsSwitchListTile extends StatefulWidget { + final bool defaultValue; + final String storeKey; + final String title; + final Function(bool) onChanged; + + const SettingsSwitchListTile({ + Key key, + this.defaultValue = false, + @required this.storeKey, + @required this.title, + this.onChanged, + }) : super(key: key); + + @override + _SettingsSwitchListTileState createState() => _SettingsSwitchListTileState(); +} + +class _SettingsSwitchListTileState extends State { + @override + Widget build(BuildContext context) { + return FutureBuilder( + future: Matrix.of(context).store.getItemBool(widget.storeKey), + builder: (context, snapshot) => SwitchListTile( + value: snapshot.data ?? widget.defaultValue, + title: Text(widget.title), + onChanged: (bool newValue) async { + widget.onChanged?.call(newValue); + await Matrix.of(context) + .store + .setItem(widget.storeKey, newValue.toString()); + setState(() => null); + }, + ), + ); + } +} diff --git a/lib/views/settings.dart b/lib/views/settings.dart index 1c08dd0b..b074e4b5 100644 --- a/lib/views/settings.dart +++ b/lib/views/settings.dart @@ -2,6 +2,7 @@ import 'package:adaptive_dialog/adaptive_dialog.dart'; import 'package:adaptive_page_layout/adaptive_page_layout.dart'; import 'package:fluffychat/components/dialogs/bootstrap_dialog.dart'; import 'package:fluffychat/components/sentry_switch_list_tile.dart'; +import 'package:fluffychat/components/settings_switch_list_tile.dart'; import 'package:flushbar/flushbar_helper.dart'; import 'package:famedlysdk/famedlysdk.dart'; import 'package:file_picker_cross/file_picker_cross.dart'; @@ -382,36 +383,23 @@ class _SettingsState extends State { AdaptivePageLayout.of(context).pushNamed('/settings/style'), trailing: Icon(Icons.style_outlined), ), - SwitchListTile( - title: Text(L10n.of(context).renderRichContent), - value: AppConfig.renderHtml, - onChanged: (bool newValue) async { - AppConfig.renderHtml = newValue; - await Matrix.of(context) - .store - .setItem(SettingKeys.renderHtml, newValue.toString()); - setState(() => null); - }, + SettingsSwitchListTile( + title: L10n.of(context).renderRichContent, + onChanged: (b) => AppConfig.renderHtml = b, + storeKey: SettingKeys.renderHtml, + defaultValue: AppConfig.renderHtml, ), - SwitchListTile( - title: Text(L10n.of(context).hideRedactedEvents), - value: AppConfig.hideRedactedEvents, - onChanged: (bool newValue) async { - AppConfig.hideRedactedEvents = newValue; - await Matrix.of(context).store.setItem( - SettingKeys.hideRedactedEvents, newValue.toString()); - setState(() => null); - }, + SettingsSwitchListTile( + title: L10n.of(context).hideRedactedEvents, + onChanged: (b) => AppConfig.hideRedactedEvents = b, + storeKey: SettingKeys.hideRedactedEvents, + defaultValue: AppConfig.hideRedactedEvents, ), - SwitchListTile( - title: Text(L10n.of(context).hideUnknownEvents), - value: AppConfig.hideUnknownEvents, - onChanged: (bool newValue) async { - AppConfig.hideUnknownEvents = newValue; - await Matrix.of(context).store.setItem( - SettingKeys.hideUnknownEvents, newValue.toString()); - setState(() => null); - }, + SettingsSwitchListTile( + title: L10n.of(context).hideUnknownEvents, + onChanged: (b) => AppConfig.hideUnknownEvents = b, + storeKey: SettingKeys.hideUnknownEvents, + defaultValue: AppConfig.hideUnknownEvents, ), ListTile( title: Text(L10n.of(context).emoteSettings),