Skip to content

Commit

Permalink
Merge pull request #201 from Adventech/tm/fix/reminder_notification
Browse files Browse the repository at this point in the history
Fix daily notification logic
  • Loading branch information
TinasheMzondiwa authored May 21, 2021
2 parents 4277437 + 6e06940 commit 59d5e7d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import com.cryart.sabbathschool.core.extensions.prefs.SSPrefs
import com.cryart.sabbathschool.settings.DailyReminder
import com.evernote.android.job.JobManager
import com.evernote.android.job.JobRequest
import java.util.concurrent.TimeUnit
import org.joda.time.DateTime
import org.joda.time.Duration
import timber.log.Timber
import java.util.concurrent.TimeUnit

class DailyReminderManager constructor(
private val jobManager: JobManager,
Expand Down Expand Up @@ -81,6 +81,8 @@ class DailyReminderManager constructor(

override fun reSchedule() {
cancelReminder()
scheduleReminder()
if (ssPrefs.reminderEnabled()) {
scheduleReminder()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import com.cryart.sabbathschool.core.extensions.coroutines.SchedulerProvider
import com.cryart.sabbathschool.core.extensions.prefs.SSPrefs
import com.cryart.sabbathschool.reminder.DailyReminderManager
import com.google.firebase.auth.FirebaseAuth
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -34,12 +35,13 @@ import javax.inject.Inject
@HiltViewModel
class SplashViewModel @Inject constructor(
private val firebaseAuth: FirebaseAuth,
ssPrefs: SSPrefs,
dailyReminderManager: DailyReminderManager,
schedulerProvider: SchedulerProvider
) : ViewModel() {

init {
if (firebaseAuth.currentUser != null) {
if (firebaseAuth.currentUser != null && ssPrefs.reminderEnabled()) {
dailyReminderManager.scheduleReminder()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class DailyReminderManagerTest {
)
every { mockJobManager.cancelAll() }.returns(1)
every { mockSSPrefs.getReminderJobId() }.returns(null)
every { mockSSPrefs.reminderEnabled() }.returns(true)

testSubject.reSchedule()

Expand All @@ -137,6 +138,30 @@ class DailyReminderManagerTest {
}
}

@Test
fun `should cancel and not schedule when reschedule is called and reminder is disabled`() {
val dateTime7am = DateTime(2020, 5, 20, 7, 30)
testSubject = DailyReminderManager(
mockJobManager,
mockSSPrefs,
mockRequestBuilder,
dateTime7am
)
every { mockJobManager.cancelAll() }.returns(1)
every { mockSSPrefs.getReminderJobId() }.returns(null)
every { mockSSPrefs.reminderEnabled() }.returns(false)

testSubject.reSchedule()

verify {
mockJobManager.cancelAll()
mockSSPrefs.setReminderJobId(null)
}
verify(inverse = true) {
mockSSPrefs.setReminderJobId(1)
}
}

companion object {
private const val MINUTE = 60000L
private const val HOUR = 60 * MINUTE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package com.cryart.sabbathschool.ui.splash

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.cryart.sabbathschool.core.extensions.prefs.SSPrefs
import com.cryart.sabbathschool.reminder.DailyReminderManager
import com.cryart.sabbathschool.test.coroutines.CoroutineTestRule
import com.google.firebase.auth.FirebaseAuth
Expand All @@ -45,15 +46,18 @@ class SplashViewModelTest {

private val mockFirebaseAuth: FirebaseAuth = mockk(relaxed = true)
private val mockDailyReminderManager: DailyReminderManager = mockk()
private val mockSSPrefs: SSPrefs = mockk()

private lateinit var viewModel: SplashViewModel

@Before
fun setUp() {
every { mockDailyReminderManager.scheduleReminder() }.returns(Unit)
every { mockSSPrefs.reminderEnabled() }.returns(true)

viewModel = SplashViewModel(
mockFirebaseAuth,
mockSSPrefs,
mockDailyReminderManager,
coroutinesTestRule.dispatcherProvider
)
Expand Down Expand Up @@ -83,10 +87,26 @@ class SplashViewModelTest {

SplashViewModel(
mockFirebaseAuth,
mockSSPrefs,
mockDailyReminderManager,
coroutinesTestRule.dispatcherProvider
)

verify { mockDailyReminderManager.scheduleReminder() }
}

@Test
fun `should not schedule reminder when user is signed in and reminder is disabled`() {
every { mockFirebaseAuth.currentUser }.returns(mockk())
every { mockSSPrefs.reminderEnabled() }.returns(false)

SplashViewModel(
mockFirebaseAuth,
mockSSPrefs,
mockDailyReminderManager,
coroutinesTestRule.dispatcherProvider
)

verify(inverse = false) { mockDailyReminderManager.scheduleReminder() }
}
}
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/BuildAndroidConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object BuildAndroidConfig {
object Version {
private const val MAJOR = 4
private const val MINOR = 1
private const val PATCH = 2
private const val PATCH = 3

const val name = "$MAJOR.$MINOR.$PATCH"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class SSPrefs(context: Context) {

private val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)

fun reminderEnabled(): Boolean = sharedPreferences.getBoolean(
SSConstants.SS_SETTINGS_REMINDER_ENABLED_KEY, true
)

fun getReminderTime(): ReminderTime {
val timeStr = sharedPreferences.getString(
SSConstants.SS_SETTINGS_REMINDER_TIME_KEY,
Expand Down

0 comments on commit 59d5e7d

Please sign in to comment.