Added functionality to cancel all alarms#18
Conversation
This commit updates Kotlin, Compose BOM, and KSP to their latest versions.
Key changes:
- **gradle/libs.versions.toml**:
- `kotlin` version updated from `2.1.20` to `2.1.21`.
- `composeBom` version updated from `2025.06.00` to `2025.06.01`.
- `ksp` version updated from `2.1.20-2.0.1` to `2.1.21-2.0.1`.
This commit updates the version name in the release workflow configuration. Key changes: - **.github/workflows/release.yaml**: - Updated `VERSION_NAME` from `0.0.6` to `0.0.7`.
This commit introduces a system to persist scheduled alarm IDs using Jetpack DataStore, enabling more robust alarm management and the ability to cancel all active alarms at once.
Key changes:
- **`TriggerXAlarmIdManager.kt`**:
- Added a new `internal` object to manage the storage and retrieval of alarm IDs using `preferencesDataStore`.
- Includes methods to save, remove, get, and clear all alarm IDs.
- **`TriggerXAlarmScheduler.kt`**:
- Added a new `suspend fun cancelAllAlarms(context)` which retrieves all stored alarm IDs and cancels each one.
- `scheduleAlarm` now saves the alarm ID to DataStore upon successful scheduling.
- `cancelAlarm` now removes the corresponding alarm ID from DataStore.
- All scheduling and cancellation methods are now `suspend` functions to support the asynchronous DataStore operations.
WalkthroughThis update introduces coroutine support and persistent alarm ID tracking to the alarm scheduling system. All public scheduling and cancellation methods are now suspend functions. A new alarm ID manager using DataStore is added for persistent storage, and a bulk cancellation method is implemented. Dependency and workflow version numbers are also incremented. Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant TriggerXAlarmScheduler
participant TriggerXAlarmIdManager
participant DataStore
Caller->>TriggerXAlarmScheduler: scheduleAlarm(context, time, type, id)
activate TriggerXAlarmScheduler
TriggerXAlarmScheduler->>TriggerXAlarmIdManager: saveAlarmId(context, id)
TriggerXAlarmIdManager->>DataStore: Store alarm ID
DataStore-->>TriggerXAlarmIdManager: Confirmation
TriggerXAlarmIdManager-->>TriggerXAlarmScheduler: Done
TriggerXAlarmScheduler-->>Caller: Boolean (success)
Caller->>TriggerXAlarmScheduler: cancelAllAlarms(context)
TriggerXAlarmScheduler->>TriggerXAlarmIdManager: getAlarmIds(context)
TriggerXAlarmIdManager->>DataStore: Retrieve all alarm IDs
DataStore-->>TriggerXAlarmIdManager: Set of IDs
TriggerXAlarmIdManager-->>TriggerXAlarmScheduler: Set of IDs
loop For each alarm ID
TriggerXAlarmScheduler->>TriggerXAlarmScheduler: cancelAlarm(context, id)
TriggerXAlarmScheduler->>TriggerXAlarmIdManager: removeAlarmId(context, id)
TriggerXAlarmIdManager->>DataStore: Remove alarm ID
end
TriggerXAlarmScheduler->>TriggerXAlarmIdManager: clearAllAlarmIds(context)
TriggerXAlarmIdManager->>DataStore: Remove all alarm IDs
TriggerXAlarmIdManager-->>TriggerXAlarmScheduler: Done
TriggerXAlarmScheduler-->>Caller: Done
Suggested reviewers
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
✅ Files skipped from review due to trivial changes (1)
🔇 Additional comments (3)
✨ Finishing Touches
🧪 Generate Unit Tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
triggerx/src/main/java/com/meticha/triggerx/preference/TriggerXAlarmIdManager.kt (2)
18-23: Consider error handling for DataStore operations.While the current implementation is functional, consider adding error handling for DataStore operations that could fail due to I/O issues or storage constraints.
suspend fun saveAlarmId(context: Context, id: Int) { - context.dataStore.edit { settings -> - val currentIds = settings[KEY_ALARM_IDS] ?: emptySet() - settings[KEY_ALARM_IDS] = currentIds + id.toString() - } + try { + context.dataStore.edit { settings -> + val currentIds = settings[KEY_ALARM_IDS] ?: emptySet() + settings[KEY_ALARM_IDS] = currentIds + id.toString() + } + } catch (e: Exception) { + // Log error or handle gracefully + throw e + } }
25-30: Optimize removeAlarmId implementation.Consider avoiding the edit operation when the ID doesn't exist to reduce unnecessary DataStore writes.
suspend fun removeAlarmId(context: Context, id: Int) { context.dataStore.edit { settings -> val currentIds = settings[KEY_ALARM_IDS] ?: return@edit - settings[KEY_ALARM_IDS] = currentIds - id.toString() + val idString = id.toString() + if (idString in currentIds) { + settings[KEY_ALARM_IDS] = currentIds - idString + } } }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
.github/workflows/release.yaml(1 hunks)gradle/libs.versions.toml(1 hunks)triggerx/src/main/java/com/meticha/triggerx/TriggerXAlarmScheduler.kt(9 hunks)triggerx/src/main/java/com/meticha/triggerx/preference/TriggerXAlarmIdManager.kt(1 hunks)
🔇 Additional comments (7)
.github/workflows/release.yaml (1)
10-10: LGTM - Version bump for new release.The version update from 0.0.6 to 0.0.7 appropriately reflects the new alarm cancellation functionality being introduced.
gradle/libs.versions.toml (1)
4-4: LGTM - Dependency updates support coroutine enhancements.The minor version updates for Kotlin (2.1.21), Compose BOM (2025.06.01), and KSP (2.1.21-2.0.1) are well-aligned and support the new coroutine-based alarm scheduling functionality.
Verify that these dependency versions are compatible and don't introduce any breaking changes:
#!/bin/bash # Check for any known issues with the updated dependency versions echo "Checking Kotlin 2.1.21 compatibility..." curl -s "https://kotlinlang.org/docs/releases.html" | grep -A 5 -B 5 "2.1.21" || echo "No specific info found for 2.1.21" echo "Checking if KSP version matches Kotlin version..." if [[ "2.1.21-2.0.1" == 2.1.21* ]]; then echo "✓ KSP version correctly matches Kotlin version" else echo "⚠ KSP version mismatch with Kotlin version" fiAlso applies to: 11-11, 16-16
triggerx/src/main/java/com/meticha/triggerx/preference/TriggerXAlarmIdManager.kt (2)
12-12: Good use of DataStore delegate pattern.The extension property approach for DataStore initialization is clean and follows Android best practices.
32-37: LGTM - Clean DataStore read operation.The implementation correctly uses
first()to get the current value and provides a sensible default.triggerx/src/main/java/com/meticha/triggerx/TriggerXAlarmScheduler.kt (3)
49-49: LGTM - Appropriate conversion to suspend functions.Converting all public methods to suspend functions enables proper coroutine support for the DataStore integration.
Also applies to: 110-110, 127-127, 145-145, 165-165, 190-190
25-25: LGTM - Proper import addition.The import for TriggerXAlarmIdManager is correctly added to support the new persistence functionality.
85-85: ```shell
#!/bin/bashShow lines 1-300 of TriggerXAlarmScheduler.kt for context on DataStore calls
sed -n '1,300p' triggerx/src/main/java/com/meticha/triggerx/TriggerXAlarmScheduler.kt
</details> </blockquote></details> </details> <!-- This is an auto-generated comment by CodeRabbit for review status -->
This commit enhances the `cancelAllAlarms` function to make it more robust and prevent potential crashes.
Key changes:
- **triggerx/src/main/java/com/meticha/triggerx/TriggerXAlarmScheduler.kt**:
- Implemented `try-catch` blocks to handle exceptions during the cancellation of individual alarms.
- Used `toIntOrNull()` for safe parsing of alarm IDs, preventing errors from invalid formats.
- Added more detailed logging to show the count of successfully cancelled and failed alarms.
This commit updates the `TriggerXAlarmIdManager` to prevent attempts to remove an alarm ID that is not present in the DataStore.
Previously, the removal operation was performed unconditionally. This change adds a check to ensure the ID exists in the set before modifying the DataStore, making the operation more robust.
Key changes:
- **triggerx/src/main/java/com/meticha/triggerx/preference/TriggerXAlarmIdManager.kt**:
- In `removeAlarmId`, added a condition to verify that the alarm ID is present in the set before executing the removal.
Summary by CodeRabbit
New Features
Improvements
Chores