Skip to content

Commit 0d97fa2

Browse files
committed
Try to centralise the usage of fragment args
1 parent 2c95265 commit 0d97fa2

File tree

4 files changed

+25
-28
lines changed

4 files changed

+25
-28
lines changed

vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewState.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.airbnb.mvrx.Async
2020
import com.airbnb.mvrx.MavericksState
2121
import com.airbnb.mvrx.Uninitialized
2222
import im.vector.app.features.home.room.detail.arguments.TimelineArgs
23+
import im.vector.app.features.share.SharedData
2324
import org.matrix.android.sdk.api.extensions.orFalse
2425
import org.matrix.android.sdk.api.session.events.model.Event
2526
import org.matrix.android.sdk.api.session.room.members.ChangeMembershipState
@@ -77,6 +78,8 @@ data class RoomDetailViewState(
7778
val threadNotificationBadgeState: ThreadNotificationBadgeState = ThreadNotificationBadgeState(),
7879
val typingUsers: List<SenderInfo>? = null,
7980
val isSharingLiveLocation: Boolean = false,
81+
val showKeyboardWhenPresented: Boolean = false,
82+
val sharedData: SharedData? = null,
8083
) : MavericksState {
8184

8285
constructor(args: TimelineArgs) : this(
@@ -86,7 +89,9 @@ data class RoomDetailViewState(
8689
// Also highlight the target event, if any
8790
highlightedEventId = args.eventId,
8891
switchToParentSpace = args.switchToParentSpace,
89-
rootThreadEventId = args.threadTimelineArgs?.rootThreadEventId
92+
rootThreadEventId = args.threadTimelineArgs?.rootThreadEventId,
93+
showKeyboardWhenPresented = args.threadTimelineArgs?.showKeyboard.orFalse(),
94+
sharedData = args.sharedData,
9095
)
9196

9297
fun isCallOptionAvailable(): Boolean {

vector/src/main/java/im/vector/app/features/home/room/detail/TimelineFragment.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,17 +300,13 @@ class TimelineFragment :
300300

301301
childFragmentManager.findFragmentById(R.id.composerContainer) as? MessageComposerFragment ?: run {
302302
childFragmentManager.commitTransaction {
303-
val fragment = MessageComposerFragment()
304-
fragment.arguments = timelineArgs.toMvRxBundle()
305-
replace(R.id.composerContainer, fragment)
303+
replace(R.id.composerContainer, MessageComposerFragment())
306304
}
307305
}
308306

309307
childFragmentManager.findFragmentById(R.id.voiceMessageRecorderContainer) as? VoiceRecorderFragment ?: run {
310308
childFragmentManager.commitTransaction {
311-
val fragment = VoiceRecorderFragment()
312-
fragment.arguments = timelineArgs.toMvRxBundle()
313-
replace(R.id.voiceMessageRecorderContainer, fragment)
309+
replace(R.id.voiceMessageRecorderContainer, VoiceRecorderFragment())
314310
}
315311
}
316312
}
@@ -2010,7 +2006,7 @@ class TimelineFragment :
20102006
/**
20112007
* Returns true if the current room is a Thread room, false otherwise.
20122008
*/
2013-
private fun isThreadTimeLine(): Boolean = timelineArgs.threadTimelineArgs?.rootThreadEventId != null
2009+
private fun isThreadTimeLine(): Boolean = withState(timelineViewModel) { it.isThreadTimeline() }
20142010

20152011
/**
20162012
* Returns true if the current room is a local room, false otherwise.
@@ -2020,5 +2016,5 @@ class TimelineFragment :
20202016
/**
20212017
* Returns the root thread event if we are in a thread room, otherwise returns null.
20222018
*/
2023-
fun getRootThreadEventId(): String? = timelineArgs.threadTimelineArgs?.rootThreadEventId
2019+
fun getRootThreadEventId(): String? = withState(timelineViewModel) { it.rootThreadEventId }
20242020
}

vector/src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerFragment.kt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import androidx.core.view.isInvisible
4040
import androidx.core.view.isVisible
4141
import androidx.lifecycle.Lifecycle
4242
import androidx.lifecycle.lifecycleScope
43-
import com.airbnb.mvrx.args
4443
import com.airbnb.mvrx.existingViewModel
4544
import com.airbnb.mvrx.withState
4645
import com.google.android.material.dialog.MaterialAlertDialogBuilder
@@ -74,7 +73,6 @@ import im.vector.app.features.home.AvatarRenderer
7473
import im.vector.app.features.home.room.detail.AutoCompleter
7574
import im.vector.app.features.home.room.detail.RoomDetailAction
7675
import im.vector.app.features.home.room.detail.TimelineViewModel
77-
import im.vector.app.features.home.room.detail.arguments.TimelineArgs
7876
import im.vector.app.features.home.room.detail.composer.voice.VoiceMessageRecorderView
7977
import im.vector.app.features.home.room.detail.timeline.action.MessageSharedActionViewModel
8078
import im.vector.app.features.home.room.detail.timeline.helper.MatrixItemColorProvider
@@ -131,14 +129,14 @@ class MessageComposerFragment : VectorBaseFragment<FragmentComposerBinding>(), A
131129
@Inject lateinit var buildMeta: BuildMeta
132130
@Inject lateinit var session: Session
133131

134-
private val timelineArgs: TimelineArgs by args()
132+
private val roomId: String get() = withState(timelineViewModel) { it.roomId }
135133

136134
private val autoCompleter: AutoCompleter by lazy {
137-
autoCompleterFactory.create(timelineArgs.roomId, isThreadTimeLine())
135+
autoCompleterFactory.create(roomId, isThreadTimeLine())
138136
}
139137

140138
private val pillsPostProcessor by lazy {
141-
pillsPostProcessorFactory.create(timelineArgs.roomId)
139+
pillsPostProcessorFactory.create(roomId)
142140
}
143141

144142
private val emojiPopup: EmojiPopup by lifecycleAwareLazy {
@@ -267,7 +265,8 @@ class MessageComposerFragment : VectorBaseFragment<FragmentComposerBinding>(), A
267265

268266
views.composerLayout.views.composerEmojiButton.isVisible = vectorPreferences.showEmojiKeyboard()
269267

270-
if (isThreadTimeLine() && timelineArgs.threadTimelineArgs?.showKeyboard == true) {
268+
val showKeyboard = withState(timelineViewModel) { it.showKeyboardWhenPresented }
269+
if (isThreadTimeLine() && showKeyboard) {
271270
// Show keyboard when the user started a thread
272271
views.composerLayout.views.composerEditText.showKeyboard(andRequestFocus = true)
273272
}
@@ -555,7 +554,7 @@ class MessageComposerFragment : VectorBaseFragment<FragmentComposerBinding>(), A
555554
views.composerLayout.setTextIfDifferent("")
556555
when (parsedCommand) {
557556
is ParsedCommand.DevTools -> {
558-
navigator.openDevTools(requireContext(), timelineArgs.roomId)
557+
navigator.openDevTools(requireContext(), roomId)
559558
}
560559
is ParsedCommand.SetMarkdown -> {
561560
showSnackWithMessage(getString(if (parsedCommand.enable) R.string.markdown_has_been_enabled else R.string.markdown_has_been_disabled))
@@ -578,12 +577,13 @@ class MessageComposerFragment : VectorBaseFragment<FragmentComposerBinding>(), A
578577

579578
private fun handleShowRoomUpgradeDialog(roomDetailViewEvents: MessageComposerViewEvents.ShowRoomUpgradeDialog) {
580579
val tag = MigrateRoomBottomSheet::javaClass.name
581-
MigrateRoomBottomSheet.newInstance(timelineArgs.roomId, roomDetailViewEvents.newVersion)
580+
val roomId = withState(timelineViewModel) { it.roomId }
581+
MigrateRoomBottomSheet.newInstance(roomId, roomDetailViewEvents.newVersion)
582582
.show(parentFragmentManager, tag)
583583
}
584584

585585
private fun openRoomMemberProfile(userId: String) {
586-
navigator.openRoomMemberProfile(userId = userId, roomId = timelineArgs.roomId, context = requireActivity())
586+
navigator.openRoomMemberProfile(userId = userId, roomId = roomId, context = requireActivity())
587587
}
588588

589589
private val contentAttachmentActivityResultLauncher = registerStartForActivityResult { activityResult ->
@@ -598,12 +598,12 @@ class MessageComposerFragment : VectorBaseFragment<FragmentComposerBinding>(), A
598598
/**
599599
* Returns the root thread event if we are in a thread room, otherwise returns null.
600600
*/
601-
fun getRootThreadEventId(): String? = timelineArgs.threadTimelineArgs?.rootThreadEventId
601+
fun getRootThreadEventId(): String? = withState(timelineViewModel) { it.rootThreadEventId }
602602

603603
/**
604604
* Returns true if the current room is a Thread room, false otherwise.
605605
*/
606-
private fun isThreadTimeLine(): Boolean = timelineArgs.threadTimelineArgs?.rootThreadEventId != null
606+
private fun isThreadTimeLine(): Boolean = withState(timelineViewModel) { it.isThreadTimeline() }
607607

608608

609609
// AttachmentsHelper.Callback
@@ -656,12 +656,12 @@ class MessageComposerFragment : VectorBaseFragment<FragmentComposerBinding>(), A
656656
AttachmentTypeSelectorView.Type.GALLERY -> attachmentsHelper.selectGallery(attachmentMediaActivityResultLauncher)
657657
AttachmentTypeSelectorView.Type.CONTACT -> attachmentsHelper.selectContact(attachmentContactActivityResultLauncher)
658658
AttachmentTypeSelectorView.Type.STICKER -> timelineViewModel.handle(RoomDetailAction.SelectStickerAttachment)
659-
AttachmentTypeSelectorView.Type.POLL -> navigator.openCreatePoll(requireContext(), timelineArgs.roomId, null, PollMode.CREATE)
659+
AttachmentTypeSelectorView.Type.POLL -> navigator.openCreatePoll(requireContext(), roomId, null, PollMode.CREATE)
660660
AttachmentTypeSelectorView.Type.LOCATION -> {
661661
navigator
662662
.openLocationSharing(
663663
context = requireContext(),
664-
roomId = timelineArgs.roomId,
664+
roomId = roomId,
665665
mode = LocationSharingMode.STATIC_SHARING,
666666
initialLocationData = null,
667667
locationOwnerId = session.myUserId
@@ -716,7 +716,7 @@ class MessageComposerFragment : VectorBaseFragment<FragmentComposerBinding>(), A
716716
}
717717

718718
private fun handleShareData() {
719-
when (val sharedData = timelineArgs.sharedData) {
719+
when (val sharedData = withState(timelineViewModel) { it.sharedData }) {
720720
is SharedData.Text -> {
721721
messageComposerViewModel.handle(MessageComposerAction.OnTextChanged(sharedData.text))
722722
messageComposerViewModel.handle(MessageComposerAction.EnterRegularMode(fromSharing = true))

vector/src/main/java/im/vector/app/features/home/room/detail/composer/voice/VoiceRecorderFragment.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import android.view.LayoutInflater
2121
import android.view.View
2222
import android.view.ViewGroup
2323
import androidx.core.view.isVisible
24-
import com.airbnb.mvrx.args
2524
import com.airbnb.mvrx.existingViewModel
2625
import com.airbnb.mvrx.withState
2726
import dagger.hilt.android.AndroidEntryPoint
@@ -35,7 +34,6 @@ import im.vector.app.core.utils.onPermissionDeniedSnackbar
3534
import im.vector.app.core.utils.registerForPermissionsResult
3635
import im.vector.app.databinding.FragmentVoiceRecorderBinding
3736
import im.vector.app.features.home.room.detail.TimelineViewModel
38-
import im.vector.app.features.home.room.detail.arguments.TimelineArgs
3937
import im.vector.app.features.home.room.detail.composer.MessageComposerAction
4038
import im.vector.app.features.home.room.detail.composer.MessageComposerViewEvents
4139
import im.vector.app.features.home.room.detail.composer.MessageComposerViewModel
@@ -48,8 +46,6 @@ class VoiceRecorderFragment : VectorBaseFragment<FragmentVoiceRecorderBinding>()
4846
@Inject lateinit var audioMessagePlaybackTracker: AudioMessagePlaybackTracker
4947
@Inject lateinit var clock: Clock
5048

51-
private val timelineArgs: TimelineArgs by args()
52-
5349
private val timelineViewModel: TimelineViewModel by existingViewModel()
5450
private val messageComposerViewModel: MessageComposerViewModel by existingViewModel()
5551

@@ -191,6 +187,6 @@ class VoiceRecorderFragment : VectorBaseFragment<FragmentVoiceRecorderBinding>()
191187
/**
192188
* Returns the root thread event if we are in a thread room, otherwise returns null.
193189
*/
194-
fun getRootThreadEventId(): String? = timelineArgs.threadTimelineArgs?.rootThreadEventId
190+
fun getRootThreadEventId(): String? = withState(timelineViewModel) { it.rootThreadEventId }
195191

196192
}

0 commit comments

Comments
 (0)