using instance based playback activity listener and updating param name to give more context

This commit is contained in:
Adam Brown 2021-11-25 09:48:14 +00:00
parent 6d2dc20ad1
commit 28af0a637f
2 changed files with 17 additions and 17 deletions

View file

@ -48,8 +48,6 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import javax.inject.Inject
private const val ROOM_DETAILS_SCREEN_ON_TRACKER = "room_details_screen_on"
@AndroidEntryPoint
class RoomDetailActivity :
VectorBaseActivity<ActivityRoomDetailBinding>(),
@ -77,6 +75,13 @@ class RoomDetailActivity :
}
}
private val playbackActivityListener = VoiceMessagePlaybackTracker.ActivityListener { isPlayingOrRecording ->
when (isPlayingOrRecording) {
true -> keepScreenOn()
false -> endKeepScreenOn()
}
}
override fun getCoordinatorLayout() = views.coordinatorLayout
@Inject lateinit var playbackTracker: VoiceMessagePlaybackTracker
@ -122,12 +127,7 @@ class RoomDetailActivity :
}
views.drawerLayout.addDrawerListener(drawerListener)
playbackTracker.trackActivity(ROOM_DETAILS_SCREEN_ON_TRACKER) { isActive ->
when (isActive) {
true -> keepScreenOn()
false -> endKeepScreenOn()
}
}
playbackTracker.trackActivity(playbackActivityListener)
}
private fun handleRoomLeft(roomLeft: RequireActiveMembershipViewEvents.RoomLeft) {
@ -150,7 +150,7 @@ class RoomDetailActivity :
override fun onDestroy() {
supportFragmentManager.unregisterFragmentLifecycleCallbacks(fragmentLifecycleCallbacks)
views.drawerLayout.removeDrawerListener(drawerListener)
playbackTracker.unTrackActivity(ROOM_DETAILS_SCREEN_ON_TRACKER)
playbackTracker.unTrackActivity(playbackActivityListener)
super.onDestroy()
}

View file

@ -26,15 +26,15 @@ class VoiceMessagePlaybackTracker @Inject constructor() {
private val mainHandler = Handler(Looper.getMainLooper())
private val listeners = mutableMapOf<String, Listener>()
private val activityListeners = mutableMapOf<String, ActivityListener>()
private val activityListeners = mutableListOf<ActivityListener>()
private val states = mutableMapOf<String, Listener.State>()
fun trackActivity(key: String, listener: ActivityListener) {
activityListeners[key] = listener
fun trackActivity(listener: ActivityListener) {
activityListeners.add(listener)
}
fun unTrackActivity(id: String) {
activityListeners.remove(id)
fun unTrackActivity(listener: ActivityListener) {
activityListeners.remove(listener)
}
fun track(id: String, listener: Listener) {
@ -61,10 +61,10 @@ class VoiceMessagePlaybackTracker @Inject constructor() {
*/
private fun setState(key: String, state: Listener.State) {
states[key] = state
val isActive = states.values.any { it is Listener.State.Playing || it is Listener.State.Recording }
val isPlayingOrRecording = states.values.any { it is Listener.State.Playing || it is Listener.State.Recording }
mainHandler.post {
listeners[key]?.onUpdate(state)
activityListeners.forEach { it.value.onUpdate(isActive) }
activityListeners.forEach { it.onUpdate(isPlayingOrRecording) }
}
}
@ -138,6 +138,6 @@ class VoiceMessagePlaybackTracker @Inject constructor() {
}
fun interface ActivityListener {
fun onUpdate(isActive: Boolean)
fun onUpdate(isPlayingOrRecording: Boolean)
}
}