Skip to content

Commit

Permalink
chore: Use legacy database if database build fails
Browse files Browse the repository at this point in the history
  • Loading branch information
krille-chan committed Dec 31, 2023
1 parent be6165f commit ac5bd56
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
10 changes: 9 additions & 1 deletion assets/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -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": {}
}
}
}
30 changes: 13 additions & 17 deletions lib/utils/client_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ abstract class ClientManager {
}
final clients = clientNames.map(createClient).toList();
if (initialize) {
FlutterLocalNotificationsPlugin? flutterLocalNotificationsPlugin;
await Future.wait(
clients.map(
(client) => client
.init(
waitForFirstSync: false,
waitUntilLoadCompletedLoaded: false,
onMigration: () {
sendMigrationNotification(
flutterLocalNotificationsPlugin ??=
FlutterLocalNotificationsPlugin(),
final l10n = lookupL10n(PlatformDispatcher.instance.locale);
sendInitNotification(
l10n.databaseMigrationTitle,
l10n.databaseMigrationBody,
);
},
)
Expand All @@ -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();
Expand Down Expand Up @@ -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'),
Expand All @@ -152,6 +147,8 @@ abstract class ClientManager {
return;
}

final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

await flutterLocalNotificationsPlugin.initialize(
const InitializationSettings(
android: AndroidInitializationSettings('notifications_icon'),
Expand All @@ -161,8 +158,8 @@ abstract class ClientManager {

flutterLocalNotificationsPlugin.show(
0,
l10n.databaseMigrationTitle,
l10n.databaseMigrationBody,
title,
body,
const NotificationDetails(
android: AndroidNotificationDetails(
AppConfig.pushNotificationsChannelId,
Expand All @@ -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'),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,32 @@ 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';
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 {
Expand Down

0 comments on commit ac5bd56

Please sign in to comment.