add : change color when selected, button for delete one element or all the elements of the DB
This commit is contained in:
parent
91bdab8f81
commit
80b84e747a
6 changed files with 130 additions and 23 deletions
|
@ -15,7 +15,9 @@ import androidx.compose.foundation.selection.selectable
|
|||
import androidx.compose.foundation.selection.selectableGroup
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.ButtonDefaults
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
|
@ -30,6 +32,7 @@ import androidx.compose.runtime.setValue
|
|||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.colorResource
|
||||
import androidx.compose.ui.semantics.Role
|
||||
import androidx.compose.ui.unit.dp
|
||||
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 currentSelection by model.selected
|
||||
|
||||
val wantToCreate by model.wantToCreate
|
||||
val wantToImport by model.wantToImport
|
||||
val creationRequest by model.creation
|
||||
val importationRequest by model.importation
|
||||
val deletionRequest by model.deletionSelect
|
||||
val deletionDBRequest by model.deletionDB
|
||||
|
||||
val errorEntry by model.error
|
||||
|
||||
if (wantToCreate) {
|
||||
CreationDialog(
|
||||
annuler = {(model::setCreation)(false)},
|
||||
model = model
|
||||
)
|
||||
}
|
||||
|
||||
if (errorEntry != null) {
|
||||
ErrorDialog(
|
||||
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(
|
||||
annuler = {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(
|
||||
modifier = Modifier.padding(padding),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
ShowList(setOfQuestions, currentSelection)
|
||||
ShowList(setOfQuestions, currentSelection, model::updateSelection)
|
||||
|
||||
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) {
|
||||
Button(onClick = {
|
||||
Toast.makeText(context, "Create", Toast.LENGTH_SHORT).show()
|
||||
model.setCreation(true)
|
||||
}) {
|
||||
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))
|
||||
}
|
||||
}
|
||||
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(
|
||||
|
@ -211,28 +241,62 @@ fun ErrorDialog(errMsg: String, dismiss: () -> Unit) =
|
|||
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
|
||||
fun ShowList(
|
||||
sets: List<SetOfQuestions>,
|
||||
currentSelection: SetQuestions?,
|
||||
updateSelection: (SetQuestions) -> Unit
|
||||
) {
|
||||
LazyColumn(
|
||||
Modifier
|
||||
.fillMaxHeight(0.7f)
|
||||
) {
|
||||
itemsIndexed(sets) { index, item ->
|
||||
ListItem(index, item, currentSelection)
|
||||
ListItem(index, item, currentSelection, updateSelection)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun ListItem(
|
||||
index: Int,
|
||||
set: SetOfQuestions,
|
||||
currentSelection: SetQuestions?,
|
||||
) = Card(Modifier.fillMaxSize()) {
|
||||
Row {
|
||||
Text(text = set.set.name, modifier = Modifier.padding(2.dp))
|
||||
updateSelection: (SetQuestions) -> Unit,
|
||||
) {
|
||||
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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package fr.uparis.diamantkennel.memorisationapplication.data
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Query
|
||||
import androidx.room.Transaction
|
||||
|
@ -15,7 +16,9 @@ interface MemoDao {
|
|||
@Query("SELECT * FROM SetQuestions")
|
||||
fun loadAllSets(): Flow<List<SetOfQuestions>>
|
||||
|
||||
@Transaction
|
||||
@Query("DELETE FROM SetQuestions")
|
||||
fun deleteTable()
|
||||
|
||||
@Delete
|
||||
fun delete(set: SetQuestions)
|
||||
}
|
||||
|
|
|
@ -10,4 +10,6 @@ data class SetOfQuestions(
|
|||
entityColumn = "setId"
|
||||
)
|
||||
val questions: List<Question>
|
||||
)
|
||||
) {
|
||||
override fun toString() = set.name
|
||||
}
|
||||
|
|
|
@ -16,8 +16,10 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
|
|||
var setFlow = dao.loadAllSets()
|
||||
var selected = mutableStateOf<SetQuestions?>(null)
|
||||
|
||||
var wantToCreate = mutableStateOf(false)
|
||||
var wantToImport = mutableStateOf(false)
|
||||
var creation = mutableStateOf(false)
|
||||
var importation = mutableStateOf(false)
|
||||
var deletionSelect = mutableStateOf(false)
|
||||
var deletionDB = mutableStateOf(false)
|
||||
|
||||
var sujet = mutableStateOf("")
|
||||
var error = mutableStateOf<ErrorsAjout?>(null)
|
||||
|
@ -28,15 +30,29 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
|
|||
}
|
||||
|
||||
fun setCreation(t: Boolean) {
|
||||
wantToCreate.value = t
|
||||
creation.value = t
|
||||
}
|
||||
|
||||
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 */
|
||||
fun updateSelection(element: SetQuestions) {
|
||||
if (selected.value == element) {
|
||||
selected.value = null
|
||||
} else {
|
||||
selected.value = element
|
||||
}
|
||||
}
|
||||
|
||||
fun addSet() {
|
||||
if (sujet.value.isBlank()) {
|
||||
|
@ -56,10 +72,28 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
|
|||
}
|
||||
|
||||
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() {
|
||||
sujet.value = ""
|
||||
error.value = null
|
||||
|
|
|
@ -7,4 +7,6 @@
|
|||
<color name="teal_700">#FF018786</color>
|
||||
<color name="black">#FF000000</color>
|
||||
<color name="white">#FFFFFFFF</color>
|
||||
<color name="red">#FFFF0000</color>
|
||||
<color name="selected">#FF6200EE</color>
|
||||
</resources>
|
|
@ -4,6 +4,8 @@
|
|||
<string name="main_button_create">Créer</string>
|
||||
<string name="main_button_modify">Modifier</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="error_bad_entry">Erreur : entrée invalide</string>
|
||||
<string name="error_duplicate">Erreur : doublon</string>
|
||||
|
|
Reference in a new issue