diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/HomeScreen.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/HomeScreen.kt index cdbb297..e1af333 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/HomeScreen.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/HomeScreen.kt @@ -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 = { - OutlinedTextField( - sujet, - label = { Text("Nouveau sujet") }, - onValueChange = model::onSujetChange - ) - }, - confirmButton = { - Button( - onClick = model::addSet, - content = { Text("Ajouter") } - ) - }, - dismissButton = { - Button( - onClick = dismiss, - content = { Text("Annuler") } - ) - } - ) + 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") }) + }) } @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, - title = { Text(text = "Erreur") }, - text = { Text(text = errMsg) }, - confirmButton = { Button(onClick = dismiss) { Text(text = "Ok") } } - ) +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 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") } } - ) +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") } } - ) +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( @@ -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)) } diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MainActivity.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MainActivity.kt index 4db5c7e..4590bba 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MainActivity.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MainActivity.kt @@ -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 = { - Text( - text = LocalContext.current.getString(R.string.app_name), - style = MaterialTheme.typography.displayMedium - ) - }) +fun TopBar() = CenterAlignedTopAppBar(title = { + Text( + text = LocalContext.current.getString(R.string.app_name), + style = MaterialTheme.typography.displayMedium + ) +}) @Composable fun BottomBar(navController: NavHostController) = @@ -91,15 +88,12 @@ fun BottomBar(navController: NavHostController) = val navBackStackEntry by navController.currentBackStackEntryAsState() val currentRoute = navBackStackEntry?.destination?.route - BottomNavigationItem( - selected = currentRoute == HOME, - onClick = { - navController.navigate(HOME) { launchSingleTop = true } - }, - icon = { - Icon( - imageVector = Icons.Default.Home, - contentDescription = LocalContext.current.getString(R.string.home_button) - ) - }) + BottomNavigationItem(selected = currentRoute == HOME, onClick = { + navController.navigate(HOME) { launchSingleTop = true } + }, icon = { + Icon( + imageVector = Icons.Default.Home, + contentDescription = LocalContext.current.getString(R.string.home_button) + ) + }) } diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/Question.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/Question.kt index cea9686..888b2ff 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/Question.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/Question.kt @@ -5,14 +5,12 @@ import androidx.room.ForeignKey import androidx.room.PrimaryKey @Entity( - foreignKeys = [ - ForeignKey( - entity = SetQuestions::class, - parentColumns = ["idSet"], - childColumns = ["setId"], - onDelete = ForeignKey.CASCADE - ) - ] + foreignKeys = [ForeignKey( + entity = SetQuestions::class, + parentColumns = ["idSet"], + childColumns = ["setId"], + onDelete = ForeignKey.CASCADE + )] ) data class Question( @PrimaryKey(autoGenerate = true) val idQuestion: Int = 0, diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/QuestionsDB.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/QuestionsDB.kt index 5537f53..352efbf 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/QuestionsDB.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/QuestionsDB.kt @@ -18,9 +18,8 @@ abstract class QuestionsDB : RoomDatabase() { fun getDataBase(c: Context): QuestionsDB { if (instance != null) return instance!! - val db = - Room.databaseBuilder(c.applicationContext, QuestionsDB::class.java, "memo") - .fallbackToDestructiveMigration().build() + val db = Room.databaseBuilder(c.applicationContext, QuestionsDB::class.java, "memo") + .fallbackToDestructiveMigration().build() instance = db return instance!! } diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/SetOfQuestions.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/SetOfQuestions.kt index cd0b12b..d267834 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/SetOfQuestions.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/SetOfQuestions.kt @@ -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 + @Embedded val set: SetQuestions, @Relation( + parentColumn = "idSet", entityColumn = "setId" + ) val questions: List ) { override fun toString() = set.name } diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/Sets.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/Sets.kt index d1d335a..a665bfa 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/Sets.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/data/Sets.kt @@ -7,19 +7,16 @@ import androidx.room.Index @Entity( indices = [Index(value = ["idQuestion"])], primaryKeys = ["idSet", "idQuestion"], - foreignKeys = [ - ForeignKey( - entity = SetQuestions::class, - parentColumns = ["idSet"], - childColumns = ["idSet"], - onDelete = ForeignKey.CASCADE - ), - ForeignKey( - entity = Question::class, - parentColumns = ["idQuestion"], - childColumns = ["idQuestion"], - onDelete = ForeignKey.CASCADE - ) - ] + foreignKeys = [ForeignKey( + entity = SetQuestions::class, + parentColumns = ["idSet"], + childColumns = ["idSet"], + onDelete = ForeignKey.CASCADE + ), ForeignKey( + entity = Question::class, + parentColumns = ["idQuestion"], + childColumns = ["idQuestion"], + onDelete = ForeignKey.CASCADE + )] ) data class Sets(val idSet: Int, val idQuestion: Int) diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/ErrorsAjout.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/ErrorsAjout.kt index 7254ffa..a030e35 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/ErrorsAjout.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/ErrorsAjout.kt @@ -1,5 +1,3 @@ package fr.uparis.diamantkennel.memorisationapplication.ui enum class ErrorsAjout { BAD_ENTRY, DUPLICATE } - -class Duplicate : Exception(ErrorsAjout.DUPLICATE.toString()) diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/HomeViewModel.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/HomeViewModel.kt index ed6fa66..c4e785d 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/HomeViewModel.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/HomeViewModel.kt @@ -138,38 +138,36 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) { } suspend fun import(ctx: Context, path: String) { - val data = - flow { - if (path.startsWith("content://")) { - // Local file - val reader = - BufferedReader( - InputStreamReader( - ctx.contentResolver.openInputStream( - Uri.parse( - path - ) - ) + val data = flow { + if (path.startsWith("content://")) { + // Local file + val reader = BufferedReader( + InputStreamReader( + ctx.contentResolver.openInputStream( + Uri.parse( + path ) ) + ) + ) - emit(reader.use { it.readText() }) - } else { - // File from internet - val url = URL(path) - val connection = url.openConnection() as HttpURLConnection - connection.requestMethod = "GET" + emit(reader.use { it.readText() }) + } else { + // File from internet + val url = URL(path) + val connection = url.openConnection() as HttpURLConnection + connection.requestMethod = "GET" - val inputStream = connection.inputStream - val reader = BufferedReader(InputStreamReader(inputStream)) + val inputStream = connection.inputStream + val reader = BufferedReader(InputStreamReader(inputStream)) - val content = reader.use { it.readText() } + val content = reader.use { it.readText() } - connection.disconnect() + connection.disconnect() - emit(content) - } - }.flowOn(Dispatchers.IO) + emit(content) + } + }.flowOn(Dispatchers.IO) dismissImportation()