merged develop
This commit is contained in:
+13
-16
@@ -1,6 +1,6 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:localstore/localstore.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../../../../session/session.dart';
|
||||
import '../../../../session/session_manager.dart';
|
||||
@@ -21,9 +21,7 @@ import 'timetable_custom_events_add.dart';
|
||||
/// failure only sees the events that were not yet moved, so nothing is
|
||||
/// duplicated. The flag is set only once MHSL reports no remaining events.
|
||||
class CustomEventsMigration {
|
||||
static const String _collection = 'MarianumMobile';
|
||||
static const String _document = 'customEventsMigration';
|
||||
static const String _doneKey = 'migratedToMc';
|
||||
static const String _doneKey = 'customEventsMigratedToMc';
|
||||
|
||||
const CustomEventsMigration._();
|
||||
|
||||
@@ -34,18 +32,23 @@ class CustomEventsMigration {
|
||||
if (session is! CredentialSession || session.isDemo) return;
|
||||
if (await _isDone()) return;
|
||||
|
||||
final epoch = SessionManager().sessionEpoch;
|
||||
try {
|
||||
final response = await GetCustomTimetableEvent(
|
||||
GetCustomTimetableEventParams(session.legacyUserSecret),
|
||||
).run();
|
||||
|
||||
for (final event in response.events) {
|
||||
// The POST authenticates with whoever is signed in now; after a
|
||||
// sign-out the previous account's events would land in the next one.
|
||||
if (!SessionManager().isCurrentSession(epoch)) return;
|
||||
await TimetableCustomEventsAdd().run(event);
|
||||
await RemoveCustomTimetableEvent(
|
||||
RemoveCustomTimetableEventParams(event.id),
|
||||
).run();
|
||||
}
|
||||
|
||||
if (!SessionManager().isCurrentSession(epoch)) return;
|
||||
await _markDone();
|
||||
log(
|
||||
'Custom events migration: moved ${response.events.length} event(s) to Marianum-Connect.',
|
||||
@@ -57,17 +60,11 @@ class CustomEventsMigration {
|
||||
}
|
||||
}
|
||||
|
||||
static Future<bool> _isDone() async {
|
||||
final data = await Localstore.instance
|
||||
.collection(_collection)
|
||||
.doc(_document)
|
||||
.get();
|
||||
return data != null && data[_doneKey] == true;
|
||||
}
|
||||
// SharedPreferences, like the old cache document, is cleared on sign-out,
|
||||
// so the next account runs its own migration.
|
||||
static Future<bool> _isDone() async =>
|
||||
(await SharedPreferences.getInstance()).getBool(_doneKey) ?? false;
|
||||
|
||||
static Future<void> _markDone() async {
|
||||
await Localstore.instance.collection(_collection).doc(_document).set({
|
||||
_doneKey: true,
|
||||
});
|
||||
}
|
||||
static Future<void> _markDone() async =>
|
||||
(await SharedPreferences.getInstance()).setBool(_doneKey, true);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user