TogetherViewModel.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/together/TogetherViewModel.kt
1package eu.friedcutlet.yokan.ui.screens.together23import androidx.lifecycle.ViewModel4import androidx.lifecycle.viewModelScope5import eu.friedcutlet.yokan.services.catchingNonCancellation6import eu.friedcutlet.yokan.services.together.HubSession7import eu.friedcutlet.yokan.services.together.ProfileTogetherSession8import eu.friedcutlet.yokan.services.together.RoomClient9import eu.friedcutlet.yokan.services.together.RoomState10import kotlinx.coroutines.CoroutineDispatcher11import kotlinx.coroutines.Dispatchers12import kotlinx.coroutines.ExperimentalCoroutinesApi13import kotlinx.coroutines.flow.MutableStateFlow14import kotlinx.coroutines.flow.StateFlow15import kotlinx.coroutines.flow.asStateFlow16import kotlinx.coroutines.flow.combine17import kotlinx.coroutines.flow.filterNotNull18import kotlinx.coroutines.flow.flatMapLatest19import kotlinx.coroutines.flow.flowOf20import kotlinx.coroutines.flow.map21import kotlinx.coroutines.flow.update22import kotlinx.coroutines.launch23import kotlinx.coroutines.withContext24import org.jetbrains.compose.resources.getString25import yokan.composeapp.generated.resources.Res26import yokan.composeapp.generated.resources.together_join_failed27import yokan.composeapp.generated.resources.together_leave_failed28import yokan.composeapp.generated.resources.together_login_to_join2930internal data class TogetherState(31 val room: RoomState? = null,32 /** The client [room] belongs to; room commands go to it. */33 val client: RoomClient? = null,34 val following: Boolean = true,35 val driving: Boolean = false,36 val message: String? = null,37)3839/**40 * Join/leave and follow/drive for the Together screen. Calls run in [viewModelScope], so a join in41 * flight completes after the screen is covered. Room errors surface as [TogetherState.message].42 */43@OptIn(ExperimentalCoroutinesApi::class)44internal class TogetherViewModel(45 private val together: ProfileTogetherSession,46 private val hubSession: () -> HubSession?,47 private val workDispatcher: CoroutineDispatcher = Dispatchers.Default,48) : ViewModel() {49 private val _state = MutableStateFlow(TogetherState())50 val state: StateFlow<TogetherState> = _state.asStateFlow()5152 init {53 viewModelScope.launch {54 val rooms = together.client.flatMapLatest { client -> client?.state?.map { client to it } ?: flowOf(null) }55 combine(rooms, together.following, together.driving, ::Triple).collect { (current, following, driving) ->56 val room = current?.second57 _state.update {58 val error = room?.lastError?.takeIf { error -> error != it.room?.lastError }59 it.copy(room = room, client = current?.first, following = following, driving = driving, message = error ?: it.message)60 }61 }62 }63 // Deep link: join as soon as a code is handed in.64 viewModelScope.launch {65 TogetherSession.pendingCode.filterNotNull().collect { TogetherSession.takePendingCode()?.let(::join) }66 }67 }6869 fun join(code: String) {70 viewModelScope.launch {71 val hub = hubSession()72 if (hub == null) {73 showMessage(getString(Res.string.together_login_to_join))74 return@launch75 }76 catchingNonCancellation { withContext(workDispatcher) { together.join(hub, code) } }77 .onFailure { error -> showMessage(error.message ?: getString(Res.string.together_join_failed)) }78 }79 }8081 fun leave() {82 val client = together.client.value ?: return83 viewModelScope.launch {84 catchingNonCancellation { withContext(workDispatcher) { together.leave(client) } }85 .onFailure { error -> showMessage(error.message ?: getString(Res.string.together_leave_failed)) }86 }87 }8889 fun setFollowing(following: Boolean) {90 val client = together.client.value ?: return91 if (!together.owns(client)) return92 together.setFollowing(following)93 client.presence(detached = !following)94 }9596 fun setDriving(driving: Boolean) {97 val client = together.client.value ?: return98 if (together.owns(client)) together.setDriving(driving)99 }100101 fun dismissMessage() {102 _state.update { it.copy(message = null) }103 }104105 private fun showMessage(message: String) {106 _state.update { it.copy(message = message) }107 }108}109