Merge branch 'krille/on-migration-callback' into 'main'

feat: Implement onMigration callback to Client.init() method

See merge request famedly/company/frontend/famedlysdk!1256
This commit is contained in:
Krille 2023-03-29 14:11:48 +00:00
commit 94cf78d7c1
1 changed files with 8 additions and 2 deletions

View File

@ -1386,6 +1386,9 @@ class Client extends MatrixApi {
String? newOlmAccount, String? newOlmAccount,
bool waitForFirstSync = true, bool waitForFirstSync = true,
bool waitUntilLoadCompletedLoaded = true, bool waitUntilLoadCompletedLoaded = true,
/// Will be called if the app performs a migration task from the [legacyDatabaseBuilder]
void Function()? onMigration,
}) async { }) async {
if ((newToken != null || if ((newToken != null ||
newUserID != null || newUserID != null ||
@ -1450,7 +1453,7 @@ class Client extends MatrixApi {
if (accessToken == null || homeserver == null || userID == null) { if (accessToken == null || homeserver == null || userID == null) {
if (legacyDatabaseBuilder != null) { if (legacyDatabaseBuilder != null) {
await _migrateFromLegacyDatabase(); await _migrateFromLegacyDatabase(onMigration: onMigration);
if (isLogged()) return; if (isLogged()) return;
} }
// we aren't logged in // we aren't logged in
@ -2912,7 +2915,9 @@ class Client extends MatrixApi {
return; return;
} }
Future<void> _migrateFromLegacyDatabase() async { Future<void> _migrateFromLegacyDatabase({
void Function()? onMigration,
}) async {
Logs().i('Check legacy database for migration data...'); Logs().i('Check legacy database for migration data...');
final legacyDatabase = await legacyDatabaseBuilder?.call(this); final legacyDatabase = await legacyDatabaseBuilder?.call(this);
final migrateClient = await legacyDatabase?.getClient(clientName); final migrateClient = await legacyDatabase?.getClient(clientName);
@ -2920,6 +2925,7 @@ class Client extends MatrixApi {
if (migrateClient != null && legacyDatabase != null && database != null) { if (migrateClient != null && legacyDatabase != null && database != null) {
Logs().i('Found data in the legacy database!'); Logs().i('Found data in the legacy database!');
onMigration?.call();
_id = migrateClient['client_id']; _id = migrateClient['client_id'];
await database.insertClient( await database.insertClient(
clientName, clientName,