werkende timers
This commit is contained in:
		
							parent
							
								
									199b4c2f6f
								
							
						
					
					
						commit
						ecea5746f4
					
				
					 7 changed files with 66 additions and 38 deletions
				
			
		|  | @ -26,6 +26,7 @@ import be.ugent.sel.studeez.screens.profile.ProfileScreen | |||
| import be.ugent.sel.studeez.screens.sign_up.SignUpScreen | ||||
| import be.ugent.sel.studeez.screens.splash.SplashScreen | ||||
| import be.ugent.sel.studeez.screens.timer_overview.TimerOverviewScreen | ||||
| import be.ugent.sel.studeez.screens.timers.TimerSelectionScreen | ||||
| import be.ugent.sel.studeez.ui.theme.StudeezTheme | ||||
| import kotlinx.coroutines.CoroutineScope | ||||
| 
 | ||||
|  | @ -130,4 +131,8 @@ fun NavGraphBuilder.studeezGraph(appState: StudeezAppstate) { | |||
|     composable(StudeezDestinations.EDIT_PROFILE_SCREEN) { | ||||
|         EditProfileScreen(goBack, openAndPopUp) | ||||
|     } | ||||
| 
 | ||||
|     composable(StudeezDestinations.TIMER_SELECTION_SCREEN) { | ||||
|         TimerSelectionScreen(open, openAndPopUp) | ||||
|     } | ||||
| } | ||||
|  | @ -5,8 +5,9 @@ object StudeezDestinations { | |||
|     const val SIGN_UP_SCREEN = "signup" | ||||
|     const val LOGIN_SCREEN = "login" | ||||
| 
 | ||||
|      const val HOME_SCREEN = "home" | ||||
|     const val HOME_SCREEN = "home" | ||||
|     const val TIMER_OVERVIEW_SCREEN = "timer_overview" | ||||
|     const val TIMER_SELECTION_SCREEN = "timer_selection" | ||||
|     const val SESSION_SCREEN = "session" | ||||
|  //    const val TASKS_SCREEN = "tasks" | ||||
|  //    const val SESSIONS_SCREEN = "sessions" | ||||
|  |  | |||
|  | @ -15,7 +15,7 @@ class HomeViewModel @Inject constructor( | |||
|     logService: LogService | ||||
| ) : StudeezViewModel(logService) { | ||||
| 
 | ||||
|     fun onStartSessionClick(openAndPopUp: (String) -> Unit) { | ||||
|         openAndPopUp(StudeezDestinations.SESSION_SCREEN) | ||||
|     fun onStartSessionClick(open: (String) -> Unit) { | ||||
|         open(StudeezDestinations.TIMER_SELECTION_SCREEN) | ||||
|     } | ||||
| } | ||||
|  | @ -14,33 +14,33 @@ import androidx.hilt.navigation.compose.hiltViewModel | |||
| import be.ugent.sel.studeez.R | ||||
| import be.ugent.sel.studeez.common.composable.PrimaryScreenTemplate | ||||
| import be.ugent.sel.studeez.resources | ||||
| import be.ugent.sel.studeez.screens.timers.TimerSelectionViewModel | ||||
| import kotlinx.coroutines.delay | ||||
| 
 | ||||
| @Composable | ||||
| fun SessionScreen( | ||||
|     open: (String) -> Unit, | ||||
|     openAndPopUp: (String, String) -> Unit, | ||||
|     viewModel: SessionViewModel = hiltViewModel() | ||||
| ) { | ||||
|     PrimaryScreenTemplate( | ||||
|         title = resources().getString(R.string.start_session), | ||||
|         open = open, | ||||
|         openAndPopUp = openAndPopUp | ||||
|     ) { | ||||
|         Timer(viewModel) | ||||
|         Timer() | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| @Composable | ||||
| private fun Timer(viewModel: SessionViewModel = hiltViewModel()) { | ||||
| fun Timer(viewModel: TimerSelectionViewModel = hiltViewModel()) { | ||||
|     var tikker by remember { mutableStateOf(false) } | ||||
|     LaunchedEffect(tikker) { | ||||
|         delay(1000) | ||||
|         viewModel.getTimer().tick() | ||||
|         viewModel.sessionTimer!!.tick() | ||||
|         tikker = !tikker | ||||
|     } | ||||
| 
 | ||||
|     val hms = viewModel.getTimer().getHoursMinutesSeconds() | ||||
|     val hms = viewModel.sessionTimer!!.getHoursMinutesSeconds() | ||||
|     Column { | ||||
|         Text( | ||||
|             text = "${hms.hours} : ${hms.minutes} : ${hms.seconds}", | ||||
|  | @ -50,7 +50,7 @@ private fun Timer(viewModel: SessionViewModel = hiltViewModel()) { | |||
|             fontSize = 80.sp | ||||
|         ) | ||||
|         Text( | ||||
|             text = viewModel.getTimer().getViewString(), | ||||
|             text = viewModel.sessionTimer!!.getViewString(), | ||||
|             modifier = Modifier.fillMaxWidth(), | ||||
|             textAlign = TextAlign.Center, | ||||
|             fontWeight = FontWeight.Light, | ||||
|  |  | |||
|  | @ -1,5 +1,6 @@ | |||
| package be.ugent.sel.studeez.screens.timer_overview | ||||
| 
 | ||||
| import androidx.annotation.StringRes | ||||
| import androidx.compose.foundation.layout.Arrangement | ||||
| import androidx.compose.foundation.layout.Column | ||||
| import androidx.compose.foundation.layout.Row | ||||
|  | @ -46,12 +47,12 @@ fun TimerOverviewScreen( | |||
|             ) { | ||||
|                 // Default Timers, cannot be edited | ||||
|                 items(viewModel.getDefaultTimers()) { | ||||
|                     TimerEntry(timerInfo = it, canEdit = false) | ||||
|                     TimerEntry(timerInfo = it, canDisplay = false) | ||||
|                 } | ||||
| 
 | ||||
|                 // User timers, can be edited | ||||
|                 items(timers.value) { | ||||
|                     TimerEntry(timerInfo = it, true) { timerInfo -> | ||||
|                     TimerEntry(timerInfo = it, true, R.string.edit) { timerInfo -> | ||||
|                         viewModel.update(timerInfo) | ||||
|                     } | ||||
|                 } | ||||
|  | @ -65,7 +66,12 @@ fun TimerOverviewScreen( | |||
| } | ||||
| 
 | ||||
| @Composable | ||||
| fun TimerEntry(timerInfo: TimerInfo, canEdit: Boolean, update: (TimerInfo) -> Unit = {}) { | ||||
| fun TimerEntry( | ||||
|     timerInfo: TimerInfo, | ||||
|     canDisplay: Boolean, | ||||
|     @StringRes buttonName: Int = -1, | ||||
|     buttonFunction: (TimerInfo) -> Unit = {} | ||||
| ) { | ||||
|     Row( | ||||
|         verticalAlignment = Alignment.CenterVertically, | ||||
|         modifier = Modifier.fillMaxWidth(), | ||||
|  | @ -83,9 +89,9 @@ fun TimerEntry(timerInfo: TimerInfo, canEdit: Boolean, update: (TimerInfo) -> Un | |||
|                 fontSize = 15.sp | ||||
|             ) | ||||
|         } | ||||
|         if (canEdit) { | ||||
|             BasicButton(R.string.edit, Modifier.card()) { | ||||
|                 // TODO | ||||
|         if (canDisplay) { | ||||
|             BasicButton(buttonName, Modifier.card()) { | ||||
|                 buttonFunction(timerInfo) | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|  |  | |||
|  | @ -4,26 +4,24 @@ import androidx.compose.foundation.layout.Arrangement | |||
| import androidx.compose.foundation.layout.Column | ||||
| import androidx.compose.foundation.lazy.LazyColumn | ||||
| import androidx.compose.foundation.lazy.items | ||||
| import androidx.compose.runtime.Composable | ||||
| import androidx.compose.runtime.collectAsState | ||||
| import androidx.compose.ui.Modifier | ||||
| import androidx.compose.runtime.* | ||||
| import androidx.compose.ui.unit.dp | ||||
| import androidx.hilt.navigation.compose.hiltViewModel | ||||
| import be.ugent.sel.studeez.R | ||||
| import be.ugent.sel.studeez.common.composable.BasicButton | ||||
| import be.ugent.sel.studeez.common.composable.PrimaryScreenTemplate | ||||
| import be.ugent.sel.studeez.common.ext.basicButton | ||||
| import be.ugent.sel.studeez.data.local.models.timer_info.TimerInfo | ||||
| import be.ugent.sel.studeez.resources | ||||
| import be.ugent.sel.studeez.screens.session.Timer | ||||
| import be.ugent.sel.studeez.screens.timer_overview.TimerEntry | ||||
| import be.ugent.sel.studeez.screens.timer_overview.TimerOverviewViewModel | ||||
| 
 | ||||
| @Composable | ||||
| fun TimerSelectScreen( | ||||
| fun TimerSelectionScreen( | ||||
|     open: (String) -> Unit, | ||||
|     openAndPopUp: (String, String) -> Unit, | ||||
|     viewModel: TimerSelectViewModel = hiltViewModel() | ||||
|     viewModel: TimerSelectionViewModel = hiltViewModel() | ||||
| ) { | ||||
| 
 | ||||
|     val inSession by remember { viewModel.inSession } | ||||
|     val timers = viewModel.getAllTimers().collectAsState(initial = emptyList()) | ||||
| 
 | ||||
|     PrimaryScreenTemplate( | ||||
|  | @ -32,19 +30,28 @@ fun TimerSelectScreen( | |||
|         openAndPopUp = openAndPopUp | ||||
|     ) { | ||||
| 
 | ||||
|         Column { | ||||
|             LazyColumn( | ||||
|                 verticalArrangement = Arrangement.spacedBy(7.dp) | ||||
|             ) { | ||||
| 
 | ||||
|                 // All timers | ||||
|                 items(timers.value) { | ||||
|                     TimerEntry(timerInfo = it, true) { timerInfo -> | ||||
|                         viewModel.startSession() | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         if (inSession) { | ||||
|             Timer(viewModel) | ||||
|         } else { | ||||
|             TimerSelection(timers = timers, viewModel = viewModel) | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| @Composable | ||||
| fun TimerSelection(timers: State<List<TimerInfo>>, viewModel: TimerSelectionViewModel, ) { | ||||
|     LazyColumn(verticalArrangement = Arrangement.spacedBy(7.dp)) { | ||||
| 
 | ||||
|         // All timers | ||||
|         items(timers.value) { | ||||
|             TimerEntry( | ||||
|                 timerInfo = it, | ||||
|                 canDisplay = true, | ||||
|                 buttonName = R.string.start | ||||
|             ) { timerInfo -> | ||||
|                 viewModel.startSession(timerInfo) | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | @ -1,5 +1,10 @@ | |||
| package be.ugent.sel.studeez.screens.timers | ||||
| 
 | ||||
| import androidx.compose.runtime.getValue | ||||
| import androidx.compose.runtime.mutableStateOf | ||||
| import androidx.compose.runtime.remember | ||||
| import androidx.compose.runtime.setValue | ||||
| import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalTimer | ||||
| import be.ugent.sel.studeez.data.local.models.timer_info.TimerInfo | ||||
| import be.ugent.sel.studeez.domain.LogService | ||||
| import be.ugent.sel.studeez.domain.TimerDAO | ||||
|  | @ -9,16 +14,20 @@ import kotlinx.coroutines.flow.Flow | |||
| import javax.inject.Inject | ||||
| 
 | ||||
| @HiltViewModel | ||||
| class TimerSelectViewModel @Inject constructor( | ||||
| class TimerSelectionViewModel @Inject constructor( | ||||
|     private val timerDAO: TimerDAO, | ||||
|     logService: LogService | ||||
| ) : StudeezViewModel(logService) { | ||||
| 
 | ||||
|     var inSession = mutableStateOf(false) | ||||
|     var sessionTimer: FunctionalTimer? = null | ||||
| 
 | ||||
|     fun getAllTimers() : Flow<List<TimerInfo>> { | ||||
|         return timerDAO.getAllTimers() | ||||
|     } | ||||
| 
 | ||||
|     fun startSession() { | ||||
| 
 | ||||
|     fun startSession(timerInfo: TimerInfo) { | ||||
|         inSession.value = true | ||||
|         sessionTimer = timerInfo.getFunctionalTimer() | ||||
|     } | ||||
| } | ||||
		Reference in a new issue
	
	 lbarraga
						lbarraga