davidliu
Committed by GitHub

Add lastSpokeAt and joinedAt fields to Participant (#226)

@@ -14,6 +14,7 @@ import io.livekit.android.util.flowDelegate @@ -14,6 +14,7 @@ import io.livekit.android.util.flowDelegate
14 import kotlinx.coroutines.* 14 import kotlinx.coroutines.*
15 import kotlinx.coroutines.flow.* 15 import kotlinx.coroutines.flow.*
16 import livekit.LivekitModels 16 import livekit.LivekitModels
  17 +import java.util.Date
17 import javax.inject.Named 18 import javax.inject.Named
18 19
19 open class Participant( 20 open class Participant(
@@ -69,6 +70,9 @@ open class Participant( @@ -69,6 +70,9 @@ open class Participant(
69 listener?.onSpeakingChanged(this) 70 listener?.onSpeakingChanged(this)
70 internalListener?.onSpeakingChanged(this) 71 internalListener?.onSpeakingChanged(this)
71 eventBus.postEvent(ParticipantEvent.SpeakingChanged(this, newValue), scope) 72 eventBus.postEvent(ParticipantEvent.SpeakingChanged(this, newValue), scope)
  73 + if (newValue) {
  74 + lastSpokeAt = Date().time
  75 + }
72 } 76 }
73 } 77 }
74 internal set 78 internal set
@@ -124,6 +128,20 @@ open class Participant( @@ -124,6 +128,20 @@ open class Participant(
124 internal set 128 internal set
125 129
126 /** 130 /**
  131 + * Timestamp when participant joined room, in milliseconds
  132 + */
  133 + val joinedAt
  134 + get() = participantInfo?.joinedAt?.times(1000)
  135 +
  136 + /**
  137 + * Timestamp when the participant last started speaking, in milliseconds
  138 + */
  139 + @FlowObservable
  140 + @get:FlowObservable
  141 + var lastSpokeAt by flowDelegate<Long?>(null)
  142 + internal set
  143 +
  144 + /**
127 * Listener for when participant properties change 145 * Listener for when participant properties change
128 */ 146 */
129 @Deprecated("Use events instead") 147 @Deprecated("Use events instead")
@@ -9,10 +9,14 @@ import kotlinx.coroutines.test.runTest @@ -9,10 +9,14 @@ import kotlinx.coroutines.test.runTest
9 import livekit.LivekitModels 9 import livekit.LivekitModels
10 import org.junit.Assert 10 import org.junit.Assert
11 import org.junit.Assert.assertEquals 11 import org.junit.Assert.assertEquals
  12 +import org.junit.Assert.assertNotNull
  13 +import org.junit.Assert.assertNull
12 import org.junit.Assert.assertTrue 14 import org.junit.Assert.assertTrue
13 import org.junit.Before 15 import org.junit.Before
14 import org.junit.Rule 16 import org.junit.Rule
15 import org.junit.Test 17 import org.junit.Test
  18 +import java.util.Date
  19 +import kotlin.math.abs
16 20
17 @ExperimentalCoroutinesApi 21 @ExperimentalCoroutinesApi
18 class ParticipantTest { 22 class ParticipantTest {
@@ -125,15 +129,27 @@ class ParticipantTest { @@ -125,15 +129,27 @@ class ParticipantTest {
125 129
126 participant.dispose() 130 participant.dispose()
127 assertEquals("", participant.sid) 131 assertEquals("", participant.sid)
128 - Assert.assertNull(participant.name)  
129 - Assert.assertNull(participant.identity)  
130 - Assert.assertNull(participant.metadata)  
131 - Assert.assertNull(participant.permissions)  
132 - Assert.assertNull(participant.participantInfo) 132 + assertNull(participant.name)
  133 + assertNull(participant.identity)
  134 + assertNull(participant.metadata)
  135 + assertNull(participant.permissions)
  136 + assertNull(participant.participantInfo)
133 Assert.assertFalse(participant.isSpeaking) 137 Assert.assertFalse(participant.isSpeaking)
134 assertEquals(ConnectionQuality.UNKNOWN, participant.connectionQuality) 138 assertEquals(ConnectionQuality.UNKNOWN, participant.connectionQuality)
135 } 139 }
136 140
  141 + @Test
  142 + fun speakingUpdatesLastSpokeAt() = runTest {
  143 + assertNull(participant.lastSpokeAt)
  144 +
  145 + participant.isSpeaking = true
  146 +
  147 + val lastSpokeAt = participant.lastSpokeAt
  148 + val timestamp = Date().time
  149 + assertNotNull(lastSpokeAt)
  150 + assertTrue(abs(lastSpokeAt!! - timestamp) < 1000)
  151 + }
  152 +
137 companion object { 153 companion object {
138 val INFO = LivekitModels.ParticipantInfo.newBuilder() 154 val INFO = LivekitModels.ParticipantInfo.newBuilder()
139 .setSid("sid") 155 .setSid("sid")
1 -Subproject commit e78c5b18c0f3a18bae5d5cec44d30bb6358b9bc4 1 +Subproject commit ac74d1e920384ac3972b6017d1514ca983450f0d