add settings screen, move db deletion into it, fix center of "no questions" message in play screen

This commit is contained in:
Mylloon 2024-01-04 18:00:15 +01:00
parent 4ebd2dc7f1
commit e7c511f5e4
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
9 changed files with 103 additions and 62 deletions

View file

@ -20,7 +20,6 @@ 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.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.ExperimentalMaterial3Api
@ -66,7 +65,6 @@ fun HomeScreen(
val creationRequest by model.creation val creationRequest by model.creation
val importationRequest by model.importation val importationRequest by model.importation
val deletionRequest by model.deletionSelect val deletionRequest by model.deletionSelect
val deletionDBRequest by model.deletionDB
val errorEntry by model.error val errorEntry by model.error
@ -98,13 +96,6 @@ fun HomeScreen(
) )
} }
if (deletionDBRequest) {
DeletionDBDialog(
model::dismissDeleteAll,
model::deleteAll
)
}
Column( Column(
modifier = Modifier.padding(padding), horizontalAlignment = Alignment.CenterHorizontally modifier = Modifier.padding(padding), horizontalAlignment = Alignment.CenterHorizontally
) { ) {
@ -120,34 +111,14 @@ fun HomeScreen(
Spacer(modifier = Modifier.padding(top = 50.dp)) Spacer(modifier = Modifier.padding(top = 50.dp))
DeleteRow(context, model) Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) {
} Button(enabled = currentSelection != null, onClick = {
} model.doAction(ActionHome.DELETION_SELECT)
@Composable
private fun DeleteRow(
context: Context, model: HomeViewModel
) {
val selection by model.selected
Row(
modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center
) {
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))
}
Spacer(modifier = Modifier.padding(2.dp))
Button(enabled = selection != null, onClick = {
(model::doAction)(ActionHome.DELETION_SELECT)
}) { }) {
Text(text = context.getString(R.string.main_button_delete)) Text(text = context.getString(R.string.main_button_delete))
} }
} }
}
} }
@Composable @Composable
@ -155,7 +126,7 @@ private fun ActionRow(context: Context, model: HomeViewModel, navController: Nav
val selection by model.selected val selection by model.selected
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) { Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) {
Button(onClick = { (model::doAction)(ActionHome.CREATION) }) { Button(onClick = { model.doAction(ActionHome.CREATION) }) {
Text(text = context.getString(R.string.main_button_create)) Text(text = context.getString(R.string.main_button_create))
} }
@ -169,7 +140,7 @@ private fun ActionRow(context: Context, model: HomeViewModel, navController: Nav
Spacer(modifier = Modifier.padding(2.dp)) Spacer(modifier = Modifier.padding(2.dp))
Button(onClick = { (model::doAction)(ActionHome.IMPORTATION) }) { Button(onClick = { model.doAction(ActionHome.IMPORTATION) }) {
Text(text = context.getString(R.string.main_button_import)) Text(text = context.getString(R.string.main_button_import))
} }
} }
@ -284,13 +255,6 @@ fun DeletionDialog(dismiss: () -> Unit, confirm: () -> Unit) =
text = { Text(text = "Voulez-vous supprimer ce jeu de question ?") }, text = { Text(text = "Voulez-vous supprimer ce jeu de question ?") },
confirmButton = { Button(onClick = confirm) { Text(text = "Ok") } }) confirmButton = { Button(onClick = confirm) { Text(text = "Ok") } })
@Composable
fun DeletionDBDialog(dismiss: () -> Unit, confirm: () -> 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 = confirm) { Text(text = "Ok") } })
@Composable @Composable
fun ShowList( fun ShowList(
sets: List<SetOfQuestions>, sets: List<SetOfQuestions>,

View file

@ -10,6 +10,7 @@ import androidx.compose.material.BottomNavigationItem
import androidx.compose.material.Icon import androidx.compose.material.Icon
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Home import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.CenterAlignedTopAppBar import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
@ -90,6 +91,7 @@ fun MainScreen() {
) )
} }
} }
composable(SETTINGS) { SettingsScreen(padding) }
} }
} }
} }
@ -117,4 +119,13 @@ fun BottomBar(navController: NavHostController) =
contentDescription = LocalContext.current.getString(R.string.home_button) contentDescription = LocalContext.current.getString(R.string.home_button)
) )
}) })
BottomNavigationItem(selected = currentRoute == SETTINGS,
onClick = { navController.navigate(SETTINGS) { popUpTo(HOME) } },
icon = {
Icon(
Icons.Default.Settings,
LocalContext.current.getString(R.string.settings_button)
)
})
} }

View file

@ -73,7 +73,9 @@ fun PlayScreen(
horizontalAlignment = Alignment.CenterHorizontally horizontalAlignment = Alignment.CenterHorizontally
) { ) {
if (question == null) { if (question == null) {
Text("Ce set n'a aucune question", fontSize = 30.sp, textAlign = TextAlign.Center) Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) {
Text("Ce set n'a aucune question", fontSize = 30.sp)
}
} else { } else {
Text(text = question!!.enonce, fontSize = 30.sp, textAlign = TextAlign.Center) Text(text = question!!.enonce, fontSize = 30.sp, textAlign = TextAlign.Center)

View file

@ -5,3 +5,4 @@ const val MODIFY_SET = "modify_set"
const val MODIFY_SET_ARGS = "id_set" const val MODIFY_SET_ARGS = "id_set"
const val PLAY = "play" const val PLAY = "play"
const val PLAY_SET_ARG = "id_set" const val PLAY_SET_ARG = "id_set"
const val SETTINGS = "settings"

View file

@ -0,0 +1,57 @@
package fr.uparis.diamantkennel.memorisationapplication
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.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
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.lifecycle.viewmodel.compose.viewModel
import fr.uparis.diamantkennel.memorisationapplication.ui.SettingsViewModel
@Composable
fun SettingsScreen(padding: PaddingValues, model: SettingsViewModel = viewModel()) {
val context = LocalContext.current
var deletionDBRequest by model.deletionDB
if (deletionDBRequest) {
DeletionDBDialog(model::deleteDb) { deletionDBRequest = false }
}
Column(
modifier = Modifier.padding(padding),
horizontalAlignment = Alignment.CenterHorizontally
) {
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) {
Button(
onClick = { deletionDBRequest = true },
colors = ButtonDefaults.buttonColors(containerColor = colorResource(id = R.color.red))
) {
Text(text = context.getString(R.string.main_button_deletebase))
}
}
}
}
@Composable
fun DeletionDBDialog(confirm: () -> Unit, 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 = confirm) { Text(text = "Oui") }
},
dismissButton = { Button(onClick = dismiss) { Text(text = "Non") } })

View file

@ -1,5 +1,5 @@
package fr.uparis.diamantkennel.memorisationapplication.ui package fr.uparis.diamantkennel.memorisationapplication.ui
enum class ActionHome { enum class ActionHome {
CREATION, IMPORTATION, DELETION_SELECT, DELETION_DB CREATION, IMPORTATION, DELETION_SELECT
} }

View file

@ -30,7 +30,6 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
var creation = mutableStateOf(false) var creation = mutableStateOf(false)
var importation = mutableStateOf(false) var importation = mutableStateOf(false)
var deletionSelect = 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)
@ -85,17 +84,6 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
deletionSelect.value = true deletionSelect.value = true
} }
} }
ActionHome.DELETION_DB -> {
deletionDB.value = true
}
}
}
fun deleteAll() {
deletionDB.value = false
viewModelScope.launch(Dispatchers.IO) {
dao.deleteTable()
} }
} }
@ -122,10 +110,6 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
deletionSelect.value = false deletionSelect.value = false
} }
fun dismissDeleteAll() {
deletionDB.value = false
}
fun dismissImportation() { fun dismissImportation() {
importation.value = false importation.value = false
} }

View file

@ -0,0 +1,21 @@
package fr.uparis.diamantkennel.memorisationapplication.ui
import android.app.Application
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import fr.uparis.diamantkennel.memorisationapplication.MemoApplication
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class SettingsViewModel(application: Application) : AndroidViewModel(application) {
private val dao = (application as MemoApplication).database.memoDao()
val deletionDB = mutableStateOf(false)
fun deleteDb() {
deletionDB.value = false
viewModelScope.launch(Dispatchers.IO) {
dao.deleteTable()
}
}
}

View file

@ -1,6 +1,7 @@
<resources> <resources>
<string name="app_name">Memorisation</string> <string name="app_name">Memorisation</string>
<string name="home_button">Page principale</string> <string name="home_button">Page principale</string>
<string name="settings_button">Paramètres</string>
<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>