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 | @Singleton | ||||||
| class SelectedTimerState @Inject constructor(){ | 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() { |     override fun tick() { | ||||||
|         if (time.time == 0) { |         if (time.time == 0) { | ||||||
|             view = StudyState.DONE |             state = StudyState.DONE | ||||||
|         } else { |         } else { | ||||||
|             time.minOne() |             time.minOne() | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     override fun hasEnded(): Boolean { |     override fun hasEnded(): Boolean { | ||||||
|         return view == StudyState.DONE |         return state == StudyState.DONE | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     override fun hasCurrentCountdownEnded(): Boolean { |     override fun hasCurrentCountdownEnded(): Boolean { | ||||||
|  |  | ||||||
|  | @ -10,17 +10,17 @@ class FunctionalPomodoroTimer( | ||||||
| 
 | 
 | ||||||
|     override fun tick() { |     override fun tick() { | ||||||
|         if (time.time == 0 && breaksRemaining == 0) { |         if (time.time == 0 && breaksRemaining == 0) { | ||||||
|             view = StudyState.DONE |             state = StudyState.DONE | ||||||
|             return |             return | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         if (time.time == 0) { |         if (time.time == 0) { | ||||||
|             if (isInBreak) { |             if (isInBreak) { | ||||||
|                 breaksRemaining-- |                 breaksRemaining-- | ||||||
|                 view = StudyState.FOCUS_REMAINING |                 state = StudyState.FOCUS_REMAINING | ||||||
|                 time.time = studyTime |                 time.time = studyTime | ||||||
|             } else { |             } else { | ||||||
|                 view = StudyState.BREAK |                 state = StudyState.BREAK | ||||||
|                 time.time = breakTime |                 time.time = breakTime | ||||||
|             } |             } | ||||||
|             isInBreak = !isInBreak |             isInBreak = !isInBreak | ||||||
|  |  | ||||||
|  | @ -2,7 +2,7 @@ package be.ugent.sel.studeez.data.local.models.timer_functional | ||||||
| 
 | 
 | ||||||
| abstract class FunctionalTimer(initialValue: Int) { | abstract class FunctionalTimer(initialValue: Int) { | ||||||
|     val time: Time = Time(initialValue) |     val time: Time = Time(initialValue) | ||||||
|     var view: StudyState = StudyState.FOCUS |     var state: StudyState = StudyState.FOCUS | ||||||
| 
 | 
 | ||||||
|     fun getHoursMinutesSeconds(): HoursMinutesSeconds { |     fun getHoursMinutesSeconds(): HoursMinutesSeconds { | ||||||
|         return time.getAsHMS() |         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.dp | ||||||
| import androidx.compose.ui.unit.sp | import androidx.compose.ui.unit.sp | ||||||
| import be.ugent.sel.studeez.R | 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.FunctionalEndlessTimer | ||||||
| import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalPomodoroTimer | 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 | ||||||
| import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalTimer.StudyState |  | ||||||
| import be.ugent.sel.studeez.navigation.StudeezDestinations | import be.ugent.sel.studeez.navigation.StudeezDestinations | ||||||
| import be.ugent.sel.studeez.resources | import be.ugent.sel.studeez.resources | ||||||
| import kotlinx.coroutines.delay | import kotlinx.coroutines.delay | ||||||
|  | @ -76,10 +76,7 @@ fun SessionRoute( | ||||||
|     mediaplayer.setOnPreparedListener { |     mediaplayer.setOnPreparedListener { | ||||||
| //        mediaplayer.start() | //        mediaplayer.start() | ||||||
|     } |     } | ||||||
|     SessionScreen( |     SessionScreen(open = open, sessionActions = getSessionActions(viewModel, mediaplayer)) | ||||||
|         open = open, |  | ||||||
|         sessionActions = getSessionActions(viewModel, mediaplayer), |  | ||||||
|     ) |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @Composable | @Composable | ||||||
|  | @ -156,15 +153,13 @@ private fun Timer( | ||||||
|             fontWeight = FontWeight.Bold, |             fontWeight = FontWeight.Bold, | ||||||
|             fontSize = 40.sp, |             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( | ||||||
|             text = stateString, |             text = stateString, | ||||||
|             modifier = Modifier.fillMaxWidth(), |             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 | @Preview | ||||||
| @Composable | @Composable | ||||||
| fun TimerPreview() { | fun TimerPreview() { | ||||||
|     Timer(sessionActions = SessionActions({ FunctionalEndlessTimer() }, { "Preview" }, {}, {})) |     Timer( | ||||||
|  |         sessionActions = SessionActions({ FunctionalEndlessTimer() }, { "Preview" }, {}, {}), | ||||||
|  |     ) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @Preview | @Preview | ||||||
|  | @ -207,6 +244,6 @@ fun TimerPreview() { | ||||||
| fun SessionPreview() { | fun SessionPreview() { | ||||||
|     SessionScreen( |     SessionScreen( | ||||||
|         open = {}, |         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 |     private val task : String = "No task selected" // placeholder for tasks implementation | ||||||
| 
 | 
 | ||||||
|     fun getTimer() : FunctionalTimer { |     fun getTimer() : FunctionalTimer { | ||||||
|         return selectedTimerState.selectedTimer!! |         return selectedTimerState.selectedTimer | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     fun getTask(): String { |     fun getTask(): String { | ||||||
|  |  | ||||||
|  | @ -38,7 +38,7 @@ class FunctionalCustomTimerUnitTest : FunctionalTimerUnitTest() { | ||||||
|         Assert.assertTrue(timer.hasEnded()) |         Assert.assertTrue(timer.hasEnded()) | ||||||
|         Assert.assertEquals( |         Assert.assertEquals( | ||||||
|             FunctionalTimer.StudyState.DONE, |             FunctionalTimer.StudyState.DONE, | ||||||
|             timer.view |             timer.state | ||||||
|         ) |         ) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | @ -39,7 +39,7 @@ class FunctionalEndlessTimerUnitTest : FunctionalTimerUnitTest() { | ||||||
|             Assert.assertFalse(timer.hasEnded()) |             Assert.assertFalse(timer.hasEnded()) | ||||||
|             Assert.assertEquals( |             Assert.assertEquals( | ||||||
|                 FunctionalTimer.StudyState.FOCUS, |                 FunctionalTimer.StudyState.FOCUS, | ||||||
|                 timer.view |                 timer.state | ||||||
|             ) |             ) | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  | @ -31,7 +31,7 @@ class FunctionalPomodoroTimerUnitTest : FunctionalTimerUnitTest() { | ||||||
|         ) |         ) | ||||||
|         Assert.assertEquals( |         Assert.assertEquals( | ||||||
|             FunctionalTimer.StudyState.FOCUS, |             FunctionalTimer.StudyState.FOCUS, | ||||||
|             pomodoroTimer.view, |             pomodoroTimer.state, | ||||||
|         ) |         ) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | @ -54,7 +54,7 @@ class FunctionalPomodoroTimerUnitTest : FunctionalTimerUnitTest() { | ||||||
|         Assert.assertTrue(pomodoroTimer.hasEnded()) |         Assert.assertTrue(pomodoroTimer.hasEnded()) | ||||||
|         Assert.assertEquals( |         Assert.assertEquals( | ||||||
|             FunctionalTimer.StudyState.DONE, |             FunctionalTimer.StudyState.DONE, | ||||||
|             pomodoroTimer.view, |             pomodoroTimer.state, | ||||||
|         ) |         ) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | @ -67,7 +67,7 @@ class FunctionalPomodoroTimerUnitTest : FunctionalTimerUnitTest() { | ||||||
|         Assert.assertTrue(pomodoroTimer.isInBreak) |         Assert.assertTrue(pomodoroTimer.isInBreak) | ||||||
|         Assert.assertEquals( |         Assert.assertEquals( | ||||||
|             FunctionalTimer.StudyState.BREAK, |             FunctionalTimer.StudyState.BREAK, | ||||||
|             pomodoroTimer.view |             pomodoroTimer.state | ||||||
|         ) |         ) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | @ -79,7 +79,7 @@ class FunctionalPomodoroTimerUnitTest : FunctionalTimerUnitTest() { | ||||||
|         Assert.assertTrue(pomodoroTimer.isInBreak) |         Assert.assertTrue(pomodoroTimer.isInBreak) | ||||||
|         Assert.assertEquals( |         Assert.assertEquals( | ||||||
|             FunctionalTimer.StudyState.BREAK, |             FunctionalTimer.StudyState.BREAK, | ||||||
|             pomodoroTimer.view |             pomodoroTimer.state | ||||||
|         ) |         ) | ||||||
|         for (i in 0..breakTime) { |         for (i in 0..breakTime) { | ||||||
|             pomodoroTimer.tick() |             pomodoroTimer.tick() | ||||||
|  | @ -92,7 +92,7 @@ class FunctionalPomodoroTimerUnitTest : FunctionalTimerUnitTest() { | ||||||
|         ) |         ) | ||||||
|         Assert.assertEquals( |         Assert.assertEquals( | ||||||
|             FunctionalTimer.StudyState.FOCUS_REMAINING, |             FunctionalTimer.StudyState.FOCUS_REMAINING, | ||||||
|             pomodoroTimer.view |             pomodoroTimer.state | ||||||
|         ) |         ) | ||||||
|     } |     } | ||||||
| } | } | ||||||
		Reference in a new issue
	
	 brreynie
						brreynie