-
Notifications
You must be signed in to change notification settings - Fork 759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor: split TimelineFragment into MessageComposerFragment and VoiceRecorderFragment #7285
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
baf527e
Extract MessageComposerFragment and VoiceRecorderFragment from Timeli…
jmartinesp 2c95265
Remove coordinator, fix minor issues
jmartinesp 0d97fa2
Try to centralise the usage of fragment args
jmartinesp 3c2e255
Simplify child fragment replacement logic
jmartinesp 321fddf
Remove TODO
jmartinesp e1cad01
Fix lint issues
jmartinesp e6a2d50
Add changelog, address review comments.
jmartinesp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Refactor TimelineFragment, split it into MessageComposerFragment and VoiceRecorderFragment. |
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
799 changes: 34 additions & 765 deletions
799
vector/src/main/java/im/vector/app/features/home/room/detail/TimelineFragment.kt
Large diffs are not rendered by default.
Oops, something went wrong.
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
803 changes: 803 additions & 0 deletions
803
...src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerFragment.kt
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
189 changes: 189 additions & 0 deletions
189
...main/java/im/vector/app/features/home/room/detail/composer/voice/VoiceRecorderFragment.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,189 @@ | ||
/* | ||
* Copyright (c) 2022 New Vector Ltd | ||
* | ||
* 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 im.vector.app.features.home.room.detail.composer.voice | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.core.view.isVisible | ||
import com.airbnb.mvrx.activityViewModel | ||
import com.airbnb.mvrx.withState | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import im.vector.app.R | ||
import im.vector.app.core.hardware.vibrate | ||
import im.vector.app.core.platform.VectorBaseFragment | ||
import im.vector.app.core.time.Clock | ||
import im.vector.app.core.utils.PERMISSIONS_FOR_VOICE_MESSAGE | ||
import im.vector.app.core.utils.checkPermissions | ||
import im.vector.app.core.utils.onPermissionDeniedSnackbar | ||
import im.vector.app.core.utils.registerForPermissionsResult | ||
import im.vector.app.databinding.FragmentVoiceRecorderBinding | ||
import im.vector.app.features.home.room.detail.TimelineViewModel | ||
import im.vector.app.features.home.room.detail.composer.MessageComposerAction | ||
import im.vector.app.features.home.room.detail.composer.MessageComposerViewEvents | ||
import im.vector.app.features.home.room.detail.composer.MessageComposerViewModel | ||
import im.vector.app.features.home.room.detail.timeline.helper.AudioMessagePlaybackTracker | ||
import javax.inject.Inject | ||
|
||
@AndroidEntryPoint | ||
class VoiceRecorderFragment : VectorBaseFragment<FragmentVoiceRecorderBinding>() { | ||
|
||
@Inject lateinit var audioMessagePlaybackTracker: AudioMessagePlaybackTracker | ||
@Inject lateinit var clock: Clock | ||
|
||
private val timelineViewModel: TimelineViewModel by activityViewModel() | ||
private val messageComposerViewModel: MessageComposerViewModel by activityViewModel() | ||
|
||
private val permissionVoiceMessageLauncher = registerForPermissionsResult { allGranted, deniedPermanently -> | ||
if (allGranted) { | ||
// In this case, let the user start again the gesture | ||
} else if (deniedPermanently) { | ||
vectorBaseActivity.onPermissionDeniedSnackbar(R.string.denied_permission_voice_message) | ||
} | ||
} | ||
|
||
override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentVoiceRecorderBinding { | ||
return FragmentVoiceRecorderBinding.inflate(inflater, container, false) | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
|
||
messageComposerViewModel.observeViewEvents { | ||
when (it) { | ||
is MessageComposerViewEvents.AnimateSendButtonVisibility -> handleSendButtonVisibilityChanged(it.isVisible) | ||
else -> Unit | ||
} | ||
} | ||
} | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
|
||
// Removed listeners should be set again | ||
setupVoiceMessageView() | ||
} | ||
|
||
override fun onPause() { | ||
super.onPause() | ||
|
||
audioMessagePlaybackTracker.pauseAllPlaybacks() | ||
} | ||
|
||
override fun invalidate() = withState(timelineViewModel, messageComposerViewModel) { mainState, messageComposerState -> | ||
if (mainState.tombstoneEvent != null) return@withState | ||
|
||
val hasVoiceDraft = messageComposerState.voiceRecordingUiState is VoiceMessageRecorderView.RecordingUiState.Draft | ||
with(views.root) { | ||
isVisible = messageComposerState.isVoiceMessageRecorderVisible || hasVoiceDraft | ||
render(messageComposerState.voiceRecordingUiState) | ||
} | ||
} | ||
|
||
private fun handleSendButtonVisibilityChanged(isSendButtonVisible: Boolean) { | ||
if (isSendButtonVisible) { | ||
views.root.isVisible = false | ||
} else { | ||
views.root.alpha = 0f | ||
views.root.isVisible = true | ||
views.root.animate().alpha(1f).setDuration(150).start() | ||
} | ||
} | ||
|
||
private fun setupVoiceMessageView() { | ||
audioMessagePlaybackTracker.track(AudioMessagePlaybackTracker.RECORDING_ID, views.voiceMessageRecorderView) | ||
views.voiceMessageRecorderView.callback = object : VoiceMessageRecorderView.Callback { | ||
|
||
override fun onVoiceRecordingStarted() { | ||
if (checkPermissions(PERMISSIONS_FOR_VOICE_MESSAGE, requireActivity(), permissionVoiceMessageLauncher)) { | ||
messageComposerViewModel.handle(MessageComposerAction.StartRecordingVoiceMessage) | ||
vibrate(requireContext()) | ||
updateRecordingUiState(VoiceMessageRecorderView.RecordingUiState.Recording(clock.epochMillis())) | ||
} | ||
} | ||
|
||
override fun onVoicePlaybackButtonClicked() { | ||
messageComposerViewModel.handle(MessageComposerAction.PlayOrPauseRecordingPlayback) | ||
} | ||
|
||
override fun onVoiceRecordingCancelled() { | ||
messageComposerViewModel.handle(MessageComposerAction.EndRecordingVoiceMessage(isCancelled = true, rootThreadEventId = getRootThreadEventId())) | ||
vibrate(requireContext()) | ||
updateRecordingUiState(VoiceMessageRecorderView.RecordingUiState.Idle) | ||
} | ||
|
||
override fun onVoiceRecordingLocked() { | ||
val startedState = withState(messageComposerViewModel) { it.voiceRecordingUiState as? VoiceMessageRecorderView.RecordingUiState.Recording } | ||
val startTime = startedState?.recordingStartTimestamp ?: clock.epochMillis() | ||
updateRecordingUiState(VoiceMessageRecorderView.RecordingUiState.Locked(startTime)) | ||
} | ||
|
||
override fun onVoiceRecordingEnded() { | ||
onSendVoiceMessage() | ||
} | ||
|
||
override fun onSendVoiceMessage() { | ||
messageComposerViewModel.handle( | ||
MessageComposerAction.EndRecordingVoiceMessage(isCancelled = false, rootThreadEventId = getRootThreadEventId()) | ||
) | ||
updateRecordingUiState(VoiceMessageRecorderView.RecordingUiState.Idle) | ||
} | ||
|
||
override fun onDeleteVoiceMessage() { | ||
messageComposerViewModel.handle( | ||
MessageComposerAction.EndRecordingVoiceMessage(isCancelled = true, rootThreadEventId = getRootThreadEventId()) | ||
) | ||
updateRecordingUiState(VoiceMessageRecorderView.RecordingUiState.Idle) | ||
} | ||
|
||
override fun onRecordingLimitReached() = pauseRecording() | ||
|
||
override fun onRecordingWaveformClicked() = pauseRecording() | ||
|
||
override fun onVoiceWaveformTouchedUp(percentage: Float, duration: Int) { | ||
messageComposerViewModel.handle( | ||
MessageComposerAction.VoiceWaveformTouchedUp(AudioMessagePlaybackTracker.RECORDING_ID, duration, percentage) | ||
) | ||
} | ||
|
||
override fun onVoiceWaveformMoved(percentage: Float, duration: Int) { | ||
messageComposerViewModel.handle( | ||
MessageComposerAction.VoiceWaveformTouchedUp(AudioMessagePlaybackTracker.RECORDING_ID, duration, percentage) | ||
) | ||
} | ||
|
||
private fun updateRecordingUiState(state: VoiceMessageRecorderView.RecordingUiState) { | ||
messageComposerViewModel.handle( | ||
MessageComposerAction.OnVoiceRecordingUiStateChanged(state) | ||
) | ||
} | ||
|
||
private fun pauseRecording() { | ||
messageComposerViewModel.handle( | ||
MessageComposerAction.PauseRecordingVoiceMessage | ||
) | ||
updateRecordingUiState(VoiceMessageRecorderView.RecordingUiState.Draft) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns the root thread event if we are in a thread room, otherwise returns null. | ||
*/ | ||
fun getRootThreadEventId(): String? = withState(timelineViewModel) { it.rootThreadEventId } | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<im.vector.app.features.home.room.detail.composer.MessageComposerView | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:id="@+id/composerLayout" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:background="?android:colorBackground" | ||
android:minHeight="56dp" | ||
android:transitionName="composer" | ||
android:visibility="gone" | ||
tools:visibility="visible" /> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?android:colorBackground
with a theme that also paints a background (inferred theme is@android:style/Theme.Holo
)?android:colorBackground
with a theme that also paints a background (inferred theme is@android:style/Theme.Holo
)