Create PermalinkUseCase to avoid injecting the Session in the View.

This commit is contained in:
Benoit Marty 2022-09-23 16:44:39 +02:00 committed by Benoit Marty
parent aa806ed2c6
commit cfca776d01
5 changed files with 50 additions and 9 deletions

View File

@ -32,6 +32,7 @@ import im.vector.app.core.resources.BuildMeta
import im.vector.app.core.utils.startSharePlainTextIntent
import im.vector.app.databinding.FragmentHomeDrawerBinding
import im.vector.app.features.analytics.plan.MobileScreen
import im.vector.app.features.permalink.PermalinkUseCase
import im.vector.app.features.settings.VectorPreferences
import im.vector.app.features.settings.VectorSettingsActivity
import im.vector.app.features.spaces.SpaceListFragment
@ -49,6 +50,7 @@ class HomeDrawerFragment :
@Inject lateinit var vectorPreferences: VectorPreferences
@Inject lateinit var avatarRenderer: AvatarRenderer
@Inject lateinit var buildMeta: BuildMeta
@Inject lateinit var permalinkUseCase: PermalinkUseCase
private lateinit var sharedActionViewModel: HomeSharedActionViewModel
@ -101,7 +103,7 @@ class HomeDrawerFragment :
}
views.homeDrawerInviteFriendButton.debouncedClicks {
session.permalinkService().createPermalink(sharedActionViewModel.session.myUserId)?.let { permalink ->
permalinkUseCase.createPermalinkOfCurrentUser()?.let { permalink ->
analyticsTracker.screen(MobileScreen(screenName = MobileScreen.ScreenName.InviteFriends))
val text = getString(R.string.invite_friends_text, permalink)

View File

@ -61,7 +61,6 @@ import im.vector.app.features.workers.signout.ServerBackupStatusAction
import im.vector.app.features.workers.signout.ServerBackupStatusViewModel
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.crypto.model.DeviceInfo
import org.matrix.android.sdk.api.session.room.model.RoomSummary
import javax.inject.Inject
@ -80,7 +79,6 @@ class NewHomeDetailFragment :
@Inject lateinit var callManager: WebRtcCallManager
@Inject lateinit var vectorPreferences: VectorPreferences
@Inject lateinit var spaceStateHandler: SpaceStateHandler
@Inject lateinit var session: Session
@Inject lateinit var buildMeta: BuildMeta
private val viewModel: HomeDetailViewModel by fragmentViewModel()

View File

@ -169,6 +169,7 @@ import im.vector.app.features.notifications.NotificationDrawerManager
import im.vector.app.features.notifications.NotificationUtils
import im.vector.app.features.permalink.NavigationInterceptor
import im.vector.app.features.permalink.PermalinkHandler
import im.vector.app.features.permalink.PermalinkUseCase
import im.vector.app.features.poll.PollMode
import im.vector.app.features.reactions.EmojiReactionPickerActivity
import im.vector.app.features.roomprofile.RoomProfileActivity
@ -247,6 +248,7 @@ class TimelineFragment :
@Inject lateinit var clock: Clock
@Inject lateinit var vectorFeatures: VectorFeatures
@Inject lateinit var galleryOrCameraDialogHelperFactory: GalleryOrCameraDialogHelperFactory
@Inject lateinit var permalinkUseCase: PermalinkUseCase
companion object {
const val MAX_TYPING_MESSAGE_USERS_COUNT = 4
@ -867,7 +869,7 @@ class TimelineFragment :
}
R.id.menu_thread_timeline_copy_link -> {
getRootThreadEventId()?.let {
val permalink = session.permalinkService().createPermalink(timelineArgs.roomId, it)
val permalink = permalinkUseCase.createPermalink(timelineArgs.roomId, it)
copyToClipboard(requireContext(), permalink, false)
showSnackWithMessage(getString(R.string.copied_to_clipboard))
}
@ -879,7 +881,7 @@ class TimelineFragment :
}
R.id.menu_thread_timeline_share -> {
getRootThreadEventId()?.let {
val permalink = session.permalinkService().createPermalink(timelineArgs.roomId, it)
val permalink = permalinkUseCase.createPermalink(timelineArgs.roomId, it)
shareText(requireContext(), permalink)
}
true
@ -1788,7 +1790,7 @@ class TimelineFragment :
}
}
is EventSharedAction.CopyPermalink -> {
val permalink = session.permalinkService().createPermalink(timelineArgs.roomId, action.eventId)
val permalink = permalinkUseCase.createPermalink(timelineArgs.roomId, action.eventId)
copyToClipboard(requireContext(), permalink, false)
showSnackWithMessage(getString(R.string.copied_to_clipboard))
}

View File

@ -0,0 +1,39 @@
/*
* Copyright 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.permalink
import org.matrix.android.sdk.api.session.Session
import javax.inject.Inject
/**
* Contains synchronous methods to create permalinks from the Session.
*/
class PermalinkUseCase @Inject constructor(
private val session: Session,
) {
fun createPermalinkOfCurrentUser(): String? {
return createPermalink(session.myUserId)
}
fun createPermalink(id: String): String? {
return session.permalinkService().createPermalink(id)
}
fun createPermalink(roomId: String, eventId: String): String {
return session.permalinkService().createPermalink(roomId, eventId)
}
}

View File

@ -38,11 +38,11 @@ import im.vector.app.databinding.FragmentPublicRoomsBinding
import im.vector.app.features.analytics.plan.ViewRoom
import im.vector.app.features.permalink.NavigationInterceptor
import im.vector.app.features.permalink.PermalinkHandler
import im.vector.app.features.permalink.PermalinkUseCase
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.room.model.roomdirectory.PublicRoom
import reactivecircus.flowbinding.appcompat.queryTextChanges
import timber.log.Timber
@ -60,7 +60,7 @@ class PublicRoomsFragment :
@Inject lateinit var publicRoomsController: PublicRoomsController
@Inject lateinit var permalinkHandler: PermalinkHandler
@Inject lateinit var session: Session
@Inject lateinit var permalinkUseCase: PermalinkUseCase
private val viewModel: RoomDirectoryViewModel by activityViewModel()
private lateinit var sharedActionViewModel: RoomDirectorySharedActionViewModel
@ -128,7 +128,7 @@ class PublicRoomsFragment :
override fun onUnknownRoomClicked(roomIdOrAlias: String) {
viewLifecycleOwner.lifecycleScope.launch {
val permalink = session.permalinkService().createPermalink(roomIdOrAlias)
val permalink = permalinkUseCase.createPermalink(roomIdOrAlias)
val isHandled = permalinkHandler
.launch(requireActivity(), permalink, object : NavigationInterceptor {
override fun navToRoom(roomId: String?, eventId: String?, deepLink: Uri?, rootThreadEventId: String?): Boolean {