From 36d1537ba835d2802ab772f28150e5661137cd74 Mon Sep 17 00:00:00 2001 From: Orkun Duman Date: Sun, 1 Dec 2024 20:03:47 -0500 Subject: [PATCH] [Android] Fix deserialization of AlarmSettings DateTime --- .../alarm/services/AlarmStorage.kt | 8 +---- lib/service/alarm_storage.dart | 31 +++++++++++++++---- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/android/src/main/kotlin/com/gdelataillade/alarm/services/AlarmStorage.kt b/android/src/main/kotlin/com/gdelataillade/alarm/services/AlarmStorage.kt index 9d9d5dac..754fae55 100644 --- a/android/src/main/kotlin/com/gdelataillade/alarm/services/AlarmStorage.kt +++ b/android/src/main/kotlin/com/gdelataillade/alarm/services/AlarmStorage.kt @@ -34,17 +34,11 @@ class AlarmStorage(context: Context) { } fun getSavedAlarms(): List { - val gsonBuilder = - GsonBuilder().registerTypeAdapter(Date::class.java, JsonDeserializer { json, _, _ -> - Date(json.asJsonPrimitive.asLong) - }) - val gson: Gson = gsonBuilder.create() - val alarms = mutableListOf() prefs.all.forEach { (key, value) -> if (key.startsWith(PREFIX) && value is String) { try { - val alarm = gson.fromJson(value, AlarmSettings::class.java) + val alarm = AlarmSettings.fromJson(value) if (alarm != null) { alarms.add(alarm) } else { diff --git a/lib/service/alarm_storage.dart b/lib/service/alarm_storage.dart index 8352578a..621c9749 100644 --- a/lib/service/alarm_storage.dart +++ b/lib/service/alarm_storage.dart @@ -28,6 +28,8 @@ class AlarmStorage { /// Stream subscription to listen to foreground/background events. static late StreamSubscription _fgbgSubscription; + static bool _initialized = false; + /// Initializes shared preferences instance. static Future init() async { _prefs = await SharedPreferences.getInstance(); @@ -36,21 +38,36 @@ class AlarmStorage { /// were made in the native code, after a notification action. _fgbgSubscription = FGBGEvents.instance.stream.listen((event) => _prefs.reload()); + + _initialized = true; + } + + static Future _waitUntilInitialized() async { + while (!_initialized) { + await Future.delayed(const Duration(milliseconds: 100)); + } } /// Saves alarm info in local storage so we can restore it later /// in the case app is terminated. - static Future saveAlarm(AlarmSettings alarmSettings) => - _prefs.setString( - '$prefix${alarmSettings.id}', - json.encode(alarmSettings.toJson()), - ); + static Future saveAlarm(AlarmSettings alarmSettings) async { + await _waitUntilInitialized(); + await _prefs.setString( + '$prefix${alarmSettings.id}', + json.encode(alarmSettings.toJson()), + ); + } /// Removes alarm from local storage. - static Future unsaveAlarm(int id) => _prefs.remove('$prefix$id'); + static Future unsaveAlarm(int id) async { + await _waitUntilInitialized(); + await _prefs.remove('$prefix$id'); + } /// Whether at least one alarm is set. static Future hasAlarm() async { + await _waitUntilInitialized(); + final keys = _prefs.getKeys(); for (final key in keys) { @@ -63,6 +80,8 @@ class AlarmStorage { /// Returns all alarms info from local storage in the case app is terminated /// and we need to restore previously scheduled alarms. static Future> getSavedAlarms() async { + await _waitUntilInitialized(); + final alarms = []; final keys = _prefs.getKeys();