Skip to content

Commit

Permalink
Revert to SharedPreferences
Browse files Browse the repository at this point in the history
  • Loading branch information
orkun1675 committed Dec 2, 2024
1 parent 752fe20 commit 3e7325d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BootReceiver : BroadcastReceiver() {
val alarmStorage = AlarmStorage(context)
val storedAlarms = alarmStorage.getSavedAlarms()

Log.d("BootReceiver", "Rescheduling ${storedAlarms.size} alarms")
Log.i("BootReceiver", "Rescheduling ${storedAlarms.size} alarms")

for (alarm in storedAlarms) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class AlarmStorage(context: Context) {
private val prefs: SharedPreferences =
context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE)

// TODO(gdelataillade): Ensure this function is called and alarms are rescheduled after device
// reboot.
// TODO(gdelataillade): Ensure this function is called or remove it.
fun saveAlarm(alarmSettings: AlarmSettings) {
val key = "$PREFIX${alarmSettings.id}"
val editor = prefs.edit()
Expand Down
2 changes: 2 additions & 0 deletions lib/alarm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Alarm {

AlarmTriggerApiImpl.ensureInitialized();

await AlarmStorage.init();

await checkAlarm();
}

Expand Down
28 changes: 24 additions & 4 deletions lib/service/alarm_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:convert';

import 'package:alarm/model/alarm_settings.dart';
import 'package:flutter_fgbg/flutter_fgbg.dart';
import 'package:shared_preferences/shared_preferences.dart';

/// Class that handles the local storage of the alarm info.
Expand All @@ -21,7 +22,21 @@ class AlarmStorage {
/// notification on app kill body.
static const notificationOnAppKillBody = 'notificationOnAppKillBody';

static final _prefs = SharedPreferencesAsync();
/// Shared preferences instance.
static late SharedPreferences _prefs;

/// Stream subscription to listen to foreground/background events.
static late StreamSubscription<FGBGType> _fgbgSubscription;

/// Initializes shared preferences instance.
static Future<void> init() async {
_prefs = await SharedPreferences.getInstance();

/// Reloads the shared preferences instance in the case modifications
/// were made in the native code, after a notification action.
_fgbgSubscription =
FGBGEvents.instance.stream.listen((event) => _prefs.reload());
}

/// Saves alarm info in local storage so we can restore it later
/// in the case app is terminated.
Expand All @@ -36,7 +51,7 @@ class AlarmStorage {

/// Whether at least one alarm is set.
static Future<bool> hasAlarm() async {
final keys = await _prefs.getKeys();
final keys = _prefs.getKeys();

for (final key in keys) {
if (key.startsWith(prefix)) return true;
Expand All @@ -49,11 +64,11 @@ class AlarmStorage {
/// and we need to restore previously scheduled alarms.
static Future<List<AlarmSettings>> getSavedAlarms() async {
final alarms = <AlarmSettings>[];
final keys = await _prefs.getKeys();
final keys = _prefs.getKeys();

for (final key in keys) {
if (key.startsWith(prefix)) {
final res = await _prefs.getString(key);
final res = _prefs.getString(key);
alarms.add(
AlarmSettings.fromJson(json.decode(res!) as Map<String, dynamic>),
);
Expand All @@ -62,4 +77,9 @@ class AlarmStorage {

return alarms;
}

/// Dispose the fgbg subscription to avoid memory leaks.
static void dispose() {
_fgbgSubscription.cancel();
}
}

0 comments on commit 3e7325d

Please sign in to comment.