Skip to content

Commit

Permalink
Address some PR comments and update example app Gradle version
Browse files Browse the repository at this point in the history
  • Loading branch information
orkun1675 committed Nov 27, 2024
1 parent b81a008 commit 752fe20
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import io.flutter.Log

class AlarmStorage(context: Context) {
companion object {
private const val PREFS_NAME = "alarm_prefs"
private const val PREFIX = "flutter.__alarm_id__"
}

private val prefs: SharedPreferences =
context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE)

// TODO(gdelataillade): Ensure this function is called and alarms are rescheduled after device
// reboot.
fun saveAlarm(alarmSettings: AlarmSettings) {
val key = "$PREFIX${alarmSettings.id}"
val editor = prefs.edit()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gdelataillade.alarm.services

import android.annotation.SuppressLint
import com.gdelataillade.alarm.models.NotificationSettings
import android.app.Notification
import android.app.NotificationChannel
Expand Down Expand Up @@ -37,6 +38,8 @@ class NotificationHandler(private val context: Context) {
}
}

// We need to use [Resources.getIdentifier] because resources are registered by Flutter.
@SuppressLint("DiscouragedApi")
fun buildNotification(
notificationSettings: NotificationSettings,
fullScreen: Boolean,
Expand Down
2 changes: 1 addition & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ android {
namespace 'com.gdelataillade.alarm.alarm_example'

compileSdkVersion 34
ndkVersion "25.1.8937393"
ndkVersion "26.1.10909125"

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand Down
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-all.zip
2 changes: 1 addition & 1 deletion example/android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pluginManagement {

plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "7.3.0" apply false
id "com.android.application" version "8.5.0" apply false
id "org.jetbrains.kotlin.android" version "1.8.0" apply false
}

Expand Down
2 changes: 1 addition & 1 deletion example/lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ExampleAlarmHomeScreen extends StatefulWidget {
}

class _ExampleAlarmHomeScreenState extends State<ExampleAlarmHomeScreen> {
late List<AlarmSettings> alarms;
List<AlarmSettings> alarms = [];

static StreamSubscription<AlarmSettings>? ringSubscription;
static StreamSubscription<int>? updateSubscription;
Expand Down
13 changes: 7 additions & 6 deletions lib/service/alarm_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,22 @@ class AlarmStorage {
/// notification on app kill body.
static const notificationOnAppKillBody = 'notificationOnAppKillBody';

static final _prefs = SharedPreferencesAsync();

/// Saves alarm info in local storage so we can restore it later
/// in the case app is terminated.
static Future<void> saveAlarm(AlarmSettings alarmSettings) =>
SharedPreferencesAsync().setString(
_prefs.setString(
'$prefix${alarmSettings.id}',
json.encode(alarmSettings.toJson()),
);

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

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

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

for (final key in keys) {
if (key.startsWith(prefix)) {
final res = await SharedPreferencesAsync().getString(key);
final res = await _prefs.getString(key);
alarms.add(
AlarmSettings.fromJson(json.decode(res!) as Map<String, dynamic>),
);
Expand Down
2 changes: 0 additions & 2 deletions pigeons/alarm_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ abstract class AlarmApi {
void disableWarningNotificationOnKill();
}

// TODO(orkun1675): Consider migrating to Pigeon type-safe channels when they
// become available. See: https://github.com/flutter/flutter/issues/66711
@FlutterApi()
abstract class AlarmTriggerApi {
@async
Expand Down

0 comments on commit 752fe20

Please sign in to comment.