From 3c3c6aeac63dd7946adb3dd439701e7747450138 Mon Sep 17 00:00:00 2001 From: ganfra Date: Tue, 15 Oct 2019 11:24:20 +0200 Subject: [PATCH] Removes the RoomList handling from a viewmodel as it doesn't have a ViewState and should be provided globally (IE, from application state) --- ...ctivityViewModel.kt => AppStateHandler.kt} | 67 ++++++++++--------- .../java/im/vector/riotx/VectorApplication.kt | 4 +- .../riotx/features/home/HomeActivity.kt | 2 - 3 files changed, 37 insertions(+), 36 deletions(-) rename vector/src/main/java/im/vector/riotx/{features/home/HomeActivityViewModel.kt => AppStateHandler.kt} (59%) diff --git a/vector/src/main/java/im/vector/riotx/features/home/HomeActivityViewModel.kt b/vector/src/main/java/im/vector/riotx/AppStateHandler.kt similarity index 59% rename from vector/src/main/java/im/vector/riotx/features/home/HomeActivityViewModel.kt rename to vector/src/main/java/im/vector/riotx/AppStateHandler.kt index 16d3f42824..4124f3c779 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/HomeActivityViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/AppStateHandler.kt @@ -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(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 { - - @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, Option, List>( - 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) } } diff --git a/vector/src/main/java/im/vector/riotx/VectorApplication.kt b/vector/src/main/java/im/vector/riotx/VectorApplication.kt index 081d1c69aa..3878420cc7 100644 --- a/vector/src/main/java/im/vector/riotx/VectorApplication.kt +++ b/vector/src/main/java/im/vector/riotx/VectorApplication.kt @@ -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) } diff --git a/vector/src/main/java/im/vector/riotx/features/home/HomeActivity.kt b/vector/src/main/java/im/vector/riotx/features/home/HomeActivity.kt index 9071b51acf..1a9ca04d7c 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/HomeActivity.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/HomeActivity.kt @@ -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