From 66f065d3f7dbc51adcd6dc149257ac87c9e705af Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 27 Nov 2023 07:10:14 +0100 Subject: [PATCH] draft: home screen --- .../memorisationapplication/HomeScreen.kt | 82 ++++++++++++++++++- .../ui/HomeViewModel.kt | 14 ++++ app/src/main/res/values/strings.xml | 6 +- 3 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/HomeViewModel.kt 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 08dda8f..0b9aa9e 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/HomeScreen.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/HomeScreen.kt @@ -1,9 +1,89 @@ package fr.uparis.diamantkennel.memorisationapplication +import android.widget.Toast +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.fillMaxHeight +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.itemsIndexed +import androidx.compose.material3.Button +import androidx.compose.material3.Card +import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.unit.dp +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.HomeViewModel @Composable -fun HomeScreen(padding: PaddingValues) { +fun HomeScreen(padding: PaddingValues, model: HomeViewModel = viewModel()) { + val context = LocalContext.current + val setOfQuestions by model.setFlow.collectAsState(listOf()) + val currentSelection by model.selected + + Column( + modifier = Modifier.padding(padding), + horizontalAlignment = Alignment.CenterHorizontally + ) { + ShowList(setOfQuestions, currentSelection) + + Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) { + Button(onClick = { Toast.makeText(context, "Create", Toast.LENGTH_SHORT).show() }) { + Text(text = context.getString(R.string.main_button_create)) + } + Button(onClick = { Toast.makeText(context, "Modify", Toast.LENGTH_SHORT).show() }) { + Text(text = context.getString(R.string.main_button_modify)) + } + Button(onClick = { Toast.makeText(context, "Import", Toast.LENGTH_SHORT).show() }) { + Text(text = context.getString(R.string.main_button_import)) + } + } + + Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) { + Button( + modifier = Modifier.fillMaxWidth(0.9f), + onClick = { Toast.makeText(context, "Start", Toast.LENGTH_SHORT).show() }) { + Text(text = context.getString(R.string.main_button_start), fontSize = 30.sp) + } + } + } +} + +@Composable +fun ShowList( + sets: List, + currentSelection: SetQuestions?, +) { + LazyColumn( + Modifier + .fillMaxHeight(0.7f) + ) { + itemsIndexed(sets) { index, item -> + ListItem(index, item, currentSelection) + } + } +} + +@Composable +fun ListItem( + index: Int, + set: SetOfQuestions, + currentSelection: SetQuestions?, +) = Card(Modifier.fillMaxSize()) { + Row { + Text(text = set.set.name, modifier = Modifier.padding(2.dp)) + } } 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 new file mode 100644 index 0000000..9d70dbc --- /dev/null +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/HomeViewModel.kt @@ -0,0 +1,14 @@ +package fr.uparis.diamantkennel.memorisationapplication.ui + +import android.app.Application +import androidx.compose.runtime.mutableStateOf +import androidx.lifecycle.AndroidViewModel +import fr.uparis.diamantkennel.memorisationapplication.MemoApplication +import fr.uparis.diamantkennel.memorisationapplication.data.SetQuestions + +class HomeViewModel(application: Application) : AndroidViewModel(application) { + private val dao = (application as MemoApplication).database.memoDao() + + var setFlow = dao.loadAllSets() + var selected = mutableStateOf(null) +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f14896f..279b2c2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,4 +1,8 @@ Memorisation Page principale - \ No newline at end of file + Créer + Modifier + Importer + Lancer +