From a98e9d34e318878533d7ade7bd2e3381e9946324 Mon Sep 17 00:00:00 2001 From: DIAMANT alexandre Date: Wed, 6 Dec 2023 11:33:09 +0100 Subject: [PATCH] add: enum action, argument model ImportDialog --- .../memorisationapplication/HomeScreen.kt | 119 +++++++++++------- .../memorisationapplication/ui/ActionHome.kt | 5 + .../ui/HomeViewModel.kt | 62 ++++++--- 3 files changed, 121 insertions(+), 65 deletions(-) create mode 100644 app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/ActionHome.kt diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/HomeScreen.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/HomeScreen.kt index 16c0e29..fbe9b1c 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/HomeScreen.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/HomeScreen.kt @@ -1,10 +1,12 @@ package fr.uparis.diamantkennel.memorisationapplication +import android.content.Context import android.widget.Toast import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth @@ -39,6 +41,7 @@ import androidx.compose.ui.unit.sp import androidx.lifecycle.viewmodel.compose.viewModel import fr.uparis.diamantkennel.memorisationapplication.data.SetOfQuestions 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.HomeViewModel @@ -67,16 +70,15 @@ fun HomeScreen(padding: PaddingValues, model: HomeViewModel = viewModel()) { if (creationRequest) { CreationDialog( - annuler = {(model::setCreation)(false)}, + dismiss = model::dismissCreation, model = model ) } if (importationRequest) { ImportDialog( - annuler = {model.setImportation(false)}, - confirmer = {model.setImportation(false)}/*, - model = model*/ + dismiss = model::dismissImportation, + model = model ) } @@ -98,43 +100,67 @@ fun HomeScreen(padding: PaddingValues, model: HomeViewModel = viewModel()) { ) { ShowList(setOfQuestions, currentSelection, model::updateSelection) - Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) { - 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)) - } + ActionRow(context, model) + + Button( + modifier = Modifier.fillMaxWidth(0.9f), + onClick = { Toast.makeText(context, "Start", Toast.LENGTH_SHORT).show() }) { + Text(text = context.getString(R.string.main_button_start), fontSize = 30.sp) } - 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)) - } + + 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)) } - Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) { - Button( - modifier = Modifier.fillMaxWidth(0.9f), - onClick = { Toast.makeText(context, "Start", Toast.LENGTH_SHORT).show() }) { - Text(text = context.getString(R.string.main_button_start), fontSize = 30.sp) - } + 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) @Composable fun CreationDialog( - annuler: () -> Unit, + dismiss: () -> Unit, model : HomeViewModel = viewModel() ) { val sujet by model.sujet AlertDialog( - onDismissRequest = annuler, + onDismissRequest = dismiss, title = { Text(text ="Créer un sujet") }, text = { OutlinedTextField( @@ -165,7 +191,7 @@ fun CreationDialog( }, dismissButton = { Button( - onClick = annuler, + onClick = dismiss, content = { Text("Annuler") } ) } @@ -174,14 +200,17 @@ fun CreationDialog( @OptIn(ExperimentalMaterial3Api::class) @Composable -fun ImportDialog(annuler: () -> Unit, confirmer: () -> Unit) { +fun ImportDialog( + dismiss: () -> Unit, + model: HomeViewModel +) { val radioOptions = listOf("Locale", "Internet") val (selectedOption, onOptionSelected) = remember {mutableStateOf(radioOptions[0])} var lien by remember { mutableStateOf("") } AlertDialog( - onDismissRequest = annuler, + onDismissRequest = dismiss, title = { Text(text = "Importer un jeu de question") }, text = { Column { @@ -220,13 +249,13 @@ fun ImportDialog(annuler: () -> Unit, confirmer: () -> Unit) { confirmButton = { Button( // TODO : on charge le jeu de question dans le sujet - onClick = confirmer, + onClick = dismiss, content = { Text("Importer") } ) }, dismissButton = { Button( - onClick = annuler, + onClick = dismiss, content = { Text("Annuler") } ) } diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/ActionHome.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/ActionHome.kt new file mode 100644 index 0000000..3c2a273 --- /dev/null +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/ActionHome.kt @@ -0,0 +1,5 @@ +package fr.uparis.diamantkennel.memorisationapplication.ui + +enum class ActionHome { + CREATION, IMPORTATION, MODIFIER, DELETION_SELECT, DELETION_DB +} \ No newline at end of file diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/HomeViewModel.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/HomeViewModel.kt index 0f82282..f64839a 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/HomeViewModel.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/HomeViewModel.kt @@ -17,6 +17,7 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) { var selected = mutableStateOf(null) var creation = mutableStateOf(false) + var modification = mutableStateOf(false) var importation = mutableStateOf(false) var deletionSelect = mutableStateOf(false) var deletionDB = mutableStateOf(false) @@ -29,22 +30,6 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) { 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 */ fun updateSelection(element: SetQuestions) { if (selected.value == element) { @@ -71,8 +56,36 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) { error.value = ErrorsAjout.DUPLICATE } - sujet.value = "" - creation.value = false + resetSujet() + 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 } - private fun reset() { + private fun resetSujet() { sujet.value = "" - error.value = null + } + + fun dismissCreation() { + creation.value = false + } + fun dismissModification() { + modification.value = false + } + fun dismissImportation() { + importation.value = false } fun cleanErrors() {