mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-22 17:35:54 +03:00
Add/Remove local alias (#2428)
This commit is contained in:
parent
5b618ba1f3
commit
27fc5f265f
19 changed files with 262 additions and 94 deletions
|
@ -21,4 +21,9 @@ interface AliasService {
|
|||
* Get list of local alias of the room
|
||||
*/
|
||||
suspend fun getRoomAliases(): List<String>
|
||||
|
||||
/**
|
||||
* Add local alias to the room
|
||||
*/
|
||||
suspend fun addAlias(aliasLocalPart: String)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
/*
|
||||
* Copyright (c) 2020 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 org.matrix.android.sdk.api.session.room.alias
|
||||
|
||||
sealed class RoomAliasError : Throwable() {
|
||||
object AliasEmpty : RoomAliasError()
|
||||
object AliasNotAvailable : RoomAliasError()
|
||||
object AliasInvalid : RoomAliasError()
|
||||
}
|
|
@ -18,13 +18,10 @@ package org.matrix.android.sdk.api.session.room.failure
|
|||
|
||||
import org.matrix.android.sdk.api.failure.Failure
|
||||
import org.matrix.android.sdk.api.failure.MatrixError
|
||||
import org.matrix.android.sdk.api.session.room.alias.RoomAliasError
|
||||
|
||||
sealed class CreateRoomFailure : Failure.FeatureFailure() {
|
||||
object CreatedWithTimeout : CreateRoomFailure()
|
||||
data class CreatedWithFederationFailure(val matrixError: MatrixError) : CreateRoomFailure()
|
||||
sealed class RoomAliasError : CreateRoomFailure() {
|
||||
object AliasEmpty : RoomAliasError()
|
||||
object AliasNotAvailable : RoomAliasError()
|
||||
object AliasInvalid : RoomAliasError()
|
||||
}
|
||||
data class AliasError(val aliasError: RoomAliasError) : CreateRoomFailure()
|
||||
}
|
||||
|
|
|
@ -38,11 +38,6 @@ interface StateService {
|
|||
*/
|
||||
fun updateName(name: String, callback: MatrixCallback<Unit>): Cancelable
|
||||
|
||||
/**
|
||||
* Add new alias to the room.
|
||||
*/
|
||||
fun addRoomAlias(roomAlias: String, callback: MatrixCallback<Unit>): Cancelable
|
||||
|
||||
/**
|
||||
* Update the canonical alias of the room
|
||||
*/
|
||||
|
|
|
@ -39,7 +39,6 @@ internal interface DirectoryAPI {
|
|||
* Add alias to the room.
|
||||
* @param roomAlias the room alias.
|
||||
*/
|
||||
// TODO Remove (https://github.com/matrix-org/matrix-doc/blob/rav/proposal/alt_canonical_aliases/proposals/2432-revised-alias-publishing.md)
|
||||
@PUT(NetworkConstants.URI_API_PREFIX_PATH_R0 + "directory/room/{roomAlias}")
|
||||
fun addRoomAlias(@Path("roomAlias") roomAlias: String,
|
||||
@Body body: AddRoomAliasBody): Call<Unit>
|
||||
|
|
|
@ -17,27 +17,33 @@
|
|||
package org.matrix.android.sdk.internal.session.room.alias
|
||||
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import org.matrix.android.sdk.internal.di.UserId
|
||||
import org.matrix.android.sdk.internal.network.executeRequest
|
||||
import org.matrix.android.sdk.internal.session.directory.DirectoryAPI
|
||||
import org.matrix.android.sdk.internal.session.room.alias.RoomAliasAvailabilityChecker.Companion.toFullAlias
|
||||
import org.matrix.android.sdk.internal.task.Task
|
||||
import javax.inject.Inject
|
||||
|
||||
internal interface AddRoomAliasTask : Task<AddRoomAliasTask.Params, Unit> {
|
||||
data class Params(
|
||||
val roomId: String,
|
||||
val roomAlias: String
|
||||
val aliasLocalPart: String
|
||||
)
|
||||
}
|
||||
|
||||
internal class DefaultAddRoomAliasTask @Inject constructor(
|
||||
@UserId private val userId: String,
|
||||
private val directoryAPI: DirectoryAPI,
|
||||
private val aliasAvailabilityChecker: RoomAliasAvailabilityChecker,
|
||||
private val eventBus: EventBus
|
||||
) : AddRoomAliasTask {
|
||||
|
||||
override suspend fun execute(params: AddRoomAliasTask.Params) {
|
||||
aliasAvailabilityChecker.check(params.aliasLocalPart)
|
||||
|
||||
executeRequest<Unit>(eventBus) {
|
||||
apiCall = directoryAPI.addRoomAlias(
|
||||
roomAlias = params.roomAlias,
|
||||
roomAlias = params.aliasLocalPart.toFullAlias(userId),
|
||||
body = AddRoomAliasBody(
|
||||
roomId = params.roomId
|
||||
)
|
||||
|
|
|
@ -22,7 +22,8 @@ import org.matrix.android.sdk.api.session.room.alias.AliasService
|
|||
|
||||
internal class DefaultAliasService @AssistedInject constructor(
|
||||
@Assisted private val roomId: String,
|
||||
private val getRoomLocalAliasesTask: GetRoomLocalAliasesTask
|
||||
private val getRoomLocalAliasesTask: GetRoomLocalAliasesTask,
|
||||
private val addRoomAliasTask: AddRoomAliasTask
|
||||
) : AliasService {
|
||||
|
||||
@AssistedInject.Factory
|
||||
|
@ -33,4 +34,8 @@ internal class DefaultAliasService @AssistedInject constructor(
|
|||
override suspend fun getRoomAliases(): List<String> {
|
||||
return getRoomLocalAliasesTask.execute(GetRoomLocalAliasesTask.Params(roomId))
|
||||
}
|
||||
|
||||
override suspend fun addAlias(aliasLocalPart: String) {
|
||||
addRoomAliasTask.execute(AddRoomAliasTask.Params(roomId, aliasLocalPart))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright (c) 2020 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 org.matrix.android.sdk.internal.session.room.alias
|
||||
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import org.matrix.android.sdk.api.failure.Failure
|
||||
import org.matrix.android.sdk.api.session.room.alias.RoomAliasError
|
||||
import org.matrix.android.sdk.internal.di.UserId
|
||||
import org.matrix.android.sdk.internal.network.executeRequest
|
||||
import org.matrix.android.sdk.internal.session.directory.DirectoryAPI
|
||||
import javax.inject.Inject
|
||||
|
||||
internal class RoomAliasAvailabilityChecker @Inject constructor(
|
||||
@UserId private val userId: String,
|
||||
private val directoryAPI: DirectoryAPI,
|
||||
private val eventBus: EventBus
|
||||
) {
|
||||
@Throws(RoomAliasError::class)
|
||||
suspend fun check(aliasLocalPart: String?) {
|
||||
if (aliasLocalPart.isNullOrEmpty()) {
|
||||
throw RoomAliasError.AliasEmpty
|
||||
}
|
||||
// Check alias availability
|
||||
val fullAlias = aliasLocalPart.toFullAlias(userId)
|
||||
try {
|
||||
executeRequest<RoomAliasDescription>(eventBus) {
|
||||
apiCall = directoryAPI.getRoomIdByAlias(fullAlias)
|
||||
}
|
||||
} catch (throwable: Throwable) {
|
||||
if (throwable is Failure.ServerError && throwable.httpCode == 404) {
|
||||
// This is a 404, so the alias is available: nominal case
|
||||
null
|
||||
} else {
|
||||
// Other error, propagate it
|
||||
throw throwable
|
||||
}
|
||||
}
|
||||
?.let {
|
||||
// Alias already exists: error case
|
||||
throw RoomAliasError.AliasNotAvailable
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
internal fun String.toFullAlias(userId: String) = "#" + this + ":" + userId.substringAfter(":")
|
||||
}
|
||||
}
|
|
@ -22,6 +22,7 @@ import kotlinx.coroutines.TimeoutCancellationException
|
|||
import org.greenrobot.eventbus.EventBus
|
||||
import org.matrix.android.sdk.api.failure.Failure
|
||||
import org.matrix.android.sdk.api.failure.MatrixError
|
||||
import org.matrix.android.sdk.api.session.room.alias.RoomAliasError
|
||||
import org.matrix.android.sdk.api.session.room.failure.CreateRoomFailure
|
||||
import org.matrix.android.sdk.api.session.room.model.create.CreateRoomParams
|
||||
import org.matrix.android.sdk.api.session.room.model.create.CreateRoomPreset
|
||||
|
@ -31,11 +32,9 @@ import org.matrix.android.sdk.internal.database.model.RoomEntityFields
|
|||
import org.matrix.android.sdk.internal.database.model.RoomSummaryEntity
|
||||
import org.matrix.android.sdk.internal.database.query.where
|
||||
import org.matrix.android.sdk.internal.di.SessionDatabase
|
||||
import org.matrix.android.sdk.internal.di.UserId
|
||||
import org.matrix.android.sdk.internal.network.executeRequest
|
||||
import org.matrix.android.sdk.internal.session.directory.DirectoryAPI
|
||||
import org.matrix.android.sdk.internal.session.room.RoomAPI
|
||||
import org.matrix.android.sdk.internal.session.room.alias.RoomAliasDescription
|
||||
import org.matrix.android.sdk.internal.session.room.alias.RoomAliasAvailabilityChecker
|
||||
import org.matrix.android.sdk.internal.session.room.read.SetReadMarkersTask
|
||||
import org.matrix.android.sdk.internal.session.user.accountdata.DirectChatsHelper
|
||||
import org.matrix.android.sdk.internal.session.user.accountdata.UpdateUserAccountDataTask
|
||||
|
@ -48,9 +47,8 @@ internal interface CreateRoomTask : Task<CreateRoomParams, String>
|
|||
|
||||
internal class DefaultCreateRoomTask @Inject constructor(
|
||||
private val roomAPI: RoomAPI,
|
||||
private val directoryAPI: DirectoryAPI,
|
||||
@UserId private val userId: String,
|
||||
@SessionDatabase private val monarchy: Monarchy,
|
||||
private val aliasAvailabilityChecker: RoomAliasAvailabilityChecker,
|
||||
private val directChatsHelper: DirectChatsHelper,
|
||||
private val updateUserAccountDataTask: UpdateUserAccountDataTask,
|
||||
private val readMarkersTask: SetReadMarkersTask,
|
||||
|
@ -67,28 +65,11 @@ internal class DefaultCreateRoomTask @Inject constructor(
|
|||
} else null
|
||||
|
||||
if (params.preset == CreateRoomPreset.PRESET_PUBLIC_CHAT) {
|
||||
if (params.roomAliasName.isNullOrEmpty()) {
|
||||
throw CreateRoomFailure.RoomAliasError.AliasEmpty
|
||||
}
|
||||
// Check alias availability
|
||||
val fullAlias = "#" + params.roomAliasName + ":" + userId.substringAfter(":")
|
||||
try {
|
||||
executeRequest<RoomAliasDescription>(eventBus) {
|
||||
apiCall = directoryAPI.getRoomIdByAlias(fullAlias)
|
||||
}
|
||||
} catch (throwable: Throwable) {
|
||||
if (throwable is Failure.ServerError && throwable.httpCode == 404) {
|
||||
// This is a 404, so the alias is available: nominal case
|
||||
null
|
||||
} else {
|
||||
// Other error, propagate it
|
||||
throw throwable
|
||||
}
|
||||
aliasAvailabilityChecker.check(params.roomAliasName)
|
||||
} catch (aliasError: RoomAliasError) {
|
||||
throw CreateRoomFailure.AliasError(aliasError)
|
||||
}
|
||||
?.let {
|
||||
// Alias already exists: error case
|
||||
throw CreateRoomFailure.RoomAliasError.AliasNotAvailable
|
||||
}
|
||||
}
|
||||
|
||||
val createRoomBody = createRoomBodyBuilder.build(params)
|
||||
|
@ -106,7 +87,7 @@ internal class DefaultCreateRoomTask @Inject constructor(
|
|||
} else if (throwable.httpCode == 400
|
||||
&& throwable.error.code == MatrixError.M_UNKNOWN
|
||||
&& throwable.error.message == "Invalid characters in room alias") {
|
||||
throw CreateRoomFailure.RoomAliasError.AliasInvalid
|
||||
throw CreateRoomFailure.AliasError(RoomAliasError.AliasInvalid)
|
||||
}
|
||||
}
|
||||
throw throwable
|
||||
|
|
|
@ -104,14 +104,6 @@ internal class DefaultStateService @AssistedInject constructor(@Assisted private
|
|||
)
|
||||
}
|
||||
|
||||
override fun addRoomAlias(roomAlias: String, callback: MatrixCallback<Unit>): Cancelable {
|
||||
return addRoomAliasTask
|
||||
.configureWith(AddRoomAliasTask.Params(roomId, roomAlias)) {
|
||||
this.callback = callback
|
||||
}
|
||||
.executeBy(taskExecutor)
|
||||
}
|
||||
|
||||
override fun updateCanonicalAlias(alias: String, callback: MatrixCallback<Unit>): Cancelable {
|
||||
return sendStateEvent(
|
||||
eventType = EventType.STATE_ROOM_CANONICAL_ALIAS,
|
||||
|
|
|
@ -20,7 +20,6 @@ import com.airbnb.epoxy.TypedEpoxyController
|
|||
import com.airbnb.mvrx.Fail
|
||||
import com.airbnb.mvrx.Loading
|
||||
import im.vector.app.R
|
||||
import im.vector.app.core.error.ErrorFormatter
|
||||
import im.vector.app.core.resources.StringProvider
|
||||
import im.vector.app.features.discovery.settingsSectionTitleItem
|
||||
import im.vector.app.features.form.formAdvancedToggleItem
|
||||
|
@ -28,11 +27,13 @@ import im.vector.app.features.form.formEditTextItem
|
|||
import im.vector.app.features.form.formEditableAvatarItem
|
||||
import im.vector.app.features.form.formSubmitButtonItem
|
||||
import im.vector.app.features.form.formSwitchItem
|
||||
import org.matrix.android.sdk.api.session.room.alias.RoomAliasError
|
||||
import org.matrix.android.sdk.api.session.room.failure.CreateRoomFailure
|
||||
import javax.inject.Inject
|
||||
|
||||
class CreateRoomController @Inject constructor(private val stringProvider: StringProvider,
|
||||
private val errorFormatter: ErrorFormatter
|
||||
class CreateRoomController @Inject constructor(
|
||||
private val stringProvider: StringProvider,
|
||||
private val roomAliasErrorFormatter: RoomAliasErrorFormatter
|
||||
) : TypedEpoxyController<CreateRoomViewState>() {
|
||||
|
||||
var listener: Listener? = null
|
||||
|
@ -103,15 +104,7 @@ class CreateRoomController @Inject constructor(private val stringProvider: Strin
|
|||
enabled(enableFormElement)
|
||||
value(viewState.roomType.aliasLocalPart)
|
||||
homeServer(":" + viewState.homeServerName)
|
||||
errorMessage(
|
||||
when ((viewState.asyncCreateRoomRequest as? Fail)?.error) {
|
||||
is CreateRoomFailure.RoomAliasError.AliasEmpty -> R.string.create_room_alias_empty
|
||||
is CreateRoomFailure.RoomAliasError.AliasNotAvailable -> R.string.create_room_alias_already_in_use
|
||||
is CreateRoomFailure.RoomAliasError.AliasInvalid -> R.string.create_room_alias_invalid
|
||||
else -> null
|
||||
}
|
||||
?.let { stringProvider.getString(it) }
|
||||
)
|
||||
errorMessage(roomAliasErrorFormatter.format((((viewState.asyncCreateRoomRequest as? Fail)?.error) as? CreateRoomFailure.AliasError)?.aliasError))
|
||||
onTextChange { value ->
|
||||
listener?.setAliasLocalPart(value)
|
||||
}
|
||||
|
|
|
@ -84,7 +84,7 @@ class CreateRoomFragment @Inject constructor(
|
|||
|
||||
override fun showFailure(throwable: Throwable) {
|
||||
// Note: RoomAliasError are displayed directly in the form
|
||||
if (throwable !is CreateRoomFailure.RoomAliasError) {
|
||||
if (throwable !is CreateRoomFailure.AliasError) {
|
||||
super.showFailure(throwable)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Copyright (c) 2020 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.roomdirectory.createroom
|
||||
|
||||
import im.vector.app.R
|
||||
import im.vector.app.core.resources.StringProvider
|
||||
import org.matrix.android.sdk.api.session.room.alias.RoomAliasError
|
||||
import javax.inject.Inject
|
||||
|
||||
class RoomAliasErrorFormatter @Inject constructor(
|
||||
private val stringProvider: StringProvider
|
||||
) {
|
||||
fun format(roomAliasError: RoomAliasError?): String? {
|
||||
return when (roomAliasError) {
|
||||
is RoomAliasError.AliasEmpty -> R.string.create_room_alias_empty
|
||||
is RoomAliasError.AliasNotAvailable -> R.string.create_room_alias_already_in_use
|
||||
is RoomAliasError.AliasInvalid -> R.string.create_room_alias_invalid
|
||||
else -> null
|
||||
}
|
||||
?.let { stringProvider.getString(it) }
|
||||
}
|
||||
}
|
|
@ -26,6 +26,7 @@ sealed class RoomAliasAction : VectorViewModelAction {
|
|||
object UnSetCanonicalAlias : RoomAliasAction()
|
||||
|
||||
// Local
|
||||
data class AddLocalAlias(val aliasLocalPart: String) : RoomAliasAction()
|
||||
data class RemoveLocalAlias(val alias: String) : RoomAliasAction()
|
||||
data class SetNewLocalAliasLocalPart(val aliasLocalPart: String) : RoomAliasAction()
|
||||
object AddLocalAlias : RoomAliasAction()
|
||||
}
|
||||
|
|
|
@ -25,28 +25,30 @@ import im.vector.app.core.epoxy.errorWithRetryItem
|
|||
import im.vector.app.core.epoxy.loadingItem
|
||||
import im.vector.app.core.epoxy.profiles.buildProfileSection
|
||||
import im.vector.app.core.error.ErrorFormatter
|
||||
import im.vector.app.core.resources.ColorProvider
|
||||
import im.vector.app.core.resources.StringProvider
|
||||
import im.vector.app.features.discovery.settingsInfoItem
|
||||
import im.vector.app.features.form.formSubmitButtonItem
|
||||
import im.vector.app.features.roomdirectory.createroom.RoomAliasErrorFormatter
|
||||
import im.vector.app.features.roomdirectory.createroom.roomAliasEditItem
|
||||
import im.vector.app.features.settings.threepids.threePidItem
|
||||
import org.matrix.android.sdk.api.session.room.alias.RoomAliasError
|
||||
import javax.inject.Inject
|
||||
|
||||
class RoomAliasController @Inject constructor(
|
||||
private val stringProvider: StringProvider,
|
||||
private val errorFormatter: ErrorFormatter,
|
||||
colorProvider: ColorProvider
|
||||
private val roomAliasErrorFormatter: RoomAliasErrorFormatter
|
||||
) : TypedEpoxyController<RoomAliasViewState>() {
|
||||
|
||||
interface Callback {
|
||||
fun removeAlias(altAlias: String)
|
||||
fun setCanonicalAlias(alias: String)
|
||||
fun unsetCanonicalAlias()
|
||||
fun addLocalAlias(alias: String)
|
||||
fun removeLocalAlias(alias: String)
|
||||
fun setNewLocalAliasLocalPart(value: String)
|
||||
fun addLocalAlias()
|
||||
}
|
||||
|
||||
private val dividerColor = colorProvider.getColorFromAttribute(R.attr.vctr_list_divider_color)
|
||||
|
||||
var callback: Callback? = null
|
||||
|
||||
init {
|
||||
|
@ -81,6 +83,10 @@ class RoomAliasController @Inject constructor(
|
|||
}
|
||||
|
||||
// Local
|
||||
buildLocalInfo(data)
|
||||
}
|
||||
|
||||
private fun buildLocalInfo(data: RoomAliasViewState) {
|
||||
buildProfileSection(
|
||||
stringProvider.getString(R.string.room_alias_local_address_title)
|
||||
)
|
||||
|
@ -89,10 +95,6 @@ class RoomAliasController @Inject constructor(
|
|||
helperText(stringProvider.getString(R.string.room_alias_local_address_subtitle, data.homeServerName))
|
||||
}
|
||||
|
||||
buildLocalInfo(data)
|
||||
}
|
||||
|
||||
private fun buildLocalInfo(data: RoomAliasViewState) {
|
||||
when (val localAliases = data.localAliases) {
|
||||
is Uninitialized -> {
|
||||
loadingItem {
|
||||
|
@ -116,5 +118,23 @@ class RoomAliasController @Inject constructor(
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add local
|
||||
roomAliasEditItem {
|
||||
id("newLocalAlias")
|
||||
value(data.newLocalAlias)
|
||||
homeServer(":" + data.homeServerName)
|
||||
showBottomSeparator(false)
|
||||
errorMessage(roomAliasErrorFormatter.format((data.asyncNewLocalAliasRequest as? Fail)?.error as? RoomAliasError))
|
||||
onTextChange { value ->
|
||||
callback?.setNewLocalAliasLocalPart(value)
|
||||
}
|
||||
}
|
||||
|
||||
formSubmitButtonItem {
|
||||
id("submit")
|
||||
buttonTitleId(R.string.action_add)
|
||||
buttonClickListener { callback?.addLocalAlias() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,6 +35,7 @@ import im.vector.app.features.home.AvatarRenderer
|
|||
import im.vector.app.features.roomprofile.RoomProfileArgs
|
||||
import kotlinx.android.synthetic.main.fragment_room_setting_generic.*
|
||||
import kotlinx.android.synthetic.main.merge_overlay_waiting_view.*
|
||||
import org.matrix.android.sdk.api.session.room.alias.RoomAliasError
|
||||
import org.matrix.android.sdk.api.util.toMatrixItem
|
||||
import javax.inject.Inject
|
||||
|
||||
|
@ -67,6 +68,12 @@ class RoomAliasFragment @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
override fun showFailure(throwable: Throwable) {
|
||||
if (throwable !is RoomAliasError) {
|
||||
super.showFailure(throwable)
|
||||
}
|
||||
}
|
||||
|
||||
private fun showSuccess() {
|
||||
activity?.toast(R.string.room_settings_save_success)
|
||||
}
|
||||
|
@ -113,8 +120,12 @@ class RoomAliasFragment @Inject constructor(
|
|||
viewModel.handle(RoomAliasAction.UnSetCanonicalAlias)
|
||||
}
|
||||
|
||||
override fun addLocalAlias(alias: String) {
|
||||
viewModel.handle(RoomAliasAction.AddLocalAlias(alias))
|
||||
override fun setNewLocalAliasLocalPart(value: String) {
|
||||
viewModel.handle(RoomAliasAction.SetNewLocalAliasLocalPart(value))
|
||||
}
|
||||
|
||||
override fun addLocalAlias() {
|
||||
viewModel.handle(RoomAliasAction.AddLocalAlias)
|
||||
}
|
||||
|
||||
override fun removeLocalAlias(alias: String) {
|
||||
|
|
|
@ -22,6 +22,7 @@ import com.airbnb.mvrx.FragmentViewModelContext
|
|||
import com.airbnb.mvrx.Loading
|
||||
import com.airbnb.mvrx.MvRxViewModelFactory
|
||||
import com.airbnb.mvrx.Success
|
||||
import com.airbnb.mvrx.Uninitialized
|
||||
import com.airbnb.mvrx.ViewModelContext
|
||||
import com.squareup.inject.assisted.Assisted
|
||||
import com.squareup.inject.assisted.AssistedInject
|
||||
|
@ -62,9 +63,9 @@ class RoomAliasViewModel @AssistedInject constructor(@Assisted initialState: Roo
|
|||
init {
|
||||
initHomeServerName()
|
||||
observeRoomSummary()
|
||||
observeMowerLevel()
|
||||
observePowerLevel()
|
||||
observeRoomCanonicalAlias()
|
||||
getRoomAlias()
|
||||
fetchRoomAlias()
|
||||
}
|
||||
|
||||
private fun initHomeServerName() {
|
||||
|
@ -75,7 +76,7 @@ class RoomAliasViewModel @AssistedInject constructor(@Assisted initialState: Roo
|
|||
}
|
||||
}
|
||||
|
||||
private fun getRoomAlias() {
|
||||
private fun fetchRoomAlias() {
|
||||
setState {
|
||||
copy(
|
||||
localAliases = Loading()
|
||||
|
@ -105,7 +106,7 @@ class RoomAliasViewModel @AssistedInject constructor(@Assisted initialState: Roo
|
|||
}
|
||||
}
|
||||
|
||||
private fun observeMowerLevel() {
|
||||
private fun observePowerLevel() {
|
||||
PowerLevelsObservableFactory(room)
|
||||
.createObservable()
|
||||
.subscribe {
|
||||
|
@ -140,29 +141,35 @@ class RoomAliasViewModel @AssistedInject constructor(@Assisted initialState: Roo
|
|||
|
||||
override fun handle(action: RoomAliasAction) {
|
||||
when (action) {
|
||||
is RoomAliasAction.AddAlias -> handleAddAlias(action)
|
||||
is RoomAliasAction.RemoveAlias -> handleRemoveAlias(action)
|
||||
is RoomAliasAction.SetCanonicalAlias -> handleSetCanonicalAlias(action)
|
||||
RoomAliasAction.UnSetCanonicalAlias -> handleUnsetCanonicalAlias()
|
||||
is RoomAliasAction.AddLocalAlias -> handleAddLocalAlias(action)
|
||||
is RoomAliasAction.RemoveLocalAlias -> handleRemoveLocalAlias(action)
|
||||
is RoomAliasAction.AddAlias -> handleAddAlias()
|
||||
is RoomAliasAction.RemoveAlias -> handleRemoveAlias(action)
|
||||
is RoomAliasAction.SetCanonicalAlias -> handleSetCanonicalAlias(action)
|
||||
RoomAliasAction.UnSetCanonicalAlias -> handleUnsetCanonicalAlias()
|
||||
is RoomAliasAction.SetNewLocalAliasLocalPart -> handleSetNewLocalAliasLocalPart(action)
|
||||
RoomAliasAction.AddLocalAlias -> handleAddLocalAlias()
|
||||
is RoomAliasAction.RemoveLocalAlias -> handleRemoveLocalAlias(action)
|
||||
}.exhaustive
|
||||
}
|
||||
|
||||
private fun handleAddAlias(action: RoomAliasAction.AddAlias) {
|
||||
TODO("Not yet implemented")
|
||||
private fun handleSetNewLocalAliasLocalPart(action: RoomAliasAction.SetNewLocalAliasLocalPart) {
|
||||
setState {
|
||||
copy(
|
||||
newLocalAlias = action.aliasLocalPart,
|
||||
asyncNewLocalAliasRequest = Uninitialized
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleAddAlias() {
|
||||
TODO()
|
||||
}
|
||||
|
||||
private fun handleRemoveAlias(action: RoomAliasAction.RemoveAlias) {
|
||||
setState {
|
||||
copy(isLoading = true)
|
||||
}
|
||||
postLoading(true)
|
||||
viewModelScope.launch {
|
||||
runCatching { session.deleteRoomAlias(action.alias) }
|
||||
.onFailure { _viewEvents.post(RoomAliasViewEvents.Failure(it)) }
|
||||
setState {
|
||||
copy(isLoading = false)
|
||||
}
|
||||
postLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -172,15 +179,48 @@ class RoomAliasViewModel @AssistedInject constructor(@Assisted initialState: Roo
|
|||
}
|
||||
|
||||
private fun handleUnsetCanonicalAlias() {
|
||||
// room.updateCanonicalAlias()
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
private fun handleAddLocalAlias(action: RoomAliasAction.AddLocalAlias) {
|
||||
TODO("Not yet implemented")
|
||||
private fun handleAddLocalAlias() = withState { state ->
|
||||
setState {
|
||||
copy(
|
||||
isLoading = true,
|
||||
asyncNewLocalAliasRequest = Loading()
|
||||
)
|
||||
}
|
||||
viewModelScope.launch {
|
||||
runCatching { room.addAlias(state.newLocalAlias) }
|
||||
.onFailure {
|
||||
setState {
|
||||
copy(
|
||||
isLoading = false,
|
||||
asyncNewLocalAliasRequest = Fail(it)
|
||||
)
|
||||
}
|
||||
_viewEvents.post(RoomAliasViewEvents.Failure(it))
|
||||
}
|
||||
.onSuccess {
|
||||
setState {
|
||||
copy(
|
||||
isLoading = false,
|
||||
asyncNewLocalAliasRequest = Uninitialized
|
||||
)
|
||||
}
|
||||
fetchRoomAlias()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleRemoveLocalAlias(action: RoomAliasAction.RemoveLocalAlias) {
|
||||
TODO("Not yet implemented")
|
||||
postLoading(true)
|
||||
viewModelScope.launch {
|
||||
runCatching { session.deleteRoomAlias(action.alias) }
|
||||
.onFailure { _viewEvents.post(RoomAliasViewEvents.Failure(it)) }
|
||||
.onSuccess { fetchRoomAlias() }
|
||||
postLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
private fun postLoading(isLoading: Boolean) {
|
||||
|
|
|
@ -30,6 +30,8 @@ data class RoomAliasViewState(
|
|||
val canonicalAlias: String? = null,
|
||||
val alternativeAliases: List<String> = emptyList(),
|
||||
val localAliases: Async<List<String>> = Uninitialized,
|
||||
val newLocalAlias: String = "",
|
||||
val asyncNewLocalAliasRequest: Async<Unit> = Uninitialized,
|
||||
val actionPermissions: ActionPermissions = ActionPermissions()
|
||||
) : MvRxState {
|
||||
|
||||
|
|
|
@ -137,6 +137,7 @@
|
|||
<string name="action_open">Open</string>
|
||||
<string name="action_close">Close</string>
|
||||
<string name="action_copy">Copy</string>
|
||||
<string name="action_add">Add</string>
|
||||
<string name="copied_to_clipboard">Copied to clipboard</string>
|
||||
<string name="disable">Disable</string>
|
||||
|
||||
|
|
Loading…
Reference in a new issue