parent
820ec2b59b
commit
f6f63d73ad
@ -0,0 +1,77 @@
|
|||||||
|
package com.example.mathseduc.viewModel
|
||||||
|
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.ui.unit.IntSize
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import com.example.mathseduc.MainActivity
|
||||||
|
import com.example.mathseduc.controllers.ControllerChapter
|
||||||
|
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
|
||||||
|
import okhttp3.MultipartBody
|
||||||
|
|
||||||
|
class CreateLobbyViewModel : ViewModel() {
|
||||||
|
|
||||||
|
private var lobbyNameState = MutableStateFlow("")
|
||||||
|
val lobbyName : StateFlow<String> = lobbyNameState
|
||||||
|
|
||||||
|
private var passwordState = MutableStateFlow("")
|
||||||
|
val password: StateFlow<String> = passwordState
|
||||||
|
|
||||||
|
private var nbPlayersState = MutableStateFlow("")
|
||||||
|
val nbPlayers: StateFlow<String> = nbPlayersState
|
||||||
|
|
||||||
|
private var difficultyState = MutableStateFlow("")
|
||||||
|
val difficulty: StateFlow<String> = difficultyState
|
||||||
|
|
||||||
|
private var chapterState = MutableStateFlow("")
|
||||||
|
val chapter : StateFlow<String> = chapterState
|
||||||
|
|
||||||
|
private var expandedDifficultyState = MutableStateFlow(false)
|
||||||
|
val expandedDifficulty: StateFlow<Boolean> = expandedDifficultyState
|
||||||
|
|
||||||
|
private var expandedChapterState = MutableStateFlow(false)
|
||||||
|
val expandedChapter: StateFlow<Boolean> = expandedChapterState
|
||||||
|
|
||||||
|
private var sizeState = MutableStateFlow(IntSize.Zero)
|
||||||
|
val size : StateFlow<IntSize> = sizeState
|
||||||
|
|
||||||
|
fun updateLobbyName(lobbyName : String) {
|
||||||
|
this.lobbyNameState.update { lobbyName }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updatePassword(password : String) {
|
||||||
|
this.passwordState.update { password }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateNbPlayers(nbPlayers : String) {
|
||||||
|
this.nbPlayersState.update { nbPlayers }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateDifficulty(difficulty : String) {
|
||||||
|
this.difficultyState.update { difficulty }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updatechapter(chapter : String) {
|
||||||
|
this.chapterState.update { chapter }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateExpandedChapter(expandedChapter : Boolean) {
|
||||||
|
this.expandedChapterState.update { expandedChapter }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateExpandedDifficulty(expandedDifficulty : Boolean) {
|
||||||
|
this.expandedDifficultyState.update { expandedDifficulty }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateSize(size : IntSize) {
|
||||||
|
this.sizeState.update { size }
|
||||||
|
}
|
||||||
|
}
|
@ -1,23 +1,73 @@
|
|||||||
package com.example.mathseduc.viewModel
|
package com.example.mathseduc.viewModel
|
||||||
|
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
|
import com.example.mathseduc.MainActivity
|
||||||
|
import com.example.mathseduc.controllers.ControllerChapter
|
||||||
import com.example.mathseduc.controllers.ControllerLobby
|
import com.example.mathseduc.controllers.ControllerLobby
|
||||||
|
import com.example.mathseduc.controllers.ControllerPlayer
|
||||||
import com.example.mathseduc.models.Lobby
|
import com.example.mathseduc.models.Lobby
|
||||||
|
import com.example.mathseduc.models.Player
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.update
|
import kotlinx.coroutines.flow.update
|
||||||
|
import okhttp3.MultipartBody
|
||||||
|
|
||||||
class ServerDetailsViewModel : ViewModel() {
|
class ServerDetailsViewModel : ViewModel() {
|
||||||
|
|
||||||
private var lobbyListState = MutableStateFlow<List<Lobby>>(emptyList())
|
private var playerListState = MutableStateFlow<List<Int>>(emptyList())
|
||||||
val lobbyList : StateFlow<List<Lobby>> = lobbyListState
|
val playerList : StateFlow<List<Int>> = playerListState
|
||||||
|
|
||||||
|
private var playerListInfosState = MutableStateFlow<List<Player>>(emptyList())
|
||||||
|
val playerListInfos: StateFlow<List<Player>> = playerListInfosState
|
||||||
|
|
||||||
|
private var isCreatorState = MutableStateFlow(false)
|
||||||
|
val isCreator: StateFlow<Boolean> = isCreatorState
|
||||||
|
|
||||||
|
private var showDialogState = MutableStateFlow(false)
|
||||||
|
val showDialog: StateFlow<Boolean> = showDialogState
|
||||||
|
|
||||||
|
private var refreshStateState = MutableStateFlow(true)
|
||||||
|
val refreshState : StateFlow<Boolean> = refreshStateState
|
||||||
|
|
||||||
|
fun updatePlayerList(lobbyId : Int) {
|
||||||
|
this.playerListState.update {
|
||||||
|
ControllerPlayer.getPlayersIdFromLobbyId(lobbyId.toString()) ?: emptyList()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updatePlayerListInfos() {
|
||||||
|
this.playerListInfosState.update {
|
||||||
|
playerList.value.mapNotNull { playerId -> ControllerPlayer.getPlayerInfoById(playerId.toString()) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun updateLobbyList() {
|
fun updateIsCreator(lobbyId : Int) {
|
||||||
this.lobbyListState.update {
|
this.isCreatorState.update {
|
||||||
ControllerLobby.getLobbies() ?: emptyList()
|
ControllerLobby.playerCreatorIdPresentInLobby(MainActivity.idPlayerConnected, lobbyId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getNbPlayerInLobby(selectedItem : Lobby) : Int {
|
fun updateRefresh(bool : Boolean) {
|
||||||
return ControllerLobby.getNbPlayerInLobby(selectedItem!!.id)
|
this.refreshStateState.update { bool }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateShowDialog(bool : Boolean) {
|
||||||
|
this.showDialogState.update { bool }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Launchedlobby(lobbyId : Int): Boolean{
|
||||||
|
return ControllerLobby.lobbyIsLaunched(lobbyId)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getNbPlayerInLobby(lobbyId : Int) : Int{
|
||||||
|
return ControllerLobby.getNbPlayerInLobby(lobbyId)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getChapterNameById(chapterId: Int): String?{
|
||||||
|
return ControllerChapter.getChapterNameById(chapterId)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateLobbyLauched(lobbyId : Int,formDataBuilder: MultipartBody.Builder){
|
||||||
|
ControllerLobby.updateLobbyLauched(lobbyId,formDataBuilder)
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in new issue