add : change color when selected, button for delete one element or all the elements of the DB

This commit is contained in:
DIAMANT alexandre 2023-12-06 10:11:08 +01:00
parent 91bdab8f81
commit 80b84e747a
6 changed files with 130 additions and 23 deletions

View file

@ -15,7 +15,9 @@ import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.selection.selectableGroup import androidx.compose.foundation.selection.selectableGroup
import androidx.compose.material3.AlertDialog import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.OutlinedTextField
@ -30,6 +32,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.Role
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
@ -46,18 +49,13 @@ fun HomeScreen(padding: PaddingValues, model: HomeViewModel = viewModel()) {
val setOfQuestions by model.setFlow.collectAsState(listOf()) val setOfQuestions by model.setFlow.collectAsState(listOf())
val currentSelection by model.selected val currentSelection by model.selected
val wantToCreate by model.wantToCreate val creationRequest by model.creation
val wantToImport by model.wantToImport val importationRequest by model.importation
val deletionRequest by model.deletionSelect
val deletionDBRequest by model.deletionDB
val errorEntry by model.error val errorEntry by model.error
if (wantToCreate) {
CreationDialog(
annuler = {(model::setCreation)(false)},
model = model
)
}
if (errorEntry != null) { if (errorEntry != null) {
ErrorDialog( ErrorDialog(
when (errorEntry!!) { when (errorEntry!!) {
@ -67,7 +65,14 @@ fun HomeScreen(padding: PaddingValues, model: HomeViewModel = viewModel()) {
) )
} }
if (wantToImport) { if (creationRequest) {
CreationDialog(
annuler = {(model::setCreation)(false)},
model = model
)
}
if (importationRequest) {
ImportDialog( ImportDialog(
annuler = {model.setImportation(false)}, annuler = {model.setImportation(false)},
confirmer = {model.setImportation(false)}/*, confirmer = {model.setImportation(false)}/*,
@ -75,15 +80,26 @@ fun HomeScreen(padding: PaddingValues, model: HomeViewModel = viewModel()) {
) )
} }
if (deletionRequest) {
DeletionDialog(
model::deleteSelected
)
}
if (deletionDBRequest) {
DeletionDBDialog(
model::deleteAll
)
}
Column( Column(
modifier = Modifier.padding(padding), modifier = Modifier.padding(padding),
horizontalAlignment = Alignment.CenterHorizontally horizontalAlignment = Alignment.CenterHorizontally
) { ) {
ShowList(setOfQuestions, currentSelection) ShowList(setOfQuestions, currentSelection, model::updateSelection)
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) { Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) {
Button(onClick = { Button(onClick = {
Toast.makeText(context, "Create", Toast.LENGTH_SHORT).show()
model.setCreation(true) model.setCreation(true)
}) { }) {
Text(text = context.getString(R.string.main_button_create)) Text(text = context.getString(R.string.main_button_create))
@ -98,6 +114,20 @@ fun HomeScreen(padding: PaddingValues, model: HomeViewModel = viewModel()) {
Text(text = context.getString(R.string.main_button_import)) 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) { Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) {
Button( Button(
@ -211,28 +241,62 @@ fun ErrorDialog(errMsg: String, dismiss: () -> Unit) =
confirmButton = {Button(onClick = dismiss) { Text(text = "Ok") }} confirmButton = {Button(onClick = dismiss) { Text(text = "Ok") }}
) )
@Composable
fun DeletionDialog(dismiss: () -> Unit) =
AlertDialog(onDismissRequest = dismiss,
title = { Text(text = "Supprimer un jeu de question")},
text = { Text(text = "Voulez-vous supprimer ce jeu de question ?") },
confirmButton = {Button(onClick = dismiss) { Text(text = "Ok") }}
)
@Composable
fun DeletionDBDialog(dismiss: () -> Unit) =
AlertDialog(onDismissRequest = dismiss,
title = { Text(text = "Supprimer la base de données")},
text = { Text(text = "Voulez-vous supprimer la base de données ?") },
confirmButton = {Button(onClick = dismiss) { Text(text = "Ok") }}
)
@Composable @Composable
fun ShowList( fun ShowList(
sets: List<SetOfQuestions>, sets: List<SetOfQuestions>,
currentSelection: SetQuestions?, currentSelection: SetQuestions?,
updateSelection: (SetQuestions) -> Unit
) { ) {
LazyColumn( LazyColumn(
Modifier Modifier
.fillMaxHeight(0.7f) .fillMaxHeight(0.7f)
) { ) {
itemsIndexed(sets) { index, item -> itemsIndexed(sets) { index, item ->
ListItem(index, item, currentSelection) ListItem(index, item, currentSelection, updateSelection)
} }
} }
} }
@OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun ListItem( fun ListItem(
index: Int, index: Int,
set: SetOfQuestions, set: SetOfQuestions,
currentSelection: SetQuestions?, currentSelection: SetQuestions?,
) = Card(Modifier.fillMaxSize()) { updateSelection: (SetQuestions) -> Unit,
Row { ) {
Text(text = set.set.name, modifier = Modifier.padding(2.dp)) val containerColor = when {
currentSelection == set.set -> colorResource(id = R.color.selected)
index % 2 == 0 -> colorResource(id = R.color.black)
else -> colorResource(id = R.color.purple_200)
}
Card(
onClick = { updateSelection(set.set) },
Modifier.fillMaxSize(),
colors = CardDefaults.cardColors(containerColor)
) {
Row (
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceEvenly
){
Text(set.toString(), modifier = Modifier.padding(2.dp))
}
} }
} }

View file

@ -1,6 +1,7 @@
package fr.uparis.diamantkennel.memorisationapplication.data package fr.uparis.diamantkennel.memorisationapplication.data
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert import androidx.room.Insert
import androidx.room.Query import androidx.room.Query
import androidx.room.Transaction import androidx.room.Transaction
@ -15,7 +16,9 @@ interface MemoDao {
@Query("SELECT * FROM SetQuestions") @Query("SELECT * FROM SetQuestions")
fun loadAllSets(): Flow<List<SetOfQuestions>> fun loadAllSets(): Flow<List<SetOfQuestions>>
@Transaction
@Query("DELETE FROM SetQuestions") @Query("DELETE FROM SetQuestions")
fun deleteTable() fun deleteTable()
@Delete
fun delete(set: SetQuestions)
} }

View file

@ -10,4 +10,6 @@ data class SetOfQuestions(
entityColumn = "setId" entityColumn = "setId"
) )
val questions: List<Question> val questions: List<Question>
) ) {
override fun toString() = set.name
}

View file

@ -16,8 +16,10 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
var setFlow = dao.loadAllSets() var setFlow = dao.loadAllSets()
var selected = mutableStateOf<SetQuestions?>(null) var selected = mutableStateOf<SetQuestions?>(null)
var wantToCreate = mutableStateOf(false) var creation = mutableStateOf(false)
var wantToImport = mutableStateOf(false) var importation = mutableStateOf(false)
var deletionSelect = mutableStateOf(false)
var deletionDB = mutableStateOf(false)
var sujet = mutableStateOf("") var sujet = mutableStateOf("")
var error = mutableStateOf<ErrorsAjout?>(null) var error = mutableStateOf<ErrorsAjout?>(null)
@ -28,15 +30,29 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
} }
fun setCreation(t: Boolean) { fun setCreation(t: Boolean) {
wantToCreate.value = t creation.value = t
} }
fun setImportation(t: Boolean) { fun setImportation(t: Boolean) {
wantToImport.value = t importation.value = t
} }
fun setDeletionSelect(t: Boolean) {
deletionSelect.value = t
}
fun setDeletionDB(t: Boolean) {
deletionDB.value = t
}
/* Methods */ /* Methods */
fun updateSelection(element: SetQuestions) {
if (selected.value == element) {
selected.value = null
} else {
selected.value = element
}
}
fun addSet() { fun addSet() {
if (sujet.value.isBlank()) { if (sujet.value.isBlank()) {
@ -56,10 +72,28 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
} }
sujet.value = "" sujet.value = ""
wantToCreate.value = false creation.value = false
} }
} }
fun deleteAll() {
deletionDB.value = false
viewModelScope.launch(Dispatchers.IO) {
dao.deleteTable()
}
}
fun deleteSelected() {
deletionSelect.value = false
if (selected.value == null) return
val selection = selected.value!!
viewModelScope.launch(Dispatchers.IO) {
dao.delete(selection)
}
selected.value = null
}
private fun reset() { private fun reset() {
sujet.value = "" sujet.value = ""
error.value = null error.value = null

View file

@ -7,4 +7,6 @@
<color name="teal_700">#FF018786</color> <color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color> <color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color> <color name="white">#FFFFFFFF</color>
<color name="red">#FFFF0000</color>
<color name="selected">#FF6200EE</color>
</resources> </resources>

View file

@ -4,6 +4,8 @@
<string name="main_button_create">Créer</string> <string name="main_button_create">Créer</string>
<string name="main_button_modify">Modifier</string> <string name="main_button_modify">Modifier</string>
<string name="main_button_import">Importer</string> <string name="main_button_import">Importer</string>
<string name="main_button_deletebase">Supprimer base</string>
<string name="main_button_delete">Supprimer</string>
<string name="main_button_start">Lancer</string> <string name="main_button_start">Lancer</string>
<string name="error_bad_entry">Erreur : entrée invalide</string> <string name="error_bad_entry">Erreur : entrée invalide</string>
<string name="error_duplicate">Erreur : doublon</string> <string name="error_duplicate">Erreur : doublon</string>