Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.loloof64.compose_chess_experiment.ui.screens.game
- import androidx.lifecycle.ViewModel
- import androidx.lifecycle.viewModelScope
- import com.loloof64.compose_chess_experiment.data.repositories.ChessGame
- import com.loloof64.compose_chess_experiment.data.repositories.ChessGameRepository
- import dagger.hilt.android.lifecycle.HiltViewModel
- import kotlinx.coroutines.Dispatchers
- import kotlinx.coroutines.flow.SharingStarted
- import kotlinx.coroutines.flow.StateFlow
- import kotlinx.coroutines.flow.map
- import kotlinx.coroutines.flow.stateIn
- import kotlinx.coroutines.launch
- import javax.inject.Inject
- data class ChessGameUiState(
- val currentPosition: String,
- )
- @HiltViewModel
- class ChessGameViewModel @Inject constructor(
- private val gameRepository: ChessGameRepository
- ) : ViewModel() {
- val uiState : StateFlow<ChessGameUiState> = gameRepository.game.map {
- return@map ChessGameUiState(currentPosition = it.currentPosition)
- }.stateIn(
- initialValue = ChessGameUiState(currentPosition = ChessGame.emptyPosition),
- scope = viewModelScope,
- started = SharingStarted.WhileSubscribed(5000)
- )
- fun newGame(startPosition: String = ChessGame.defaultPosition) {
- viewModelScope.launch(Dispatchers.Default) {
- gameRepository.newGame(startPosition)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement