refactor: Store and fix missing persistence of some values

This commit is contained in:
krille-chan 2023-11-01 18:00:10 +01:00
commit c9c2620ad4
No known key found for this signature in database
12 changed files with 120 additions and 165 deletions

View file

@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:go_router/go_router.dart';
import 'package:matrix/matrix.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:fluffychat/config/routes.dart';
import 'package:fluffychat/config/themes.dart';
@ -16,11 +17,13 @@ class FluffyChatApp extends StatelessWidget {
final Widget? testWidget;
final List<Client> clients;
final String? pincode;
final SharedPreferences store;
const FluffyChatApp({
super.key,
this.testWidget,
required this.clients,
required this.store,
this.pincode,
});
@ -55,6 +58,7 @@ class FluffyChatApp extends StatelessWidget {
onGenerateRoute: (_) => MaterialPageRoute(
builder: (_) => Matrix(
clients: clients,
store: store,
child: testWidget ?? child,
),
),

View file

@ -15,6 +15,7 @@ import 'package:image_picker/image_picker.dart';
import 'package:matrix/encryption.dart';
import 'package:matrix/matrix.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:universal_html/html.dart' as html;
import 'package:url_launcher/url_launcher_string.dart';
@ -29,7 +30,6 @@ import '../config/setting_keys.dart';
import '../pages/key_verification/key_verification_dialog.dart';
import '../utils/account_bundles.dart';
import '../utils/background_push.dart';
import '../utils/famedlysdk_store.dart';
import 'local_notifications_extension.dart';
// import 'package:flutter_secure_storage/flutter_secure_storage.dart';
@ -41,9 +41,12 @@ class Matrix extends StatefulWidget {
final Map<String, String>? queryParameters;
final SharedPreferences store;
const Matrix({
this.child,
required this.clients,
required this.store,
this.queryParameters,
super.key,
});
@ -59,7 +62,7 @@ class Matrix extends StatefulWidget {
class MatrixState extends State<Matrix> with WidgetsBindingObserver {
int _activeClient = -1;
String? activeBundle;
Store store = Store();
SharedPreferences get store => widget.store;
HomeserverSummary? loginHomeserverSummary;
XFile? loginAvatar;
@ -158,7 +161,10 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
if (!widget.clients.contains(_loginClientCandidate)) {
widget.clients.add(_loginClientCandidate!);
}
ClientManager.addClientNameToStore(_loginClientCandidate!.clientName);
ClientManager.addClientNameToStore(
_loginClientCandidate!.clientName,
store,
);
_registerSubs(_loginClientCandidate!.clientName);
_loginClientCandidate = null;
FluffyChatApp.router.go('/rooms');
@ -187,7 +193,7 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
try {
if (client.isLogged()) {
// TODO: Figure out how this works in multi account
final statusMsg = await store.getItem(SettingKeys.ownStatusMessage);
final statusMsg = store.getString(SettingKeys.ownStatusMessage);
if (statusMsg?.isNotEmpty ?? false) {
Logs().v('Send cached status message: "$statusMsg"');
await client.setPresence(
@ -314,7 +320,7 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
if (loggedInWithMultipleClients && state != LoginState.loggedIn) {
_cancelSubs(c.clientName);
widget.clients.remove(c);
ClientManager.removeClientNameFromStore(c.clientName);
ClientManager.removeClientNameFromStore(c.clientName, store);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(L10n.of(context)!.oneClientLoggedOut),
@ -391,7 +397,7 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
);
}
if (result == OkCancelResult.cancel) {
await store.setItemBool(SettingKeys.showNoGoogle, true);
await store.setBool(SettingKeys.showNoGoogle, true);
}
},
);
@ -401,7 +407,7 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
}
void createVoipPlugin() async {
if (await store.getItemBool(SettingKeys.experimentalVoip) == false) {
if (store.getBool(SettingKeys.experimentalVoip) == false) {
voipPlugin = null;
return;
}
@ -419,47 +425,40 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
}
void initSettings() {
store.getItem(SettingKeys.wallpaper).then((final path) async {
if (path == null) return;
final file = File(path);
if (await file.exists()) {
wallpaper = file;
}
});
store.getItem(SettingKeys.fontSizeFactor).then(
(value) => AppConfig.fontSizeFactor =
double.tryParse(value ?? '') ?? AppConfig.fontSizeFactor,
);
store
.getItemBool(SettingKeys.renderHtml, AppConfig.renderHtml)
.then((value) => AppConfig.renderHtml = value);
store
.getItemBool(
SettingKeys.hideRedactedEvents,
AppConfig.hideRedactedEvents,
)
.then((value) => AppConfig.hideRedactedEvents = value);
store
.getItemBool(SettingKeys.hideUnknownEvents, AppConfig.hideUnknownEvents)
.then((value) => AppConfig.hideUnknownEvents = value);
store
.getItemBool(SettingKeys.separateChatTypes, AppConfig.separateChatTypes)
.then((value) => AppConfig.separateChatTypes = value);
store
.getItemBool(SettingKeys.autoplayImages, AppConfig.autoplayImages)
.then((value) => AppConfig.autoplayImages = value);
store
.getItemBool(
SettingKeys.sendTypingNotifications,
AppConfig.sendTypingNotifications,
)
.then((value) => AppConfig.sendTypingNotifications = value);
store
.getItemBool(SettingKeys.sendOnEnter, AppConfig.sendOnEnter)
.then((value) => AppConfig.sendOnEnter = value);
store
.getItemBool(SettingKeys.experimentalVoip, AppConfig.experimentalVoip)
.then((value) => AppConfig.experimentalVoip = value);
final path = store.getString(SettingKeys.wallpaper);
if (path != null) wallpaper = File(path);
AppConfig.fontSizeFactor =
double.tryParse(store.getString(SettingKeys.fontSizeFactor) ?? '') ??
AppConfig.fontSizeFactor;
AppConfig.renderHtml =
store.getBool(SettingKeys.renderHtml) ?? AppConfig.renderHtml;
AppConfig.hideRedactedEvents =
store.getBool(SettingKeys.hideRedactedEvents) ??
AppConfig.hideRedactedEvents;
AppConfig.hideUnknownEvents =
store.getBool(SettingKeys.hideUnknownEvents) ??
AppConfig.hideUnknownEvents;
AppConfig.separateChatTypes =
store.getBool(SettingKeys.separateChatTypes) ??
AppConfig.separateChatTypes;
AppConfig.autoplayImages =
store.getBool(SettingKeys.autoplayImages) ?? AppConfig.autoplayImages;
AppConfig.sendTypingNotifications =
store.getBool(SettingKeys.sendTypingNotifications) ??
AppConfig.sendTypingNotifications;
AppConfig.sendOnEnter =
store.getBool(SettingKeys.sendOnEnter) ?? AppConfig.sendOnEnter;
AppConfig.experimentalVoip = store.getBool(SettingKeys.experimentalVoip) ??
AppConfig.experimentalVoip;
}
@override

View file

@ -23,19 +23,15 @@ class SettingsSwitchListTile extends StatefulWidget {
class SettingsSwitchListTileState extends State<SettingsSwitchListTile> {
@override
Widget build(BuildContext context) {
return FutureBuilder<bool>(
future: Matrix.of(context)
.store
.getItemBool(widget.storeKey, widget.defaultValue),
builder: (context, snapshot) => SwitchListTile.adaptive(
value: snapshot.data ?? widget.defaultValue,
title: Text(widget.title),
onChanged: (bool newValue) async {
widget.onChanged?.call(newValue);
await Matrix.of(context).store.setItemBool(widget.storeKey, newValue);
setState(() {});
},
),
return SwitchListTile.adaptive(
value: Matrix.of(context).store.getBool(widget.storeKey) ??
widget.defaultValue,
title: Text(widget.title),
onChanged: (bool newValue) async {
widget.onChanged?.call(newValue);
await Matrix.of(context).store.setBool(widget.storeKey, newValue);
setState(() {});
},
);
}
}