feat : ajout MultiPageViewModel utilisable
continuous-integration/drone/push Build is passing Details

androidCompose
Jeremy DUCOURTHIAL 1 year ago
parent 5382e21c65
commit a1665f119c

@ -68,7 +68,6 @@ dependencies {
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
implementation("com.squareup.okhttp3:okhttp:4.10.0")
implementation("com.google.code.gson:gson:2.10.1")
implementation("org.mindrot:jbcrypt:0.4")

@ -32,6 +32,7 @@ import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@ -51,6 +52,7 @@ import androidx.compose.ui.unit.sp
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.lifecycle.ViewModelProvider
import com.example.mathseduc.CreateLobbyActivity
import com.example.mathseduc.MainActivity
import com.example.mathseduc.MultiActivity
@ -60,6 +62,7 @@ import com.example.mathseduc.controllers.ControllerLobby
import com.example.mathseduc.controllers.ControllerUtiliser
import com.example.mathseduc.models.Lobby
import com.example.mathseduc.ui.theme.Colors
import com.example.mathseduc.viewModel.MultiPageViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
@ -70,8 +73,10 @@ import okhttp3.MultipartBody
@Composable
fun MultiPage(activity: MultiActivity) {
val context = LocalContext.current
var selectedItem by rememberSaveable { mutableStateOf<Lobby?>(null) }
var lobbyList by remember { mutableStateOf<List<Lobby>>(emptyList()) }
val viewModel = ViewModelProvider(activity).get(MultiPageViewModel::class.java)
val lobbyList by viewModel.lobbyList.collectAsState(initial = emptyList())
var selectedItem : Lobby? = null
val scope = rememberCoroutineScope()
val isPortrait = LocalConfiguration.current.orientation == Configuration.ORIENTATION_PORTRAIT
@ -90,11 +95,7 @@ fun MultiPage(activity: MultiActivity) {
// Fonction pour actualiser la liste des lobbies
suspend fun refreshLobbyList() {
val refreshedLobbyList = withContext(Dispatchers.IO) {
ControllerLobby.getLobbies() ?: emptyList()
}
lobbyList = refreshedLobbyList
Log.e("MainActivity", "Refreshed Lobby List")
viewModel.updateLobbyList()
}
DisposableEffect(Unit) {
@ -177,7 +178,7 @@ fun MultiPage(activity: MultiActivity) {
LobbyItem(lobby, selectedItem == lobby) {
selectedItem = it
if (ControllerLobby.getNbPlayerInLobby(selectedItem!!.id) < selectedItem!!.nbplayers) {
if (viewModel.getNbPlayerInLobby(selectedItem!!) < selectedItem!!.nbplayers) {
// Créer un Utiliser si le lobby n'est pas plein
scope.launch {

@ -0,0 +1,37 @@
package com.example.mathseduc.viewModel
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import com.example.mathseduc.controllers.ControllerLobby
import com.example.mathseduc.controllers.ControllerPlayer
import com.example.mathseduc.models.Lobby
import com.example.mathseduc.models.Player
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
class MultiPageViewModel : ViewModel() {
private var lobbyListState = MutableStateFlow<List<Lobby>>(emptyList())
val lobbyList : StateFlow<List<Lobby>> = lobbyListState
/*
var isCreator by rememberSaveable { mutableStateOf(false) }
var playerListInfos: List<Player> by rememberSaveable { mutableStateOf(emptyList()) }
var playerList by rememberSaveable { mutableStateOf(ControllerPlayer.getPlayersIdFromLobbyId(lobbyId.toString()) ?: emptyList()) }
var refreshState by rememberSaveable { mutableStateOf(true) }
*/
fun updateLobbyList() {
this.lobbyListState.update {
ControllerLobby.getLobbies() ?: emptyList()
}
}
fun getNbPlayerInLobby(selectedItem : Lobby) : Int {
return ControllerLobby.getNbPlayerInLobby(selectedItem!!.id)
}
}

@ -0,0 +1,23 @@
package com.example.mathseduc.viewModel
import androidx.lifecycle.ViewModel
import com.example.mathseduc.controllers.ControllerLobby
import com.example.mathseduc.models.Lobby
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
class ServerDetailsViewModel : ViewModel() {
private var lobbyListState = MutableStateFlow<List<Lobby>>(emptyList())
val lobbyList : StateFlow<List<Lobby>> = lobbyListState
fun updateLobbyList() {
this.lobbyListState.update {
ControllerLobby.getLobbies() ?: emptyList()
}
}
fun getNbPlayerInLobby(selectedItem : Lobby) : Int {
return ControllerLobby.getNbPlayerInLobby(selectedItem!!.id)
}
}
Loading…
Cancel
Save