-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add reminders with hard coded editor data
- Loading branch information
Showing
29 changed files
with
1,906 additions
and
10 deletions.
There are no files selected for viewing
531 changes: 531 additions & 0 deletions
531
app/schemas/io.github.janmalch.woroboro.data.AppDatabase/2.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 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
25 changes: 25 additions & 0 deletions
25
app/src/main/kotlin/io/github/janmalch/woroboro/business/LaunchDataService.kt
This file contains 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,25 @@ | ||
package io.github.janmalch.woroboro.business | ||
|
||
import android.util.Log | ||
import io.github.janmalch.woroboro.models.RoutineFilter | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class LaunchDataService @Inject constructor() { | ||
|
||
private var routineFilter: RoutineFilter? = null | ||
|
||
fun setRoutineFilter(routineFilter: RoutineFilter?) { | ||
if (routineFilter != null) { | ||
Log.w("LaunchDataRepository", "A routine filter is already set. Overwriting it.") | ||
} | ||
this.routineFilter = routineFilter | ||
} | ||
|
||
fun consumeRoutineFilter(): RoutineFilter? { | ||
val filter = routineFilter | ||
routineFilter = null | ||
return filter | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
app/src/main/kotlin/io/github/janmalch/woroboro/business/ReminderRepository.kt
This file contains 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,62 @@ | ||
package io.github.janmalch.woroboro.business | ||
|
||
import androidx.room.withTransaction | ||
import io.github.janmalch.woroboro.business.reminders.ReminderScheduler | ||
import io.github.janmalch.woroboro.data.AppDatabase | ||
import io.github.janmalch.woroboro.data.dao.ReminderDao | ||
import io.github.janmalch.woroboro.data.model.ReminderEntityWithFilterTags | ||
import io.github.janmalch.woroboro.data.model.asModel | ||
import io.github.janmalch.woroboro.models.Reminder | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import java.util.UUID | ||
import javax.inject.Inject | ||
|
||
interface ReminderRepository { | ||
fun findAll(): Flow<List<Reminder>> | ||
|
||
fun findOne(id: UUID): Flow<Reminder?> | ||
|
||
suspend fun insert(reminder: Reminder): UUID | ||
|
||
suspend fun update(reminder: Reminder): UUID | ||
|
||
suspend fun delete(reminderId: UUID) | ||
} | ||
|
||
class ReminderRepositoryImpl @Inject constructor( | ||
private val database: AppDatabase, | ||
private val reminderDao: ReminderDao, | ||
private val reminderScheduler: ReminderScheduler, | ||
) : ReminderRepository { | ||
override fun findAll(): Flow<List<Reminder>> { | ||
return reminderDao.findAll().map { list -> list.map(ReminderEntityWithFilterTags::asModel) } | ||
} | ||
|
||
override fun findOne(id: UUID): Flow<Reminder?> { | ||
return reminderDao.findOne(id).map { it?.asModel() } | ||
} | ||
|
||
override suspend fun insert(reminder: Reminder): UUID { | ||
val model = reminder.copy(id = UUID.randomUUID()) | ||
val (entity, filterTags) = model.asEntities() | ||
reminderDao.upsert(entity, filterTags) | ||
reminderScheduler.schedule(model) | ||
return model.id | ||
} | ||
|
||
override suspend fun update(reminder: Reminder): UUID { | ||
return database.withTransaction { | ||
// delete and insert for new ID, | ||
// so that notifications don't get weird (?) | ||
delete(reminder.id) | ||
insert(reminder) | ||
} | ||
} | ||
|
||
override suspend fun delete(reminderId: UUID) { | ||
reminderScheduler.cancel(reminderId) | ||
reminderDao.delete(reminderId) | ||
} | ||
|
||
} |
This file contains 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
40 changes: 40 additions & 0 deletions
40
app/src/main/kotlin/io/github/janmalch/woroboro/business/reminders/BootCompletedReceiver.kt
This file contains 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,40 @@ | ||
package io.github.janmalch.woroboro.business.reminders | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.util.Log | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import io.github.janmalch.woroboro.business.ReminderRepository | ||
import io.github.janmalch.woroboro.utils.ApplicationScope | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@AndroidEntryPoint | ||
class BootCompletedReceiver : BroadcastReceiver() { | ||
|
||
@Inject | ||
lateinit var scheduler: ReminderScheduler | ||
|
||
@Inject | ||
lateinit var repository: ReminderRepository | ||
|
||
@Inject | ||
@ApplicationScope | ||
lateinit var applicationScope: CoroutineScope | ||
|
||
override fun onReceive(context: Context?, intent: Intent?) { | ||
if (intent?.action == Intent.ACTION_BOOT_COMPLETED) { | ||
Log.d( | ||
"BootCompletedReceiver", | ||
"Received boot completed event. Rescheduling all reminders." | ||
) | ||
applicationScope.launch { | ||
val reminders = repository.findAll().first() | ||
reminders.forEach(scheduler::schedule) | ||
} | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
app/src/main/kotlin/io/github/janmalch/woroboro/business/reminders/ReminderNotifications.kt
This file contains 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,96 @@ | ||
package io.github.janmalch.woroboro.business.reminders | ||
|
||
import android.Manifest | ||
import android.app.Notification | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.app.PendingIntent | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.pm.PackageManager | ||
import android.graphics.Bitmap | ||
import android.os.Build | ||
import android.os.Bundle | ||
import androidx.core.app.ActivityCompat | ||
import androidx.core.app.NotificationCompat | ||
import androidx.core.app.NotificationManagerCompat | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import io.github.janmalch.woroboro.MainActivity | ||
import io.github.janmalch.woroboro.models.Reminder | ||
import io.github.janmalch.woroboro.models.RoutineFilter | ||
import javax.inject.Inject | ||
|
||
|
||
interface ReminderNotifications { | ||
fun show(reminder: Reminder, image: Bitmap? = null) | ||
} | ||
|
||
class AndroidReminderNotifications @Inject constructor( | ||
@ApplicationContext private val context: Context, | ||
) : ReminderNotifications { | ||
override fun show(reminder: Reminder, image: Bitmap?) { | ||
createNotificationChannel() | ||
|
||
val intent = Intent(context, MainActivity::class.java).apply { | ||
// TODO: revisit flags | ||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK | ||
putExtra(INTENT_EXTRA_FILTER, reminder.filter) | ||
} | ||
val pendingIntent = | ||
PendingIntent.getActivity( | ||
context, | ||
reminder.id.hashCode(), | ||
intent, | ||
PendingIntent.FLAG_IMMUTABLE | ||
) | ||
|
||
val notification = NotificationCompat.Builder(context, CHANNEL_ID) | ||
.setSmallIcon(android.R.drawable.ic_lock_idle_alarm) | ||
.setContentTitle(reminder.name) | ||
.setContentText("Es ist Zeit für eine deiner Routinen!") // TODO: i18n | ||
.setLargeIcon(image) | ||
.setPriority(NotificationCompat.PRIORITY_DEFAULT) | ||
.setCategory(Notification.CATEGORY_REMINDER) | ||
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) | ||
.setContentIntent(pendingIntent) | ||
.setAutoCancel(true) // remove on tap | ||
.setOnlyAlertOnce(false) // alert again if same reminder triggers again | ||
.build() | ||
|
||
with(NotificationManagerCompat.from(context)) { | ||
if ( | ||
ActivityCompat.checkSelfPermission( | ||
context, | ||
Manifest.permission.POST_NOTIFICATIONS | ||
) == PackageManager.PERMISSION_GRANTED | ||
) { | ||
notify(reminder.id.hashCode(), notification) | ||
} | ||
} | ||
} | ||
|
||
private fun createNotificationChannel() { | ||
val name = "Woroboro Reminders" // TODO: i18n | ||
val descriptionText = | ||
"Benachrichtigungen für deine selbsterstellten Erinnerungen." // TODO: i18n | ||
val importance = NotificationManager.IMPORTANCE_DEFAULT | ||
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply { | ||
description = descriptionText | ||
} | ||
val notificationManager: NotificationManager = | ||
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
notificationManager.createNotificationChannel(channel) | ||
} | ||
|
||
companion object { | ||
const val CHANNEL_ID = "Woroboro Reminders" | ||
private const val INTENT_EXTRA_FILTER = "filters" | ||
|
||
fun Bundle.getRoutineFilter(): RoutineFilter? = | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
getParcelable(INTENT_EXTRA_FILTER, RoutineFilter::class.java) | ||
} else { | ||
getParcelable(INTENT_EXTRA_FILTER) | ||
} | ||
} | ||
} |
Oops, something went wrong.