Skip to content

Commit

Permalink
[Android] Fix deserialization of AlarmSettings DateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
orkun1675 committed Dec 2, 2024
1 parent 3e7325d commit 36d1537
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,11 @@ class AlarmStorage(context: Context) {
}

fun getSavedAlarms(): List<AlarmSettings> {
val gsonBuilder =
GsonBuilder().registerTypeAdapter(Date::class.java, JsonDeserializer { json, _, _ ->
Date(json.asJsonPrimitive.asLong)
})
val gson: Gson = gsonBuilder.create()

val alarms = mutableListOf<AlarmSettings>()
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 {
Expand Down
31 changes: 25 additions & 6 deletions lib/service/alarm_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class AlarmStorage {
/// Stream subscription to listen to foreground/background events.
static late StreamSubscription<FGBGType> _fgbgSubscription;

static bool _initialized = false;

/// Initializes shared preferences instance.
static Future<void> init() async {
_prefs = await SharedPreferences.getInstance();
Expand All @@ -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<void> _waitUntilInitialized() async {
while (!_initialized) {
await Future<void>.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<void> saveAlarm(AlarmSettings alarmSettings) =>
_prefs.setString(
'$prefix${alarmSettings.id}',
json.encode(alarmSettings.toJson()),
);
static Future<void> saveAlarm(AlarmSettings alarmSettings) async {
await _waitUntilInitialized();
await _prefs.setString(
'$prefix${alarmSettings.id}',
json.encode(alarmSettings.toJson()),
);
}

/// Removes alarm from local storage.
static Future<void> unsaveAlarm(int id) => _prefs.remove('$prefix$id');
static Future<void> unsaveAlarm(int id) async {
await _waitUntilInitialized();
await _prefs.remove('$prefix$id');
}

/// Whether at least one alarm is set.
static Future<bool> hasAlarm() async {
await _waitUntilInitialized();

final keys = _prefs.getKeys();

for (final key in keys) {
Expand All @@ -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<List<AlarmSettings>> getSavedAlarms() async {
await _waitUntilInitialized();

final alarms = <AlarmSettings>[];
final keys = _prefs.getKeys();

Expand Down

0 comments on commit 36d1537

Please sign in to comment.