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

View file

@ -1,13 +1,21 @@
package fr.uparis.diamantkennel.memorisationapplication.data package fr.uparis.diamantkennel.memorisationapplication.data
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query import androidx.room.Query
import androidx.room.Transaction import androidx.room.Transaction
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
@Dao @Dao
interface MemoDao { interface MemoDao {
@Insert
suspend fun insert(set: SetQuestions)
@Transaction @Transaction
@Query("SELECT * FROM SetQuestions") @Query("SELECT * FROM SetQuestions")
fun loadAllSets(): Flow<List<SetOfQuestions>> 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 package fr.uparis.diamantkennel.memorisationapplication.ui
import android.app.Application import android.app.Application
import android.database.sqlite.SQLiteConstraintException
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import fr.uparis.diamantkennel.memorisationapplication.MemoApplication import fr.uparis.diamantkennel.memorisationapplication.MemoApplication
import fr.uparis.diamantkennel.memorisationapplication.data.SetQuestions import fr.uparis.diamantkennel.memorisationapplication.data.SetQuestions
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class HomeViewModel(application: Application) : AndroidViewModel(application) { class HomeViewModel(application: Application) : AndroidViewModel(application) {
private val dao = (application as MemoApplication).database.memoDao() private val dao = (application as MemoApplication).database.memoDao()
var setFlow = dao.loadAllSets() var setFlow = dao.loadAllSets()
var selected = mutableStateOf<SetQuestions?>(null) 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_modify">Modifier</string>
<string name="main_button_import">Importer</string> <string name="main_button_import">Importer</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_duplicate">Erreur : doublon</string>
</resources> </resources>