wrap SessionScreen in route
This commit is contained in:
		
							parent
							
								
									f1efad5120
								
							
						
					
					
						commit
						6bfa96bab7
					
				
					 3 changed files with 86 additions and 34 deletions
				
			
		|  | @ -29,6 +29,7 @@ import be.ugent.sel.studeez.screens.home.HomeRoute | |||
| import be.ugent.sel.studeez.screens.log_in.LoginRoute | ||||
| import be.ugent.sel.studeez.screens.profile.EditProfileRoute | ||||
| import be.ugent.sel.studeez.screens.profile.ProfileRoute | ||||
| import be.ugent.sel.studeez.screens.session.SessionRoute | ||||
| import be.ugent.sel.studeez.screens.session.SessionScreen | ||||
| import be.ugent.sel.studeez.screens.sign_up.SignUpRoute | ||||
| import be.ugent.sel.studeez.screens.splash.SplashRoute | ||||
|  | @ -128,7 +129,7 @@ fun NavGraphBuilder.studeezGraph(appState: StudeezAppstate) { | |||
|     } | ||||
| 
 | ||||
|     composable(StudeezDestinations.SESSION_SCREEN) { | ||||
|         SessionScreen(open, openAndPopUp) | ||||
|         SessionRoute(open, viewModel = hiltViewModel()) | ||||
|     } | ||||
| 
 | ||||
|     // TODO Timers screen | ||||
|  |  | |||
|  | @ -10,38 +10,61 @@ import androidx.compose.foundation.layout.Column | |||
| import androidx.compose.foundation.layout.fillMaxWidth | ||||
| import androidx.compose.foundation.layout.padding | ||||
| import androidx.compose.foundation.shape.RoundedCornerShape | ||||
| import androidx.compose.material.Button | ||||
| import androidx.compose.material.Text | ||||
| import androidx.compose.material.TextButton | ||||
| import androidx.compose.ui.Alignment | ||||
| import androidx.compose.runtime.Composable | ||||
| import androidx.compose.runtime.LaunchedEffect | ||||
| import androidx.compose.runtime.getValue | ||||
| import androidx.compose.runtime.mutableStateOf | ||||
| import androidx.compose.runtime.remember | ||||
| import androidx.compose.runtime.setValue | ||||
| import androidx.compose.ui.Alignment | ||||
| import androidx.compose.ui.Modifier | ||||
| import androidx.compose.ui.graphics.Color | ||||
| import androidx.compose.ui.platform.LocalContext | ||||
| import androidx.compose.ui.text.font.FontWeight | ||||
| import androidx.compose.ui.text.style.TextAlign | ||||
| import androidx.compose.ui.tooling.preview.Preview | ||||
| import androidx.compose.ui.unit.dp | ||||
| import be.ugent.sel.studeez.navigation.StudeezDestinations | ||||
| import androidx.compose.ui.unit.sp | ||||
| import androidx.hilt.navigation.compose.hiltViewModel | ||||
| import be.ugent.sel.studeez.R | ||||
| 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 | ||||
| import kotlin.time.Duration.Companion.seconds | ||||
| 
 | ||||
| var timerEnd = false | ||||
| 
 | ||||
| data class SessionActions( | ||||
|     val getTimer: () -> FunctionalTimer, | ||||
|     val getTask: () -> String, | ||||
|     val prepareMediaPlayer: () -> Unit, | ||||
|     val releaseMediaPlayer: () -> Unit, | ||||
|     val play: () -> Unit, | ||||
| ) | ||||
| 
 | ||||
| fun getSessionActions( | ||||
|     viewModel: SessionViewModel, | ||||
|     mediaplayer: MediaPlayer, | ||||
| ): SessionActions { | ||||
|     return SessionActions( | ||||
|         getTimer = viewModel::getTimer, | ||||
|         getTask = viewModel::getTask, | ||||
|         prepareMediaPlayer = mediaplayer::prepare, | ||||
|         releaseMediaPlayer = mediaplayer::release, | ||||
|         play = mediaplayer::start, | ||||
|     ) | ||||
| } | ||||
| 
 | ||||
| @Composable | ||||
| fun SessionScreen( | ||||
| fun SessionRoute( | ||||
|     open: (String) -> Unit, | ||||
|     openAndPopUp: (String, String) -> Unit, | ||||
|     viewModel: SessionViewModel = hiltViewModel() | ||||
|     viewModel: SessionViewModel, | ||||
| ) { | ||||
|     val context = LocalContext.current | ||||
|     val uri: Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) | ||||
|  | @ -57,24 +80,34 @@ fun SessionScreen( | |||
|     mediaplayer.setOnPreparedListener { | ||||
|         mediaplayer.start() | ||||
|     } | ||||
|     SessionScreen( | ||||
|         open = open, | ||||
|         sessionActions = getSessionActions(viewModel, mediaplayer), | ||||
|     ) | ||||
| } | ||||
| 
 | ||||
| @Composable | ||||
| fun SessionScreen( | ||||
|     open: (String) -> Unit, | ||||
|     sessionActions: SessionActions, | ||||
| ) { | ||||
|     Column( | ||||
|        modifier = Modifier.padding(10.dp) | ||||
|    ) { | ||||
|         Timer(viewModel, mediaplayer) | ||||
| 
 | ||||
|         modifier = Modifier.padding(10.dp) | ||||
|     ) { | ||||
|         Timer( | ||||
|             sessionActions = sessionActions, | ||||
|         ) | ||||
|         Box( | ||||
|             contentAlignment = Alignment.Center, | ||||
|             modifier = Modifier | ||||
|             contentAlignment = Alignment.Center, modifier = Modifier | ||||
|                 .fillMaxWidth() | ||||
|                 .padding(50.dp) | ||||
|         ) { | ||||
|             TextButton( | ||||
|                 onClick = { | ||||
|                     mediaplayer.release() | ||||
|                     sessionActions.releaseMediaPlayer | ||||
|                     open(StudeezDestinations.HOME_SCREEN) | ||||
|                     // Vanaf hier ook naar report gaan als "end session" knop word ingedrukt | ||||
|                   }, | ||||
|                 }, | ||||
|                 modifier = Modifier | ||||
|                     .padding(horizontal = 20.dp) | ||||
|                     .border(1.dp, Color.Red, RoundedCornerShape(32.dp)) | ||||
|  | @ -89,28 +122,37 @@ fun SessionScreen( | |||
|                 ) | ||||
|             } | ||||
|         } | ||||
| //        Button(onClick = sessionActions.play) { | ||||
| //            Text(text = "Play") | ||||
| //        } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| @Composable | ||||
| private fun Timer(viewModel: SessionViewModel = hiltViewModel(), mediaplayer: MediaPlayer) { | ||||
| private fun Timer( | ||||
|     sessionActions: SessionActions, | ||||
| ) { | ||||
|     var tikker by remember { mutableStateOf(false) } | ||||
|     LaunchedEffect(tikker) { | ||||
|         delay(1.seconds) | ||||
|         viewModel.getTimer().tick() | ||||
|         sessionActions.getTimer().tick() | ||||
|         tikker = !tikker | ||||
|     } | ||||
| 
 | ||||
|     if (viewModel.getTimer().hasCurrentCountdownEnded() && !viewModel.getTimer().hasEnded()) { | ||||
|         mediaplayer.prepare() | ||||
|     if ( | ||||
|         sessionActions.getTimer().hasCurrentCountdownEnded() && !sessionActions.getTimer() | ||||
|             .hasEnded() | ||||
|     ) { | ||||
|         sessionActions.prepareMediaPlayer() | ||||
|     } | ||||
| 
 | ||||
|     if (!timerEnd && viewModel.getTimer().hasEnded()) { | ||||
|         mediaplayer.prepare() | ||||
|         timerEnd = true // Placeholder, vanaf hier moet het report opgestart worden en de sessie afgesloten | ||||
|     if (!timerEnd && sessionActions.getTimer().hasEnded()) { | ||||
|         sessionActions.prepareMediaPlayer() | ||||
|         timerEnd = | ||||
|             true // Placeholder, vanaf hier moet het report opgestart worden en de sessie afgesloten | ||||
|     } | ||||
| 
 | ||||
|     val hms = viewModel.getTimer().getHoursMinutesSeconds() | ||||
|     val hms = sessionActions.getTimer().getHoursMinutesSeconds() | ||||
|     Column { | ||||
|         Text( | ||||
|             text = "${hms.hours} : ${hms.minutes} : ${hms.seconds}", | ||||
|  | @ -121,13 +163,12 @@ private fun Timer(viewModel: SessionViewModel = hiltViewModel(), mediaplayer: Me | |||
|             fontWeight = FontWeight.Bold, | ||||
|             fontSize = 40.sp, | ||||
|         ) | ||||
|         val stateString: String = when (viewModel.getTimer().view) { | ||||
|         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 -> | ||||
|                 (viewModel.getTimer() as FunctionalPomodoroTimer?)?.breaksRemaining?.let { | ||||
|                     resources().getQuantityString(R.plurals.state_focus_remaining, it, it) | ||||
|             StudyState.FOCUS_REMAINING -> (sessionActions.getTimer() as FunctionalPomodoroTimer?)?.breaksRemaining?.let { | ||||
|                 resources().getQuantityString(R.plurals.state_focus_remaining, it, it) | ||||
|             }.toString() | ||||
|         } | ||||
| 
 | ||||
|  | @ -140,8 +181,7 @@ private fun Timer(viewModel: SessionViewModel = hiltViewModel(), mediaplayer: Me | |||
|         ) | ||||
| 
 | ||||
|         Box( | ||||
|             contentAlignment = Alignment.Center, | ||||
|             modifier = Modifier | ||||
|             contentAlignment = Alignment.Center, modifier = Modifier | ||||
|                 .fillMaxWidth() | ||||
|                 .padding(50.dp) | ||||
|         ) { | ||||
|  | @ -152,16 +192,28 @@ private fun Timer(viewModel: SessionViewModel = hiltViewModel(), mediaplayer: Me | |||
|                     .background(Color.Blue, RoundedCornerShape(32.dp)) | ||||
|             ) { | ||||
|                 Text( | ||||
|                     text = viewModel.getTask(), | ||||
|                     text = sessionActions.getTask(), | ||||
|                     color = Color.White, | ||||
|                     fontSize = 18.sp, | ||||
|                     fontWeight = FontWeight.Bold, | ||||
|                     modifier = Modifier | ||||
|                         .padding(vertical = 4.dp, horizontal = 20.dp) | ||||
|                     modifier = Modifier.padding(vertical = 4.dp, horizontal = 20.dp) | ||||
|                 ) | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| @Preview | ||||
| @Composable | ||||
| fun TimerPreview() { | ||||
|     Timer(sessionActions = SessionActions({ FunctionalEndlessTimer() }, { "Preview" }, {}, {}, {})) | ||||
| } | ||||
| 
 | ||||
| } | ||||
| @Preview | ||||
| @Composable | ||||
| fun SessionPreview() { | ||||
|     SessionScreen( | ||||
|         open = {}, | ||||
|         sessionActions = SessionActions({ FunctionalEndlessTimer() }, { "Preview" }, {}, {}, {}) | ||||
|     ) | ||||
| } | ||||
|  |  | |||
		Reference in a new issue
	
	 brreynie
						brreynie