package eu.friedcutlet.yokan.ui.screens.together import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import eu.friedcutlet.yokan.services.catchingNonCancellation import eu.friedcutlet.yokan.services.together.HubSession import eu.friedcutlet.yokan.services.together.ProfileTogetherSession import eu.friedcutlet.yokan.services.together.RoomClient import eu.friedcutlet.yokan.services.together.RoomState import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.jetbrains.compose.resources.getString import yokan.composeapp.generated.resources.Res import yokan.composeapp.generated.resources.together_join_failed import yokan.composeapp.generated.resources.together_leave_failed import yokan.composeapp.generated.resources.together_login_to_join internal data class TogetherState( val room: RoomState? = null, /** The client [room] belongs to; room commands go to it. */ val client: RoomClient? = null, val following: Boolean = true, val driving: Boolean = false, val message: String? = null, ) /** * Join/leave and follow/drive for the Together screen. Calls run in [viewModelScope], so a join in * flight completes after the screen is covered. Room errors surface as [TogetherState.message]. */ @OptIn(ExperimentalCoroutinesApi::class) internal class TogetherViewModel( private val together: ProfileTogetherSession, private val hubSession: () -> HubSession?, private val workDispatcher: CoroutineDispatcher = Dispatchers.Default, ) : ViewModel() { private val _state = MutableStateFlow(TogetherState()) val state: StateFlow = _state.asStateFlow() init { viewModelScope.launch { val rooms = together.client.flatMapLatest { client -> client?.state?.map { client to it } ?: flowOf(null) } combine(rooms, together.following, together.driving, ::Triple).collect { (current, following, driving) -> val room = current?.second _state.update { val error = room?.lastError?.takeIf { error -> error != it.room?.lastError } it.copy(room = room, client = current?.first, following = following, driving = driving, message = error ?: it.message) } } } // Deep link: join as soon as a code is handed in. viewModelScope.launch { TogetherSession.pendingCode.filterNotNull().collect { TogetherSession.takePendingCode()?.let(::join) } } } fun join(code: String) { viewModelScope.launch { val hub = hubSession() if (hub == null) { showMessage(getString(Res.string.together_login_to_join)) return@launch } catchingNonCancellation { withContext(workDispatcher) { together.join(hub, code) } } .onFailure { error -> showMessage(error.message ?: getString(Res.string.together_join_failed)) } } } fun leave() { val client = together.client.value ?: return viewModelScope.launch { catchingNonCancellation { withContext(workDispatcher) { together.leave(client) } } .onFailure { error -> showMessage(error.message ?: getString(Res.string.together_leave_failed)) } } } fun setFollowing(following: Boolean) { val client = together.client.value ?: return if (!together.owns(client)) return together.setFollowing(following) client.presence(detached = !following) } fun setDriving(driving: Boolean) { val client = together.client.value ?: return if (together.owns(client)) together.setDriving(driving) } fun dismissMessage() { _state.update { it.copy(message = null) } } private fun showMessage(message: String) { _state.update { it.copy(message = message) } } }