|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 New Vector Ltd |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package im.vector.app.features.home.room.detail.composer.voice |
| 18 | + |
| 19 | +import android.os.Bundle |
| 20 | +import android.view.LayoutInflater |
| 21 | +import android.view.View |
| 22 | +import android.view.ViewGroup |
| 23 | +import androidx.core.view.isVisible |
| 24 | +import com.airbnb.mvrx.activityViewModel |
| 25 | +import com.airbnb.mvrx.withState |
| 26 | +import dagger.hilt.android.AndroidEntryPoint |
| 27 | +import im.vector.app.R |
| 28 | +import im.vector.app.core.hardware.vibrate |
| 29 | +import im.vector.app.core.platform.VectorBaseFragment |
| 30 | +import im.vector.app.core.time.Clock |
| 31 | +import im.vector.app.core.utils.PERMISSIONS_FOR_VOICE_MESSAGE |
| 32 | +import im.vector.app.core.utils.checkPermissions |
| 33 | +import im.vector.app.core.utils.onPermissionDeniedSnackbar |
| 34 | +import im.vector.app.core.utils.registerForPermissionsResult |
| 35 | +import im.vector.app.databinding.FragmentVoiceRecorderBinding |
| 36 | +import im.vector.app.features.home.room.detail.TimelineViewModel |
| 37 | +import im.vector.app.features.home.room.detail.composer.MessageComposerAction |
| 38 | +import im.vector.app.features.home.room.detail.composer.MessageComposerViewEvents |
| 39 | +import im.vector.app.features.home.room.detail.composer.MessageComposerViewModel |
| 40 | +import im.vector.app.features.home.room.detail.timeline.helper.AudioMessagePlaybackTracker |
| 41 | +import javax.inject.Inject |
| 42 | + |
| 43 | +@AndroidEntryPoint |
| 44 | +class VoiceRecorderFragment : VectorBaseFragment<FragmentVoiceRecorderBinding>() { |
| 45 | + |
| 46 | + @Inject lateinit var audioMessagePlaybackTracker: AudioMessagePlaybackTracker |
| 47 | + @Inject lateinit var clock: Clock |
| 48 | + |
| 49 | + private val timelineViewModel: TimelineViewModel by activityViewModel() |
| 50 | + private val messageComposerViewModel: MessageComposerViewModel by activityViewModel() |
| 51 | + |
| 52 | + private val permissionVoiceMessageLauncher = registerForPermissionsResult { allGranted, deniedPermanently -> |
| 53 | + if (allGranted) { |
| 54 | + // In this case, let the user start again the gesture |
| 55 | + } else if (deniedPermanently) { |
| 56 | + vectorBaseActivity.onPermissionDeniedSnackbar(R.string.denied_permission_voice_message) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentVoiceRecorderBinding { |
| 61 | + return FragmentVoiceRecorderBinding.inflate(inflater, container, false) |
| 62 | + } |
| 63 | + |
| 64 | + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
| 65 | + super.onViewCreated(view, savedInstanceState) |
| 66 | + |
| 67 | + messageComposerViewModel.observeViewEvents { |
| 68 | + when (it) { |
| 69 | + is MessageComposerViewEvents.AnimateSendButtonVisibility -> handleSendButtonVisibilityChanged(it.isVisible) |
| 70 | + else -> Unit |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + override fun onResume() { |
| 76 | + super.onResume() |
| 77 | + |
| 78 | + // Removed listeners should be set again |
| 79 | + setupVoiceMessageView() |
| 80 | + } |
| 81 | + |
| 82 | + override fun onPause() { |
| 83 | + super.onPause() |
| 84 | + |
| 85 | + audioMessagePlaybackTracker.pauseAllPlaybacks() |
| 86 | + } |
| 87 | + |
| 88 | + override fun invalidate() = withState(timelineViewModel, messageComposerViewModel) { mainState, messageComposerState -> |
| 89 | + if (mainState.tombstoneEvent != null) return@withState |
| 90 | + |
| 91 | + val hasVoiceDraft = messageComposerState.voiceRecordingUiState is VoiceMessageRecorderView.RecordingUiState.Draft |
| 92 | + with(views.root) { |
| 93 | + isVisible = messageComposerState.isVoiceMessageRecorderVisible || hasVoiceDraft |
| 94 | + render(messageComposerState.voiceRecordingUiState) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private fun handleSendButtonVisibilityChanged(isSendButtonVisible: Boolean) { |
| 99 | + if (isSendButtonVisible) { |
| 100 | + views.root.isVisible = false |
| 101 | + } else { |
| 102 | + views.root.alpha = 0f |
| 103 | + views.root.isVisible = true |
| 104 | + views.root.animate().alpha(1f).setDuration(150).start() |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private fun setupVoiceMessageView() { |
| 109 | + audioMessagePlaybackTracker.track(AudioMessagePlaybackTracker.RECORDING_ID, views.voiceMessageRecorderView) |
| 110 | + views.voiceMessageRecorderView.callback = object : VoiceMessageRecorderView.Callback { |
| 111 | + |
| 112 | + override fun onVoiceRecordingStarted() { |
| 113 | + if (checkPermissions(PERMISSIONS_FOR_VOICE_MESSAGE, requireActivity(), permissionVoiceMessageLauncher)) { |
| 114 | + messageComposerViewModel.handle(MessageComposerAction.StartRecordingVoiceMessage) |
| 115 | + vibrate(requireContext()) |
| 116 | + updateRecordingUiState(VoiceMessageRecorderView.RecordingUiState.Recording(clock.epochMillis())) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + override fun onVoicePlaybackButtonClicked() { |
| 121 | + messageComposerViewModel.handle(MessageComposerAction.PlayOrPauseRecordingPlayback) |
| 122 | + } |
| 123 | + |
| 124 | + override fun onVoiceRecordingCancelled() { |
| 125 | + messageComposerViewModel.handle(MessageComposerAction.EndRecordingVoiceMessage(isCancelled = true, rootThreadEventId = getRootThreadEventId())) |
| 126 | + vibrate(requireContext()) |
| 127 | + updateRecordingUiState(VoiceMessageRecorderView.RecordingUiState.Idle) |
| 128 | + } |
| 129 | + |
| 130 | + override fun onVoiceRecordingLocked() { |
| 131 | + val startedState = withState(messageComposerViewModel) { it.voiceRecordingUiState as? VoiceMessageRecorderView.RecordingUiState.Recording } |
| 132 | + val startTime = startedState?.recordingStartTimestamp ?: clock.epochMillis() |
| 133 | + updateRecordingUiState(VoiceMessageRecorderView.RecordingUiState.Locked(startTime)) |
| 134 | + } |
| 135 | + |
| 136 | + override fun onVoiceRecordingEnded() { |
| 137 | + onSendVoiceMessage() |
| 138 | + } |
| 139 | + |
| 140 | + override fun onSendVoiceMessage() { |
| 141 | + messageComposerViewModel.handle( |
| 142 | + MessageComposerAction.EndRecordingVoiceMessage(isCancelled = false, rootThreadEventId = getRootThreadEventId()) |
| 143 | + ) |
| 144 | + updateRecordingUiState(VoiceMessageRecorderView.RecordingUiState.Idle) |
| 145 | + } |
| 146 | + |
| 147 | + override fun onDeleteVoiceMessage() { |
| 148 | + messageComposerViewModel.handle( |
| 149 | + MessageComposerAction.EndRecordingVoiceMessage(isCancelled = true, rootThreadEventId = getRootThreadEventId()) |
| 150 | + ) |
| 151 | + updateRecordingUiState(VoiceMessageRecorderView.RecordingUiState.Idle) |
| 152 | + } |
| 153 | + |
| 154 | + override fun onRecordingLimitReached() = pauseRecording() |
| 155 | + |
| 156 | + override fun onRecordingWaveformClicked() = pauseRecording() |
| 157 | + |
| 158 | + override fun onVoiceWaveformTouchedUp(percentage: Float, duration: Int) { |
| 159 | + messageComposerViewModel.handle( |
| 160 | + MessageComposerAction.VoiceWaveformTouchedUp(AudioMessagePlaybackTracker.RECORDING_ID, duration, percentage) |
| 161 | + ) |
| 162 | + } |
| 163 | + |
| 164 | + override fun onVoiceWaveformMoved(percentage: Float, duration: Int) { |
| 165 | + messageComposerViewModel.handle( |
| 166 | + MessageComposerAction.VoiceWaveformTouchedUp(AudioMessagePlaybackTracker.RECORDING_ID, duration, percentage) |
| 167 | + ) |
| 168 | + } |
| 169 | + |
| 170 | + private fun updateRecordingUiState(state: VoiceMessageRecorderView.RecordingUiState) { |
| 171 | + messageComposerViewModel.handle( |
| 172 | + MessageComposerAction.OnVoiceRecordingUiStateChanged(state) |
| 173 | + ) |
| 174 | + } |
| 175 | + |
| 176 | + private fun pauseRecording() { |
| 177 | + messageComposerViewModel.handle( |
| 178 | + MessageComposerAction.PauseRecordingVoiceMessage |
| 179 | + ) |
| 180 | + updateRecordingUiState(VoiceMessageRecorderView.RecordingUiState.Draft) |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Returns the root thread event if we are in a thread room, otherwise returns null. |
| 187 | + */ |
| 188 | + fun getRootThreadEventId(): String? = withState(timelineViewModel) { it.rootThreadEventId } |
| 189 | +} |
0 commit comments