Removes the RoomList handling from a viewmodel as it doesn't have a ViewState and should be provided globally (IE, from application state)

This commit is contained in:
ganfra 2019-10-15 11:24:20 +02:00
parent c28be6adb0
commit 3c3c6aeac6
3 changed files with 37 additions and 36 deletions

View File

@ -14,56 +14,57 @@
* limitations under the License.
*/
package im.vector.riotx.features.home
package im.vector.riotx
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import arrow.core.Option
import com.airbnb.mvrx.ActivityViewModelContext
import com.airbnb.mvrx.MvRxState
import com.airbnb.mvrx.MvRxViewModelFactory
import com.airbnb.mvrx.ViewModelContext
import com.squareup.inject.assisted.Assisted
import com.squareup.inject.assisted.AssistedInject
import im.vector.matrix.android.api.session.Session
import im.vector.matrix.android.api.session.group.model.GroupSummary
import im.vector.matrix.android.api.session.room.model.RoomSummary
import im.vector.matrix.rx.rx
import im.vector.riotx.core.platform.VectorViewModel
import im.vector.riotx.core.di.ActiveSessionHolder
import im.vector.riotx.features.home.HomeRoomListObservableStore
import im.vector.riotx.features.home.group.ALL_COMMUNITIES_GROUP_ID
import im.vector.riotx.features.home.group.SelectedGroupStore
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.functions.BiFunction
import io.reactivex.rxkotlin.addTo
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Singleton
data class EmptyState(val isEmpty: Boolean = true) : MvRxState
class HomeActivityViewModel @AssistedInject constructor(@Assisted initialState: EmptyState,
private val session: Session,
private val selectedGroupStore: SelectedGroupStore,
private val homeRoomListStore: HomeRoomListObservableStore
) : VectorViewModel<EmptyState>(initialState) {
/**
* This class handles the main room list at the moment. It will dispatch the results to the store.
* It requires to be added with ProcessLifecycleOwner.get().lifecycle.addObserver
*/
@Singleton
class AppStateHandler @Inject constructor(
private val activeSessionHolder: ActiveSessionHolder,
private val homeRoomListStore: HomeRoomListObservableStore,
private val selectedGroupStore: SelectedGroupStore) : LifecycleObserver {
@AssistedInject.Factory
interface Factory {
fun create(initialState: EmptyState): HomeActivityViewModel
private val compositeDisposable = CompositeDisposable()
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun entersForeground() {
observeRoomsAndGroup()
}
companion object : MvRxViewModelFactory<HomeActivityViewModel, EmptyState> {
@JvmStatic
override fun create(viewModelContext: ViewModelContext, state: EmptyState): HomeActivityViewModel? {
val homeActivity: HomeActivity = (viewModelContext as ActivityViewModelContext).activity()
return homeActivity.homeActivityViewModelFactory.create(state)
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun entersBackground() {
compositeDisposable.clear()
}
init {
observeRoomAndGroup()
}
private fun observeRoomAndGroup() {
private fun observeRoomsAndGroup() {
Observable
.combineLatest<List<RoomSummary>, Option<GroupSummary>, List<RoomSummary>>(
session.rx().liveRoomSummaries().throttleLast(300, TimeUnit.MILLISECONDS),
activeSessionHolder.getActiveSession()
.rx()
.liveRoomSummaries()
.throttleLast(300, TimeUnit.MILLISECONDS),
selectedGroupStore.observe(),
BiFunction { rooms, selectedGroupOption ->
val selectedGroup = selectedGroupOption.orNull()
@ -83,7 +84,7 @@ class HomeActivityViewModel @AssistedInject constructor(@Assisted initialState:
.filter { !it.isDirect }
.filter {
selectedGroup?.groupId == ALL_COMMUNITIES_GROUP_ID
|| selectedGroup?.roomIds?.contains(it.roomId) ?: true
|| selectedGroup?.roomIds?.contains(it.roomId) ?: true
}
filteredDirectRooms + filteredGroupRooms
}
@ -91,6 +92,6 @@ class HomeActivityViewModel @AssistedInject constructor(@Assisted initialState:
.subscribe {
homeRoomListStore.post(it)
}
.disposeOnClear()
.addTo(compositeDisposable)
}
}

View File

@ -74,6 +74,7 @@ class VectorApplication : Application(), HasVectorInjector, MatrixConfiguration.
@Inject lateinit var vectorPreferences: VectorPreferences
@Inject lateinit var versionProvider: VersionProvider
@Inject lateinit var notificationUtils: NotificationUtils
@Inject lateinit var appStateHandler: AppStateHandler
lateinit var vectorComponent: VectorComponent
private var fontThreadHandler: Handler? = null
@ -134,7 +135,8 @@ class VectorApplication : Application(), HasVectorInjector, MatrixConfiguration.
FcmHelper.onEnterBackground(appContext, vectorPreferences, activeSessionHolder)
}
})
// This should be done as early as possible
ProcessLifecycleOwner.get().lifecycle.addObserver(appStateHandler)
//This should be done as early as possible
initKnownEmojiHashSet(appContext)
}

View File

@ -55,11 +55,9 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable {
object OpenGroup : Navigation()
}
private val homeActivityViewModel: HomeActivityViewModel by viewModel()
private lateinit var navigationViewModel: HomeNavigationViewModel
@Inject lateinit var activeSessionHolder: ActiveSessionHolder
@Inject lateinit var homeActivityViewModelFactory: HomeActivityViewModel.Factory
@Inject lateinit var vectorUncaughtExceptionHandler: VectorUncaughtExceptionHandler
@Inject lateinit var pushManager: PushersManager
@Inject lateinit var notificationDrawerManager: NotificationDrawerManager