diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt index 2588f99861..376debc700 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt @@ -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(), @@ -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() } diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/VoiceMessagePlaybackTracker.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/VoiceMessagePlaybackTracker.kt index cf00c3e636..6bcc018167 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/VoiceMessagePlaybackTracker.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/VoiceMessagePlaybackTracker.kt @@ -26,15 +26,15 @@ class VoiceMessagePlaybackTracker @Inject constructor() { private val mainHandler = Handler(Looper.getMainLooper()) private val listeners = mutableMapOf() - private val activityListeners = mutableMapOf() + private val activityListeners = mutableListOf() private val states = mutableMapOf() - 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) } }