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

Added Visual confirmation when Wake Up timer is set. Request solution #502. #554

Merged
merged 8 commits into from
Mar 11, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ class WakeUpTimerFragmentTest {

@Before
fun setup() {
mockkObject(InAppReviewFlowManager)
mockkObject(Preset.Companion)
mockkObject(WakeUpTimerManager)
mockkObject(InAppReviewFlowManager, Preset.Companion, WakeUpTimerManager)
every { WakeUpTimerManager.set(any(), any()) } returns Unit
fragmentScenario = launchFragmentInContainer(null, R.style.Theme_App)
}
Expand Down Expand Up @@ -167,32 +165,31 @@ class WakeUpTimerFragmentTest {
}
)

onView(withId(R.id.select_preset_button))
.check(matches(withText(R.string.select_preset)))
.perform(click())

EspressoX.waitForView(withId(android.R.id.list))
.check(matches(withChild(withText("test-1"))))
.check(matches(withChild(withText("test-2"))))

onView(withText("test-1"))
.perform(click())

onView(withId(R.id.select_preset_button))
.check(matches(withText("test-1")))

verify(exactly = 0) { WakeUpTimerManager.set(any(), any()) }

onView(withId(R.id.time_picker))
.perform(PickerActions.setTime(1, 2))

val am = ApplicationProvider.getApplicationContext<Context>().getSystemService<AudioManager>()
requireNotNull(am)
val maxVol = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
val minVol = AudioManagerCompat.getStreamMinVolume(am, AudioManager.STREAM_MUSIC)
val expectedVolume = Random.nextInt(minVol, maxVol)

for (shouldUpdateMediaVolume in arrayOf(true, false)) {
onView(withId(R.id.select_preset_button))
.perform(click())

EspressoX.waitForView(withId(android.R.id.list))
.check(matches(withChild(withText("test-1"))))
.check(matches(withChild(withText("test-2"))))

onView(withText("test-1"))
.perform(click())

onView(withId(R.id.select_preset_button))
.check(matches(withText("test-1")))

verify(exactly = 0) { WakeUpTimerManager.set(any(), any()) }

onView(withId(R.id.time_picker))
.perform(PickerActions.setTime(1, 2))

onView(withId(R.id.should_update_media_volume))
.perform(scrollTo(), EspressoX.setChecked(shouldUpdateMediaVolume))

Expand Down Expand Up @@ -231,6 +228,10 @@ class WakeUpTimerFragmentTest {
}

onView(withId(R.id.reset_time_button)).check(matches(isEnabled()))
onView(withSubstring("The alarm will go off in"))
.check(matches(isDisplayed()))

fragmentScenario.recreate()
}
}

Expand Down Expand Up @@ -271,8 +272,7 @@ class WakeUpTimerFragmentTest {
mockk(relaxed = true) {
every { id } returns "test-not-saved-preset-id-1"
every { name } returns "test-not-saved-preset-1"
}
,
},
mockk(relaxed = true) {
every { id } returns "test-saved-preset-id"
every { name } returns "test-saved-preset"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.github.ashutoshgngwr.noice.databinding.WakeUpTimerFragmentBinding
import com.github.ashutoshgngwr.noice.sound.Preset
import com.google.android.material.snackbar.Snackbar
import java.util.*
import java.util.concurrent.TimeUnit

class WakeUpTimerFragment : Fragment() {

Expand Down Expand Up @@ -121,7 +122,9 @@ class WakeUpTimerFragment : Fragment() {
}

selectedTime = calendar.timeInMillis

notifyUpdate()
notifyScheduleLeftTime()

// maybe show in-app review dialog to the user
InAppReviewFlowManager.maybeAskForReview(requireActivity())
Expand Down Expand Up @@ -200,6 +203,45 @@ class WakeUpTimerFragment : Fragment() {
}
}


private fun notifyScheduleLeftTime() {
val differenceMillis = selectedTime - System.currentTimeMillis()
if (differenceMillis < 0) {
return // should it ever happen?
}

val diffHours = TimeUnit.MILLISECONDS.toHours(differenceMillis).toInt()
val diffMinutes = TimeUnit.MILLISECONDS.toMinutes(differenceMillis).toInt() % 60

Snackbar.make(
requireView(),
getRelativeDurationString(diffHours, diffMinutes),
Snackbar.LENGTH_LONG
).show()
}

private val matchSpacesRegex = """\s+""".toRegex()

private fun getRelativeDurationString(hours: Int, minutes: Int): String {
var minutePlural = ""
if (minutes > 0 || hours == 0) {
minutePlural = resources.getQuantityString(R.plurals.time_minutes, minutes, minutes)
}

var hourPlural = ""
if (hours > 0) {
hourPlural = resources.getQuantityString(R.plurals.time_hours, hours, hours)
}

var timeBridge = ""
if (hours * minutes != 0) {
timeBridge = getString(R.string.time_bridge)
}

return getString(R.string.wake_up_timer_schedule_set, hourPlural, timeBridge, minutePlural)
.replace(matchSpacesRegex, " ")
}

private fun resetControls() {
loadSelectedPresetID()
selectedTime = 0
Expand Down
12 changes: 11 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@
<string name="schedule">Schedule</string>
<string name="select_preset">Select Preset</string>
<string name="wake_up_timer_cancelled">Wake-up Timer cancelled!</string>
<string name="wake_up_timer_schedule_set">The alarm will go off in %1$s %2$s %3$s</string>
<string name="time_bridge">and</string>
<plurals name="time_hours">
<item quantity="one">%1$d hour</item>
<item quantity="other">%1$d hours</item>
</plurals>
<plurals name="time_minutes">
<item quantity="one">%1$d minute</item>
<item quantity="other">%1$d minutes</item>
</plurals>
<string name="wake_up_timer_description">
Noice will start the selected preset when the timer ends. Use the buttons below to select a
preset and set the desired time to start the wake-up timer.
Expand Down Expand Up @@ -193,4 +203,4 @@
<string name="never">Never</string>
<string name="notification_channel_default__description">Media playback controls</string>
<string name="notification_channel_default__name">Media Playback</string>
</resources>
</resources>