This commit is contained in:
Mylloon 2023-12-19 19:28:22 +01:00
parent 337f5c6342
commit fd95c308d0
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
8 changed files with 105 additions and 169 deletions

View file

@ -79,15 +79,13 @@ fun HomeScreen(padding: PaddingValues, model: HomeViewModel = viewModel()) {
if (creationRequest) {
CreationDialog(
dismiss = model::dismissCreation,
model = model
dismiss = model::dismissCreation, model = model
)
}
if (importationRequest) {
ImportDialog(
dismiss = model::dismissImportation,
model = model
dismiss = model::dismissImportation, model = model
)
}
@ -104,15 +102,13 @@ fun HomeScreen(padding: PaddingValues, model: HomeViewModel = viewModel()) {
}
Column(
modifier = Modifier.padding(padding),
horizontalAlignment = Alignment.CenterHorizontally
modifier = Modifier.padding(padding), horizontalAlignment = Alignment.CenterHorizontally
) {
ShowList(setOfQuestions, currentSelection, model::updateSelection)
ActionRow(context, model)
Button(
onClick = { Toast.makeText(context, "Start", Toast.LENGTH_SHORT).show() }) {
Button(onClick = { Toast.makeText(context, "Start", Toast.LENGTH_SHORT).show() }) {
Text(text = context.getString(R.string.main_button_start), fontSize = 30.sp)
}
@ -124,12 +120,10 @@ fun HomeScreen(padding: PaddingValues, model: HomeViewModel = viewModel()) {
@Composable
private fun DeleteRow(
context: Context,
model: HomeViewModel
context: Context, model: HomeViewModel
) {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center
modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center
) {
Button(
onClick = {
@ -142,8 +136,7 @@ private fun DeleteRow(
Spacer(modifier = Modifier.padding(2.dp))
Button(
enabled = false, /*TODO: Activer uniquement quand on a qqch de sélectionner */
Button(enabled = false, /*TODO: Activer uniquement quand on a qqch de sélectionner */
onClick = {
(model::doAction)(ActionHome.DELETION_SELECT)
}) {
@ -154,8 +147,7 @@ private fun DeleteRow(
@Composable
private fun ActionRow(
context: Context,
model: HomeViewModel
context: Context, model: HomeViewModel
) {
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) {
Button(onClick = {
@ -183,41 +175,25 @@ private fun ActionRow(
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CreationDialog(
dismiss: () -> Unit,
model: HomeViewModel = viewModel()
dismiss: () -> Unit, model: HomeViewModel = viewModel()
) {
val sujet by model.sujet
AlertDialog(
onDismissRequest = dismiss,
title = { Text(text = "Créer un sujet") },
text = {
AlertDialog(onDismissRequest = dismiss, title = { Text(text = "Créer un sujet") }, text = {
OutlinedTextField(
sujet,
label = { Text("Nouveau sujet") },
onValueChange = model::onSujetChange
)
},
confirmButton = {
Button(
onClick = model::addSet,
content = { Text("Ajouter") }
)
},
dismissButton = {
Button(
onClick = dismiss,
content = { Text("Annuler") }
)
}
sujet, label = { Text("Nouveau sujet") }, onValueChange = model::onSujetChange
)
}, confirmButton = {
Button(onClick = model::addSet, content = { Text("Ajouter") })
}, dismissButton = {
Button(onClick = dismiss, content = { Text("Annuler") })
})
}
@OptIn(ExperimentalMaterial3Api::class, DelicateCoroutinesApi::class)
@Composable
fun ImportDialog(
dismiss: () -> Unit,
model: HomeViewModel
dismiss: () -> Unit, model: HomeViewModel
) {
val (selectedOption, onOptionSelected) = remember { mutableStateOf(ActionImport.FILE) }
@ -233,8 +209,7 @@ fun ImportDialog(
}
}
AlertDialog(
onDismissRequest = dismiss,
AlertDialog(onDismissRequest = dismiss,
title = { Text(text = "Importer un jeu de question") },
text = {
Column {
@ -251,8 +226,7 @@ fun ImportDialog(
verticalAlignment = Alignment.CenterVertically,
) {
RadioButton(
selected = (text == selectedOption),
onClick = null
selected = (text == selectedOption), onClick = null
)
Text(
text = text.toString(),
@ -261,17 +235,14 @@ fun ImportDialog(
)
if (text == ActionImport.FILE) {
Button(
enabled = selectedOption == ActionImport.FILE,
Button(enabled = selectedOption == ActionImport.FILE,
modifier = Modifier.padding(start = 16.dp),
onClick = {
filePickerLauncher.launch(
Intent(Intent.ACTION_OPEN_DOCUMENT)
.addCategory(Intent.CATEGORY_OPENABLE)
Intent(Intent.ACTION_OPEN_DOCUMENT).addCategory(Intent.CATEGORY_OPENABLE)
.setType("*/*")
)
}
) {
}) {
Text(
text = "Explorateur",
style = MaterialTheme.typography.labelSmall
@ -282,52 +253,38 @@ fun ImportDialog(
}
}
OutlinedTextField(
enabled = selectedOption == ActionImport.INTERNET,
OutlinedTextField(enabled = selectedOption == ActionImport.INTERNET,
value = lien,
onValueChange = { newTextValue -> lien = newTextValue },
label = { Text(text = "Lien") }
)
label = { Text(text = "Lien") })
}
},
confirmButton = {
Button(
onClick = { GlobalScope.launch { model.import(ctx, lien) } },
content = { Text("Importer") }
)
Button(onClick = { GlobalScope.launch { model.import(ctx, lien) } },
content = { Text("Importer") })
},
dismissButton = {
Button(
onClick = dismiss,
content = { Text("Annuler") }
)
}
)
Button(onClick = dismiss, content = { Text("Annuler") })
})
}
@Composable
fun ErrorDialog(errMsg: String, dismiss: () -> Unit) =
AlertDialog(onDismissRequest = dismiss,
fun ErrorDialog(errMsg: String, dismiss: () -> Unit) = AlertDialog(onDismissRequest = dismiss,
title = { Text(text = "Erreur") },
text = { Text(text = errMsg) },
confirmButton = { Button(onClick = dismiss) { Text(text = "Ok") } }
)
confirmButton = { Button(onClick = dismiss) { Text(text = "Ok") } })
@Composable
fun DeletionDialog(dismiss: () -> Unit) =
AlertDialog(onDismissRequest = dismiss,
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") } }
)
confirmButton = { Button(onClick = dismiss) { Text(text = "Ok") } })
@Composable
fun DeletionDBDialog(dismiss: () -> Unit) =
AlertDialog(onDismissRequest = dismiss,
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") } }
)
confirmButton = { Button(onClick = dismiss) { Text(text = "Ok") } })
@Composable
fun ShowList(
@ -336,8 +293,7 @@ fun ShowList(
updateSelection: (SetQuestions) -> Unit
) {
LazyColumn(
Modifier
.fillMaxHeight(0.6f)
Modifier.fillMaxHeight(0.6f)
) {
itemsIndexed(sets) { index, item ->
ListItem(index, item, currentSelection, updateSelection)
@ -365,8 +321,7 @@ fun ListItem(
colors = CardDefaults.cardColors(containerColor)
) {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceEvenly
modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly
) {
Text(set.toString(), modifier = Modifier.padding(2.dp))
}

View file

@ -35,8 +35,7 @@ class MainActivity : ComponentActivity() {
MemorisationApplicationTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background
) {
MainScreenMainActivity()
}
@ -61,8 +60,7 @@ fun MainScreenActivityPreview() {
fun MainScreen() {
val navController = rememberNavController()
Scaffold(topBar = { TopBar() },
bottomBar = { BottomBar(navController) }) { padding ->
Scaffold(topBar = { TopBar() }, bottomBar = { BottomBar(navController) }) { padding ->
NavHost(
navController = navController,
startDestination = HOME,
@ -77,13 +75,12 @@ fun MainScreen() {
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TopBar() =
CenterAlignedTopAppBar(title = {
fun TopBar() = CenterAlignedTopAppBar(title = {
Text(
text = LocalContext.current.getString(R.string.app_name),
style = MaterialTheme.typography.displayMedium
)
})
})
@Composable
fun BottomBar(navController: NavHostController) =
@ -91,12 +88,9 @@ fun BottomBar(navController: NavHostController) =
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route
BottomNavigationItem(
selected = currentRoute == HOME,
onClick = {
BottomNavigationItem(selected = currentRoute == HOME, onClick = {
navController.navigate(HOME) { launchSingleTop = true }
},
icon = {
}, icon = {
Icon(
imageVector = Icons.Default.Home,
contentDescription = LocalContext.current.getString(R.string.home_button)

View file

@ -5,14 +5,12 @@ import androidx.room.ForeignKey
import androidx.room.PrimaryKey
@Entity(
foreignKeys = [
ForeignKey(
foreignKeys = [ForeignKey(
entity = SetQuestions::class,
parentColumns = ["idSet"],
childColumns = ["setId"],
onDelete = ForeignKey.CASCADE
)
]
)]
)
data class Question(
@PrimaryKey(autoGenerate = true) val idQuestion: Int = 0,

View file

@ -18,8 +18,7 @@ abstract class QuestionsDB : RoomDatabase() {
fun getDataBase(c: Context): QuestionsDB {
if (instance != null) return instance!!
val db =
Room.databaseBuilder(c.applicationContext, QuestionsDB::class.java, "memo")
val db = Room.databaseBuilder(c.applicationContext, QuestionsDB::class.java, "memo")
.fallbackToDestructiveMigration().build()
instance = db
return instance!!

View file

@ -4,12 +4,9 @@ import androidx.room.Embedded
import androidx.room.Relation
data class SetOfQuestions(
@Embedded val set: SetQuestions,
@Relation(
parentColumn = "idSet",
entityColumn = "setId"
)
val questions: List<Question>
@Embedded val set: SetQuestions, @Relation(
parentColumn = "idSet", entityColumn = "setId"
) val questions: List<Question>
) {
override fun toString() = set.name
}

View file

@ -7,19 +7,16 @@ import androidx.room.Index
@Entity(
indices = [Index(value = ["idQuestion"])],
primaryKeys = ["idSet", "idQuestion"],
foreignKeys = [
ForeignKey(
foreignKeys = [ForeignKey(
entity = SetQuestions::class,
parentColumns = ["idSet"],
childColumns = ["idSet"],
onDelete = ForeignKey.CASCADE
),
ForeignKey(
), ForeignKey(
entity = Question::class,
parentColumns = ["idQuestion"],
childColumns = ["idQuestion"],
onDelete = ForeignKey.CASCADE
)
]
)]
)
data class Sets(val idSet: Int, val idQuestion: Int)

View file

@ -1,5 +1,3 @@
package fr.uparis.diamantkennel.memorisationapplication.ui
enum class ErrorsAjout { BAD_ENTRY, DUPLICATE }
class Duplicate : Exception(ErrorsAjout.DUPLICATE.toString())

View file

@ -138,12 +138,10 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
}
suspend fun import(ctx: Context, path: String) {
val data =
flow {
val data = flow {
if (path.startsWith("content://")) {
// Local file
val reader =
BufferedReader(
val reader = BufferedReader(
InputStreamReader(
ctx.contentResolver.openInputStream(
Uri.parse(