parent
							
								
									71590bd5a2
								
							
						
					
					
						commit
						831050f3f2
					
				
					 9 changed files with 207 additions and 3 deletions
				
			
		| 
						 | 
				
			
			@ -0,0 +1,106 @@
 | 
			
		|||
package be.ugent.sel.studeez.screens.tasks.forms
 | 
			
		||||
 | 
			
		||||
import androidx.annotation.StringRes
 | 
			
		||||
import androidx.compose.foundation.layout.Column
 | 
			
		||||
import androidx.compose.material.OutlinedTextField
 | 
			
		||||
import androidx.compose.material.Text
 | 
			
		||||
import androidx.compose.runtime.Composable
 | 
			
		||||
import androidx.compose.runtime.getValue
 | 
			
		||||
import androidx.compose.ui.Modifier
 | 
			
		||||
import androidx.compose.ui.res.stringResource
 | 
			
		||||
import androidx.compose.ui.tooling.preview.Preview
 | 
			
		||||
import be.ugent.sel.studeez.R
 | 
			
		||||
import be.ugent.sel.studeez.common.composable.BasicButton
 | 
			
		||||
import be.ugent.sel.studeez.common.composable.DeleteButton
 | 
			
		||||
import be.ugent.sel.studeez.common.composable.SecondaryScreenTemplate
 | 
			
		||||
import be.ugent.sel.studeez.common.ext.basicButton
 | 
			
		||||
import be.ugent.sel.studeez.common.ext.fieldModifier
 | 
			
		||||
import be.ugent.sel.studeez.resources
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun SubjectAddRoute(
 | 
			
		||||
    goBack: () -> Unit,
 | 
			
		||||
    open: (String) -> Unit,
 | 
			
		||||
    viewModel: SubjectFormViewModel,
 | 
			
		||||
) {
 | 
			
		||||
    val uiState by viewModel.uiState
 | 
			
		||||
    SubjectForm(
 | 
			
		||||
        title = R.string.new_subject,
 | 
			
		||||
        goBack = goBack,
 | 
			
		||||
        uiState = uiState,
 | 
			
		||||
        onConfirm = { viewModel.onCreate(open) },
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun SubjectEditRoute(
 | 
			
		||||
    goBack: () -> Unit,
 | 
			
		||||
    open: (String) -> Unit,
 | 
			
		||||
    viewModel: SubjectFormViewModel,
 | 
			
		||||
) {
 | 
			
		||||
    val uiState by viewModel.uiState
 | 
			
		||||
    SubjectForm(
 | 
			
		||||
        title = R.string.edit_subject,
 | 
			
		||||
        goBack = goBack,
 | 
			
		||||
        uiState = uiState,
 | 
			
		||||
        onConfirm = { viewModel.onEdit(open) },
 | 
			
		||||
    ) {
 | 
			
		||||
        DeleteButton(onClick = viewModel::onDelete)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun SubjectForm(
 | 
			
		||||
    @StringRes title: Int,
 | 
			
		||||
    goBack: () -> Unit,
 | 
			
		||||
    uiState: SubjectFormUiState,
 | 
			
		||||
    onConfirm: () -> Unit,
 | 
			
		||||
    extraButton: @Composable () -> Unit = {},
 | 
			
		||||
) {
 | 
			
		||||
    SecondaryScreenTemplate(
 | 
			
		||||
        title = resources().getString(title),
 | 
			
		||||
        popUp = goBack,
 | 
			
		||||
    ) {
 | 
			
		||||
        Column {
 | 
			
		||||
            OutlinedTextField(
 | 
			
		||||
                singleLine = true,
 | 
			
		||||
                value = uiState.name,
 | 
			
		||||
                onValueChange = {},
 | 
			
		||||
                placeholder = { Text(stringResource(id = R.string.username)) },
 | 
			
		||||
                modifier = Modifier.fieldModifier(),
 | 
			
		||||
            )
 | 
			
		||||
            BasicButton(
 | 
			
		||||
                text = R.string.confirm,
 | 
			
		||||
                modifier = Modifier.basicButton(),
 | 
			
		||||
                onClick = onConfirm,
 | 
			
		||||
            )
 | 
			
		||||
            extraButton()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Preview
 | 
			
		||||
@Composable
 | 
			
		||||
fun AddSubjectFormPreview() {
 | 
			
		||||
    SubjectForm(
 | 
			
		||||
        title = R.string.new_subject,
 | 
			
		||||
        goBack = {},
 | 
			
		||||
        uiState = SubjectFormUiState(),
 | 
			
		||||
        onConfirm = {},
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Preview
 | 
			
		||||
@Composable
 | 
			
		||||
fun EditSubjectFormPreview() {
 | 
			
		||||
    SubjectForm(
 | 
			
		||||
        title = R.string.edit_subject,
 | 
			
		||||
        goBack = {},
 | 
			
		||||
        uiState = SubjectFormUiState(
 | 
			
		||||
            name = "Test Subject",
 | 
			
		||||
        ),
 | 
			
		||||
        onConfirm = {},
 | 
			
		||||
    ) {
 | 
			
		||||
        DeleteButton {}
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,6 @@
 | 
			
		|||
package be.ugent.sel.studeez.screens.tasks.forms
 | 
			
		||||
 | 
			
		||||
data class SubjectFormUiState(
 | 
			
		||||
    val name: String = "",
 | 
			
		||||
    val color: Long = 0xFFF44336,
 | 
			
		||||
)
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,63 @@
 | 
			
		|||
package be.ugent.sel.studeez.screens.tasks.forms
 | 
			
		||||
 | 
			
		||||
import androidx.compose.runtime.mutableStateOf
 | 
			
		||||
import be.ugent.sel.studeez.data.SelectedSubject
 | 
			
		||||
import be.ugent.sel.studeez.data.local.models.task.Subject
 | 
			
		||||
import be.ugent.sel.studeez.domain.LogService
 | 
			
		||||
import be.ugent.sel.studeez.domain.SubjectDAO
 | 
			
		||||
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 SubjectFormViewModel @Inject constructor(
 | 
			
		||||
    private val subjectDAO: SubjectDAO,
 | 
			
		||||
    private val selectedSubject: SelectedSubject,
 | 
			
		||||
    logService: LogService,
 | 
			
		||||
) : StudeezViewModel(logService) {
 | 
			
		||||
    var uiState = mutableStateOf(SubjectFormUiState())
 | 
			
		||||
        private set
 | 
			
		||||
 | 
			
		||||
    private val name: String
 | 
			
		||||
        get() = uiState.value.name
 | 
			
		||||
 | 
			
		||||
    private val color: Long
 | 
			
		||||
        get() = uiState.value.color
 | 
			
		||||
 | 
			
		||||
    fun onNameChange(newValue: String) {
 | 
			
		||||
        uiState.value = uiState.value.copy(name = newValue)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun onColorChange(newValue: Long) {
 | 
			
		||||
        uiState.value = uiState.value.copy(color = newValue)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun onDelete() {
 | 
			
		||||
        subjectDAO.deleteSubject(selectedSubject())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun onCreate(open: (String) -> Unit) {
 | 
			
		||||
        val newSubject = Subject(
 | 
			
		||||
            name = name,
 | 
			
		||||
            argb_color = color,
 | 
			
		||||
        )
 | 
			
		||||
        subjectDAO.saveSubject(
 | 
			
		||||
            newSubject
 | 
			
		||||
        )
 | 
			
		||||
        selectedSubject.set(newSubject)
 | 
			
		||||
        // TODO open newly created subject
 | 
			
		||||
//        open(StudeezDestinations.TASKS_SCREEN)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun onEdit(open: (String) -> Unit) {
 | 
			
		||||
        val newSubject = selectedSubject().copy(
 | 
			
		||||
            name = name,
 | 
			
		||||
            argb_color = color,
 | 
			
		||||
        )
 | 
			
		||||
        subjectDAO.updateSubject(
 | 
			
		||||
            newSubject
 | 
			
		||||
        )
 | 
			
		||||
        open(StudeezDestinations.TASKS_SCREEN)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in a new issue