Skip to content

Commit

Permalink
fix: clean tom db on logout
Browse files Browse the repository at this point in the history
This Closes #99
  • Loading branch information
Julian KOUNE committed Aug 29, 2023
1 parent 9d3c612 commit 2b46325
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
9 changes: 5 additions & 4 deletions lib/data/hive/hive_collection_tom_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,8 @@ class HiveCollectionToMDatabase {
try {
await db.open();
} catch (e, s) {
Logs().w('Unable to open Hive. Delete database and storage key...', e, s);
const FlutterSecureStorage()
.delete(key: FlutterHiveCollectionsDatabase.cipherStorageKey);
Logs().w('Unable to open Hive.', e, s);
await db.clear().catchError((_) {});
await Hive.deleteFromDisk();
rethrow;
}
Logs().d('Hive for ToM is ready');
Expand Down Expand Up @@ -123,6 +120,10 @@ class HiveCollectionToMDatabase {
}

Future<void> clear() async {
Logs().w('Delete database and storage key...');
const FlutterSecureStorage()
.delete(key: FlutterHiveCollectionsDatabase.cipherStorageKey);
await tomConfigurationsBox.clear();
await Hive.deleteFromDisk();
}
}
2 changes: 1 addition & 1 deletion lib/pages/settings/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class SettingsController extends State<Settings> {
final matrix = Matrix.of(context);
await showFutureLoadingDialog(
context: context,
future: () => matrix.client.logout(),
future: () => matrix.logout(),
).then((value) => context.go('/home'));
}

Expand Down
14 changes: 11 additions & 3 deletions lib/widgets/matrix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'dart:io';
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:collection/collection.dart';
import 'package:desktop_notifications/desktop_notifications.dart';
import 'package:fluffychat/data/hive/hive_collection_tom_database.dart';
import 'package:fluffychat/data/network/interceptor/authorization_interceptor.dart';
import 'package:fluffychat/data/network/interceptor/dynamic_url_interceptor.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
Expand Down Expand Up @@ -106,6 +107,8 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
late String currentClientSecret;
RequestTokenResponse? currentThreepidCreds;

final hiveCollectionToMDatabase = getIt.get<HiveCollectionToMDatabase>();

void setActiveClient(Client? cl) {
final i = widget.clients.indexWhere((c) => c == cl);
if (i != -1) {
Expand Down Expand Up @@ -186,6 +189,11 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
return candidate;
}

Future<void> logout() async {
await client.logout();
await hiveCollectionToMDatabase.clear();
}

Client? getClientByName(String name) =>
widget.clients.firstWhereOrNull((c) => c.clientName == name);

Expand Down Expand Up @@ -351,13 +359,13 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
),
);

if (state != LoginState.loggedIn) {
navigatorContext?.go('/rooms');
}
navigatorContext?.go('/rooms');
} else {
setUpToMServicesInLogin(c);
if (state == LoginState.loggedIn) {
navigatorContext?.go('/rooms');
} else {
hiveCollectionToMDatabase.clear();
}
}
});
Expand Down

0 comments on commit 2b46325

Please sign in to comment.