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. * 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 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.group.model.GroupSummary
import im.vector.matrix.android.api.session.room.model.RoomSummary import im.vector.matrix.android.api.session.room.model.RoomSummary
import im.vector.matrix.rx.rx 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.ALL_COMMUNITIES_GROUP_ID
import im.vector.riotx.features.home.group.SelectedGroupStore import im.vector.riotx.features.home.group.SelectedGroupStore
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.functions.BiFunction import io.reactivex.functions.BiFunction
import io.reactivex.rxkotlin.addTo
import java.util.concurrent.TimeUnit 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, * This class handles the main room list at the moment. It will dispatch the results to the store.
private val selectedGroupStore: SelectedGroupStore, * It requires to be added with ProcessLifecycleOwner.get().lifecycle.addObserver
private val homeRoomListStore: HomeRoomListObservableStore */
) : VectorViewModel<EmptyState>(initialState) { @Singleton
class AppStateHandler @Inject constructor(
private val activeSessionHolder: ActiveSessionHolder,
private val homeRoomListStore: HomeRoomListObservableStore,
private val selectedGroupStore: SelectedGroupStore) : LifecycleObserver {
@AssistedInject.Factory private val compositeDisposable = CompositeDisposable()
interface Factory {
fun create(initialState: EmptyState): HomeActivityViewModel @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun entersForeground() {
observeRoomsAndGroup()
} }
companion object : MvRxViewModelFactory<HomeActivityViewModel, EmptyState> { @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun entersBackground() {
@JvmStatic compositeDisposable.clear()
override fun create(viewModelContext: ViewModelContext, state: EmptyState): HomeActivityViewModel? {
val homeActivity: HomeActivity = (viewModelContext as ActivityViewModelContext).activity()
return homeActivity.homeActivityViewModelFactory.create(state)
}
} }
init { private fun observeRoomsAndGroup() {
observeRoomAndGroup()
}
private fun observeRoomAndGroup() {
Observable Observable
.combineLatest<List<RoomSummary>, Option<GroupSummary>, List<RoomSummary>>( .combineLatest<List<RoomSummary>, Option<GroupSummary>, List<RoomSummary>>(
session.rx().liveRoomSummaries().throttleLast(300, TimeUnit.MILLISECONDS), activeSessionHolder.getActiveSession()
.rx()
.liveRoomSummaries()
.throttleLast(300, TimeUnit.MILLISECONDS),
selectedGroupStore.observe(), selectedGroupStore.observe(),
BiFunction { rooms, selectedGroupOption -> BiFunction { rooms, selectedGroupOption ->
val selectedGroup = selectedGroupOption.orNull() val selectedGroup = selectedGroupOption.orNull()
@ -91,6 +92,6 @@ class HomeActivityViewModel @AssistedInject constructor(@Assisted initialState:
.subscribe { .subscribe {
homeRoomListStore.post(it) 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 vectorPreferences: VectorPreferences
@Inject lateinit var versionProvider: VersionProvider @Inject lateinit var versionProvider: VersionProvider
@Inject lateinit var notificationUtils: NotificationUtils @Inject lateinit var notificationUtils: NotificationUtils
@Inject lateinit var appStateHandler: AppStateHandler
lateinit var vectorComponent: VectorComponent lateinit var vectorComponent: VectorComponent
private var fontThreadHandler: Handler? = null private var fontThreadHandler: Handler? = null
@ -134,7 +135,8 @@ class VectorApplication : Application(), HasVectorInjector, MatrixConfiguration.
FcmHelper.onEnterBackground(appContext, vectorPreferences, activeSessionHolder) 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) initKnownEmojiHashSet(appContext)
} }

View File

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