TogetherActions.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/together/TogetherActions.kt
1package eu.friedcutlet.yokan.ui.screens.together23import eu.friedcutlet.yokan.services.http.HttpService4import eu.friedcutlet.yokan.services.together.ProfileTogetherSession5import kotlinx.coroutines.CancellationException6import kotlinx.coroutines.currentCoroutineContext7import kotlinx.coroutines.ensureActive8import eu.friedcutlet.yokan.services.together.RoomClient9import eu.friedcutlet.yokan.services.together.RoomState10import eu.friedcutlet.yokan.services.together.roomMediaFor1112/** Shared by the title menu: start a room, or add/suggest the title to the current one. */13internal object TogetherActions {14 /** Creates a room for [manga] and joins it, calling [onRoomOpened] on success; returns the outcome. */15 suspend fun startRoom(together: ProfileTogetherSession, http: HttpService, manga: eu.friedcutlet.yokan.domain.Manga, chapter: eu.friedcutlet.yokan.domain.Chapter?, onRoomOpened: () -> Unit): String {16 val result = together.createAndJoin(http, roomMediaFor(manga, chapter))17 ?: return "Log in to your hub under Settings → Sync first."18 val joined = result.getOrElse { return it.message ?: "Could not create the room." }19 currentCoroutineContext().ensureActive()20 if (!together.owns(joined.client)) throw CancellationException("Together room changed before opening it.")21 val created = joined.created22 onRoomOpened()23 return "Room ${created.code} is open. Share the code or ${created.joinUrl}."24 }2526 fun addToRoom(manga: eu.friedcutlet.yokan.domain.Manga, chapter: eu.friedcutlet.yokan.domain.Chapter?, room: RoomState, client: RoomClient): String {27 val media = roomMediaFor(manga, chapter)28 return when {29 room.canAddToPlaylist -> { client.playlistAdd(media); "Added to the room playlist." }30 room.canSuggest -> { client.playlistSuggest(media); "Suggested to the host." }31 else -> "Only the host changes this room's playlist."32 }33 }34}35