refactor to get string in sessionscreen much cleaner
This commit is contained in:
parent
c73c06b11c
commit
50e2a3377e
9 changed files with 67 additions and 30 deletions
|
@ -10,5 +10,5 @@ import javax.inject.Singleton
|
|||
*/
|
||||
@Singleton
|
||||
class SelectedTimerState @Inject constructor(){
|
||||
var selectedTimer: FunctionalTimer? = null
|
||||
lateinit var selectedTimer: FunctionalTimer
|
||||
}
|
|
@ -4,14 +4,14 @@ class FunctionalCustomTimer(studyTime: Int) : FunctionalTimer(studyTime) {
|
|||
|
||||
override fun tick() {
|
||||
if (time.time == 0) {
|
||||
view = StudyState.DONE
|
||||
state = StudyState.DONE
|
||||
} else {
|
||||
time.minOne()
|
||||
}
|
||||
}
|
||||
|
||||
override fun hasEnded(): Boolean {
|
||||
return view == StudyState.DONE
|
||||
return state == StudyState.DONE
|
||||
}
|
||||
|
||||
override fun hasCurrentCountdownEnded(): Boolean {
|
||||
|
|
|
@ -10,17 +10,17 @@ class FunctionalPomodoroTimer(
|
|||
|
||||
override fun tick() {
|
||||
if (time.time == 0 && breaksRemaining == 0) {
|
||||
view = StudyState.DONE
|
||||
state = StudyState.DONE
|
||||
return
|
||||
}
|
||||
|
||||
if (time.time == 0) {
|
||||
if (isInBreak) {
|
||||
breaksRemaining--
|
||||
view = StudyState.FOCUS_REMAINING
|
||||
state = StudyState.FOCUS_REMAINING
|
||||
time.time = studyTime
|
||||
} else {
|
||||
view = StudyState.BREAK
|
||||
state = StudyState.BREAK
|
||||
time.time = breakTime
|
||||
}
|
||||
isInBreak = !isInBreak
|
||||
|
|
|
@ -2,7 +2,7 @@ package be.ugent.sel.studeez.data.local.models.timer_functional
|
|||
|
||||
abstract class FunctionalTimer(initialValue: Int) {
|
||||
val time: Time = Time(initialValue)
|
||||
var view: StudyState = StudyState.FOCUS
|
||||
var state: StudyState = StudyState.FOCUS
|
||||
|
||||
fun getHoursMinutesSeconds(): HoursMinutesSeconds {
|
||||
return time.getAsHMS()
|
||||
|
|
|
@ -28,10 +28,10 @@ import androidx.compose.ui.tooling.preview.Preview
|
|||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import be.ugent.sel.studeez.R
|
||||
import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalCustomTimer
|
||||
import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalEndlessTimer
|
||||
import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalPomodoroTimer
|
||||
import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalTimer
|
||||
import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalTimer.StudyState
|
||||
import be.ugent.sel.studeez.navigation.StudeezDestinations
|
||||
import be.ugent.sel.studeez.resources
|
||||
import kotlinx.coroutines.delay
|
||||
|
@ -76,10 +76,7 @@ fun SessionRoute(
|
|||
mediaplayer.setOnPreparedListener {
|
||||
// mediaplayer.start()
|
||||
}
|
||||
SessionScreen(
|
||||
open = open,
|
||||
sessionActions = getSessionActions(viewModel, mediaplayer),
|
||||
)
|
||||
SessionScreen(open = open, sessionActions = getSessionActions(viewModel, mediaplayer))
|
||||
}
|
||||
|
||||
@Composable
|
||||
|
@ -156,15 +153,13 @@ private fun Timer(
|
|||
fontWeight = FontWeight.Bold,
|
||||
fontSize = 40.sp,
|
||||
)
|
||||
val stateString: String = when (sessionActions.getTimer().view) {
|
||||
StudyState.DONE -> resources().getString(R.string.state_done)
|
||||
StudyState.FOCUS -> resources().getString(R.string.state_focus)
|
||||
StudyState.BREAK -> resources().getString(R.string.state_take_a_break)
|
||||
StudyState.FOCUS_REMAINING -> (sessionActions.getTimer() as FunctionalPomodoroTimer?)?.breaksRemaining?.let {
|
||||
resources().getQuantityString(R.plurals.state_focus_remaining, it, it)
|
||||
}.toString()
|
||||
}
|
||||
|
||||
val stateString = when (val timer = sessionActions.getTimer()) {
|
||||
is FunctionalCustomTimer -> customTimerStateString(timer = timer)
|
||||
is FunctionalPomodoroTimer -> pomodoroTimerStateString(timer = timer)
|
||||
is FunctionalEndlessTimer -> endlessTimerStateString(timer = timer)
|
||||
else -> "Unknown timer."
|
||||
}
|
||||
Text(
|
||||
text = stateString,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
|
@ -196,10 +191,52 @@ private fun Timer(
|
|||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun customTimerStateString(
|
||||
timer: FunctionalCustomTimer,
|
||||
): String {
|
||||
return when (timer.state) {
|
||||
FunctionalTimer.StudyState.DONE -> resources().getString(R.string.state_done)
|
||||
FunctionalTimer.StudyState.FOCUS -> resources().getString(R.string.state_focus)
|
||||
else -> "Unknown state"
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun pomodoroTimerStateString(
|
||||
timer: FunctionalPomodoroTimer,
|
||||
): String {
|
||||
return when (timer.state) {
|
||||
FunctionalTimer.StudyState.DONE -> resources().getString(R.string.state_done)
|
||||
FunctionalTimer.StudyState.FOCUS -> resources().getString(R.string.state_focus)
|
||||
FunctionalTimer.StudyState.BREAK -> resources().getString(R.string.state_take_a_break)
|
||||
FunctionalTimer.StudyState.FOCUS_REMAINING -> resources().getQuantityString(
|
||||
R.plurals.state_focus_remaining,
|
||||
timer.breaksRemaining,
|
||||
timer.breaksRemaining,
|
||||
)
|
||||
|
||||
else -> "Unknown state"
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun endlessTimerStateString(
|
||||
timer: FunctionalEndlessTimer,
|
||||
): String {
|
||||
return when (timer.state) {
|
||||
FunctionalTimer.StudyState.DONE -> resources().getString(R.string.state_done)
|
||||
FunctionalTimer.StudyState.FOCUS -> resources().getString(R.string.state_focus)
|
||||
else -> "Unknown state"
|
||||
}
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
fun TimerPreview() {
|
||||
Timer(sessionActions = SessionActions({ FunctionalEndlessTimer() }, { "Preview" }, {}, {}))
|
||||
Timer(
|
||||
sessionActions = SessionActions({ FunctionalEndlessTimer() }, { "Preview" }, {}, {}),
|
||||
)
|
||||
}
|
||||
|
||||
@Preview
|
||||
|
@ -207,6 +244,6 @@ fun TimerPreview() {
|
|||
fun SessionPreview() {
|
||||
SessionScreen(
|
||||
open = {},
|
||||
sessionActions = SessionActions({ FunctionalEndlessTimer() }, { "Preview" }, {}, {})
|
||||
sessionActions = SessionActions({ FunctionalEndlessTimer() }, { "Preview" }, {}, {}),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ class SessionViewModel @Inject constructor(
|
|||
private val task : String = "No task selected" // placeholder for tasks implementation
|
||||
|
||||
fun getTimer() : FunctionalTimer {
|
||||
return selectedTimerState.selectedTimer!!
|
||||
return selectedTimerState.selectedTimer
|
||||
}
|
||||
|
||||
fun getTask(): String {
|
||||
|
|
|
@ -38,7 +38,7 @@ class FunctionalCustomTimerUnitTest : FunctionalTimerUnitTest() {
|
|||
Assert.assertTrue(timer.hasEnded())
|
||||
Assert.assertEquals(
|
||||
FunctionalTimer.StudyState.DONE,
|
||||
timer.view
|
||||
timer.state
|
||||
)
|
||||
}
|
||||
}
|
|
@ -39,7 +39,7 @@ class FunctionalEndlessTimerUnitTest : FunctionalTimerUnitTest() {
|
|||
Assert.assertFalse(timer.hasEnded())
|
||||
Assert.assertEquals(
|
||||
FunctionalTimer.StudyState.FOCUS,
|
||||
timer.view
|
||||
timer.state
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ class FunctionalPomodoroTimerUnitTest : FunctionalTimerUnitTest() {
|
|||
)
|
||||
Assert.assertEquals(
|
||||
FunctionalTimer.StudyState.FOCUS,
|
||||
pomodoroTimer.view,
|
||||
pomodoroTimer.state,
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -54,7 +54,7 @@ class FunctionalPomodoroTimerUnitTest : FunctionalTimerUnitTest() {
|
|||
Assert.assertTrue(pomodoroTimer.hasEnded())
|
||||
Assert.assertEquals(
|
||||
FunctionalTimer.StudyState.DONE,
|
||||
pomodoroTimer.view,
|
||||
pomodoroTimer.state,
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -67,7 +67,7 @@ class FunctionalPomodoroTimerUnitTest : FunctionalTimerUnitTest() {
|
|||
Assert.assertTrue(pomodoroTimer.isInBreak)
|
||||
Assert.assertEquals(
|
||||
FunctionalTimer.StudyState.BREAK,
|
||||
pomodoroTimer.view
|
||||
pomodoroTimer.state
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -79,7 +79,7 @@ class FunctionalPomodoroTimerUnitTest : FunctionalTimerUnitTest() {
|
|||
Assert.assertTrue(pomodoroTimer.isInBreak)
|
||||
Assert.assertEquals(
|
||||
FunctionalTimer.StudyState.BREAK,
|
||||
pomodoroTimer.view
|
||||
pomodoroTimer.state
|
||||
)
|
||||
for (i in 0..breakTime) {
|
||||
pomodoroTimer.tick()
|
||||
|
@ -92,7 +92,7 @@ class FunctionalPomodoroTimerUnitTest : FunctionalTimerUnitTest() {
|
|||
)
|
||||
Assert.assertEquals(
|
||||
FunctionalTimer.StudyState.FOCUS_REMAINING,
|
||||
pomodoroTimer.view
|
||||
pomodoroTimer.state
|
||||
)
|
||||
}
|
||||
}
|
Reference in a new issue