-
Notifications
You must be signed in to change notification settings - Fork 0
feat: due dates, exact time selection, repeatable reminders & notification permission for tasks #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
8
commits into
main
Choose a base branch
from
copilot/add-due-dates-and-reminders
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bd4cbdd
feat: add due dates and local reminder notifications for tasks
Copilot a2a4584
fix: remove unnecessary cancel for new tasks in TaskEditorViewModel; …
Copilot f10474b
refactor: update UseCaseModule to use SingletonComponent and adjust V…
benju69 afa0422
feat: add exact time selection and repeatable tasks (daily/weekly/mon…
Copilot 9cfde22
fix: address code review feedback (repeat suffix, epochDay simplifica…
Copilot d9a5003
Merge branch 'main' into copilot/add-due-dates-and-reminders
benju69 eb5bc96
feat: request POST_NOTIFICATIONS permission when user sets due date &…
Copilot f0d068b
fix: consume permission flag before launching to prevent re-trigger o…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package fr.benju.tasks.di | ||
|
|
||
| import dagger.Binds | ||
| import dagger.Module | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.components.SingletonComponent | ||
| import fr.benju.tasks.domain.service.ReminderScheduler | ||
| import fr.benju.tasks.notification.ReminderSchedulerImpl | ||
| import javax.inject.Singleton | ||
|
|
||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| abstract class NotificationModule { | ||
|
|
||
| @Binds | ||
| @Singleton | ||
| abstract fun bindReminderScheduler( | ||
| impl: ReminderSchedulerImpl | ||
| ): ReminderScheduler | ||
| } |
42 changes: 42 additions & 0 deletions
42
app/src/main/java/fr/benju/tasks/notification/BootReceiver.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package fr.benju.tasks.notification | ||
|
|
||
| import android.content.BroadcastReceiver | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import dagger.hilt.android.AndroidEntryPoint | ||
| import fr.benju.tasks.domain.model.TaskFilter | ||
| import fr.benju.tasks.domain.service.ReminderScheduler | ||
| import fr.benju.tasks.domain.usecase.GetTasksUseCase | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.flow.first | ||
| import kotlinx.coroutines.launch | ||
| import javax.inject.Inject | ||
|
|
||
| @AndroidEntryPoint | ||
| class BootReceiver : BroadcastReceiver() { | ||
|
|
||
| @Inject | ||
| lateinit var getTasksUseCase: GetTasksUseCase | ||
|
|
||
| @Inject | ||
| lateinit var reminderScheduler: ReminderScheduler | ||
|
|
||
| override fun onReceive(context: Context, intent: Intent) { | ||
| if (intent.action != Intent.ACTION_BOOT_COMPLETED) return | ||
|
|
||
| val pendingResult = goAsync() | ||
| CoroutineScope(Dispatchers.IO).launch { | ||
| try { | ||
| getTasksUseCase(TaskFilter.ACTIVE) | ||
| .first() | ||
| .filter { it.dueDate != null } | ||
| .forEach { task -> | ||
| reminderScheduler.schedule(task.id, task.title, task.dueDate!!, task.repeatInterval) | ||
| } | ||
| } finally { | ||
| pendingResult.finish() | ||
| } | ||
| } | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
app/src/main/java/fr/benju/tasks/notification/NotificationHelper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package fr.benju.tasks.notification | ||
|
|
||
| import android.app.NotificationChannel | ||
| import android.app.NotificationManager | ||
| import android.content.Context | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import fr.benju.tasks.R | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class NotificationHelper @Inject constructor( | ||
| @ApplicationContext private val context: Context | ||
| ) { | ||
|
|
||
| fun createNotificationChannel() { | ||
| val channel = NotificationChannel( | ||
| CHANNEL_ID, | ||
| context.getString(R.string.notification_channel_name), | ||
| NotificationManager.IMPORTANCE_HIGH | ||
| ).apply { | ||
| description = context.getString(R.string.notification_channel_description) | ||
| } | ||
| val nm = context.getSystemService(NotificationManager::class.java) | ||
| nm.createNotificationChannel(channel) | ||
| } | ||
|
|
||
| companion object { | ||
| const val CHANNEL_ID = "task_reminders" | ||
| } | ||
| } |
61 changes: 61 additions & 0 deletions
61
app/src/main/java/fr/benju/tasks/notification/ReminderSchedulerImpl.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package fr.benju.tasks.notification | ||
|
|
||
| import android.app.AlarmManager | ||
| import android.app.PendingIntent | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.os.Build | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import fr.benju.tasks.domain.model.RepeatInterval | ||
| import fr.benju.tasks.domain.service.ReminderScheduler | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class ReminderSchedulerImpl @Inject constructor( | ||
| @ApplicationContext private val context: Context | ||
| ) : ReminderScheduler { | ||
|
|
||
| override fun schedule( | ||
| taskId: Long, | ||
| taskTitle: String, | ||
| dueDate: Long, | ||
| repeatInterval: RepeatInterval | ||
| ) { | ||
| if (dueDate <= System.currentTimeMillis()) return | ||
|
|
||
| val intent = Intent(context, TaskReminderReceiver::class.java).apply { | ||
| putExtra(TaskReminderReceiver.EXTRA_TASK_ID, taskId) | ||
| putExtra(TaskReminderReceiver.EXTRA_TASK_TITLE, taskTitle) | ||
| putExtra(TaskReminderReceiver.EXTRA_DUE_DATE_MS, dueDate) | ||
| putExtra(TaskReminderReceiver.EXTRA_REPEAT_INTERVAL, repeatInterval.name) | ||
| } | ||
| val pendingIntent = PendingIntent.getBroadcast( | ||
| context, | ||
| taskId.hashCode(), | ||
| intent, | ||
| PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE | ||
| ) | ||
|
|
||
| val alarmManager = context.getSystemService(AlarmManager::class.java) | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !alarmManager.canScheduleExactAlarms()) { | ||
| alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, dueDate, pendingIntent) | ||
| } else { | ||
| alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, dueDate, pendingIntent) | ||
| } | ||
| } | ||
|
|
||
| override fun cancel(taskId: Long) { | ||
| val intent = Intent(context, TaskReminderReceiver::class.java) | ||
| val pendingIntent = PendingIntent.getBroadcast( | ||
| context, | ||
| taskId.hashCode(), | ||
| intent, | ||
| PendingIntent.FLAG_NO_CREATE or PendingIntent.FLAG_IMMUTABLE | ||
| ) ?: return | ||
|
|
||
| val alarmManager = context.getSystemService(AlarmManager::class.java) | ||
| alarmManager.cancel(pendingIntent) | ||
| pendingIntent.cancel() | ||
| } | ||
| } | ||
101 changes: 101 additions & 0 deletions
101
app/src/main/java/fr/benju/tasks/notification/TaskReminderReceiver.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package fr.benju.tasks.notification | ||
|
|
||
| import android.Manifest | ||
| import android.content.BroadcastReceiver | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.content.pm.PackageManager | ||
| import androidx.core.app.NotificationCompat | ||
| import androidx.core.app.NotificationManagerCompat | ||
| import androidx.core.content.ContextCompat | ||
| import dagger.hilt.android.AndroidEntryPoint | ||
| import fr.benju.tasks.R | ||
| import fr.benju.tasks.domain.model.RepeatInterval | ||
| import fr.benju.tasks.domain.service.ReminderScheduler | ||
| import fr.benju.tasks.domain.usecase.GetTaskByIdUseCase | ||
| import fr.benju.tasks.domain.usecase.UpdateTaskUseCase | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.launch | ||
| import java.time.Instant | ||
| import java.time.ZoneId | ||
| import javax.inject.Inject | ||
|
|
||
| @AndroidEntryPoint | ||
| class TaskReminderReceiver : BroadcastReceiver() { | ||
|
|
||
| @Inject | ||
| lateinit var reminderScheduler: ReminderScheduler | ||
|
|
||
| @Inject | ||
| lateinit var getTaskByIdUseCase: GetTaskByIdUseCase | ||
|
|
||
| @Inject | ||
| lateinit var updateTaskUseCase: UpdateTaskUseCase | ||
|
|
||
| override fun onReceive(context: Context, intent: Intent) { | ||
| val taskId = intent.getLongExtra(EXTRA_TASK_ID, -1L) | ||
| val taskTitle = intent.getStringExtra(EXTRA_TASK_TITLE) ?: return | ||
| if (taskId == -1L) return | ||
|
|
||
| // ── Show notification ──────────────────────────────────────────────── | ||
| val hasPermission = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) { | ||
| ContextCompat.checkSelfPermission( | ||
| context, | ||
| Manifest.permission.POST_NOTIFICATIONS | ||
| ) == PackageManager.PERMISSION_GRANTED | ||
| } else { | ||
| true | ||
| } | ||
| if (hasPermission) { | ||
| val notification = NotificationCompat.Builder(context, NotificationHelper.CHANNEL_ID) | ||
| .setSmallIcon(R.drawable.ic_launcher_monochrome) | ||
| .setContentTitle(context.getString(R.string.notification_title)) | ||
| .setContentText(context.getString(R.string.notification_text, taskTitle)) | ||
| .setPriority(NotificationCompat.PRIORITY_HIGH) | ||
| .setAutoCancel(true) | ||
| .build() | ||
|
|
||
| NotificationManagerCompat.from(context).notify(taskId.hashCode(), notification) | ||
| } | ||
|
|
||
| // ── Reschedule repeating tasks ──────────────────────────────────────── | ||
| val dueDateMs = intent.getLongExtra(EXTRA_DUE_DATE_MS, -1L) | ||
| val repeatInterval = runCatching { | ||
| RepeatInterval.valueOf(intent.getStringExtra(EXTRA_REPEAT_INTERVAL) ?: "NONE") | ||
| }.getOrDefault(RepeatInterval.NONE) | ||
|
|
||
| if (repeatInterval != RepeatInterval.NONE && dueDateMs != -1L) { | ||
| val pendingResult = goAsync() | ||
| CoroutineScope(Dispatchers.IO).launch { | ||
| try { | ||
| val nextDueDate = computeNextOccurrence(dueDateMs, repeatInterval) | ||
| val task = getTaskByIdUseCase(taskId) | ||
| if (task != null) { | ||
| updateTaskUseCase(task.copy(dueDate = nextDueDate)) | ||
| reminderScheduler.schedule(taskId, taskTitle, nextDueDate, repeatInterval) | ||
|
Comment on lines
+72
to
+76
|
||
| } | ||
| } finally { | ||
| pendingResult.finish() | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| const val EXTRA_TASK_ID = "extra_task_id" | ||
| const val EXTRA_TASK_TITLE = "extra_task_title" | ||
| const val EXTRA_DUE_DATE_MS = "extra_due_date_ms" | ||
| const val EXTRA_REPEAT_INTERVAL = "extra_repeat_interval" | ||
|
|
||
| fun computeNextOccurrence(currentMs: Long, interval: RepeatInterval): Long { | ||
| val zdt = Instant.ofEpochMilli(currentMs).atZone(ZoneId.systemDefault()) | ||
| return when (interval) { | ||
| RepeatInterval.DAILY -> zdt.plusDays(1) | ||
| RepeatInterval.WEEKLY -> zdt.plusWeeks(1) | ||
| RepeatInterval.MONTHLY -> zdt.plusMonths(1) | ||
| RepeatInterval.NONE -> zdt | ||
| }.toInstant().toEpochMilli() | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using taskId.toInt() as the PendingIntent requestCode can silently overflow/collide once IDs exceed Int.MAX_VALUE, leading to reminders overwriting/canceling the wrong task. Prefer a stable Int derived from the Long (e.g., hashCode()) or store/use an Int primary key for alarms.