mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-23 01:45:52 +03:00
Move logic to ViewModel
This commit is contained in:
parent
07d2a15cf8
commit
ddadefdbd6
5 changed files with 55 additions and 28 deletions
|
@ -28,4 +28,5 @@ sealed class RoomMemberProfileAction : VectorViewModelAction {
|
|||
object VerifyUser : RoomMemberProfileAction()
|
||||
object ShareRoomMemberProfile : RoomMemberProfileAction()
|
||||
data class SetPowerLevel(val previousValue: Int, val newValue: Int, val askForValidation: Boolean) : RoomMemberProfileAction()
|
||||
data class SetUserColorOverride(val newColor: String) : RoomMemberProfileAction()
|
||||
}
|
||||
|
|
|
@ -178,6 +178,7 @@ class RoomMemberProfileController @Inject constructor(
|
|||
id = "overrideColor",
|
||||
editable = false,
|
||||
title = stringProvider.getString(R.string.room_member_override_nick_color),
|
||||
subtitle = state.userColorOverride,
|
||||
divider = false,
|
||||
action = { callback?.onOverrideColorClicked() }
|
||||
)
|
||||
|
|
|
@ -24,7 +24,6 @@ import android.view.MenuItem
|
|||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.airbnb.mvrx.Fail
|
||||
import com.airbnb.mvrx.Incomplete
|
||||
import com.airbnb.mvrx.Success
|
||||
|
@ -56,10 +55,8 @@ import im.vector.app.features.home.room.detail.RoomDetailPendingActionStore
|
|||
import im.vector.app.features.home.room.detail.timeline.helper.MatrixItemColorProvider
|
||||
import im.vector.app.features.roommemberprofile.devices.DeviceListBottomSheet
|
||||
import im.vector.app.features.roommemberprofile.powerlevel.EditPowerLevelDialogs
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import org.matrix.android.sdk.api.crypto.RoomEncryptionTrustLevel
|
||||
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes
|
||||
import org.matrix.android.sdk.api.session.room.powerlevels.Role
|
||||
import org.matrix.android.sdk.api.util.MatrixItem
|
||||
import javax.inject.Inject
|
||||
|
@ -328,36 +325,20 @@ class RoomMemberProfileFragment @Inject constructor(
|
|||
navigator.openBigImageViewer(requireActivity(), view, userMatrixItem)
|
||||
}
|
||||
|
||||
override fun onOverrideColorClicked(): Unit = withState(viewModel) { state ->
|
||||
override fun onOverrideColorClicked(): Unit = withState(viewModel) { state ->
|
||||
val inflater = requireActivity().layoutInflater
|
||||
val layout = inflater.inflate(R.layout.dialog_base_edit_text, null)
|
||||
val views = DialogBaseEditTextBinding.bind(layout)
|
||||
val session = injector().activeSessionHolder().getActiveSession()
|
||||
val overrideColorsSetting = session.accountDataService().getUserAccountDataEvent(UserAccountDataTypes.TYPE_OVERRIDE_COLORS)
|
||||
val overrideColorSpecs = overrideColorsSetting?.content?.toMap().orEmpty()
|
||||
val userId = state.userId
|
||||
val overrideColorSpec : String? = overrideColorSpecs[userId]?.toString()
|
||||
views.editText.setText(overrideColorSpec)
|
||||
views.editText.setText(state.userColorOverride)
|
||||
views.editText.hint = "#000000"
|
||||
|
||||
MaterialAlertDialogBuilder(requireContext())
|
||||
.setTitle(R.string.room_member_override_nick_color)
|
||||
.setView(layout)
|
||||
.setPositiveButton(R.string.ok) { _, _ ->
|
||||
val newOverrideColorSpec = views.editText.text.toString()
|
||||
if (newOverrideColorSpec != overrideColorSpec) {
|
||||
val newOverrideColorSpecs = overrideColorSpecs.toMutableMap()
|
||||
if (matrixItemColorProvider.setOverrideColor(userId, newOverrideColorSpec)) {
|
||||
newOverrideColorSpecs[userId] = newOverrideColorSpec
|
||||
} else {
|
||||
newOverrideColorSpecs.remove(userId)
|
||||
}
|
||||
viewModel.viewModelScope.launch {
|
||||
session.accountDataService().updateUserAccountData(
|
||||
type = UserAccountDataTypes.TYPE_OVERRIDE_COLORS,
|
||||
content = newOverrideColorSpecs)
|
||||
}
|
||||
invalidate()
|
||||
val newColor = views.editText.text.toString()
|
||||
if (newColor != state.userColorOverride) {
|
||||
viewModel.handle(RoomMemberProfileAction.SetUserColorOverride(newColor))
|
||||
}
|
||||
}
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
|
|
|
@ -28,22 +28,27 @@ import dagger.assisted.AssistedInject
|
|||
import im.vector.app.R
|
||||
import im.vector.app.core.di.MavericksAssistedViewModelFactory
|
||||
import im.vector.app.core.di.hiltMavericksViewModelFactory
|
||||
import im.vector.app.core.extensions.exhaustive
|
||||
import im.vector.app.core.mvrx.runCatchingToAsync
|
||||
import im.vector.app.core.platform.VectorViewModel
|
||||
import im.vector.app.core.resources.StringProvider
|
||||
import im.vector.app.features.displayname.getBestName
|
||||
import im.vector.app.features.home.room.detail.timeline.helper.MatrixItemColorProvider
|
||||
import im.vector.app.features.powerlevel.PowerLevelsFlowFactory
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.mapNotNull
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.matrix.android.sdk.api.query.QueryStringValue
|
||||
import org.matrix.android.sdk.api.session.Session
|
||||
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes
|
||||
import org.matrix.android.sdk.api.session.events.model.EventType
|
||||
import org.matrix.android.sdk.api.session.events.model.toContent
|
||||
import org.matrix.android.sdk.api.session.events.model.toModel
|
||||
import org.matrix.android.sdk.api.session.profile.ProfileService
|
||||
import org.matrix.android.sdk.api.session.room.Room
|
||||
import org.matrix.android.sdk.api.session.room.members.roomMemberQueryParams
|
||||
|
@ -57,10 +62,12 @@ import org.matrix.android.sdk.api.util.toOptional
|
|||
import org.matrix.android.sdk.flow.flow
|
||||
import org.matrix.android.sdk.flow.unwrap
|
||||
|
||||
class RoomMemberProfileViewModel @AssistedInject constructor(@Assisted private val initialState: RoomMemberProfileViewState,
|
||||
private val stringProvider: StringProvider,
|
||||
private val session: Session) :
|
||||
VectorViewModel<RoomMemberProfileViewState, RoomMemberProfileAction, RoomMemberProfileViewEvents>(initialState) {
|
||||
class RoomMemberProfileViewModel @AssistedInject constructor(
|
||||
@Assisted private val initialState: RoomMemberProfileViewState,
|
||||
private val stringProvider: StringProvider,
|
||||
private val matrixItemColorProvider: MatrixItemColorProvider,
|
||||
private val session: Session
|
||||
) : VectorViewModel<RoomMemberProfileViewState, RoomMemberProfileAction, RoomMemberProfileViewEvents>(initialState) {
|
||||
|
||||
@AssistedFactory
|
||||
interface Factory : MavericksAssistedViewModelFactory<RoomMemberProfileViewModel, RoomMemberProfileViewState> {
|
||||
|
@ -85,6 +92,7 @@ class RoomMemberProfileViewModel @AssistedInject constructor(@Assisted private v
|
|||
)
|
||||
}
|
||||
observeIgnoredState()
|
||||
observeAccountData()
|
||||
viewModelScope.launch(Dispatchers.Main) {
|
||||
// Do we have a room member for this id.
|
||||
val roomMember = withContext(Dispatchers.Default) {
|
||||
|
@ -121,6 +129,24 @@ class RoomMemberProfileViewModel @AssistedInject constructor(@Assisted private v
|
|||
}
|
||||
}
|
||||
|
||||
private fun observeAccountData() {
|
||||
session.flow()
|
||||
.liveUserAccountData(setOf(UserAccountDataTypes.TYPE_OVERRIDE_COLORS))
|
||||
.mapNotNull { it.firstOrNull() }
|
||||
.map { it.content.toModel<Map<String, String>>() }
|
||||
.map { userColorAccountDataContent ->
|
||||
userColorAccountDataContent?.get(initialState.userId)
|
||||
}
|
||||
.onEach {
|
||||
setState {
|
||||
copy(
|
||||
userColorOverride = it
|
||||
)
|
||||
}
|
||||
}
|
||||
.launchIn(viewModelScope)
|
||||
}
|
||||
|
||||
private fun observeIgnoredState() {
|
||||
session.flow().liveIgnoredUsers()
|
||||
.map { ignored ->
|
||||
|
@ -143,6 +169,23 @@ class RoomMemberProfileViewModel @AssistedInject constructor(@Assisted private v
|
|||
is RoomMemberProfileAction.BanOrUnbanUser -> handleBanOrUnbanAction(action)
|
||||
is RoomMemberProfileAction.KickUser -> handleKickAction(action)
|
||||
RoomMemberProfileAction.InviteUser -> handleInviteAction()
|
||||
is RoomMemberProfileAction.SetUserColorOverride -> handleSetUserColorOverride(action)
|
||||
}.exhaustive
|
||||
}
|
||||
|
||||
private fun handleSetUserColorOverride(action: RoomMemberProfileAction.SetUserColorOverride) {
|
||||
val newOverrideColorSpecs = session.accountDataService().getUserAccountDataEvent(UserAccountDataTypes.TYPE_OVERRIDE_COLORS)
|
||||
?.content?.toMap().orEmpty().toMutableMap()
|
||||
if (matrixItemColorProvider.setOverrideColor(initialState.userId, action.newColor)) {
|
||||
newOverrideColorSpecs[initialState.userId] = action.newColor
|
||||
} else {
|
||||
newOverrideColorSpecs.remove(initialState.userId)
|
||||
}
|
||||
viewModelScope.launch {
|
||||
session.accountDataService().updateUserAccountData(
|
||||
type = UserAccountDataTypes.TYPE_OVERRIDE_COLORS,
|
||||
content = newOverrideColorSpecs
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -41,6 +41,7 @@ data class RoomMemberProfileViewState(
|
|||
val allDevicesAreCrossSignedTrusted: Boolean = false,
|
||||
val asyncMembership: Async<Membership> = Uninitialized,
|
||||
val hasReadReceipt: Boolean = false,
|
||||
val userColorOverride: String? = null,
|
||||
val actionPermissions: ActionPermissions = ActionPermissions()
|
||||
) : MavericksState {
|
||||
|
||||
|
|
Loading…
Reference in a new issue