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 count past zero when counting down #18

Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -398,10 +398,32 @@ class ClockPageViewModel (
}

private fun countDown() {
currCountDownSeconds = calculateCurrCountDownSeconds(countDownEndTime)
if(currCountDownSeconds <= 0) {
stopClock(tappedStopButton = false)
}
updateCountDownTextFieldValues(currCountDownSeconds)
currCountDownSeconds = getCountDownSeconds(
countDownEndTime = countDownEndTime,
stopClockFunc = this::stopClock,
updateFields = this::updateCountDownTextFieldValues
)
}
}

/**
* Gets the current count down seconds based on an end time. If the end time has
* already passed, then call the stopClock function. Updates the text with the
* updateFields function.
*
* This really allows me to quickly test the fix for this bug:
* https://github.com/NicksPatties/timeclock/issues/12
*/
fun getCountDownSeconds(
countDownEndTime: Long = 0L,
stopClockFunc: (Boolean) -> Unit = {},
updateFields: (Int) -> Unit = {}
): Int {
var currSeconds = calculateCurrCountDownSeconds(countDownEndTime)
if(currSeconds <= 0) {
stopClockFunc(false)
currSeconds = 0
}
updateFields(currSeconds)
return currSeconds
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.nickspatties.timeclock.ui.viewmodel

import com.google.common.truth.Truth.assertThat
import com.nickspatties.timeclock.util.MILLIS_PER_SECOND
import org.junit.Test

class ClockPageViewModelTest {

@Test
fun getCountDownSeconds_doesNotReturnNegativeNumber() {
val pastEndTime = System.currentTimeMillis() - MILLIS_PER_SECOND
val actualSeconds = getCountDownSeconds(pastEndTime)
assertThat(actualSeconds).isEqualTo(0)
}
}