Skip to content

Commit

Permalink
Remove Dart's AlarmStorage & shared_preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
gdelataillade committed Jul 16, 2024
1 parent f62dc64 commit 8f0d513
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ class AlarmPlugin: FlutterPlugin, MethodCallHandler {
stopNotificationOnKillService(context)
result.success(true)
}
"getSavedAlarms" -> {
val alarms = AlarmStorage(context).getSavedAlarms()
result.success(alarms)
}
else -> {
result.notImplemented()
}
Expand All @@ -121,7 +125,8 @@ class AlarmPlugin: FlutterPlugin, MethodCallHandler {
val alarm = AlarmSettings.fromJson(alarmJsonMap)
if (alarm != null) {
Log.d("AlarmPlugin", "Alarm ID: ${alarm.id}")

AlarmStorage(context).saveAlarm(alarm)

val alarmIntent = createAlarmIntent(context, call, id)
val delayInSeconds = (alarm.dateTime.time - System.currentTimeMillis()) / 1000

Expand All @@ -143,6 +148,8 @@ class AlarmPlugin: FlutterPlugin, MethodCallHandler {
}

fun stopAlarm(id: Int, result: Result? = null) {
AlarmStorage(context).unsaveAlarm(id)

// Check if the alarm is currently ringing
if (AlarmService.ringingAlarmIds.contains(id)) {
// If the alarm is ringing, stop the alarm service for this ID
Expand Down
4 changes: 2 additions & 2 deletions example/lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class _ExampleAlarmHomeScreenState extends State<ExampleAlarmHomeScreen> {
}

void loadAlarms() {
setState(() {
alarms = Alarm.getAlarms();
setState(() async {
alarms = await Alarm.getSavedAlarms();
alarms.sort((a, b) => a.dateTime.isBefore(b.dateTime) ? 0 : 1);
});
}
Expand Down
124 changes: 2 additions & 122 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.1"
ffi:
dependency: transitive
description:
name: ffi
sha256: "493f37e7df1804778ff3a53bd691d8692ddf69702cf4c1c1096a2e41b4779e21"
url: "https://pub.dev"
source: hosted
version: "2.1.2"
file:
dependency: transitive
description:
name: file
sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c"
url: "https://pub.dev"
source: hosted
version: "7.0.0"
flutter:
dependency: "direct main"
description: flutter
Expand Down Expand Up @@ -159,30 +143,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.9.0"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
sha256: f7a1fe3a634fe7734c8d3f2766ad746ae2a2884abe22e241a8b301bf5cac3279
url: "https://pub.dev"
source: hosted
version: "2.2.1"
path_provider_platform_interface:
dependency: transitive
description:
name: path_provider_platform_interface
sha256: "88f5779f72ba699763fa3a3b06aa4bf6de76c8e5de842cf6f29e2e06476c2334"
url: "https://pub.dev"
source: hosted
version: "2.1.2"
path_provider_windows:
dependency: transitive
description:
name: path_provider_windows
sha256: bd6f00dbd873bfb70d0761682da2b3a2c2fccc2b9e84c495821639601d81afe7
url: "https://pub.dev"
source: hosted
version: "2.3.0"
permission_handler:
dependency: "direct main"
description:
Expand Down Expand Up @@ -231,14 +191,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.2.1"
platform:
dependency: transitive
description:
name: platform
sha256: "9b71283fc13df574056616011fb138fd3b793ea47cc509c189a6c3fa5f8a1a65"
url: "https://pub.dev"
source: hosted
version: "3.1.5"
plugin_platform_interface:
dependency: transitive
description:
Expand All @@ -247,62 +199,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.8"
shared_preferences:
dependency: transitive
description:
name: shared_preferences
sha256: d3bbe5553a986e83980916ded2f0b435ef2e1893dfaa29d5a7a790d0eca12180
url: "https://pub.dev"
source: hosted
version: "2.2.3"
shared_preferences_android:
dependency: transitive
description:
name: shared_preferences_android
sha256: "93d0ec9dd902d85f326068e6a899487d1f65ffcd5798721a95330b26c8131577"
url: "https://pub.dev"
source: hosted
version: "2.2.3"
shared_preferences_foundation:
dependency: transitive
description:
name: shared_preferences_foundation
sha256: "0a8a893bf4fd1152f93fec03a415d11c27c74454d96e2318a7ac38dd18683ab7"
url: "https://pub.dev"
source: hosted
version: "2.4.0"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
sha256: "9f2cbcf46d4270ea8be39fa156d86379077c8a5228d9dfdb1164ae0bb93f1faa"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
sha256: "22e2ecac9419b4246d7c22bfbbda589e3acf5c0351137d87dd2939d984d37c3b"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
sha256: "9aee1089b36bd2aafe06582b7d7817fd317ef05fc30e6ba14bff247d0933042a"
url: "https://pub.dev"
source: hosted
version: "2.3.0"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
sha256: "841ad54f3c8381c480d0c9b508b89a34036f512482c407e6df7a9c4aa2ef8f59"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -380,22 +276,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "14.2.1"
web:
dependency: transitive
description:
name: web
sha256: "97da13628db363c635202ad97068d47c5b8aa555808e7a9411963c533b449b27"
url: "https://pub.dev"
source: hosted
version: "0.5.1"
xdg_directories:
dependency: transitive
description:
name: xdg_directories
sha256: faea9dee56b520b55a566385b84f2e8de55e7496104adada9962e0bd11bcff1d
url: "https://pub.dev"
source: hosted
version: "1.0.4"
sdks:
dart: ">=3.4.0 <4.0.0"
flutter: ">=3.22.0"
dart: ">=3.3.0 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54"
25 changes: 15 additions & 10 deletions ios/Classes/SwiftAlarmPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin {
self.notificationTitleOnKill = (args["notifTitleOnAppKill"] as! String)
self.notificationBodyOnKill = (args["notifDescriptionOnAppKill"] as! String)
result(true)
case "getSavedAlarms":
let savedAlarmsLocal = AlarmStorage.shared.getSavedAlarms()
let alarmsJson = savedAlarmsLocal.map { AlarmSettings.toJson(alarmSettings: $0) }
result(alarmsJson)
default:
// Removed unnecessary DispatchQueue.main.sync
result(FlutterMethodNotImplemented)
}
}
Expand All @@ -78,15 +81,13 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin {
}
NSLog("SwiftAlarmPlugin: stopAlarmFromNotification...")
// channel.invokeMethod("alarmStoppedFromNotification", arguments: ["id": id])

AlarmStorage.shared.unsaveAlarm(id: id)
}

func snoozeAlarmFromNotification(id: Int, snoozeDurationInSeconds: Int) {
// safeModifyResources {
// self.stopAlarm(id: id, cancelNotif: true, result: { _ in })
// }
channel.invokeMethod("alarmSnoozedFromNotification", arguments: ["id": id, "snoozeDurationInSeconds": snoozeDurationInSeconds])
safeModifyResources {
self.stopAlarm(id: id, cancelNotif: true, result: { _ in })
}
// channel.invokeMethod("alarmSnoozedFromNotification", arguments: ["id": id, "snoozeDurationInSeconds": snoozeDurationInSeconds])
}

func safeModifyResources(_ modificationBlock: @escaping () -> Void) {
Expand All @@ -105,10 +106,9 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin {
return
}

let actionSettings = alarmSettings.notificationActionSettings
AlarmStorage.shared.saveAlarm(alarmSettings: alarmSettings)

NSLog("SwiftAlarmPlugin: AlarmSettings: \(alarmSettings)")
NSLog("SwiftAlarmPlugin: NotificationActionSettings: \(actionSettings)")

var volumeFloat: Float? = nil
if let volumeValue = alarmSettings.volume {
Expand All @@ -118,6 +118,9 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin {
let id = alarmSettings.id
let delayInSeconds = alarmSettings.dateTime.timeIntervalSinceNow

NSLog("SwiftAlarmPlugin: Alarm dateTime: \(alarmSettings.dateTime)")
NSLog("SwiftAlarmPlugin: Alarm scheduled in \(delayInSeconds) seconds")

let alarmConfig = AlarmConfiguration(
id: id,
assetAudio: alarmSettings.assetAudioPath,
Expand All @@ -131,7 +134,7 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin {
let notificationTitle = args["notificationTitle"] as? String
let notificationBody = args["notificationBody"] as? String
if let title = notificationTitle, let body = notificationBody, delayInSeconds >= 1.0 {
NotificationManager.shared.scheduleNotification(id: id, delayInSeconds: Int(floor(delayInSeconds)), title: title, body: body, actionSettings: actionSettings) { error in
NotificationManager.shared.scheduleNotification(id: id, delayInSeconds: Int(floor(delayInSeconds)), title: title, body: body, actionSettings: alarmSettings.notificationActionSettings) { error in
if let error = error {
NSLog("[SwiftAlarmPlugin] Error scheduling notification: \(error.localizedDescription)")
}
Expand Down Expand Up @@ -303,6 +306,8 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin {
}

private func stopAlarm(id: Int, cancelNotif: Bool, result: FlutterResult) {
AlarmStorage.shared.unsaveAlarm(id: id)

if cancelNotif {
NotificationManager.shared.cancelNotification(id: id)
}
Expand Down
19 changes: 18 additions & 1 deletion ios/Classes/models/AlarmSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct AlarmSettings: Codable {
return nil
}

let dateTime = Date(timeIntervalSince1970: TimeInterval(dateTimeMillis) / 1000)
let dateTime = Date(timeIntervalSince1970: TimeInterval(dateTimeMillis) / 1000000)
let volume = json["volume"] as? Double
let notificationActionSettings = NotificationActionSettings.fromJson(json: notificationActionSettingsDict)

Expand All @@ -48,4 +48,21 @@ struct AlarmSettings: Codable {
notificationActionSettings: notificationActionSettings
)
}

static func toJson(alarmSettings: AlarmSettings) -> [String: Any] {
return [
"id": alarmSettings.id,
"dateTime": Int64(alarmSettings.dateTime.timeIntervalSince1970 * 1000000),
"assetAudioPath": alarmSettings.assetAudioPath,
"loopAudio": alarmSettings.loopAudio,
"vibrate": alarmSettings.vibrate,
"volume": alarmSettings.volume,
"fadeDuration": alarmSettings.fadeDuration,
"notificationTitle": alarmSettings.notificationTitle,
"notificationBody": alarmSettings.notificationBody,
"enableNotificationOnKill": alarmSettings.enableNotificationOnKill,
"androidFullScreenIntent": alarmSettings.androidFullScreenIntent,
"notificationActionSettings": NotificationActionSettings.toJson(notificationActionSettings: alarmSettings.notificationActionSettings)
]
}
}
10 changes: 10 additions & 0 deletions ios/Classes/models/NotificationActionSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ struct NotificationActionSettings: Codable {
snoozeDurationInSeconds: json["snoozeDurationInSeconds"] as? Int ?? 9 * 60
)
}

static func toJson(notificationActionSettings: NotificationActionSettings) -> [String: Any] {
return [
"hasStopButton": notificationActionSettings.hasStopButton,
"hasSnoozeButton": notificationActionSettings.hasSnoozeButton,
"stopButtonText": notificationActionSettings.stopButtonText,
"snoozeButtonText": notificationActionSettings.snoozeButtonText,
"snoozeDurationInSeconds": notificationActionSettings.snoozeDurationInSeconds
]
}
}
Loading

0 comments on commit 8f0d513

Please sign in to comment.