chore: Use legacy database if database build fails

This commit is contained in:
krille-chan 2023-12-31 08:39:05 +01:00
parent be6165f422
commit ac5bd56c03
No known key found for this signature in database
3 changed files with 39 additions and 22 deletions

View file

@ -2384,5 +2384,13 @@
"addChatOrSubSpace": "Add chat or sub space",
"subspace": "Subspace",
"decline": "Decline",
"thisDevice": "This device:"
"thisDevice": "This device:",
"initAppError": "An error occured while init the app",
"databaseBuildErrorBody": "Unable to build the SQlite database. The app tries to use the legacy database for now. Please report this error to the developers at {url}",
"@databaseBuildErrorBody": {
"type": "text",
"placeholders": {
"url": {}
}
}
}

View file

@ -44,7 +44,6 @@ abstract class ClientManager {
}
final clients = clientNames.map(createClient).toList();
if (initialize) {
FlutterLocalNotificationsPlugin? flutterLocalNotificationsPlugin;
await Future.wait(
clients.map(
(client) => client
@ -52,9 +51,10 @@ abstract class ClientManager {
waitForFirstSync: false,
waitUntilLoadCompletedLoaded: false,
onMigration: () {
sendMigrationNotification(
flutterLocalNotificationsPlugin ??=
FlutterLocalNotificationsPlugin(),
final l10n = lookupL10n(PlatformDispatcher.instance.locale);
sendInitNotification(
l10n.databaseMigrationTitle,
l10n.databaseMigrationBody,
);
},
)
@ -63,7 +63,6 @@ abstract class ClientManager {
),
),
);
flutterLocalNotificationsPlugin?.cancel(0);
}
if (clients.length > 1 && clients.any((c) => !c.isLogged())) {
final loggedOutClients = clients.where((c) => !c.isLogged()).toList();
@ -128,22 +127,18 @@ abstract class ClientManager {
);
}
static void sendMigrationNotification(
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin,
) async {
final l10n = lookupL10n(PlatformDispatcher.instance.locale);
static void sendInitNotification(String title, String body) async {
if (kIsWeb) {
html.Notification(
l10n.databaseMigrationTitle,
body: l10n.databaseMigrationBody,
title,
body: body,
);
return;
}
if (Platform.isLinux) {
await NotificationsClient().notify(
l10n.databaseMigrationTitle,
body: l10n.databaseMigrationBody,
title,
body: body,
appName: AppConfig.applicationName,
hints: [
NotificationHint.soundName('message-new-instant'),
@ -152,6 +147,8 @@ abstract class ClientManager {
return;
}
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin.initialize(
const InitializationSettings(
android: AndroidInitializationSettings('notifications_icon'),
@ -161,8 +158,8 @@ abstract class ClientManager {
flutterLocalNotificationsPlugin.show(
0,
l10n.databaseMigrationTitle,
l10n.databaseMigrationBody,
title,
body,
const NotificationDetails(
android: AndroidNotificationDetails(
AppConfig.pushNotificationsChannelId,
@ -171,7 +168,6 @@ abstract class ClientManager {
importance: Importance.max,
priority: Priority.max,
fullScreenIntent: true, // To show notification popup
showProgress: true,
),
iOS: DarwinNotificationDetails(sound: 'notification.caf'),
),

View file

@ -4,6 +4,7 @@ import 'dart:math';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:matrix/matrix.dart';
import 'package:path_provider/path_provider.dart';
@ -11,12 +12,24 @@ import 'package:sqflite_common_ffi/sqflite_ffi.dart' as ffi;
import 'package:sqflite_sqlcipher/sqflite.dart';
import 'package:universal_html/html.dart' as html;
import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/utils/client_manager.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/flutter_hive_collections_database.dart';
import 'package:fluffychat/utils/platform_infos.dart';
Future<MatrixSdkDatabase> flutterMatrixSdkDatabaseBuilder(Client client) async {
final database = await _constructDatabase(client);
await database.open();
return database;
Future<DatabaseApi> flutterMatrixSdkDatabaseBuilder(Client client) async {
try {
final database = await _constructDatabase(client);
await database.open();
return database;
} catch (e) {
final l10n = lookupL10n(PlatformDispatcher.instance.locale);
ClientManager.sendInitNotification(
l10n.initAppError,
l10n.databaseBuildErrorBody(AppConfig.newIssueUrl.toString()),
);
return FlutterHiveCollectionsDatabase.databaseBuilder(client);
}
}
Future<MatrixSdkDatabase> _constructDatabase(Client client) async {