Code review
This commit is contained in:
parent
804afc9a1d
commit
8e6e6736a3
|
@ -35,6 +35,11 @@ interface UserService {
|
|||
*/
|
||||
fun getUser(userId: String): User?
|
||||
|
||||
/**
|
||||
* Try to resolve user from known users, or using profile api
|
||||
*/
|
||||
fun resolveUser(userId: String, callback: MatrixCallback<User>)
|
||||
|
||||
/**
|
||||
* Search list of users on server directory.
|
||||
* @param search the searched term
|
||||
|
|
|
@ -19,10 +19,13 @@ package org.matrix.android.sdk.internal.session.user
|
|||
import androidx.lifecycle.LiveData
|
||||
import androidx.paging.PagedList
|
||||
import org.matrix.android.sdk.api.MatrixCallback
|
||||
import org.matrix.android.sdk.api.session.profile.ProfileService
|
||||
import org.matrix.android.sdk.api.session.user.UserService
|
||||
import org.matrix.android.sdk.api.session.user.model.User
|
||||
import org.matrix.android.sdk.api.util.Cancelable
|
||||
import org.matrix.android.sdk.api.util.JsonDict
|
||||
import org.matrix.android.sdk.api.util.Optional
|
||||
import org.matrix.android.sdk.internal.session.profile.GetProfileInfoTask
|
||||
import org.matrix.android.sdk.internal.session.user.accountdata.UpdateIgnoredUserIdsTask
|
||||
import org.matrix.android.sdk.internal.session.user.model.SearchUserTask
|
||||
import org.matrix.android.sdk.internal.task.TaskExecutor
|
||||
|
@ -32,12 +35,40 @@ import javax.inject.Inject
|
|||
internal class DefaultUserService @Inject constructor(private val userDataSource: UserDataSource,
|
||||
private val searchUserTask: SearchUserTask,
|
||||
private val updateIgnoredUserIdsTask: UpdateIgnoredUserIdsTask,
|
||||
private val getProfileInfoTask: GetProfileInfoTask,
|
||||
private val taskExecutor: TaskExecutor) : UserService {
|
||||
|
||||
override fun getUser(userId: String): User? {
|
||||
return userDataSource.getUser(userId)
|
||||
}
|
||||
|
||||
override fun resolveUser(userId: String, callback: MatrixCallback<User>) {
|
||||
val known = getUser(userId)
|
||||
if (known != null) {
|
||||
callback.onSuccess(known)
|
||||
} else {
|
||||
val params = GetProfileInfoTask.Params(userId)
|
||||
getProfileInfoTask
|
||||
.configureWith(params) {
|
||||
this.callback = object : MatrixCallback<JsonDict> {
|
||||
override fun onSuccess(data: JsonDict) {
|
||||
callback.onSuccess(
|
||||
User(
|
||||
userId,
|
||||
data[ProfileService.DISPLAY_NAME_KEY] as? String,
|
||||
data[ProfileService.AVATAR_URL_KEY] as? String)
|
||||
)
|
||||
}
|
||||
|
||||
override fun onFailure(failure: Throwable) {
|
||||
callback.onFailure(failure)
|
||||
}
|
||||
}
|
||||
}
|
||||
.executeBy(taskExecutor)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getUserLive(userId: String): LiveData<Optional<User>> {
|
||||
return userDataSource.getUserLive(userId)
|
||||
}
|
||||
|
|
|
@ -22,6 +22,14 @@ import com.airbnb.mvrx.Uninitialized
|
|||
import org.matrix.android.sdk.api.util.MatrixItem
|
||||
|
||||
data class MatrixToBottomSheetState(
|
||||
val userId: String? = null,
|
||||
val deepLink: String? = null,
|
||||
val matrixItem: Async<MatrixItem> = Uninitialized,
|
||||
val startChattingState: Async<Unit> = Uninitialized
|
||||
) : MvRxState
|
||||
) : MvRxState {
|
||||
|
||||
constructor(args: MatrixToBottomSheet.MatrixToArgs) : this(
|
||||
userId = args.userId,
|
||||
deepLink = args.matrixToLink
|
||||
)
|
||||
}
|
||||
|
|
|
@ -39,24 +39,20 @@ import org.matrix.android.sdk.api.raw.RawService
|
|||
import org.matrix.android.sdk.api.session.Session
|
||||
import org.matrix.android.sdk.api.session.permalinks.PermalinkData
|
||||
import org.matrix.android.sdk.api.session.permalinks.PermalinkParser
|
||||
import org.matrix.android.sdk.api.session.profile.ProfileService
|
||||
import org.matrix.android.sdk.api.session.room.model.create.CreateRoomParams
|
||||
import org.matrix.android.sdk.api.session.user.model.User
|
||||
import org.matrix.android.sdk.api.util.JsonDict
|
||||
import org.matrix.android.sdk.api.util.toMatrixItem
|
||||
import org.matrix.android.sdk.internal.util.awaitCallback
|
||||
|
||||
class MatrixToBottomSheetViewModel @AssistedInject constructor(
|
||||
@Assisted initialState: MatrixToBottomSheetState,
|
||||
@Assisted val args: MatrixToBottomSheet.MatrixToArgs,
|
||||
private val session: Session,
|
||||
private val stringProvider: StringProvider,
|
||||
private val rawService: RawService) : VectorViewModel<MatrixToBottomSheetState, MatrixToAction, MatrixToViewEvents>(initialState) {
|
||||
|
||||
@AssistedInject.Factory
|
||||
interface Factory {
|
||||
fun create(initialState: MatrixToBottomSheetState,
|
||||
args: MatrixToBottomSheet.MatrixToArgs): MatrixToBottomSheetViewModel
|
||||
fun create(initialState: MatrixToBottomSheetState): MatrixToBottomSheetViewModel
|
||||
}
|
||||
|
||||
init {
|
||||
|
@ -64,14 +60,14 @@ class MatrixToBottomSheetViewModel @AssistedInject constructor(
|
|||
copy(matrixItem = Loading())
|
||||
}
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
resolveLink()
|
||||
resolveLink(initialState)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun resolveLink() {
|
||||
private suspend fun resolveLink(initialState: MatrixToBottomSheetState) {
|
||||
when {
|
||||
args.matrixToLink != null -> {
|
||||
val linkedId = PermalinkParser.parse(args.matrixToLink)
|
||||
initialState.deepLink != null -> {
|
||||
val linkedId = PermalinkParser.parse(initialState.deepLink)
|
||||
if (linkedId is PermalinkData.FallbackLink) {
|
||||
setState {
|
||||
copy(
|
||||
|
@ -92,7 +88,9 @@ class MatrixToBottomSheetViewModel @AssistedInject constructor(
|
|||
)
|
||||
}
|
||||
}
|
||||
is PermalinkData.RoomLink -> TODO()
|
||||
is PermalinkData.RoomLink -> {
|
||||
// not yet supported
|
||||
}
|
||||
is PermalinkData.GroupLink -> {
|
||||
// not yet supported
|
||||
}
|
||||
|
@ -100,8 +98,8 @@ class MatrixToBottomSheetViewModel @AssistedInject constructor(
|
|||
}
|
||||
}
|
||||
}
|
||||
args.userId != null -> {
|
||||
val user = resolveUser(args.userId)
|
||||
initialState.userId != null -> {
|
||||
val user = resolveUser(initialState.userId)
|
||||
|
||||
setState {
|
||||
copy(
|
||||
|
@ -110,7 +108,7 @@ class MatrixToBottomSheetViewModel @AssistedInject constructor(
|
|||
)
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
else -> {
|
||||
setState {
|
||||
copy(
|
||||
matrixItem = Fail(IllegalArgumentException(stringProvider.getString(R.string.unexpected_error))),
|
||||
|
@ -122,24 +120,20 @@ class MatrixToBottomSheetViewModel @AssistedInject constructor(
|
|||
}
|
||||
|
||||
private suspend fun resolveUser(userId: String): User {
|
||||
return (session.getUser(userId)
|
||||
?: tryOrNull {
|
||||
awaitCallback<JsonDict> {
|
||||
session.getProfile(userId, it)
|
||||
return tryOrNull {
|
||||
awaitCallback<User> {
|
||||
session.resolveUser(userId, it)
|
||||
}
|
||||
}?.let {
|
||||
User(userId, it[ProfileService.DISPLAY_NAME_KEY] as? String, it[ProfileService.AVATAR_URL_KEY] as? String)
|
||||
}
|
||||
// Create raw Uxid in case the user is not searchable
|
||||
?: User(userId, null, null))
|
||||
// Create raw user in case the user is not searchable
|
||||
?: User(userId, null, null)
|
||||
}
|
||||
|
||||
companion object : MvRxViewModelFactory<MatrixToBottomSheetViewModel, MatrixToBottomSheetState> {
|
||||
override fun create(viewModelContext: ViewModelContext, state: MatrixToBottomSheetState): MatrixToBottomSheetViewModel? {
|
||||
val fragment: MatrixToBottomSheet = (viewModelContext as FragmentViewModelContext).fragment()
|
||||
val args: MatrixToBottomSheet.MatrixToArgs = viewModelContext.args()
|
||||
|
||||
return fragment.matrixToBottomSheetViewModelFactory.create(state, args)
|
||||
return fragment.matrixToBottomSheetViewModelFactory.create(state)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -35,10 +35,8 @@ import org.matrix.android.sdk.api.raw.RawService
|
|||
import org.matrix.android.sdk.api.session.Session
|
||||
import org.matrix.android.sdk.api.session.permalinks.PermalinkData
|
||||
import org.matrix.android.sdk.api.session.permalinks.PermalinkParser
|
||||
import org.matrix.android.sdk.api.session.profile.ProfileService
|
||||
import org.matrix.android.sdk.api.session.room.model.create.CreateRoomParams
|
||||
import org.matrix.android.sdk.api.session.user.model.User
|
||||
import org.matrix.android.sdk.api.util.JsonDict
|
||||
import org.matrix.android.sdk.api.util.toMatrixItem
|
||||
import org.matrix.android.sdk.internal.util.awaitCallback
|
||||
|
||||
|
@ -147,15 +145,12 @@ class UserCodeSharedViewModel @AssistedInject constructor(
|
|||
_viewEvents.post(UserCodeShareViewEvents.ToastMessage(stringProvider.getString(R.string.not_implemented)))
|
||||
}
|
||||
is PermalinkData.UserLink -> {
|
||||
val user = session.getUser(linkedId.userId)
|
||||
?: tryOrNull {
|
||||
awaitCallback<JsonDict> {
|
||||
session.getProfile(linkedId.userId, it)
|
||||
}
|
||||
}?.let {
|
||||
User(linkedId.userId, it[ProfileService.DISPLAY_NAME_KEY] as? String, it[ProfileService.AVATAR_URL_KEY] as? String)
|
||||
}
|
||||
// Create raw Uxid in case the user is not searchable
|
||||
val user = tryOrNull {
|
||||
awaitCallback<User> {
|
||||
session.resolveUser(linkedId.userId, it)
|
||||
}
|
||||
}
|
||||
// Create raw Uxid in case the user is not searchable
|
||||
?: User(linkedId.userId, null, null)
|
||||
|
||||
setState {
|
||||
|
|
Loading…
Reference in New Issue