#40 button now starts a new session
This commit is contained in:
		
							parent
							
								
									d409294ad8
								
							
						
					
					
						commit
						d5b9e1dae8
					
				
					 4 changed files with 61 additions and 66 deletions
				
			
		| 
						 | 
				
			
			@ -0,0 +1,11 @@
 | 
			
		|||
package be.ugent.sel.studeez.screens.timers
 | 
			
		||||
 | 
			
		||||
import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalTimer
 | 
			
		||||
import javax.inject.Inject
 | 
			
		||||
import javax.inject.Singleton
 | 
			
		||||
 | 
			
		||||
@Singleton
 | 
			
		||||
class SelectedTimerRepo @Inject constructor(){
 | 
			
		||||
    var selectedTimer: FunctionalTimer? = null
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,44 @@
 | 
			
		|||
package be.ugent.sel.studeez.screens.timer_selection
 | 
			
		||||
 | 
			
		||||
import androidx.compose.foundation.layout.Arrangement
 | 
			
		||||
import androidx.compose.foundation.lazy.LazyColumn
 | 
			
		||||
import androidx.compose.foundation.lazy.items
 | 
			
		||||
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.PrimaryScreenTemplate
 | 
			
		||||
import be.ugent.sel.studeez.resources
 | 
			
		||||
import be.ugent.sel.studeez.screens.timer_overview.TimerEntry
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun TimerSelectionScreen(
 | 
			
		||||
    open: (String) -> Unit,
 | 
			
		||||
    openAndPopUp: (String, String) -> Unit,
 | 
			
		||||
    viewModel: TimerSelectionViewModel = hiltViewModel()
 | 
			
		||||
) {
 | 
			
		||||
 | 
			
		||||
    val timers = viewModel.getAllTimers().collectAsState(initial = emptyList())
 | 
			
		||||
 | 
			
		||||
    PrimaryScreenTemplate(
 | 
			
		||||
        title = resources().getString(R.string.timers),
 | 
			
		||||
        open = open,
 | 
			
		||||
        openAndPopUp = openAndPopUp,
 | 
			
		||||
    ) {
 | 
			
		||||
 | 
			
		||||
        LazyColumn(verticalArrangement = Arrangement.spacedBy(7.dp)) {
 | 
			
		||||
 | 
			
		||||
            // All timers
 | 
			
		||||
            items(timers.value) {
 | 
			
		||||
                TimerEntry(
 | 
			
		||||
                    timerInfo = it,
 | 
			
		||||
                    canDisplay = true,
 | 
			
		||||
                    buttonName = R.string.start
 | 
			
		||||
                ) { timerInfo ->
 | 
			
		||||
                    viewModel.startSession(open, timerInfo)
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,13 +1,12 @@
 | 
			
		|||
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.SelectedTimerRepo
 | 
			
		||||
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
 | 
			
		||||
import be.ugent.sel.studeez.navigation.StudeezDestinations
 | 
			
		||||
import be.ugent.sel.studeez.screens.StudeezViewModel
 | 
			
		||||
import dagger.hilt.android.lifecycle.HiltViewModel
 | 
			
		||||
import kotlinx.coroutines.flow.Flow
 | 
			
		||||
| 
						 | 
				
			
			@ -16,18 +15,16 @@ import javax.inject.Inject
 | 
			
		|||
@HiltViewModel
 | 
			
		||||
class TimerSelectionViewModel @Inject constructor(
 | 
			
		||||
    private val timerDAO: TimerDAO,
 | 
			
		||||
    private val selectedTimerRepo: SelectedTimerRepo,
 | 
			
		||||
    logService: LogService
 | 
			
		||||
) : StudeezViewModel(logService) {
 | 
			
		||||
 | 
			
		||||
    var inSession = mutableStateOf(false)
 | 
			
		||||
    var sessionTimer: FunctionalTimer? = null
 | 
			
		||||
 | 
			
		||||
    fun getAllTimers() : Flow<List<TimerInfo>> {
 | 
			
		||||
        return timerDAO.getAllTimers()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun startSession(timerInfo: TimerInfo) {
 | 
			
		||||
        inSession.value = true
 | 
			
		||||
        sessionTimer = timerInfo.getFunctionalTimer()
 | 
			
		||||
    fun startSession(open: (String) -> Unit, timerInfo: TimerInfo) {
 | 
			
		||||
        selectedTimerRepo.selectedTimer = timerInfo.getFunctionalTimer()
 | 
			
		||||
        open(StudeezDestinations.SESSION_SCREEN)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,57 +0,0 @@
 | 
			
		|||
package be.ugent.sel.studeez.screens.timers
 | 
			
		||||
 | 
			
		||||
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.*
 | 
			
		||||
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.PrimaryScreenTemplate
 | 
			
		||||
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
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun TimerSelectionScreen(
 | 
			
		||||
    open: (String) -> Unit,
 | 
			
		||||
    openAndPopUp: (String, String) -> Unit,
 | 
			
		||||
    viewModel: TimerSelectionViewModel = hiltViewModel()
 | 
			
		||||
) {
 | 
			
		||||
 | 
			
		||||
    val inSession by remember { viewModel.inSession }
 | 
			
		||||
    val timers = viewModel.getAllTimers().collectAsState(initial = emptyList())
 | 
			
		||||
 | 
			
		||||
    PrimaryScreenTemplate(
 | 
			
		||||
        title = resources().getString(R.string.timers),
 | 
			
		||||
        open = open,
 | 
			
		||||
        openAndPopUp = openAndPopUp
 | 
			
		||||
    ) {
 | 
			
		||||
 | 
			
		||||
        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)
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in a new issue