Code review

This commit is contained in:
Valere 2021-04-17 09:03:41 +02:00
parent 8146d8ab1e
commit 4d7aeff54a
6 changed files with 57 additions and 56 deletions

View File

@ -0,0 +1,37 @@
/*
* Copyright 2021 The Matrix.org Foundation C.I.C.
*
* 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.query
import io.realm.RealmQuery
import io.realm.Sort
import org.matrix.android.sdk.api.session.room.RoomSortOrder
import org.matrix.android.sdk.internal.database.model.RoomSummaryEntity
import org.matrix.android.sdk.internal.database.model.RoomSummaryEntityFields
internal fun RealmQuery<RoomSummaryEntity>.process(sortOrder: RoomSortOrder): RealmQuery<RoomSummaryEntity> {
when (sortOrder) {
RoomSortOrder.NAME -> {
sort(RoomSummaryEntityFields.DISPLAY_NAME, Sort.ASCENDING)
}
RoomSortOrder.ACTIVITY -> {
sort(RoomSummaryEntityFields.LAST_ACTIVITY_TIME, Sort.DESCENDING)
}
RoomSortOrder.NONE -> {
}
}
return this
}

View File

@ -16,21 +16,21 @@
package org.matrix.android.sdk.internal.query
import org.matrix.android.sdk.api.query.QueryStringValue
import io.realm.Case
import io.realm.RealmObject
import io.realm.RealmQuery
import org.matrix.android.sdk.api.query.QueryStringValue
import timber.log.Timber
fun <T : RealmObject> RealmQuery<T>.process(field: String, queryStringValue: QueryStringValue): RealmQuery<T> {
when (queryStringValue) {
is QueryStringValue.NoCondition -> Timber.v("No condition to process")
is QueryStringValue.IsNotNull -> isNotNull(field)
is QueryStringValue.IsNull -> isNull(field)
is QueryStringValue.IsEmpty -> isEmpty(field)
is QueryStringValue.IsNotEmpty -> isNotEmpty(field)
is QueryStringValue.Equals -> equalTo(field, queryStringValue.string, queryStringValue.case.toRealmCase())
is QueryStringValue.Contains -> contains(field, queryStringValue.string, queryStringValue.case.toRealmCase())
is QueryStringValue.IsNotNull -> isNotNull(field)
is QueryStringValue.IsNull -> isNull(field)
is QueryStringValue.IsEmpty -> isEmpty(field)
is QueryStringValue.IsNotEmpty -> isNotEmpty(field)
is QueryStringValue.Equals -> equalTo(field, queryStringValue.string, queryStringValue.case.toRealmCase())
is QueryStringValue.Contains -> contains(field, queryStringValue.string, queryStringValue.case.toRealmCase())
}
return this
}
@ -38,6 +38,6 @@ fun <T : RealmObject> RealmQuery<T>.process(field: String, queryStringValue: Que
private fun QueryStringValue.Case.toRealmCase(): Case {
return when (this) {
QueryStringValue.Case.INSENSITIVE -> Case.INSENSITIVE
QueryStringValue.Case.SENSITIVE -> Case.SENSITIVE
QueryStringValue.Case.SENSITIVE -> Case.SENSITIVE
}
}

View File

@ -166,19 +166,7 @@ internal class RoomSummaryDataSource @Inject constructor(@SessionDatabase privat
pagedListConfig: PagedList.Config,
sortOrder: RoomSortOrder): LiveData<PagedList<RoomSummary>> {
val realmDataSourceFactory = monarchy.createDataSourceFactory { realm ->
roomSummariesQuery(realm, queryParams)
.apply {
when (sortOrder) {
RoomSortOrder.NAME -> {
sort(RoomSummaryEntityFields.DISPLAY_NAME, Sort.ASCENDING)
}
RoomSortOrder.ACTIVITY -> {
sort(RoomSummaryEntityFields.LAST_ACTIVITY_TIME, Sort.DESCENDING)
}
RoomSortOrder.NONE -> {
}
}
}
roomSummariesQuery(realm, queryParams).process(sortOrder)
}
val dataSourceFactory = realmDataSourceFactory.map {
roomSummaryMapper.map(it)
@ -193,19 +181,7 @@ internal class RoomSummaryDataSource @Inject constructor(@SessionDatabase privat
pagedListConfig: PagedList.Config,
sortOrder: RoomSortOrder): UpdatableLivePageResult {
val realmDataSourceFactory = monarchy.createDataSourceFactory { realm ->
roomSummariesQuery(realm, queryParams)
.apply {
when (sortOrder) {
RoomSortOrder.NAME -> {
sort(RoomSummaryEntityFields.DISPLAY_NAME, Sort.ASCENDING)
}
RoomSortOrder.ACTIVITY -> {
sort(RoomSummaryEntityFields.LAST_ACTIVITY_TIME, Sort.DESCENDING)
}
RoomSortOrder.NONE -> {
}
}
}
roomSummariesQuery(realm, queryParams).process(sortOrder)
}
val dataSourceFactory = realmDataSourceFactory.map {
roomSummaryMapper.map(it)
@ -221,19 +197,7 @@ internal class RoomSummaryDataSource @Inject constructor(@SessionDatabase privat
override fun updateQuery(builder: (RoomSummaryQueryParams) -> RoomSummaryQueryParams) {
realmDataSourceFactory.updateQuery {
roomSummariesQuery(it, builder.invoke(queryParams))
.apply {
when (sortOrder) {
RoomSortOrder.NAME -> {
sort(RoomSummaryEntityFields.DISPLAY_NAME, Sort.ASCENDING)
}
RoomSortOrder.ACTIVITY -> {
sort(RoomSummaryEntityFields.LAST_ACTIVITY_TIME, Sort.DESCENDING)
}
RoomSortOrder.NONE -> {
}
}
}
roomSummariesQuery(it, builder.invoke(queryParams)).process(sortOrder)
}
}
}

View File

@ -26,7 +26,6 @@ import androidx.appcompat.app.AlertDialog
import com.airbnb.mvrx.Loading
import com.airbnb.mvrx.activityViewModel
import com.airbnb.mvrx.fragmentViewModel
import com.airbnb.mvrx.withState
import com.jakewharton.rxbinding3.appcompat.queryTextChanges
import im.vector.app.R
import im.vector.app.core.extensions.cleanup
@ -117,7 +116,8 @@ class SpaceAddRoomFragment @Inject constructor(
.setNegativeButton(R.string.cancel, null)
.show()
}
SpaceAddRoomsViewEvents.SaveFailed -> {
is SpaceAddRoomsViewEvents.SaveFailed -> {
showErrorInSnackbar(it.reason)
invalidateOptionsMenu()
}
SpaceAddRoomsViewEvents.SavedDone -> {
@ -127,10 +127,6 @@ class SpaceAddRoomFragment @Inject constructor(
}
}
override fun invalidate() = withState(viewModel) {
super.invalidate()
}
override fun onPrepareOptionsMenu(menu: Menu) {
super.onPrepareOptionsMenu(menu)
menu.findItem(R.id.spaceAddRoomSaveItem).isVisible = saveNeeded

View File

@ -21,5 +21,5 @@ import im.vector.app.core.platform.VectorViewEvents
sealed class SpaceAddRoomsViewEvents : VectorViewEvents {
object WarnUnsavedChanged : SpaceAddRoomsViewEvents()
object SavedDone : SpaceAddRoomsViewEvents()
object SaveFailed : SpaceAddRoomsViewEvents()
data class SaveFailed(val reason: Throwable): SpaceAddRoomsViewEvents()
}

View File

@ -42,7 +42,11 @@ import org.matrix.android.sdk.api.session.room.UpdatableLivePageResult
import org.matrix.android.sdk.api.session.room.model.Membership
import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams
class AddRoomError(val errorList: Map<String, Throwable>) : Throwable()
class AddRoomError(val errorList: Map<String, Throwable>) : Throwable() {
override fun getLocalizedMessage(): String? {
return errorList.map { it.value.localizedMessage }.joinToString(",")
}
}
class SpaceAddRoomsViewModel @AssistedInject constructor(
@Assisted val initialState: SpaceAddRoomsState,
@ -186,7 +190,7 @@ class SpaceAddRoomsViewModel @AssistedInject constructor(
isSaving = Fail(AddRoomError(errors))
)
}
_viewEvents.post(SpaceAddRoomsViewEvents.SaveFailed)
_viewEvents.post(SpaceAddRoomsViewEvents.SaveFailed(AddRoomError(errors)))
}
}
}