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

@ -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;
}
}