started implementing set addition

This commit is contained in:
DIAMANT alexandre 2023-12-05 19:31:16 +01:00
parent d3f46d48d4
commit c114ed74f6
5 changed files with 111 additions and 16 deletions

View file

@ -36,6 +36,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.ErrorsAjout
import fr.uparis.diamantkennel.memorisationapplication.ui.HomeViewModel
@Composable
@ -45,20 +46,33 @@ fun HomeScreen(padding: PaddingValues, model: HomeViewModel = viewModel()) {
val setOfQuestions by model.setFlow.collectAsState(listOf())
val currentSelection by model.selected
var wantToCreate by remember { mutableStateOf(false) }
var wantToImport by remember { mutableStateOf(false) }
val wantToCreate by model.wantToCreate
val wantToImport by model.wantToImport
val errorEntry by model.error
if (wantToCreate) {
DialogCreation(
annuler = {wantToCreate = false},
confirmer = {wantToCreate = false}
CreationDialog(
annuler = {model.setCreation(false)},
confirmer = {model.setCreation(false)},
model = model
)
}
if (errorEntry != null) {
ErrorDialog(
when (errorEntry!!) {
ErrorsAjout.BAD_ENTRY -> context.getString(R.string.error_bad_entry)
ErrorsAjout.DUPLICATE -> context.getString(R.string.error_duplicate)
}, model::cleanErrors
)
}
if (wantToImport) {
DialogImportation(
annuler = {wantToImport = false},
confirmer = {wantToImport = false}
ImportDialog(
annuler = {model.setImportation(false)},
confirmer = {model.setImportation(false)}/*,
model = model*/
)
}
@ -71,7 +85,7 @@ fun HomeScreen(padding: PaddingValues, model: HomeViewModel = viewModel()) {
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) {
Button(onClick = {
Toast.makeText(context, "Create", Toast.LENGTH_SHORT).show()
wantToCreate = true
model.setCreation(true)
}) {
Text(text = context.getString(R.string.main_button_create))
}
@ -80,7 +94,7 @@ fun HomeScreen(padding: PaddingValues, model: HomeViewModel = viewModel()) {
}
Button(onClick = {
Toast.makeText(context, "Import", Toast.LENGTH_SHORT).show()
wantToImport = true
model.setImportation(true)
}) {
Text(text = context.getString(R.string.main_button_import))
}
@ -98,17 +112,21 @@ fun HomeScreen(padding: PaddingValues, model: HomeViewModel = viewModel()) {
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DialogCreation(annuler: () -> Unit, confirmer: () -> Unit) {
var sujet by remember {mutableStateOf("")}
fun CreationDialog(
annuler: () -> Unit,
confirmer: () -> Unit,
model : HomeViewModel = viewModel()
) {
val sujet by model.sujet
AlertDialog(
onDismissRequest = annuler,
title = { Text(text ="Créer un sujet") },
text = {
OutlinedTextField(
value = sujet,
onValueChange = { newTextValue -> sujet = newTextValue},
label = { Text("Nouveau sujet") }
sujet,
label = { Text("Nouveau sujet") },
onValueChange = { model.onSujetChange(it) }
)
},
confirmButton = {
@ -129,7 +147,7 @@ fun DialogCreation(annuler: () -> Unit, confirmer: () -> Unit) {
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DialogImportation(annuler: () -> Unit, confirmer: () -> Unit) {
fun ImportDialog(annuler: () -> Unit, confirmer: () -> Unit) {
val radioOptions = listOf("Locale", "Internet")
val (selectedOption, onOptionSelected) = remember {mutableStateOf(radioOptions[0])}
@ -188,6 +206,14 @@ fun DialogImportation(annuler: () -> Unit, confirmer: () -> Unit) {
)
}
@Composable
fun ErrorDialog(errMsg: String, dismiss: () -> Unit) =
AlertDialog(onDismissRequest = dismiss,
title = { Text(text = "Erreur")},
text = { Text(text = errMsg) },
confirmButton = {Button(onClick = dismiss) { Text(text = "Ok") }}
)
@Composable
fun ShowList(
sets: List<SetOfQuestions>,

View file

@ -1,13 +1,21 @@
package fr.uparis.diamantkennel.memorisationapplication.data
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Transaction
import kotlinx.coroutines.flow.Flow
@Dao
interface MemoDao {
@Insert
suspend fun insert(set: SetQuestions)
@Transaction
@Query("SELECT * FROM SetQuestions")
fun loadAllSets(): Flow<List<SetOfQuestions>>
@Transaction
@Query("DELETE FROM SetQuestions")
fun deleteTable()
}

View file

@ -0,0 +1,3 @@
package fr.uparis.diamantkennel.memorisationapplication.ui
enum class ErrorsAjout { BAD_ENTRY, DUPLICATE}

View file

@ -1,14 +1,70 @@
package fr.uparis.diamantkennel.memorisationapplication.ui
import android.app.Application
import android.database.sqlite.SQLiteConstraintException
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import fr.uparis.diamantkennel.memorisationapplication.MemoApplication
import fr.uparis.diamantkennel.memorisationapplication.data.SetQuestions
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class HomeViewModel(application: Application) : AndroidViewModel(application) {
private val dao = (application as MemoApplication).database.memoDao()
var setFlow = dao.loadAllSets()
var selected = mutableStateOf<SetQuestions?>(null)
var wantToCreate = mutableStateOf(false)
var wantToImport = mutableStateOf(false)
var sujet = mutableStateOf("")
var error = mutableStateOf<ErrorsAjout?>(null)
/* Listener */
fun onSujetChange(s: String) {
sujet.value = s
}
fun setCreation(t: Boolean) {
wantToCreate.value = t
}
fun setImportation(t: Boolean) {
wantToImport.value = t
}
/* Methods */
fun addSet() {
if (sujet.value.isBlank()) {
error.value = ErrorsAjout.BAD_ENTRY
return
}
viewModelScope.launch(Dispatchers.IO) {
try {
dao.insert(
SetQuestions(
name = sujet.value.trim()
)
)
} catch (_: SQLiteConstraintException) {
error.value = ErrorsAjout.DUPLICATE
}
sujet.value = ""
}
}
private fun reset() {
sujet.value = ""
error.value = null
}
fun cleanErrors() {
error.value = null
}
}

View file

@ -5,4 +5,6 @@
<string name="main_button_modify">Modifier</string>
<string name="main_button_import">Importer</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>
</resources>