Skip to content
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

Don't send reminder notification when entry for today is already available #115

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class FakeEntryRepository : EntryRepository {
return liveData
}

override suspend fun getEntryImmediately(date: LocalDate): Entry? {
return entriesDatabase[date]
}

override suspend fun getEntries(): List<Entry> {
val list = mutableListOf<Entry>()
entriesDatabase.forEach { (date, entry) -> list.add(entry) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import javax.inject.Singleton
SettingsModule::class,
CloudUploadModule::class,
WorkerModule::class,
AndroidSupportInjectionModule::class
AndroidSupportInjectionModule::class,
ReminderModule::class
]
)
interface ApplicationComponent : AndroidInjector<GratitudeApplication> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package journal.gratitude.com.gratitudejournal.di

import dagger.Module
import dagger.android.ContributesAndroidInjector
import journal.gratitude.com.gratitudejournal.util.reminders.ReminderReceiver

@Module
abstract class ReminderModule {

@ContributesAndroidInjector
internal abstract fun reminderReceiver(): ReminderReceiver
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface EntryRepository {

fun getEntry(date: LocalDate): LiveData<Entry>

suspend fun getEntryImmediately(date: LocalDate): Entry?

suspend fun getEntriesFlow(): Flow<List<Entry>>

suspend fun getEntries(): List<Entry>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class EntryRepositoryImpl @Inject constructor(private val entryDao: EntryDao): E
return entryDao.getEntry(date)
}

override suspend fun getEntryImmediately(date: LocalDate): Entry? {
return entryDao.getEntryImmediately(date)
}

override suspend fun getEntriesFlow(): Flow<List<Entry>> {
return entryDao.getEntriesFlow()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ interface EntryDao {
@Query("SELECT * FROM entries WHERE entryDate = :date")
fun getEntry(date: LocalDate): LiveData<Entry>

@Query("SELECT * FROM entries WHERE entryDate = :date")
suspend fun getEntryImmediately(date: LocalDate): Entry?

@Delete
fun delete(entry: Entry)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,39 @@ package journal.gratitude.com.gratitudejournal.util.reminders

import android.app.Notification
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import dagger.android.DaggerBroadcastReceiver
import journal.gratitude.com.gratitudejournal.ContainerActivity
import journal.gratitude.com.gratitudejournal.ContainerActivity.Companion.CHANNEL_ID
import journal.gratitude.com.gratitudejournal.R
import journal.gratitude.com.gratitudejournal.repository.EntryRepository
import journal.gratitude.com.gratitudejournal.util.reminders.NotificationScheduler.Companion.ALARM_TYPE_RTC
import kotlinx.coroutines.runBlocking
import org.threeten.bp.LocalDate
import javax.inject.Inject

/**
* Receives broadcasts from an alarm and creates notifications
*/
class ReminderReceiver : BroadcastReceiver() {
class ReminderReceiver : DaggerBroadcastReceiver() {

@Inject
lateinit var repository: EntryRepository

override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)

val entry = runBlocking {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tested this on the emulator and it seems to be fine. Not sure if there's a better way.

Copy link
Owner

Choose a reason for hiding this comment

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

Hiya! I've thought about doing this before but was hesitant because I didn't want to do any blocking work in the BroadcastReceiver since it runs on the main thread.

Another option would be to reschedule the notification to the next day whenever the user writes for the current day. I think there's probably some rewriting that should be done to make that code more testable and easier to work with, but its doable!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm wondering if it's worth the effort and complexity to handle this in the entry saving logic. You would introduce a coupling between the entry saving and the notification logic when it's not strictly necessary.

One database query shouldn't block long enough to be noticeable.

Also, you would introduce corner cases like: The user wrote something, then removed it. Do we still want to show a reminder? If yes, we would have to reschedule again.

Just some food for thought. Let me know, what you think.

repository.getEntryImmediately(LocalDate.now())
}

if (entry != null) {
return
}

val openActivityIntent = Intent(context, ContainerActivity::class.java)
openActivityIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP //set flag to restart/relaunch the app
openActivityIntent.putExtra(fromNotification, true)
Expand Down