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