Check if user has enough power level to change room avatar.

This commit is contained in:
onurays 2020-06-24 11:07:51 +03:00 committed by Benoit Marty
parent 8787f5d920
commit a03f69fb98
4 changed files with 24 additions and 9 deletions

View File

@ -77,7 +77,7 @@ class BigImageViewerActivity : VectorBaseActivity(), AvatarSelectorView.Callback
} }
override fun onPrepareOptionsMenu(menu: Menu): Boolean { override fun onPrepareOptionsMenu(menu: Menu): Boolean {
menu.findItem(R.id.bigAvatarEditAction).isVisible = uri != null menu.findItem(R.id.bigAvatarEditAction).isVisible = shouldShowEditAction()
return super.onPrepareOptionsMenu(menu) return super.onPrepareOptionsMenu(menu)
} }
@ -89,6 +89,10 @@ class BigImageViewerActivity : VectorBaseActivity(), AvatarSelectorView.Callback
return super.onOptionsItemSelected(item) return super.onOptionsItemSelected(item)
} }
private fun shouldShowEditAction(): Boolean {
return uri != null && intent.getBooleanExtra(EXTRA_CAN_EDIT_IMAGE, false)
}
private fun showAvatarSelector() { private fun showAvatarSelector() {
if (!::avatarSelector.isInitialized) { if (!::avatarSelector.isInitialized) {
avatarSelector = AvatarSelectorView(this, layoutInflater, this) avatarSelector = AvatarSelectorView(this, layoutInflater, this)
@ -180,12 +184,14 @@ class BigImageViewerActivity : VectorBaseActivity(), AvatarSelectorView.Callback
companion object { companion object {
private const val EXTRA_TITLE = "EXTRA_TITLE" private const val EXTRA_TITLE = "EXTRA_TITLE"
private const val EXTRA_IMAGE_URL = "EXTRA_IMAGE_URL" private const val EXTRA_IMAGE_URL = "EXTRA_IMAGE_URL"
private const val EXTRA_CAN_EDIT_IMAGE = "EXTRA_CAN_EDIT_IMAGE"
const val REQUEST_CODE = 1000 const val REQUEST_CODE = 1000
fun newIntent(context: Context, title: String?, imageUrl: String): Intent { fun newIntent(context: Context, title: String?, imageUrl: String, canEditImage: Boolean = false): Intent {
return Intent(context, BigImageViewerActivity::class.java).apply { return Intent(context, BigImageViewerActivity::class.java).apply {
putExtra(EXTRA_TITLE, title) putExtra(EXTRA_TITLE, title)
putExtra(EXTRA_IMAGE_URL, imageUrl) putExtra(EXTRA_IMAGE_URL, imageUrl)
putExtra(EXTRA_CAN_EDIT_IMAGE, canEditImage)
} }
} }
} }

View File

@ -241,14 +241,13 @@ class RoomProfileFragment @Inject constructor(
startSharePlainTextIntent(fragment = this, chooserTitle = null, text = permalink) startSharePlainTextIntent(fragment = this, chooserTitle = null, text = permalink)
} }
private fun onAvatarClicked(view: View, matrixItem: MatrixItem.RoomItem) { private fun onAvatarClicked(view: View, matrixItem: MatrixItem.RoomItem) = withState(roomProfileViewModel) {
if (matrixItem.avatarUrl.isNullOrEmpty()) { if (matrixItem.avatarUrl?.isNotEmpty() == true) {
showAvatarSelector() val intent = BigImageViewerActivity.newIntent(requireContext(), matrixItem.getBestName(), matrixItem.avatarUrl!!, it.canChangeAvatar)
} else {
val intent = BigImageViewerActivity.newIntent(requireContext(), matrixItem.getBestName(), matrixItem.avatarUrl!!)
val options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity!!, view, ViewCompat.getTransitionName(view) ?: "") val options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity!!, view, ViewCompat.getTransitionName(view) ?: "")
startActivityForResult(intent, BigImageViewerActivity.REQUEST_CODE, options.toBundle()) startActivityForResult(intent, BigImageViewerActivity.REQUEST_CODE, options.toBundle())
} else if (it.canChangeAvatar) {
showAvatarSelector()
} }
} }

View File

@ -25,11 +25,13 @@ import com.squareup.inject.assisted.AssistedInject
import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.permalinks.PermalinkFactory import im.vector.matrix.android.api.permalinks.PermalinkFactory
import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.Session
import im.vector.matrix.android.api.session.room.powerlevels.PowerLevelsHelper
import im.vector.matrix.rx.rx import im.vector.matrix.rx.rx
import im.vector.matrix.rx.unwrap import im.vector.matrix.rx.unwrap
import im.vector.riotx.R import im.vector.riotx.R
import im.vector.riotx.core.platform.VectorViewModel import im.vector.riotx.core.platform.VectorViewModel
import im.vector.riotx.core.resources.StringProvider import im.vector.riotx.core.resources.StringProvider
import im.vector.riotx.features.powerlevel.PowerLevelsObservableFactory
import java.util.UUID import java.util.UUID
class RoomProfileViewModel @AssistedInject constructor(@Assisted private val initialState: RoomProfileViewState, class RoomProfileViewModel @AssistedInject constructor(@Assisted private val initialState: RoomProfileViewState,
@ -63,6 +65,13 @@ class RoomProfileViewModel @AssistedInject constructor(@Assisted private val ini
.execute { .execute {
copy(roomSummary = it) copy(roomSummary = it)
} }
val powerLevelsContentLive = PowerLevelsObservableFactory(room).createObservable()
powerLevelsContentLive.subscribe {
val powerLevelsHelper = PowerLevelsHelper(it)
setState { copy(canChangeAvatar = powerLevelsHelper.isUserAbleToChangeRoomAvatar(session.myUserId)) }
}.disposeOnClear()
} }
override fun handle(action: RoomProfileAction) = when (action) { override fun handle(action: RoomProfileAction) = when (action) {

View File

@ -24,7 +24,8 @@ import im.vector.matrix.android.api.session.room.model.RoomSummary
data class RoomProfileViewState( data class RoomProfileViewState(
val roomId: String, val roomId: String,
val roomSummary: Async<RoomSummary> = Uninitialized val roomSummary: Async<RoomSummary> = Uninitialized,
val canChangeAvatar: Boolean = false
) : MvRxState { ) : MvRxState {
constructor(args: RoomProfileArgs) : this(roomId = args.roomId) constructor(args: RoomProfileArgs) : this(roomId = args.roomId)