Skip to content

Commit

Permalink
Integrated NotificationWorkManager with NotificationHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
prateek-singh-3212 committed Aug 14, 2022
1 parent b4d37d9 commit 76a979b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

package com.ichi2.anki.worker

import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.ichi2.anki.*
import com.ichi2.compat.CompatHelper
import com.ichi2.libanki.sched.Counts
import com.ichi2.libanki.sched.DeckDueTreeNode
import com.ichi2.libanki.utils.TimeManager
Expand Down Expand Up @@ -79,7 +82,7 @@ class NotificationWorkManager(val context: Context, workerParameters: WorkerPara
// Decks may have been deleted
if (deckIdsToTrigger.size != 0) {
Timber.d("Decks deleted")
// TODO: Cancel deck notification when user deletes a particular deck to handle case [user deletes a deck between the notification being added and executed]
NotificationHelper(context).removeDeckNotification(deckIds = deckIdsToTrigger.toLongArray())
}

// Updating time for next trigger.
Expand All @@ -98,6 +101,8 @@ class NotificationWorkManager(val context: Context, workerParameters: WorkerPara
*/
private fun fireDeckNotification(deck: DeckDueTreeNode) {
Timber.d("Firing deck notification for did -> %d", deck.did)
val notificationHelper = NotificationHelper(context)

val title = context.getString(R.string.reminder_title)
val counts =
Counts(deck.newCount, deck.lrnCount, deck.revCount)
Expand All @@ -107,17 +112,34 @@ class NotificationWorkManager(val context: Context, workerParameters: WorkerPara
deck.fullDeckName
)

// TODO: Remove log used for now to remove compilation error.
Timber.d("$title $counts $message")
// TODO: Check the minimum no. of cards to send notification.
// TODO: Build and fire notification.
// TODO: Check the minimum no. of cards to send notification. This will be Implemented after successful Implementation of Deck Notification UI.

// Creates an explicit intent for an DeckPiker Activity.
val resultIntent = Intent(context, DeckPicker::class.java)
resultIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
val resultPendingIntent = CompatHelper.compat.getImmutableActivityIntent(
context, 0, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)

// Build and fire notification.
val notification = notificationHelper.buildNotification(
NotificationChannels.Channel.GENERAL,
title,
message,
resultPendingIntent
)

notificationHelper.triggerNotificationNow(INDIVIDUAL_DECK_NOTIFICATION, notification)
}

/**
* Fire the notification for due cards in all decks including subdecks.
* */
private fun fireAllDeckNotification(deckList: List<DeckDueTreeNode>) {
Timber.d("Firing all deck notification.")
val notificationHelper = NotificationHelper(context)

val preferences = AnkiDroidApp.getSharedPrefs(context)
val minCardsDue = preferences.getInt(
Preferences.MINIMUM_CARDS_DUE_FOR_NOTIFICATION,
Expand All @@ -132,11 +154,31 @@ class NotificationWorkManager(val context: Context, workerParameters: WorkerPara
totalDueCount.addRev(it.revCount)
}

// Creates an explicit intent for an DeckPiker Activity.
val resultIntent = Intent(context, DeckPicker::class.java)
resultIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
val resultPendingIntent = CompatHelper.compat.getImmutableActivityIntent(
context, 0, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)

if (totalDueCount.count() < minCardsDue) {
// Due card limit is higher.
return
}
// TODO: Build & Fire all deck notification.

// Build the notification
val notification = notificationHelper.buildNotification(
NotificationChannels.Channel.GENERAL,
context.resources.getString(R.string.all_deck_notification_new_title),
context.resources.getQuantityString(
R.plurals.all_deck_notification_new_message,
totalDueCount.count()
),
resultPendingIntent
)

notificationHelper.triggerNotificationNow(ALL_DECK_NOTIFICATION_ID, notification)
}

/**
Expand All @@ -152,14 +194,16 @@ class NotificationWorkManager(val context: Context, workerParameters: WorkerPara
val initialDiff = TimeManager.time.intTimeMS() - nextTriggerTime

Timber.d("Next trigger time $nextTriggerTime")
// TODO: Start work manager with initial delay though Notification Helper.
NotificationHelper(context).startNotificationWorkManager(initialDiff, true)

return result
}

companion object {
const val ONE_HOUR_MS = 60 * 60 * 1000
const val ONE_DAY_MS = ONE_HOUR_MS * 24
private const val ALL_DECK_NOTIFICATION_ID = 11
private const val INDIVIDUAL_DECK_NOTIFICATION = 22

/**
* Calculates the next time to trigger the Notification WorkManager.
Expand Down
7 changes: 7 additions & 0 deletions AnkiDroid/src/main/res/values/02-strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,11 @@

<!-- Manage note types -->
<string name="field_editor_model_not_available">Failed to access collection. Please try again!</string>

<!-- notification-->
<string name="all_deck_notification_new_title">All Deck Reminder</string>
<plurals name="all_deck_notification_new_message">
<item quantity="one">%1$s card due</item>
<item quantity="other">%1$s cards due</item>
</plurals>
</resources>

0 comments on commit 76a979b

Please sign in to comment.