Code review

This commit is contained in:
Valere 2021-07-20 10:36:44 +02:00
parent 8faee24d00
commit e8d01becfe
4 changed files with 117 additions and 38 deletions

View file

@ -33,7 +33,6 @@ import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import im.vector.app.AppStateHandler
import im.vector.app.R
import im.vector.app.RoomGroupingMethod
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.di.ScreenComponent
import im.vector.app.core.extensions.exhaustive
@ -71,13 +70,8 @@ import im.vector.app.features.workers.signout.ServerBackupStatusViewState
import im.vector.app.push.fcm.FcmHelper
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.parcelize.Parcelize
import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.session.events.model.EventType
import org.matrix.android.sdk.api.session.events.model.toModel
import org.matrix.android.sdk.api.session.initsync.InitialSyncProgressService
import org.matrix.android.sdk.api.session.permalinks.PermalinkService
import org.matrix.android.sdk.api.session.room.model.PowerLevelsContent
import org.matrix.android.sdk.api.session.room.powerlevels.PowerLevelsHelper
import org.matrix.android.sdk.api.util.MatrixItem
import org.matrix.android.sdk.internal.session.sync.InitialSyncStrategy
import org.matrix.android.sdk.internal.session.sync.initialSyncStrategy
@ -96,6 +90,7 @@ class HomeActivity :
UnknownDeviceDetectorSharedViewModel.Factory,
ServerBackupStatusViewModel.Factory,
UnreadMessagesSharedViewModel.Factory,
PromoteRestrictedViewModel.Factory,
NavigationInterceptor,
SpaceInviteBottomSheet.InteractionListener {
@ -106,6 +101,8 @@ class HomeActivity :
private val serverBackupStatusViewModel: ServerBackupStatusViewModel by viewModel()
@Inject lateinit var serverBackupviewModelFactory: ServerBackupStatusViewModel.Factory
@Inject lateinit var promoteRestrictedViewModelFactory: PromoteRestrictedViewModel.Factory
private val promoteRestrictedViewModel: PromoteRestrictedViewModel by viewModel()
@Inject lateinit var activeSessionHolder: ActiveSessionHolder
@Inject lateinit var vectorUncaughtExceptionHandler: VectorUncaughtExceptionHandler
@ -179,30 +176,6 @@ class HomeActivity :
replaceFragment(R.id.homeDetailFragmentContainer, HomeDetailFragment::class.java)
replaceFragment(R.id.homeDrawerFragmentContainer, HomeDrawerFragment::class.java)
}
if (!vectorPreferences.didPromoteNewRestrictedFeature()) {
appStateHandler.selectedRoomGroupingObservable.subscribe {
(it.orNull() as? RoomGroupingMethod.BySpace)?.spaceSummary?.let { currentSpaceSummary ->
if (!currentSpaceSummary.isPublic && currentSpaceSummary.otherMemberIds.isNotEmpty()) {
val isAdmin = activeSessionHolder
.getSafeActiveSession()
?.getRoom(currentSpaceSummary.roomId)
?.getStateEvent(EventType.STATE_ROOM_POWER_LEVELS, QueryStringValue.NoCondition)
?.content?.toModel<PowerLevelsContent>()
?.let { PowerLevelsHelper(it) }
?.isUserAllowedToSend(activeSessionHolder.getActiveSession().myUserId, true, EventType.STATE_SPACE_CHILD)
?: false
// It's a private space with some members show this once
if (isAdmin && !popupAlertManager.hasAlertsToShow()) {
if (!vectorPreferences.didPromoteNewRestrictedFeature()) {
vectorPreferences.setDidPromoteNewRestrictedFeature()
RestrictedPromoBottomSheet().show(supportFragmentManager, "FOO")
}
}
}
}
}.disposeOnDestroy()
}
sharedActionViewModel
.observe()
@ -270,6 +243,21 @@ class HomeActivity :
shortcutsHandler.observeRoomsAndBuildShortcuts()
.disposeOnDestroy()
if (!vectorPreferences.didPromoteNewRestrictedFeature()) {
promoteRestrictedViewModel.subscribe(this) {
if (it.activeSpaceSummary != null && !it.activeSpaceSummary.isPublic
&& it.activeSpaceSummary.otherMemberIds.isNotEmpty()) {
// It's a private space with some members show this once
if (it.canUserManageSpace && !popupAlertManager.hasAlertsToShow()) {
if (!vectorPreferences.didPromoteNewRestrictedFeature()) {
vectorPreferences.setDidPromoteNewRestrictedFeature()
RestrictedPromoBottomSheet().show(supportFragmentManager, "FOO")
}
}
}
}
}
if (isFirstCreation()) {
handleIntent(intent)
}
@ -576,4 +564,6 @@ class HomeActivity :
private const val ROOM_LINK_PREFIX = "${MATRIX_TO_CUSTOM_SCHEME_URL_BASE}room/"
private const val USER_LINK_PREFIX = "${MATRIX_TO_CUSTOM_SCHEME_URL_BASE}user/"
}
override fun create(initialState: ActiveSpaceViewState) = promoteRestrictedViewModelFactory.create(initialState)
}

View file

@ -0,0 +1,92 @@
/*
* Copyright (c) 2021 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
import com.airbnb.mvrx.ActivityViewModelContext
import com.airbnb.mvrx.FragmentViewModelContext
import com.airbnb.mvrx.MvRxState
import com.airbnb.mvrx.MvRxViewModelFactory
import com.airbnb.mvrx.ViewModelContext
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import im.vector.app.AppStateHandler
import im.vector.app.RoomGroupingMethod
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.platform.EmptyAction
import im.vector.app.core.platform.EmptyViewEvents
import im.vector.app.core.platform.VectorViewModel
import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.session.events.model.EventType
import org.matrix.android.sdk.api.session.events.model.toModel
import org.matrix.android.sdk.api.session.room.model.PowerLevelsContent
import org.matrix.android.sdk.api.session.room.model.RoomSummary
import org.matrix.android.sdk.api.session.room.powerlevels.PowerLevelsHelper
data class ActiveSpaceViewState(
val isInSpaceMode: Boolean = false,
val activeSpaceSummary: RoomSummary? = null,
val canUserManageSpace: Boolean = false
) : MvRxState
class PromoteRestrictedViewModel @AssistedInject constructor(
@Assisted initialState: ActiveSpaceViewState,
private val activeSessionHolder: ActiveSessionHolder,
private val appStateHandler: AppStateHandler
) : VectorViewModel<ActiveSpaceViewState, EmptyAction, EmptyViewEvents>(initialState) {
init {
appStateHandler.selectedRoomGroupingObservable.distinctUntilChanged().execute {
val groupingMethod = it.invoke()?.orNull()
val isSpaceMode = groupingMethod is RoomGroupingMethod.BySpace
val currentSpace = (groupingMethod as? RoomGroupingMethod.BySpace)?.spaceSummary
val canManage = currentSpace?.roomId?.let { roomId ->
activeSessionHolder.getSafeActiveSession()
?.getRoom(currentSpace.roomId)
?.getStateEvent(EventType.STATE_ROOM_POWER_LEVELS, QueryStringValue.NoCondition)
?.content?.toModel<PowerLevelsContent>()?.let {
PowerLevelsHelper(it).isUserAllowedToSend(activeSessionHolder.getActiveSession().myUserId, true, EventType.STATE_SPACE_CHILD)
} ?: false
} ?: false
copy(
isInSpaceMode = isSpaceMode,
activeSpaceSummary = currentSpace,
canUserManageSpace = canManage
)
}
}
@AssistedFactory
interface Factory {
fun create(initialState: ActiveSpaceViewState): PromoteRestrictedViewModel
}
companion object : MvRxViewModelFactory<PromoteRestrictedViewModel, ActiveSpaceViewState> {
@JvmStatic
override fun create(viewModelContext: ViewModelContext, state: ActiveSpaceViewState): PromoteRestrictedViewModel? {
val factory = when (viewModelContext) {
is FragmentViewModelContext -> viewModelContext.fragment as? Factory
is ActivityViewModelContext -> viewModelContext.activity as? Factory
}
return factory?.create(state) ?: error("You should let your activity/fragment implements Factory interface")
}
}
override fun handle(action: EmptyAction) {}
}

View file

@ -32,7 +32,7 @@ class RestrictedPromoBottomSheet : VectorBaseBottomSheetDialogFragment<BottomShe
override val showExpanded = true
var learnMorelMode: Boolean = false
var learnMoreMode: Boolean = false
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
@ -42,24 +42,23 @@ class RestrictedPromoBottomSheet : VectorBaseBottomSheetDialogFragment<BottomShe
}
views.learnMore.debouncedClicks {
if (learnMorelMode) {
if (learnMoreMode) {
dismiss()
} else {
learnMorelMode = true
learnMoreMode = true
render()
}
}
}
private fun render() {
if (learnMorelMode) {
if (learnMoreMode) {
views.title.text = getString(R.string.new_let_people_in_spaces_find_and_join)
views.topDescription.text = getString(R.string.to_help_space_members_find_and_join)
views.imageHint.isVisible = true
views.bottomDescription.isVisible = true
views.bottomDescription.text = getString(R.string.this_makes_it_easy_for_rooms_to_stay_private_to_a_space)
views.skipButton.isVisible = false
views.learnMore.isVisible = true
views.learnMore.text = getString(R.string.ok)
} else {
views.title.text = getString(R.string.help_space_members)
@ -68,9 +67,7 @@ class RestrictedPromoBottomSheet : VectorBaseBottomSheetDialogFragment<BottomShe
views.bottomDescription.isVisible = false
views.skipButton.isVisible = true
views.learnMore.isVisible = true
views.skipButton.text = getString(R.string.skip)
views.learnMore.text = getString(R.string.learn_more)
views.learnMore.isVisible = true
}
}
}

View file

@ -72,7 +72,7 @@
<Button
android:id="@+id/skipButton"
style="@style/Widget.Vector.Button.Outlined"
style="@style/Widget.Vector.Button.Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"