update voc en vm

android
Antoine JOURDAIN 1 year ago
parent e8bf8156c5
commit 27f0a6ba4c

@ -0,0 +1,26 @@
package sae.android.sae_2a.VM
import androidx.compose.runtime.mutableStateListOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import sae.android.sae_2a.data.Vocabulary
class VocabularyViewModel : ViewModel() {
private val _vocabulary = mutableStateListOf<Vocabulary>()
val vocabulary: List<Vocabulary> get() = _vocabulary
init {
loadVocabulary()
}
private fun loadVocabulary() {
viewModelScope.launch {
_vocabulary.add(Vocabulary("test","Auteur", hashMapOf("Fromage" to "Cheese", "Pomme" to "Apple")))
}
}
fun addVocabulary(vocabulary: Vocabulary){
_vocabulary.add(vocabulary)
}
}

@ -10,8 +10,8 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentWidth import androidx.compose.foundation.layout.wrapContentWidth
//import androidx.compose.material.BottomNavigation import androidx.compose.material.BottomNavigation
//import androidx.compose.material.BottomNavigationItem import androidx.compose.material.BottomNavigationItem
import androidx.compose.material3.Button import androidx.compose.material3.Button
import androidx.compose.material3.Icon import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold import androidx.compose.material3.Scaffold

@ -25,6 +25,7 @@ import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Card import androidx.compose.material3.Card
import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text import androidx.compose.material3.Text
@ -40,12 +41,14 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import sae.android.sae_2a.R import sae.android.sae_2a.R
import sae.android.sae_2a.VM.VocabularyViewModel
@Composable @Composable
fun VocabularyScreen(onNavigateToList: () -> Unit, onNavigateToDetails: (Vocabulary) -> Unit = {}){ fun VocabularyScreen(onNavigateToList: () -> Unit, onNavigateToDetails: (Vocabulary) -> Unit = {}){
val vocabulary = listOf( /*val vocabulary = listOf(
Vocabulary("test","Auteur", hashMapOf("Fromage" to "Cheese", "Pomme" to "Apple")), Vocabulary("test","Auteur", hashMapOf("Fromage" to "Cheese", "Pomme" to "Apple")),
Vocabulary("test2","Auteur2", hashMapOf("Fromage" to "Cheese", "Pomme" to "Apple")), Vocabulary("test2","Auteur2", hashMapOf("Fromage" to "Cheese", "Pomme" to "Apple")),
Vocabulary("test","Auteur", hashMapOf("Fromage" to "Cheese", "Pomme" to "Apple")), Vocabulary("test","Auteur", hashMapOf("Fromage" to "Cheese", "Pomme" to "Apple")),
@ -64,21 +67,30 @@ fun VocabularyScreen(onNavigateToList: () -> Unit, onNavigateToDetails: (Vocabul
Vocabulary("test2","Auteur2", hashMapOf("Fromage" to "Cheese", "Pomme" to "Apple")), Vocabulary("test2","Auteur2", hashMapOf("Fromage" to "Cheese", "Pomme" to "Apple")),
Vocabulary("test","Auteur", hashMapOf("Fromage" to "Cheese", "Pomme" to "Apple")), Vocabulary("test","Auteur", hashMapOf("Fromage" to "Cheese", "Pomme" to "Apple")),
Vocabulary("test2","Auteur2", hashMapOf("Fromage" to "Cheese", "Pomme" to "Apple")), Vocabulary("test2","Auteur2", hashMapOf("Fromage" to "Cheese", "Pomme" to "Apple")),
) )*/
DisplayLists(vocabulary, onNavigateToDetails) val vocVM: VocabularyViewModel = viewModel()
DisplayLists(vocVM.vocabulary, vocVM, onNavigateToDetails)
} }
@Composable @Composable
fun DisplayLists(vocabulary: List<Vocabulary>, onCardClick: (Vocabulary) -> Unit = {}){ fun DisplayLists(vocabulary: List<Vocabulary>, vocVM: VocabularyViewModel, onCardClick: (Vocabulary) -> Unit = {}){
LazyVerticalGrid( columns = GridCells.Adaptive(150.dp), LazyVerticalGrid( columns = GridCells.Adaptive(150.dp),
horizontalArrangement = Arrangement.spacedBy(10.dp), horizontalArrangement = Arrangement.spacedBy(10.dp),
contentPadding = PaddingValues(10.dp), contentPadding = PaddingValues(10.dp),
verticalArrangement = Arrangement.spacedBy(10.dp)) { verticalArrangement = Arrangement.spacedBy(10.dp),
modifier = Modifier.padding(0.dp, 40.dp, 0.dp, 0.dp)) {
items(vocabulary.size) { voc -> items(vocabulary.size) { voc ->
VocCard(vocabulary[voc]) VocCard(vocabulary[voc])
Spacer(modifier = Modifier.size(10.dp)) Spacer(modifier = Modifier.size(10.dp))
} }
} }
Text(text = stringResource(id = R.string.voc_list_title),
fontSize = 30.sp,
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth())
Button(onClick = { vocVM.addVocabulary(Vocabulary("test2","Auteur2", hashMapOf("Fromage" to "Cheese", "Pomme" to "Apple"))) }) {
Text(text = stringResource(id = R.string.add_voc))
}
} }

@ -15,4 +15,6 @@
<string name="register">Register</string> <string name="register">Register</string>
<string name="accountDetails">Account Details</string> <string name="accountDetails">Account Details</string>
<string name="changepswd">Change Password</string> <string name="changepswd">Change Password</string>
<string name="voc_list_title">Vocabulary lists</string>
<string name="add_voc">New list</string>
</resources> </resources>

@ -1,5 +1,5 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules. // Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins { plugins {
id("com.android.application") version "8.2.2" apply false id("com.android.application") version "8.3.1" apply false
id("org.jetbrains.kotlin.android") version "1.9.0" apply false id("org.jetbrains.kotlin.android") version "1.9.0" apply false
} }

@ -1,6 +1,6 @@
#Wed Mar 13 08:54:26 CET 2024 #Wed Mar 13 08:54:26 CET 2024
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists

Loading…
Cancel
Save