refactor: Store and fix missing persistence of some values

This commit is contained in:
krille-chan 2023-11-01 18:00:10 +01:00
parent c10fe91636
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:collection/collection.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:matrix/matrix.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/utils/client_manager.dart';
@ -21,7 +22,8 @@ void main() async {
WidgetsFlutterBinding.ensureInitialized();
Logs().nativeColors = !PlatformInfos.isIOS;
final clients = await ClientManager.getClients();
final store = await SharedPreferences.getInstance();
final clients = await ClientManager.getClients(store: store);
// If the app starts in detached mode, we assume that it is in
// background fetch mode for processing push notifications. This is
@ -32,7 +34,7 @@ void main() async {
// starting the Flutter engine but process incoming push notifications.
BackgroundPush.clientOnly(clients.first);
// To start the flutter engine afterwards we add an custom observer.
WidgetsBinding.instance.addObserver(AppStarter(clients));
WidgetsBinding.instance.addObserver(AppStarter(clients, store));
Logs().i(
'${AppConfig.applicationName} started in background-fetch mode. No GUI will be created unless the app is no longer detached.',
);
@ -43,11 +45,11 @@ void main() async {
Logs().i(
'${AppConfig.applicationName} started in foreground mode. Rendering GUI...',
);
await startGui(clients);
await startGui(clients, store);
}
/// Fetch the pincode for the applock and start the flutter engine.
Future<void> startGui(List<Client> clients) async {
Future<void> startGui(List<Client> clients, SharedPreferences store) async {
// Fetch the pin for the applock if existing for mobile applications.
String? pin;
if (PlatformInfos.isMobile) {
@ -65,16 +67,17 @@ Future<void> startGui(List<Client> clients) async {
await firstClient?.accountDataLoading;
ErrorWidget.builder = (details) => FluffyChatErrorWidget(details);
runApp(FluffyChatApp(clients: clients, pincode: pin));
runApp(FluffyChatApp(clients: clients, pincode: pin, store: store));
}
/// Watches the lifecycle changes to start the application when it
/// is no longer detached.
class AppStarter with WidgetsBindingObserver {
final List<Client> clients;
final SharedPreferences store;
bool guiStarted = false;
AppStarter(this.clients);
AppStarter(this.clients, this.store);
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
@ -84,7 +87,7 @@ class AppStarter with WidgetsBindingObserver {
Logs().i(
'${AppConfig.applicationName} switches from the detached background-fetch mode to ${state.name} mode. Rendering GUI...',
);
startGui(clients);
startGui(clients, store);
// We must make sure that the GUI is only started once.
guiStarted = true;
}

View file

@ -17,7 +17,6 @@ import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/config/themes.dart';
import 'package:fluffychat/pages/chat_list/chat_list_view.dart';
import 'package:fluffychat/pages/settings_security/settings_security.dart';
import 'package:fluffychat/utils/famedlysdk_store.dart';
import 'package:fluffychat/utils/localized_exception_extension.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/client_stories_extension.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
@ -189,7 +188,7 @@ class ChatListController extends State<ChatList>
],
);
if (newServer == null) return;
Store().setItem(_serverStoreNamespace, newServer.single);
Matrix.of(context).store.setString(_serverStoreNamespace, newServer.single);
setState(() {
searchServer = newServer.single;
});
@ -382,7 +381,8 @@ class ChatListController extends State<ChatList>
CallKeepManager().initialize();
WidgetsBinding.instance.addPostFrameCallback((_) async {
if (mounted) {
searchServer = await Store().getItem(_serverStoreNamespace);
searchServer =
Matrix.of(context).store.getString(_serverStoreNamespace);
Matrix.of(context).backgroundPush?.setupPush();
}

View file

@ -30,13 +30,13 @@ class SettingsStyleController extends State<SettingsStyle> {
if (pickedFile == null) return;
await Matrix.of(context)
.store
.setItem(SettingKeys.wallpaper, pickedFile.path);
.setString(SettingKeys.wallpaper, pickedFile.path!);
setState(() {});
}
void deleteWallpaperAction() async {
Matrix.of(context).wallpaper = null;
await Matrix.of(context).store.deleteItem(SettingKeys.wallpaper);
await Matrix.of(context).store.remove(SettingKeys.wallpaper);
setState(() {});
}
@ -76,7 +76,7 @@ class SettingsStyleController extends State<SettingsStyle> {
void changeFontSizeFactor(double d) {
setState(() => AppConfig.fontSizeFactor = d);
Matrix.of(context).store.setItem(
Matrix.of(context).store.setString(
SettingKeys.fontSizeFactor,
AppConfig.fontSizeFactor.toString(),
);

View file

@ -37,7 +37,6 @@ import 'package:fluffychat/widgets/fluffy_chat_app.dart';
import '../config/app_config.dart';
import '../config/setting_keys.dart';
import '../widgets/matrix.dart';
import 'famedlysdk_store.dart';
import 'platform_infos.dart';
//import 'package:fcm_shared_isolate/fcm_shared_isolate.dart';
@ -55,8 +54,7 @@ class BackgroundPush {
String? _fcmToken;
void Function(String errorMsg, {Uri? link})? onFcmError;
L10n? l10n;
Store? _store;
Store get store => _store ??= Store();
Future<void> loadLocale() async {
final context = matrix?.context;
// inspired by _lookupL10n in .dart_tool/flutter_gen/gen_l10n/l10n.dart
@ -271,7 +269,7 @@ class BackgroundPush {
if (matrix == null) {
return;
}
if (await store.getItemBool(SettingKeys.showNoGoogle, false) == true) {
if ((matrix?.store.getBool(SettingKeys.showNoGoogle) ?? false) == true) {
return;
}
await loadLocale();
@ -374,16 +372,17 @@ class BackgroundPush {
oldTokens: oldTokens,
useDeviceSpecificAppId: true,
);
await store.setItem(SettingKeys.unifiedPushEndpoint, newEndpoint);
await store.setItemBool(SettingKeys.unifiedPushRegistered, true);
await matrix?.store.setString(SettingKeys.unifiedPushEndpoint, newEndpoint);
await matrix?.store.setBool(SettingKeys.unifiedPushRegistered, true);
}
Future<void> _upUnregistered(String i) async {
upAction = true;
Logs().i('[Push] Removing UnifiedPush endpoint...');
final oldEndpoint = await store.getItem(SettingKeys.unifiedPushEndpoint);
await store.setItemBool(SettingKeys.unifiedPushRegistered, false);
await store.deleteItem(SettingKeys.unifiedPushEndpoint);
final oldEndpoint =
matrix?.store.getString(SettingKeys.unifiedPushEndpoint);
await matrix?.store.setBool(SettingKeys.unifiedPushRegistered, false);
await matrix?.store.remove(SettingKeys.unifiedPushEndpoint);
if (oldEndpoint?.isNotEmpty ?? false) {
// remove the old pusher
await setupPusher(
@ -409,12 +408,12 @@ class BackgroundPush {
/// Workaround for the problem that local notification IDs must be int but we
/// sort by [roomId] which is a String. To make sure that we don't have duplicated
/// IDs we map the [roomId] to a number and store this number.
/// IDs we map the [roomId] to a number and matrix?.store this number.
late Map<String, int> idMap;
Future<void> _loadIdMap() async {
idMap = Map<String, int>.from(
json.decode(
(await store.getItem(SettingKeys.notificationCurrentIds)) ?? '{}',
(matrix?.store.getString(SettingKeys.notificationCurrentIds)) ?? '{}',
),
);
}
@ -488,7 +487,7 @@ class BackgroundPush {
}
}
if (changed) {
await store.setItem(
await matrix?.store.setString(
SettingKeys.notificationCurrentIds,
json.encode(idMap),
);

View file

@ -1,21 +1,22 @@
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:matrix/encryption/utils/key_verification.dart';
import 'package:matrix/matrix.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:fluffychat/utils/custom_http_client.dart';
import 'package:fluffychat/utils/custom_image_resizer.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/flutter_hive_collections_database.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'famedlysdk_store.dart';
abstract class ClientManager {
static const String clientNamespace = 'im.fluffychat.store.clients';
static Future<List<Client>> getClients({bool initialize = true}) async {
static Future<List<Client>> getClients({
bool initialize = true,
required SharedPreferences store,
}) async {
if (PlatformInfos.isLinux) {
Hive.init((await getApplicationSupportDirectory()).path);
} else {
@ -23,19 +24,15 @@ abstract class ClientManager {
}
final clientNames = <String>{};
try {
final rawClientNames = await Store().getItem(clientNamespace);
if (rawClientNames != null) {
final clientNamesList =
(jsonDecode(rawClientNames) as List).cast<String>();
clientNames.addAll(clientNamesList);
}
final clientNamesList = store.getStringList(clientNamespace) ?? [];
clientNames.addAll(clientNamesList);
} catch (e, s) {
Logs().w('Client names in store are corrupted', e, s);
await Store().deleteItem(clientNamespace);
await store.remove(clientNamespace);
}
if (clientNames.isEmpty) {
clientNames.add(PlatformInfos.clientName);
await Store().setItem(clientNamespace, jsonEncode(clientNames.toList()));
await store.setStringList(clientNamespace, clientNames.toList());
}
final clients = clientNames.map(createClient).toList();
if (initialize) {
@ -61,31 +58,27 @@ abstract class ClientManager {
clientNames.remove(client.clientName);
clients.remove(client);
}
await Store().setItem(clientNamespace, jsonEncode(clientNames.toList()));
await store.setStringList(clientNamespace, clientNames.toList());
}
return clients;
}
static Future<void> addClientNameToStore(String clientName) async {
final clientNamesList = <String>[];
final rawClientNames = await Store().getItem(clientNamespace);
if (rawClientNames != null) {
final stored = (jsonDecode(rawClientNames) as List).cast<String>();
clientNamesList.addAll(stored);
}
static Future<void> addClientNameToStore(
String clientName,
SharedPreferences store,
) async {
final clientNamesList = store.getStringList(clientNamespace) ?? [];
clientNamesList.add(clientName);
await Store().setItem(clientNamespace, jsonEncode(clientNamesList));
await store.setStringList(clientNamespace, clientNamesList);
}
static Future<void> removeClientNameFromStore(String clientName) async {
final clientNamesList = <String>[];
final rawClientNames = await Store().getItem(clientNamespace);
if (rawClientNames != null) {
final stored = (jsonDecode(rawClientNames) as List).cast<String>();
clientNamesList.addAll(stored);
}
static Future<void> removeClientNameFromStore(
String clientName,
SharedPreferences store,
) async {
final clientNamesList = store.getStringList(clientNamespace) ?? [];
clientNamesList.remove(clientName);
await Store().setItem(clientNamespace, jsonEncode(clientNamesList));
await store.setStringList(clientNamespace, clientNamesList);
}
static NativeImplementations get nativeImplementations => kIsWeb

View file

@ -1,43 +0,0 @@
import 'dart:core';
import 'package:shared_preferences/shared_preferences.dart';
class Store {
SharedPreferences? _prefs;
Future<void> _setupLocalStorage() async {
_prefs ??= await SharedPreferences.getInstance();
}
Future<String?> getItem(String key) async {
await _setupLocalStorage();
return _prefs!.getString(key);
}
Future<bool> getItemBool(String key, [bool? defaultValue]) async {
await _setupLocalStorage();
return _prefs!.getBool(key) ?? defaultValue ?? true;
}
Future<void> setItem(String key, String? value) async {
await _setupLocalStorage();
if (value == null) {
await _prefs!.remove(key);
return;
}
await _prefs!.setString(key, value);
return;
}
Future<void> setItemBool(String key, bool value) async {
await _setupLocalStorage();
await _prefs!.setBool(key, value);
return;
}
Future<void> deleteItem(String key) async {
await _setupLocalStorage();
await _prefs!.remove(key);
return;
}
}

View file

@ -98,7 +98,11 @@ Future<void> _tryPushHelper(
//onDidReceiveBackgroundNotificationResponse: onSelectNotification,
);
client ??= (await ClientManager.getClients(initialize: false)).first;
client ??= (await ClientManager.getClients(
initialize: false,
store: await SharedPreferences.getInstance(),
))
.first;
final event = await client.getEventByPushNotification(
notification,
storeInDatabase: isBackgroundMessage,

View file

@ -11,7 +11,6 @@ import 'package:webrtc_interface/webrtc_interface.dart' hide Navigator;
import 'package:fluffychat/pages/chat_list/chat_list.dart';
import 'package:fluffychat/pages/dialer/dialer.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import '../../utils/famedlysdk_store.dart';
import '../../utils/voip/callkeep_manager.dart';
import '../../utils/voip/user_media_manager.dart';
import '../widgets/matrix.dart';
@ -133,7 +132,8 @@ class VoipPlugin with WidgetsBindingObserver implements WebRTCDelegate {
} else {
try {
final wasForeground = await FlutterForegroundTask.isAppOnForeground;
await Store().setItem(
await matrix.store.setString(
'wasForeground',
wasForeground == true ? 'true' : 'false',
);
@ -172,7 +172,7 @@ class VoipPlugin with WidgetsBindingObserver implements WebRTCDelegate {
if (PlatformInfos.isAndroid) {
FlutterForegroundTask.setOnLockScreenVisibility(false);
FlutterForegroundTask.stopService();
final wasForeground = await Store().getItem('wasForeground');
final wasForeground = matrix.store.getString('wasForeground');
wasForeground == 'false' ? FlutterForegroundTask.minimizeApp() : null;
}
}

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(() {});
},
);
}
}

View file

@ -123,11 +123,11 @@ index 85aa8647..3b7e09e7 100644
}
diff --git a/lib/utils/background_push.dart b/lib/utils/background_push.dart
index cd79b0ab..c2db0f1e 100644
index 8e67ae92..da4da5c3 100644
--- a/lib/utils/background_push.dart
+++ b/lib/utils/background_push.dart
@@ -39,7 +39,7 @@ import '../config/setting_keys.dart';
import 'famedlysdk_store.dart';
import '../widgets/matrix.dart';
import 'platform_infos.dart';
-//import 'package:fcm_shared_isolate/fcm_shared_isolate.dart';
@ -135,7 +135,7 @@ index cd79b0ab..c2db0f1e 100644
class NoTokenException implements Exception {
String get cause => 'Cannot get firebase token';
@@ -65,7 +65,7 @@ class BackgroundPush {
@@ -64,7 +64,7 @@ class BackgroundPush {
final pendingTests = <String, Completer<void>>{};