-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* save * rememberTranscriptions hooks * Use derivedstateof for flow creation * fix * spotless
- Loading branch information
Showing
4 changed files
with
195 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...s/src/main/java/io/livekit/android/compose/state/transcriptions/RememberTranscriptions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright 2024 LiveKit, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.livekit.android.compose.state.transcriptions | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.derivedStateOf | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateMapOf | ||
import androidx.compose.runtime.remember | ||
import io.livekit.android.annotations.Beta | ||
import io.livekit.android.compose.flow.rememberEventSelector | ||
import io.livekit.android.compose.local.requireParticipant | ||
import io.livekit.android.compose.local.requireRoom | ||
import io.livekit.android.compose.types.TrackReference | ||
import io.livekit.android.events.ParticipantEvent | ||
import io.livekit.android.events.RoomEvent | ||
import io.livekit.android.events.TrackPublicationEvent | ||
import io.livekit.android.room.Room | ||
import io.livekit.android.room.participant.Participant | ||
import io.livekit.android.room.types.TranscriptionSegment | ||
import io.livekit.android.room.types.mergeNewSegments | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
|
||
/** | ||
* Collect all the transcriptions for the room. | ||
* | ||
* @return Returns the collected transcriptions, ordered by [TranscriptionSegment.firstReceivedTime]. | ||
*/ | ||
@Beta | ||
@Composable | ||
fun rememberTranscriptions(passedRoom: Room? = null): List<TranscriptionSegment> { | ||
val room = requireRoom(passedRoom) | ||
val events = rememberEventSelector<RoomEvent.TranscriptionReceived>(room) | ||
val flow by remember(events) { | ||
derivedStateOf { | ||
events.map { it.transcriptionSegments } | ||
} | ||
} | ||
|
||
return rememberTranscriptionsImpl(transcriptionsFlow = flow) | ||
} | ||
|
||
/** | ||
* Collect all the transcriptions for a track reference. | ||
* | ||
* @return Returns the collected transcriptions, ordered by [TranscriptionSegment.firstReceivedTime]. | ||
*/ | ||
@Beta | ||
@Composable | ||
fun rememberTrackTranscriptions(trackReference: TrackReference): List<TranscriptionSegment> { | ||
val publication = trackReference.publication ?: return emptyList() | ||
val events = rememberEventSelector<TrackPublicationEvent.TranscriptionReceived>(publication) | ||
val flow by remember(events) { | ||
derivedStateOf { | ||
events.map { it.transcriptions } | ||
} | ||
} | ||
|
||
return rememberTranscriptionsImpl(transcriptionsFlow = flow) | ||
} | ||
|
||
/** | ||
* Collect all the transcriptions for a participant. | ||
* | ||
* @return Returns the collected transcriptions, ordered by [TranscriptionSegment.firstReceivedTime]. | ||
*/ | ||
@Beta | ||
@Composable | ||
fun rememberParticipantTranscriptions(passedParticipant: Participant? = null): List<TranscriptionSegment> { | ||
val participant = requireParticipant(passedParticipant) | ||
val events = rememberEventSelector<ParticipantEvent.TranscriptionReceived>(participant) | ||
val flow by remember(events) { | ||
derivedStateOf { | ||
events.map { it.transcriptions } | ||
} | ||
} | ||
|
||
return rememberTranscriptionsImpl(transcriptionsFlow = flow) | ||
} | ||
|
||
@Composable | ||
internal fun rememberTranscriptionsImpl(transcriptionsFlow: Flow<List<TranscriptionSegment>>): List<TranscriptionSegment> { | ||
val segments = remember(transcriptionsFlow) { mutableStateMapOf<String, TranscriptionSegment>() } | ||
val orderedSegments = remember(segments) { | ||
derivedStateOf { | ||
segments.values.sortedBy { segment -> segment.firstReceivedTime } | ||
} | ||
} | ||
LaunchedEffect(transcriptionsFlow) { | ||
transcriptionsFlow.collect { | ||
segments.mergeNewSegments(it) | ||
} | ||
} | ||
|
||
return orderedSegments.value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters