add: enum action, argument model ImportDialog

This commit is contained in:
DIAMANT alexandre 2023-12-06 11:33:09 +01:00
parent 80b84e747a
commit a98e9d34e3
3 changed files with 121 additions and 65 deletions

View file

@ -1,10 +1,12 @@
package fr.uparis.diamantkennel.memorisationapplication package fr.uparis.diamantkennel.memorisationapplication
import android.content.Context
import android.widget.Toast import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
@ -39,6 +41,7 @@ import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import fr.uparis.diamantkennel.memorisationapplication.data.SetOfQuestions import fr.uparis.diamantkennel.memorisationapplication.data.SetOfQuestions
import fr.uparis.diamantkennel.memorisationapplication.data.SetQuestions import fr.uparis.diamantkennel.memorisationapplication.data.SetQuestions
import fr.uparis.diamantkennel.memorisationapplication.ui.ActionHome
import fr.uparis.diamantkennel.memorisationapplication.ui.ErrorsAjout import fr.uparis.diamantkennel.memorisationapplication.ui.ErrorsAjout
import fr.uparis.diamantkennel.memorisationapplication.ui.HomeViewModel import fr.uparis.diamantkennel.memorisationapplication.ui.HomeViewModel
@ -67,16 +70,15 @@ fun HomeScreen(padding: PaddingValues, model: HomeViewModel = viewModel()) {
if (creationRequest) { if (creationRequest) {
CreationDialog( CreationDialog(
annuler = {(model::setCreation)(false)}, dismiss = model::dismissCreation,
model = model model = model
) )
} }
if (importationRequest) { if (importationRequest) {
ImportDialog( ImportDialog(
annuler = {model.setImportation(false)}, dismiss = model::dismissImportation,
confirmer = {model.setImportation(false)}/*, model = model
model = model*/
) )
} }
@ -98,43 +100,67 @@ fun HomeScreen(padding: PaddingValues, model: HomeViewModel = viewModel()) {
) { ) {
ShowList(setOfQuestions, currentSelection, model::updateSelection) ShowList(setOfQuestions, currentSelection, model::updateSelection)
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) { ActionRow(context, model)
Button(onClick = {
model.setCreation(true)
}) {
Text(text = context.getString(R.string.main_button_create))
}
Button(onClick = { Toast.makeText(context, "Modify", Toast.LENGTH_SHORT).show() }) {
Text(text = context.getString(R.string.main_button_modify))
}
Button(onClick = {
Toast.makeText(context, "Import", Toast.LENGTH_SHORT).show()
model.setImportation(true)
}) {
Text(text = context.getString(R.string.main_button_import))
}
}
Row (modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly){
Button(onClick = {
Toast.makeText(context, "DeleteBase", Toast.LENGTH_SHORT).show()
model.setDeletionDB(true)
}, colors = ButtonDefaults.buttonColors(containerColor = colorResource(id = R.color.red))) {
Text(text = context.getString(R.string.main_button_deletebase))
}
Button(onClick = {
Toast.makeText(context, "Delete", Toast.LENGTH_SHORT).show()
model.setDeletionSelect(true)
}) {
Text(text = context.getString(R.string.main_button_delete))
}
}
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) {
Button( Button(
modifier = Modifier.fillMaxWidth(0.9f), modifier = Modifier.fillMaxWidth(0.9f),
onClick = { Toast.makeText(context, "Start", Toast.LENGTH_SHORT).show() }) { onClick = { Toast.makeText(context, "Start", Toast.LENGTH_SHORT).show() }) {
Text(text = context.getString(R.string.main_button_start), fontSize = 30.sp) Text(text = context.getString(R.string.main_button_start), fontSize = 30.sp)
} }
Spacer(modifier = Modifier.padding(top = 50.dp))
DeleteRow(context, model)
}
}
@Composable
private fun DeleteRow(
context: Context,
model: HomeViewModel
) {
Row(modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center,/*, horizontalArrangement = Arrangement.SpaceEvenly*/) {
Button(onClick = {
(model::doAction)(ActionHome.DELETION_DB)
}, colors = ButtonDefaults.buttonColors(containerColor = colorResource(id = R.color.red))) {
Text(text = context.getString(R.string.main_button_deletebase))
}
Spacer(modifier = Modifier.padding(2.dp))
Button(onClick = {
(model::doAction)(ActionHome.DELETION_SELECT)
}) {
Text(text = context.getString(R.string.main_button_delete))
}
}
}
@Composable
private fun ActionRow(
context: Context,
model: HomeViewModel
) {
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) {
Button(onClick = {
(model::doAction)(ActionHome.CREATION)
}) {
Text(text = context.getString(R.string.main_button_create))
}
Spacer(modifier = Modifier.padding(2.dp))
Button(onClick = { Toast.makeText(context, "Modify", Toast.LENGTH_SHORT).show() }) {
Text(text = context.getString(R.string.main_button_modify))
}
Spacer(modifier = Modifier.padding(2.dp))
Button(onClick = {
(model::doAction)(ActionHome.IMPORTATION)
}) {
Text(text = context.getString(R.string.main_button_import))
} }
} }
} }
@ -142,13 +168,13 @@ fun HomeScreen(padding: PaddingValues, model: HomeViewModel = viewModel()) {
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun CreationDialog( fun CreationDialog(
annuler: () -> Unit, dismiss: () -> Unit,
model : HomeViewModel = viewModel() model : HomeViewModel = viewModel()
) { ) {
val sujet by model.sujet val sujet by model.sujet
AlertDialog( AlertDialog(
onDismissRequest = annuler, onDismissRequest = dismiss,
title = { Text(text ="Créer un sujet") }, title = { Text(text ="Créer un sujet") },
text = { text = {
OutlinedTextField( OutlinedTextField(
@ -165,7 +191,7 @@ fun CreationDialog(
}, },
dismissButton = { dismissButton = {
Button( Button(
onClick = annuler, onClick = dismiss,
content = { Text("Annuler") } content = { Text("Annuler") }
) )
} }
@ -174,14 +200,17 @@ fun CreationDialog(
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun ImportDialog(annuler: () -> Unit, confirmer: () -> Unit) { fun ImportDialog(
dismiss: () -> Unit,
model: HomeViewModel
) {
val radioOptions = listOf("Locale", "Internet") val radioOptions = listOf("Locale", "Internet")
val (selectedOption, onOptionSelected) = remember {mutableStateOf(radioOptions[0])} val (selectedOption, onOptionSelected) = remember {mutableStateOf(radioOptions[0])}
var lien by remember { mutableStateOf("") } var lien by remember { mutableStateOf("") }
AlertDialog( AlertDialog(
onDismissRequest = annuler, onDismissRequest = dismiss,
title = { Text(text = "Importer un jeu de question") }, title = { Text(text = "Importer un jeu de question") },
text = { text = {
Column { Column {
@ -220,13 +249,13 @@ fun ImportDialog(annuler: () -> Unit, confirmer: () -> Unit) {
confirmButton = { confirmButton = {
Button( Button(
// TODO : on charge le jeu de question dans le sujet // TODO : on charge le jeu de question dans le sujet
onClick = confirmer, onClick = dismiss,
content = { Text("Importer") } content = { Text("Importer") }
) )
}, },
dismissButton = { dismissButton = {
Button( Button(
onClick = annuler, onClick = dismiss,
content = { Text("Annuler") } content = { Text("Annuler") }
) )
} }

View file

@ -0,0 +1,5 @@
package fr.uparis.diamantkennel.memorisationapplication.ui
enum class ActionHome {
CREATION, IMPORTATION, MODIFIER, DELETION_SELECT, DELETION_DB
}

View file

@ -17,6 +17,7 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
var selected = mutableStateOf<SetQuestions?>(null) var selected = mutableStateOf<SetQuestions?>(null)
var creation = mutableStateOf(false) var creation = mutableStateOf(false)
var modification = mutableStateOf(false)
var importation = mutableStateOf(false) var importation = mutableStateOf(false)
var deletionSelect = mutableStateOf(false) var deletionSelect = mutableStateOf(false)
var deletionDB = mutableStateOf(false) var deletionDB = mutableStateOf(false)
@ -29,22 +30,6 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
sujet.value = s sujet.value = s
} }
fun setCreation(t: Boolean) {
creation.value = t
}
fun setImportation(t: Boolean) {
importation.value = t
}
fun setDeletionSelect(t: Boolean) {
deletionSelect.value = t
}
fun setDeletionDB(t: Boolean) {
deletionDB.value = t
}
/* Methods */ /* Methods */
fun updateSelection(element: SetQuestions) { fun updateSelection(element: SetQuestions) {
if (selected.value == element) { if (selected.value == element) {
@ -71,8 +56,36 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
error.value = ErrorsAjout.DUPLICATE error.value = ErrorsAjout.DUPLICATE
} }
sujet.value = "" resetSujet()
creation.value = false cleanErrors()
}
dismissCreation()
}
fun doAction(action: ActionHome) {
when (action) {
ActionHome.CREATION -> {
creation.value = true
}
ActionHome.IMPORTATION -> {
importation.value = true
}
ActionHome.MODIFIER -> {
if (selected.value != null) {
modification.value = true
}
}
ActionHome.DELETION_SELECT -> {
if (selected.value != null) {
deletionSelect.value = true
}
}
ActionHome.DELETION_DB -> {
deletionDB.value = true
}
null -> return
} }
} }
@ -94,9 +107,18 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
selected.value = null selected.value = null
} }
private fun reset() { private fun resetSujet() {
sujet.value = "" sujet.value = ""
error.value = null }
fun dismissCreation() {
creation.value = false
}
fun dismissModification() {
modification.value = false
}
fun dismissImportation() {
importation.value = false
} }
fun cleanErrors() { fun cleanErrors() {