#76 session recap viewmodel en screen
This commit is contained in:
		
							parent
							
								
									53d292aef4
								
							
						
					
					
						commit
						96795d0e18
					
				
					 2 changed files with 93 additions and 1 deletions
				
			
		| 
						 | 
				
			
			@ -1,2 +1,65 @@
 | 
			
		|||
package be.ugent.sel.studeez.screens.session_recap
 | 
			
		||||
 | 
			
		||||
import androidx.compose.foundation.layout.Column
 | 
			
		||||
import androidx.compose.material.ButtonDefaults
 | 
			
		||||
import androidx.compose.material.Text
 | 
			
		||||
import androidx.compose.runtime.Composable
 | 
			
		||||
import androidx.compose.ui.Modifier
 | 
			
		||||
import androidx.compose.ui.graphics.Color
 | 
			
		||||
import be.ugent.sel.studeez.R
 | 
			
		||||
import be.ugent.sel.studeez.common.composable.BasicButton
 | 
			
		||||
import be.ugent.sel.studeez.common.ext.basicButton
 | 
			
		||||
import be.ugent.sel.studeez.data.local.models.SessionReport
 | 
			
		||||
import be.ugent.sel.studeez.data.local.models.timer_functional.HoursMinutesSeconds
 | 
			
		||||
import be.ugent.sel.studeez.data.local.models.timer_functional.Time
 | 
			
		||||
 | 
			
		||||
data class SessionRecapActions(
 | 
			
		||||
    val getSessionReport: () -> SessionReport,
 | 
			
		||||
    val saveSession: () -> Unit,
 | 
			
		||||
    val discardSession: () -> Unit
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
fun getSessionRecapActions(
 | 
			
		||||
    viewModel: SessionRecapViewModel,
 | 
			
		||||
    openAndPopUp: (String, String) -> Unit,
 | 
			
		||||
): SessionRecapActions {
 | 
			
		||||
    return SessionRecapActions(
 | 
			
		||||
        viewModel::getSessionReport,
 | 
			
		||||
        {viewModel.saveSession(openAndPopUp)},
 | 
			
		||||
        {viewModel.saveSession(openAndPopUp)}
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun SessionRecapRoute(
 | 
			
		||||
    openAndPopUp: (String, String) -> Unit,
 | 
			
		||||
    modifier: Modifier = Modifier,
 | 
			
		||||
    viewModel: SessionRecapViewModel,
 | 
			
		||||
) {
 | 
			
		||||
    SessionRecapScreen(
 | 
			
		||||
        modifier = modifier,
 | 
			
		||||
        getSessionRecapActions(viewModel, openAndPopUp)
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun SessionRecapScreen(modifier: Modifier, sessionRecapActions: SessionRecapActions) {
 | 
			
		||||
    val sessionReport: SessionReport = sessionRecapActions.getSessionReport()
 | 
			
		||||
    val studyTime: Int = sessionReport.studyTime
 | 
			
		||||
    val hms: HoursMinutesSeconds = Time(studyTime).getAsHMS()
 | 
			
		||||
    Column {
 | 
			
		||||
        Text(text = "You studied: ${hms.hours} : ${hms.minutes} : ${hms.seconds}")
 | 
			
		||||
 | 
			
		||||
        BasicButton(
 | 
			
		||||
            R.string.save, Modifier.basicButton()
 | 
			
		||||
        ) {
 | 
			
		||||
            sessionRecapActions.saveSession()
 | 
			
		||||
        }
 | 
			
		||||
        BasicButton(
 | 
			
		||||
            R.string.discard, Modifier.basicButton(),
 | 
			
		||||
            colors = ButtonDefaults.buttonColors(backgroundColor = Color.Red)
 | 
			
		||||
        ) {
 | 
			
		||||
            sessionRecapActions.discardSession()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,4 +1,33 @@
 | 
			
		|||
package be.ugent.sel.studeez.screens.session_recap
 | 
			
		||||
 | 
			
		||||
class SessionRecapViewModel {
 | 
			
		||||
import be.ugent.sel.studeez.data.SessionReportState
 | 
			
		||||
import be.ugent.sel.studeez.data.local.models.SessionReport
 | 
			
		||||
import be.ugent.sel.studeez.domain.LogService
 | 
			
		||||
import be.ugent.sel.studeez.domain.SessionDAO
 | 
			
		||||
import be.ugent.sel.studeez.navigation.StudeezDestinations
 | 
			
		||||
import be.ugent.sel.studeez.screens.StudeezViewModel
 | 
			
		||||
import dagger.hilt.android.lifecycle.HiltViewModel
 | 
			
		||||
import javax.inject.Inject
 | 
			
		||||
 | 
			
		||||
@HiltViewModel
 | 
			
		||||
class SessionRecapViewModel @Inject constructor(
 | 
			
		||||
    private val sessionReportState: SessionReportState,
 | 
			
		||||
    private val sessionDAO: SessionDAO,
 | 
			
		||||
    logService: LogService
 | 
			
		||||
) : StudeezViewModel(logService) {
 | 
			
		||||
 | 
			
		||||
    private val report: SessionReport = sessionReportState.sessionReport!!
 | 
			
		||||
 | 
			
		||||
    fun getSessionReport(): SessionReport {
 | 
			
		||||
        return report
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun saveSession(open: (String, String) -> Unit) {
 | 
			
		||||
        sessionDAO.saveSession(getSessionReport())
 | 
			
		||||
        open(StudeezDestinations.HOME_SCREEN, StudeezDestinations.SESSION_RECAP)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun discardSession(open: (String, String) -> Unit) {
 | 
			
		||||
        open(StudeezDestinations.HOME_SCREEN, StudeezDestinations.SESSION_RECAP)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in a new issue