Skip to content

Added functionality to cancel all alarms#18

Merged
meticha-admin merged 5 commits into
mainfrom
feature/cancel-all-alarm
Jun 20, 2025
Merged

Added functionality to cancel all alarms#18
meticha-admin merged 5 commits into
mainfrom
feature/cancel-all-alarm

Conversation

@cavin-macwan

@cavin-macwan cavin-macwan commented Jun 20, 2025

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features

    • Added persistent storage and management of alarm IDs, enabling bulk cancellation of all scheduled alarms.
    • Introduced the ability to cancel all alarms at once.
  • Improvements

    • All alarm scheduling and cancellation operations now support asynchronous execution for better performance and reliability.
  • Chores

    • Updated dependency versions for Kotlin, Compose BOM, and KSP.
    • Incremented release version to 0.0.7.

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.
@cavin-macwan cavin-macwan self-assigned this Jun 20, 2025
@cavin-macwan cavin-macwan added the enhancement New feature or request label Jun 20, 2025
@coderabbitai

coderabbitai Bot commented Jun 20, 2025

Copy link
Copy Markdown

Walkthrough

This 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

File(s) Change Summary
.github/workflows/release.yaml Updated workflow environment variable VERSION_NAME from "0.0.6" to "0.0.7".
gradle/libs.versions.toml Bumped versions: Kotlin (2.1.20 → 2.1.21), Compose BOM (2025.06.00 → 2025.06.01), KSP updated.
triggerx/src/main/java/com/meticha/triggerx/TriggerXAlarmScheduler.kt All public methods made suspend; added persistent alarm ID tracking and bulk cancellation.
triggerx/src/main/java/com/meticha/triggerx/preference/TriggerXAlarmIdManager.kt New: Internal object for persistent alarm ID management using DataStore with suspend methods.

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
Loading

Suggested reviewers

  • meticha-admin

Poem

Hop, hop, a scheduler's leap,
Now alarms persist while you sleep.
With coroutines they dance,
And bulk cancel at a glance—
A bunny’s delight,
In code shining bright,
Version bumps bring joy, not a peep!
🐇⏰✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bbd5c25 and f933a2e.

📒 Files selected for processing (2)
  • triggerx/src/main/java/com/meticha/triggerx/TriggerXAlarmScheduler.kt (9 hunks)
  • triggerx/src/main/java/com/meticha/triggerx/preference/TriggerXAlarmIdManager.kt (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • triggerx/src/main/java/com/meticha/triggerx/preference/TriggerXAlarmIdManager.kt
🔇 Additional comments (3)
triggerx/src/main/java/com/meticha/triggerx/TriggerXAlarmScheduler.kt (3)

25-25: LGTM: Import addition for alarm ID management.

The import for TriggerXAlarmIdManager is appropriately added to support the new persistent alarm ID tracking functionality.


49-49: LGTM: Suspend function conversions for coroutine support.

All public methods have been correctly converted to suspend functions, enabling proper coroutine-based asynchronous execution. The method signatures remain consistent and backward-compatible for coroutine contexts.

Also applies to: 110-110, 127-127, 145-145, 165-165, 190-190


85-85: LGTM: Persistent alarm ID tracking integration.

The integration with TriggerXAlarmIdManager is well-placed:

  • Saving alarm IDs after successful scheduling ensures tracking of active alarms
  • Removing alarm IDs during cancellation maintains data consistency

This enables proper bulk cancellation functionality while keeping the persistent storage in sync.

Also applies to: 205-205

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Post Copyable Unit Tests in Comment
  • Commit Unit Tests in branch feature/cancel-all-alarm

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 968eec9 and bbd5c25.

📒 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"
fi

Also 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/bash

Show 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.
@meticha-admin meticha-admin merged commit 4ee1897 into main Jun 20, 2025
1 check passed
@meticha-admin meticha-admin deleted the feature/cancel-all-alarm branch June 20, 2025 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants