add radioItem, RoomNotificationSettingsController and render it in fragment

This commit is contained in:
David Langley 2021-07-23 14:16:32 +01:00
parent 9811d6fefc
commit df7e6bd00d
13 changed files with 281 additions and 52 deletions

View file

@ -106,6 +106,7 @@ import im.vector.app.features.roomprofile.RoomProfileFragment
import im.vector.app.features.roomprofile.alias.RoomAliasFragment
import im.vector.app.features.roomprofile.banned.RoomBannedMemberListFragment
import im.vector.app.features.roomprofile.members.RoomMemberListFragment
import im.vector.app.features.roomprofile.notifications.RoomNotificationSettingsFragment
import im.vector.app.features.roomprofile.permissions.RoomPermissionsFragment
import im.vector.app.features.roomprofile.settings.RoomSettingsFragment
import im.vector.app.features.roomprofile.uploads.RoomUploadsFragment
@ -715,6 +716,12 @@ interface FragmentModule {
@FragmentKey(RoomBannedMemberListFragment::class)
fun bindRoomBannedMemberListFragment(fragment: RoomBannedMemberListFragment): Fragment
@Binds
@IntoMap
@FragmentKey(RoomNotificationSettingsFragment::class)
fun bindRoomNotificationSettingsFragment(fragment: RoomNotificationSettingsFragment): Fragment
@Binds
@IntoMap
@FragmentKey(SearchFragment::class)

View file

@ -0,0 +1,68 @@
/*
* 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.core.epoxy
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import com.airbnb.epoxy.EpoxyAttribute
import com.airbnb.epoxy.EpoxyModelClass
import im.vector.app.R
/**
* A action for bottom sheet.
*/
@EpoxyModelClass(layout = R.layout.item_radio)
abstract class RadioButtonItem : VectorEpoxyModel<RadioButtonItem.Holder>() {
@EpoxyAttribute
var title: CharSequence? = null
@StringRes
@EpoxyAttribute
var titleRes: Int? = null
@EpoxyAttribute
var selected = false
@EpoxyAttribute(EpoxyAttribute.Option.DoNotHash)
lateinit var listener: ClickListener
override fun bind(holder: Holder) {
super.bind(holder)
holder.view.onClick(listener)
if (titleRes != null) {
holder.titleText.setText(titleRes!!)
} else {
holder.titleText.text = title
}
if (selected) {
holder.radioImage.setImageDrawable(ContextCompat.getDrawable(holder.view.context, R.drawable.ic_radio_on))
holder.radioImage.contentDescription = holder.view.context.getString(R.string.a11y_checked)
} else {
holder.radioImage.setImageDrawable(ContextCompat.getDrawable(holder.view.context, R.drawable.ic_radio_off))
holder.radioImage.contentDescription = holder.view.context.getString(R.string.a11y_unchecked)
}
}
class Holder : VectorEpoxyHolder() {
val titleText by bind<TextView>(R.id.actionTitle)
val radioImage by bind<ImageView>(R.id.radioIcon)
}
}

View file

@ -39,6 +39,7 @@ import im.vector.app.features.roomprofile.banned.RoomBannedMemberListFragment
import im.vector.app.features.roomprofile.members.RoomMemberListFragment
import im.vector.app.features.roomprofile.settings.RoomSettingsFragment
import im.vector.app.features.roomprofile.alias.RoomAliasFragment
import im.vector.app.features.roomprofile.notifications.RoomNotificationSettingsFragment
import im.vector.app.features.roomprofile.permissions.RoomPermissionsFragment
import im.vector.app.features.roomprofile.uploads.RoomUploadsFragment
import javax.inject.Inject
@ -107,12 +108,13 @@ class RoomProfileActivity :
.observe()
.subscribe { sharedAction ->
when (sharedAction) {
RoomProfileSharedAction.OpenRoomMembers -> openRoomMembers()
RoomProfileSharedAction.OpenRoomSettings -> openRoomSettings()
RoomProfileSharedAction.OpenRoomAliasesSettings -> openRoomAlias()
RoomProfileSharedAction.OpenRoomPermissionsSettings -> openRoomPermissions()
RoomProfileSharedAction.OpenRoomUploads -> openRoomUploads()
RoomProfileSharedAction.OpenBannedRoomMembers -> openBannedRoomMembers()
RoomProfileSharedAction.OpenRoomMembers -> openRoomMembers()
RoomProfileSharedAction.OpenRoomSettings -> openRoomSettings()
RoomProfileSharedAction.OpenRoomAliasesSettings -> openRoomAlias()
RoomProfileSharedAction.OpenRoomPermissionsSettings -> openRoomPermissions()
RoomProfileSharedAction.OpenRoomUploads -> openRoomUploads()
RoomProfileSharedAction.OpenBannedRoomMembers -> openBannedRoomMembers()
RoomProfileSharedAction.OpenRoomNotificaitonSettings -> openRoomNotificationSettings()
}.exhaustive
}
.disposeOnDestroy()
@ -162,6 +164,10 @@ class RoomProfileActivity :
addFragmentToBackstack(R.id.simpleFragmentContainer, RoomBannedMemberListFragment::class.java, roomProfileArgs)
}
private fun openRoomNotificationSettings() {
addFragmentToBackstack(R.id.simpleFragmentContainer, RoomNotificationSettingsFragment::class.java, roomProfileArgs)
}
override fun configure(toolbar: MaterialToolbar) {
configureToolbar(toolbar)
}

View file

@ -30,6 +30,7 @@ import com.airbnb.mvrx.args
import com.airbnb.mvrx.fragmentViewModel
import com.airbnb.mvrx.withState
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import im.vector.app.BuildConfig
import im.vector.app.R
import im.vector.app.core.animations.AppBarStateChangeListener
import im.vector.app.core.animations.MatrixItemAppBarStateChangeListener
@ -253,9 +254,14 @@ class RoomProfileFragment @Inject constructor(
}
override fun onNotificationsClicked() {
RoomListQuickActionsBottomSheet
.newInstance(roomProfileArgs.roomId, RoomListActionsArgs.Mode.NOTIFICATIONS)
.show(childFragmentManager, "ROOM_PROFILE_NOTIFICATIONS")
// TODO: Use BuildConfig here when merged in
if (false) {
RoomListQuickActionsBottomSheet
.newInstance(roomProfileArgs.roomId, RoomListActionsArgs.Mode.NOTIFICATIONS)
.show(childFragmentManager, "ROOM_PROFILE_NOTIFICATIONS")
} else {
roomProfileSharedActionViewModel.post(RoomProfileSharedAction.OpenRoomNotificaitonSettings)
}
}
override fun onUploadsClicked() {

View file

@ -28,4 +28,5 @@ sealed class RoomProfileSharedAction : VectorSharedAction {
object OpenRoomUploads : RoomProfileSharedAction()
object OpenRoomMembers : RoomProfileSharedAction()
object OpenBannedRoomMembers : RoomProfileSharedAction()
object OpenRoomNotificaitonSettings : RoomProfileSharedAction()
}

View file

@ -21,5 +21,4 @@ import org.matrix.android.sdk.api.session.room.notification.RoomNotificationStat
sealed class RoomNotificationSettingsAction : VectorViewModelAction {
data class SelectNotificationState(val notificationState: RoomNotificationState): RoomNotificationSettingsAction()
object Save: RoomNotificationSettingsAction()
}

View file

@ -0,0 +1,62 @@
/*
* 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.roomprofile.notifications
import androidx.annotation.StringRes
import com.airbnb.epoxy.TypedEpoxyController
import im.vector.app.R
import im.vector.app.core.epoxy.radioButtonItem
import org.matrix.android.sdk.api.session.room.notification.RoomNotificationState
import javax.inject.Inject
class RoomNotificationSettingsController @Inject constructor() : TypedEpoxyController<RoomNotificationSettingsViewState>() {
interface Callback {
fun didSelectRoomNotificationState(roomNotificationState: RoomNotificationState)
}
var callback: Callback? = null
init {
setData(null)
}
@StringRes
private fun titleForNotificationState(notificationState: RoomNotificationState) = when(notificationState) {
RoomNotificationState.ALL_MESSAGES_NOISY -> R.string.room_settings_all_messages_noisy
RoomNotificationState.ALL_MESSAGES -> R.string.room_settings_all_messages
RoomNotificationState.MENTIONS_ONLY -> R.string.room_settings_mention_only
RoomNotificationState.MUTE -> R.string.room_settings_mute
}
override fun buildModels(data: RoomNotificationSettingsViewState?) {
val host = this
data ?: return
data.notificationOptions.forEach { notificationState ->
val title = titleForNotificationState(notificationState)
radioButtonItem {
id(notificationState.name)
titleRes(title)
selected(data.notificationState() == notificationState)
listener {
host.callback?.didSelectRoomNotificationState(notificationState)
}
}
}
}
}

View file

@ -20,40 +20,56 @@ 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.fragmentViewModel
import com.airbnb.mvrx.withState
import im.vector.app.R
import im.vector.app.core.extensions.configureWith
import im.vector.app.core.platform.VectorBaseFragment
import im.vector.app.databinding.ViewEditRoomNotificationSettingsBinding
import im.vector.app.features.home.AvatarRenderer
import im.vector.app.databinding.FragmentRoomSettingGenericBinding
import org.matrix.android.sdk.api.session.room.notification.RoomNotificationState
import javax.inject.Inject
/**
* In this screen:
* - the account has been created and we propose the user to set an avatar and a display name
*/
class RoomNotificationSettingsFragment @Inject constructor(
val roomNotificationSettingsViewModel: RoomNotificationSettingsViewModel.Factory,
private val avatarRenderer: AvatarRenderer,
) : VectorBaseFragment<ViewEditRoomNotificationSettingsBinding>() {
val roomNotificationSettingsController: RoomNotificationSettingsController
) : VectorBaseFragment<FragmentRoomSettingGenericBinding>(),
RoomNotificationSettingsController.Callback {
private val viewModel: RoomNotificationSettingsViewModel by fragmentViewModel()
override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): ViewEditRoomNotificationSettingsBinding {
return ViewEditRoomNotificationSettingsBinding.inflate(inflater, container, false)
override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentRoomSettingGenericBinding {
return FragmentRoomSettingGenericBinding.inflate(inflater, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupToolbar(views.roomSettingsToolbar)
views.roomSettingsToolbarTitleView.setText(R.string.settings_notifications)
roomNotificationSettingsController.callback = this
views.roomSettingsRecyclerView.configureWith(roomNotificationSettingsController, hasFixedSize = true)
setupWaitingView()
observeViewEvents()
}
private fun setupWaitingView() {
views.waitingView.waitingStatusText.setText(R.string.please_wait)
views.waitingView.waitingStatusText.isVisible = true
}
private fun observeViewEvents() {
viewModel.observeViewEvents {
when (it) {
is RoomNotificationSettingsViewEvents.Failure -> displayErrorDialog(it.throwable)
RoomNotificationSettingsViewEvents.SaveComplete -> TODO()
}
}
}
override fun invalidate() = withState(viewModel) { viewState ->
roomNotificationSettingsController.setData(viewState)
views.waitingView.root.isVisible = viewState.isLoading
}
override fun didSelectRoomNotificationState(roomNotificationState: RoomNotificationState) {
viewModel.handle(RoomNotificationSettingsAction.SelectNotificationState(roomNotificationState))
}
}

View file

@ -20,5 +20,4 @@ import im.vector.app.core.platform.VectorViewEvents
sealed class RoomNotificationSettingsViewEvents : VectorViewEvents {
data class Failure(val throwable: Throwable) : RoomNotificationSettingsViewEvents()
object SaveComplete : RoomNotificationSettingsViewEvents()
}

View file

@ -19,18 +19,19 @@ package im.vector.app.features.roomprofile.notifications
import androidx.lifecycle.viewModelScope
import com.airbnb.mvrx.FragmentViewModelContext
import com.airbnb.mvrx.MvRxViewModelFactory
import com.airbnb.mvrx.Success
import com.airbnb.mvrx.ViewModelContext
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import im.vector.app.core.platform.VectorViewModel
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.session.room.Room
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.rx.rx
class RoomNotificationSettingsViewModel @AssistedInject constructor(
@Assisted initialState: RoomNotificationSettingsViewState,
private val room: Room
session: Session
) : VectorViewModel<RoomNotificationSettingsViewState, RoomNotificationSettingsAction, RoomNotificationSettingsViewEvents>(initialState) {
@AssistedFactory
@ -41,50 +42,46 @@ class RoomNotificationSettingsViewModel @AssistedInject constructor(
companion object : MvRxViewModelFactory<RoomNotificationSettingsViewModel, RoomNotificationSettingsViewState> {
@JvmStatic
override fun create(viewModelContext: ViewModelContext, state: RoomNotificationSettingsViewState): RoomNotificationSettingsViewModel? {
override fun create(viewModelContext: ViewModelContext, state: RoomNotificationSettingsViewState): RoomNotificationSettingsViewModel {
val fragment: RoomNotificationSettingsFragment = (viewModelContext as FragmentViewModelContext).fragment()
return fragment.roomNotificationSettingsViewModel.create(state)
}
}
private val room = session.getRoom(initialState.roomId)!!
init {
initEncrypted()
observeNotificationState()
}
private fun initEncrypted() {
setState {
copy(roomEncrypted = room.isEncrypted())
}
}
private fun observeNotificationState() {
room.rx()
.liveNotificationState()
.subscribe{
setState {
copy(notificationState = it )
}
.execute {
copy(notificationState = it)
}
.disposeOnClear()
}
override fun handle(action: RoomNotificationSettingsAction) {
when (action) {
is RoomNotificationSettingsAction.SelectNotificationState -> handleSelectNotificationState(action)
is RoomNotificationSettingsAction.Save -> handleSaveNotificationSelection(action)
}
}
private fun handleSelectNotificationState(action: RoomNotificationSettingsAction.SelectNotificationState) {
setState {
copy(notificationState = action.notificationState)
}
}
private fun handleSaveNotificationSelection(action: RoomNotificationSettingsAction.Save) {
setState { copy(isLoading = true) }
withState { state ->
viewModelScope.launch {
runCatching { room.setRoomNotificationState(state.notificationState) }
.onFailure { _viewEvents.post(RoomNotificationSettingsViewEvents.Failure(it)) }
setState {
copy(isLoading = false)
}
_viewEvents.post(RoomNotificationSettingsViewEvents.SaveComplete)
viewModelScope.launch {
runCatching { room.setRoomNotificationState(action.notificationState) }
.onFailure { _viewEvents.post(RoomNotificationSettingsViewEvents.Failure(it)) }
setState {
copy(isLoading = false, notificationState = Success(action.notificationState))
}
}
}

View file

@ -16,15 +16,22 @@
package im.vector.app.features.roomprofile.notifications
import com.airbnb.mvrx.Async
import com.airbnb.mvrx.MvRxState
import com.airbnb.mvrx.Uninitialized
import im.vector.app.features.roomprofile.RoomProfileArgs
import org.matrix.android.sdk.api.session.room.notification.RoomNotificationState
data class RoomNotificationSettingsViewState(
val isLoading: Boolean,
val roomEncrypted: Boolean,
val notificationState: RoomNotificationState,
val avatarData: AvatarData?
) : MvRxState
val roomId: String,
val isLoading: Boolean = false,
val roomEncrypted: Boolean = false,
val notificationState: Async<RoomNotificationState> = Uninitialized,
val avatarData: AvatarData? = null
) : MvRxState {
constructor(args: RoomProfileArgs) : this(roomId = args.roomId)
}
data class AvatarData (
val displayName: String,
@ -34,7 +41,7 @@ data class AvatarData (
val RoomNotificationSettingsViewState.notificationOptions: List<RoomNotificationState>
get() {
return if (roomEncrypted) {
listOf(RoomNotificationState.ALL_MESSAGES, RoomNotificationState.MUTE)
listOf(RoomNotificationState.ALL_MESSAGES_NOISY, RoomNotificationState.ALL_MESSAGES, RoomNotificationState.MUTE)
} else
RoomNotificationState.values().asList()
}

View file

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:minHeight="64dp"
android:foreground="?attr/selectableItemBackground"
android:paddingStart="@dimen/layout_horizontal_margin"
android:paddingEnd="@dimen/layout_horizontal_margin">
<TextView
android:id="@+id/actionTitle"
style="@style/Widget.Vector.TextView.Subtitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/layout_horizontal_margin"
android:ellipsize="end"
android:maxLines="2"
android:textColor="?vctr_content_primary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/radioIcon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="@string/room_settings_all_messages" />
<ImageView
android:id="@+id/radioIcon"
android:layout_width="20dp"
android:layout_height="20dp"
android:contentDescription="@string/a11y_checked"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="MissingPrefix"
tools:src="@drawable/ic_radio_on" />
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -10,5 +10,26 @@
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/allMesssagesRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/room_settings_all_messages"
android:textColor="?vctr_content_primary" />
<RadioButton
android:id="@+id/mentionsOnlyRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/room_settings_mention_only"
android:textColor="?vctr_content_primary" />
<RadioButton
android:id="@+id/muteRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/room_settings_mute"
android:textColor="?vctr_content_primary" />
</RadioGroup>
</LinearLayout>