Code review
This commit is contained in:
parent
8146d8ab1e
commit
4d7aeff54a
|
@ -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
|
||||||
|
}
|
|
@ -16,21 +16,21 @@
|
||||||
|
|
||||||
package org.matrix.android.sdk.internal.query
|
package org.matrix.android.sdk.internal.query
|
||||||
|
|
||||||
import org.matrix.android.sdk.api.query.QueryStringValue
|
|
||||||
import io.realm.Case
|
import io.realm.Case
|
||||||
import io.realm.RealmObject
|
import io.realm.RealmObject
|
||||||
import io.realm.RealmQuery
|
import io.realm.RealmQuery
|
||||||
|
import org.matrix.android.sdk.api.query.QueryStringValue
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
fun <T : RealmObject> RealmQuery<T>.process(field: String, queryStringValue: QueryStringValue): RealmQuery<T> {
|
fun <T : RealmObject> RealmQuery<T>.process(field: String, queryStringValue: QueryStringValue): RealmQuery<T> {
|
||||||
when (queryStringValue) {
|
when (queryStringValue) {
|
||||||
is QueryStringValue.NoCondition -> Timber.v("No condition to process")
|
is QueryStringValue.NoCondition -> Timber.v("No condition to process")
|
||||||
is QueryStringValue.IsNotNull -> isNotNull(field)
|
is QueryStringValue.IsNotNull -> isNotNull(field)
|
||||||
is QueryStringValue.IsNull -> isNull(field)
|
is QueryStringValue.IsNull -> isNull(field)
|
||||||
is QueryStringValue.IsEmpty -> isEmpty(field)
|
is QueryStringValue.IsEmpty -> isEmpty(field)
|
||||||
is QueryStringValue.IsNotEmpty -> isNotEmpty(field)
|
is QueryStringValue.IsNotEmpty -> isNotEmpty(field)
|
||||||
is QueryStringValue.Equals -> equalTo(field, queryStringValue.string, queryStringValue.case.toRealmCase())
|
is QueryStringValue.Equals -> equalTo(field, queryStringValue.string, queryStringValue.case.toRealmCase())
|
||||||
is QueryStringValue.Contains -> contains(field, queryStringValue.string, queryStringValue.case.toRealmCase())
|
is QueryStringValue.Contains -> contains(field, queryStringValue.string, queryStringValue.case.toRealmCase())
|
||||||
}
|
}
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,6 @@ fun <T : RealmObject> RealmQuery<T>.process(field: String, queryStringValue: Que
|
||||||
private fun QueryStringValue.Case.toRealmCase(): Case {
|
private fun QueryStringValue.Case.toRealmCase(): Case {
|
||||||
return when (this) {
|
return when (this) {
|
||||||
QueryStringValue.Case.INSENSITIVE -> Case.INSENSITIVE
|
QueryStringValue.Case.INSENSITIVE -> Case.INSENSITIVE
|
||||||
QueryStringValue.Case.SENSITIVE -> Case.SENSITIVE
|
QueryStringValue.Case.SENSITIVE -> Case.SENSITIVE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -166,19 +166,7 @@ internal class RoomSummaryDataSource @Inject constructor(@SessionDatabase privat
|
||||||
pagedListConfig: PagedList.Config,
|
pagedListConfig: PagedList.Config,
|
||||||
sortOrder: RoomSortOrder): LiveData<PagedList<RoomSummary>> {
|
sortOrder: RoomSortOrder): LiveData<PagedList<RoomSummary>> {
|
||||||
val realmDataSourceFactory = monarchy.createDataSourceFactory { realm ->
|
val realmDataSourceFactory = monarchy.createDataSourceFactory { realm ->
|
||||||
roomSummariesQuery(realm, queryParams)
|
roomSummariesQuery(realm, queryParams).process(sortOrder)
|
||||||
.apply {
|
|
||||||
when (sortOrder) {
|
|
||||||
RoomSortOrder.NAME -> {
|
|
||||||
sort(RoomSummaryEntityFields.DISPLAY_NAME, Sort.ASCENDING)
|
|
||||||
}
|
|
||||||
RoomSortOrder.ACTIVITY -> {
|
|
||||||
sort(RoomSummaryEntityFields.LAST_ACTIVITY_TIME, Sort.DESCENDING)
|
|
||||||
}
|
|
||||||
RoomSortOrder.NONE -> {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
val dataSourceFactory = realmDataSourceFactory.map {
|
val dataSourceFactory = realmDataSourceFactory.map {
|
||||||
roomSummaryMapper.map(it)
|
roomSummaryMapper.map(it)
|
||||||
|
@ -193,19 +181,7 @@ internal class RoomSummaryDataSource @Inject constructor(@SessionDatabase privat
|
||||||
pagedListConfig: PagedList.Config,
|
pagedListConfig: PagedList.Config,
|
||||||
sortOrder: RoomSortOrder): UpdatableLivePageResult {
|
sortOrder: RoomSortOrder): UpdatableLivePageResult {
|
||||||
val realmDataSourceFactory = monarchy.createDataSourceFactory { realm ->
|
val realmDataSourceFactory = monarchy.createDataSourceFactory { realm ->
|
||||||
roomSummariesQuery(realm, queryParams)
|
roomSummariesQuery(realm, queryParams).process(sortOrder)
|
||||||
.apply {
|
|
||||||
when (sortOrder) {
|
|
||||||
RoomSortOrder.NAME -> {
|
|
||||||
sort(RoomSummaryEntityFields.DISPLAY_NAME, Sort.ASCENDING)
|
|
||||||
}
|
|
||||||
RoomSortOrder.ACTIVITY -> {
|
|
||||||
sort(RoomSummaryEntityFields.LAST_ACTIVITY_TIME, Sort.DESCENDING)
|
|
||||||
}
|
|
||||||
RoomSortOrder.NONE -> {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
val dataSourceFactory = realmDataSourceFactory.map {
|
val dataSourceFactory = realmDataSourceFactory.map {
|
||||||
roomSummaryMapper.map(it)
|
roomSummaryMapper.map(it)
|
||||||
|
@ -221,19 +197,7 @@ internal class RoomSummaryDataSource @Inject constructor(@SessionDatabase privat
|
||||||
|
|
||||||
override fun updateQuery(builder: (RoomSummaryQueryParams) -> RoomSummaryQueryParams) {
|
override fun updateQuery(builder: (RoomSummaryQueryParams) -> RoomSummaryQueryParams) {
|
||||||
realmDataSourceFactory.updateQuery {
|
realmDataSourceFactory.updateQuery {
|
||||||
roomSummariesQuery(it, builder.invoke(queryParams))
|
roomSummariesQuery(it, builder.invoke(queryParams)).process(sortOrder)
|
||||||
.apply {
|
|
||||||
when (sortOrder) {
|
|
||||||
RoomSortOrder.NAME -> {
|
|
||||||
sort(RoomSummaryEntityFields.DISPLAY_NAME, Sort.ASCENDING)
|
|
||||||
}
|
|
||||||
RoomSortOrder.ACTIVITY -> {
|
|
||||||
sort(RoomSummaryEntityFields.LAST_ACTIVITY_TIME, Sort.DESCENDING)
|
|
||||||
}
|
|
||||||
RoomSortOrder.NONE -> {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,6 @@ import androidx.appcompat.app.AlertDialog
|
||||||
import com.airbnb.mvrx.Loading
|
import com.airbnb.mvrx.Loading
|
||||||
import com.airbnb.mvrx.activityViewModel
|
import com.airbnb.mvrx.activityViewModel
|
||||||
import com.airbnb.mvrx.fragmentViewModel
|
import com.airbnb.mvrx.fragmentViewModel
|
||||||
import com.airbnb.mvrx.withState
|
|
||||||
import com.jakewharton.rxbinding3.appcompat.queryTextChanges
|
import com.jakewharton.rxbinding3.appcompat.queryTextChanges
|
||||||
import im.vector.app.R
|
import im.vector.app.R
|
||||||
import im.vector.app.core.extensions.cleanup
|
import im.vector.app.core.extensions.cleanup
|
||||||
|
@ -117,7 +116,8 @@ class SpaceAddRoomFragment @Inject constructor(
|
||||||
.setNegativeButton(R.string.cancel, null)
|
.setNegativeButton(R.string.cancel, null)
|
||||||
.show()
|
.show()
|
||||||
}
|
}
|
||||||
SpaceAddRoomsViewEvents.SaveFailed -> {
|
is SpaceAddRoomsViewEvents.SaveFailed -> {
|
||||||
|
showErrorInSnackbar(it.reason)
|
||||||
invalidateOptionsMenu()
|
invalidateOptionsMenu()
|
||||||
}
|
}
|
||||||
SpaceAddRoomsViewEvents.SavedDone -> {
|
SpaceAddRoomsViewEvents.SavedDone -> {
|
||||||
|
@ -127,10 +127,6 @@ class SpaceAddRoomFragment @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun invalidate() = withState(viewModel) {
|
|
||||||
super.invalidate()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onPrepareOptionsMenu(menu: Menu) {
|
override fun onPrepareOptionsMenu(menu: Menu) {
|
||||||
super.onPrepareOptionsMenu(menu)
|
super.onPrepareOptionsMenu(menu)
|
||||||
menu.findItem(R.id.spaceAddRoomSaveItem).isVisible = saveNeeded
|
menu.findItem(R.id.spaceAddRoomSaveItem).isVisible = saveNeeded
|
||||||
|
|
|
@ -21,5 +21,5 @@ import im.vector.app.core.platform.VectorViewEvents
|
||||||
sealed class SpaceAddRoomsViewEvents : VectorViewEvents {
|
sealed class SpaceAddRoomsViewEvents : VectorViewEvents {
|
||||||
object WarnUnsavedChanged : SpaceAddRoomsViewEvents()
|
object WarnUnsavedChanged : SpaceAddRoomsViewEvents()
|
||||||
object SavedDone : SpaceAddRoomsViewEvents()
|
object SavedDone : SpaceAddRoomsViewEvents()
|
||||||
object SaveFailed : SpaceAddRoomsViewEvents()
|
data class SaveFailed(val reason: Throwable): SpaceAddRoomsViewEvents()
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.model.Membership
|
||||||
import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams
|
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(
|
class SpaceAddRoomsViewModel @AssistedInject constructor(
|
||||||
@Assisted val initialState: SpaceAddRoomsState,
|
@Assisted val initialState: SpaceAddRoomsState,
|
||||||
|
@ -186,7 +190,7 @@ class SpaceAddRoomsViewModel @AssistedInject constructor(
|
||||||
isSaving = Fail(AddRoomError(errors))
|
isSaving = Fail(AddRoomError(errors))
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
_viewEvents.post(SpaceAddRoomsViewEvents.SaveFailed)
|
_viewEvents.post(SpaceAddRoomsViewEvents.SaveFailed(AddRoomError(errors)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue