diff --git a/vector/src/main/java/im/vector/riotx/core/di/ScreenComponent.kt b/vector/src/main/java/im/vector/riotx/core/di/ScreenComponent.kt index da9daac68d..0efbc0e173 100644 --- a/vector/src/main/java/im/vector/riotx/core/di/ScreenComponent.kt +++ b/vector/src/main/java/im/vector/riotx/core/di/ScreenComponent.kt @@ -41,11 +41,12 @@ import im.vector.riotx.features.home.createdirect.CreateDirectRoomKnownUsersFrag import im.vector.riotx.features.home.group.GroupListFragment import im.vector.riotx.features.home.room.detail.RoomDetailFragment import im.vector.riotx.features.home.room.detail.readreceipts.DisplayReadReceiptsBottomSheet -import im.vector.riotx.features.home.room.detail.timeline.action.* +import im.vector.riotx.features.home.room.detail.timeline.action.MessageActionsBottomSheet import im.vector.riotx.features.home.room.detail.timeline.edithistory.ViewEditHistoryBottomSheet import im.vector.riotx.features.home.room.detail.timeline.reactions.ViewReactionsBottomSheet import im.vector.riotx.features.home.room.filtered.FilteredRoomsActivity import im.vector.riotx.features.home.room.list.RoomListFragment +import im.vector.riotx.features.home.room.list.RoomListModule import im.vector.riotx.features.invite.VectorInviteView import im.vector.riotx.features.link.LinkHandlerActivity import im.vector.riotx.features.login.LoginActivity @@ -70,7 +71,17 @@ import im.vector.riotx.features.settings.push.PushGatewaysFragment import im.vector.riotx.features.share.IncomingShareActivity import im.vector.riotx.features.ui.UiStateRepository -@Component(dependencies = [VectorComponent::class], modules = [AssistedInjectModule::class, ViewModelModule::class, HomeModule::class]) +@Component( + dependencies = [ + VectorComponent::class + ], + modules = [ + AssistedInjectModule::class, + ViewModelModule::class, + HomeModule::class, + RoomListModule::class + ] +) @ScreenScope interface ScreenComponent { diff --git a/vector/src/main/java/im/vector/riotx/core/di/VectorComponent.kt b/vector/src/main/java/im/vector/riotx/core/di/VectorComponent.kt index a59620aacb..2dfbb5f799 100644 --- a/vector/src/main/java/im/vector/riotx/core/di/VectorComponent.kt +++ b/vector/src/main/java/im/vector/riotx/core/di/VectorComponent.kt @@ -23,6 +23,7 @@ import dagger.Component import im.vector.matrix.android.api.Matrix import im.vector.matrix.android.api.auth.Authenticator import im.vector.matrix.android.api.session.Session +import im.vector.riotx.ActiveSessionObservableStore import im.vector.riotx.EmojiCompatFontProvider import im.vector.riotx.EmojiCompatWrapper import im.vector.riotx.VectorApplication @@ -42,6 +43,7 @@ import im.vector.riotx.features.rageshake.VectorFileLogger import im.vector.riotx.features.rageshake.VectorUncaughtExceptionHandler import im.vector.riotx.features.session.SessionListener import im.vector.riotx.features.settings.VectorPreferences +import im.vector.riotx.features.share.ShareRoomListObservableStore import im.vector.riotx.features.ui.UiStateRepository import javax.inject.Singleton @@ -85,8 +87,12 @@ interface VectorComponent { fun homeRoomListObservableStore(): HomeRoomListObservableStore + fun shareRoomListObservableStore(): ShareRoomListObservableStore + fun selectedGroupStore(): SelectedGroupStore + fun activeSessionObservableStore(): ActiveSessionObservableStore + fun incomingVerificationRequestHandler(): IncomingVerificationRequestHandler fun incomingKeyRequestHandler(): KeyRequestHandler diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListModule.kt b/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListModule.kt new file mode 100644 index 0000000000..4541b5d2b5 --- /dev/null +++ b/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListModule.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2019 New Vector Ltd + * + * 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 im.vector.riotx.features.home.room.list + +import dagger.Binds +import dagger.Module + +@Module +abstract class RoomListModule { + + @Binds + abstract fun providesRoomListViewModelFactory(roomListViewModelFactory: RoomListViewModelFactory): RoomListViewModel.Factory +} diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListViewModel.kt b/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListViewModel.kt index 686693c776..c413a09c8a 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListViewModel.kt @@ -21,8 +21,6 @@ import androidx.lifecycle.MutableLiveData import com.airbnb.mvrx.FragmentViewModelContext 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.MatrixCallback import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.room.model.Membership @@ -31,18 +29,18 @@ import im.vector.matrix.android.api.session.room.model.tag.RoomTag import im.vector.riotx.core.extensions.postLiveEvent import im.vector.riotx.core.platform.VectorViewModel import im.vector.riotx.core.utils.LiveEvent -import im.vector.riotx.features.home.HomeRoomListObservableStore +import im.vector.riotx.core.utils.RxStore import io.reactivex.schedulers.Schedulers import timber.log.Timber +import javax.inject.Inject -class RoomListViewModel @AssistedInject constructor(@Assisted initialState: RoomListViewState, - private val session: Session, - private val homeRoomListObservableStore: HomeRoomListObservableStore, - private val alphabeticalRoomComparator: AlphabeticalRoomComparator, - private val chronologicalRoomComparator: ChronologicalRoomComparator) +class RoomListViewModel @Inject constructor(initialState: RoomListViewState, + private val session: Session, + private val roomSummariesStore: RxStore>, + private val alphabeticalRoomComparator: AlphabeticalRoomComparator, + private val chronologicalRoomComparator: ChronologicalRoomComparator) : VectorViewModel(initialState) { - @AssistedInject.Factory interface Factory { fun create(initialState: RoomListViewState): RoomListViewModel } @@ -101,7 +99,7 @@ class RoomListViewModel @AssistedInject constructor(@Assisted initialState: Room } private fun observeRoomSummaries() { - homeRoomListObservableStore + roomSummariesStore .observe() .observeOn(Schedulers.computation()) .map { @@ -111,7 +109,7 @@ class RoomListViewModel @AssistedInject constructor(@Assisted initialState: Room copy(asyncRooms = asyncRooms) } - homeRoomListObservableStore + roomSummariesStore .observe() .observeOn(Schedulers.computation()) .map { buildRoomSummaries(it) } diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListViewModelFactory.kt b/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListViewModelFactory.kt new file mode 100644 index 0000000000..5895aa4e52 --- /dev/null +++ b/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListViewModelFactory.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2019 New Vector Ltd + * + * 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 im.vector.riotx.features.home.room.list + +import im.vector.matrix.android.api.session.Session +import im.vector.riotx.features.home.HomeRoomListObservableStore +import im.vector.riotx.features.share.ShareRoomListObservableStore +import javax.inject.Inject +import javax.inject.Provider + +class RoomListViewModelFactory @Inject constructor(private val session: Provider, + private val homeRoomListObservableStore: Provider, + private val shareRoomListObservableStore: Provider, + private val alphabeticalRoomComparator: Provider, + private val chronologicalRoomComparator: Provider) : RoomListViewModel.Factory { + + override fun create(initialState: RoomListViewState): RoomListViewModel { + return RoomListViewModel( + initialState, + session.get(), + if (initialState.displayMode == RoomListFragment.DisplayMode.SHARE) shareRoomListObservableStore.get() else homeRoomListObservableStore.get(), + alphabeticalRoomComparator.get(), + chronologicalRoomComparator.get()) + } +} diff --git a/vector/src/main/java/im/vector/riotx/features/share/IncomingShareActivity.kt b/vector/src/main/java/im/vector/riotx/features/share/IncomingShareActivity.kt index 0ce28f1f4b..5e471cf78b 100644 --- a/vector/src/main/java/im/vector/riotx/features/share/IncomingShareActivity.kt +++ b/vector/src/main/java/im/vector/riotx/features/share/IncomingShareActivity.kt @@ -21,6 +21,7 @@ import android.content.Intent import android.os.Bundle import android.widget.Toast import androidx.appcompat.widget.SearchView +import com.airbnb.mvrx.viewModel import com.kbeanie.multipicker.utils.IntentUtils import im.vector.matrix.android.api.session.content.ContentAttachmentData import im.vector.riotx.R @@ -40,8 +41,10 @@ class IncomingShareActivity : VectorBaseActivity(), AttachmentsHelper.Callback { @Inject lateinit var sessionHolder: ActiveSessionHolder + @Inject lateinit var incomingShareViewModelFactory: IncomingShareViewModel.Factory private var roomListFragment: RoomListFragment? = null private lateinit var attachmentsHelper: AttachmentsHelper + private val incomingShareViewModel: IncomingShareViewModel by viewModel() override fun getLayoutRes(): Int { return R.layout.activity_incoming_share diff --git a/vector/src/main/java/im/vector/riotx/features/share/IncomingShareViewModel.kt b/vector/src/main/java/im/vector/riotx/features/share/IncomingShareViewModel.kt new file mode 100644 index 0000000000..51485ecbf9 --- /dev/null +++ b/vector/src/main/java/im/vector/riotx/features/share/IncomingShareViewModel.kt @@ -0,0 +1,73 @@ +/* + * Copyright 2019 New Vector Ltd + * + * 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 im.vector.riotx.features.share + +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.rx.rx +import im.vector.riotx.ActiveSessionObservableStore +import im.vector.riotx.core.platform.VectorViewModel +import io.reactivex.Observable +import io.reactivex.android.schedulers.AndroidSchedulers +import java.util.concurrent.TimeUnit + +data class IncomingShareState(private val dummy: Boolean = false) : MvRxState + +/** + * View model used to observe the room list and post update to the ShareRoomListObservableStore + */ +class IncomingShareViewModel @AssistedInject constructor(@Assisted initialState: IncomingShareState, + private val sessionObservableStore: ActiveSessionObservableStore, + private val shareRoomListObservableStore: ShareRoomListObservableStore) + : VectorViewModel(initialState) { + + @AssistedInject.Factory + interface Factory { + fun create(initialState: IncomingShareState): IncomingShareViewModel + } + + companion object : MvRxViewModelFactory { + + @JvmStatic + override fun create(viewModelContext: ViewModelContext, state: IncomingShareState): IncomingShareViewModel? { + val activity: IncomingShareActivity = (viewModelContext as ActivityViewModelContext).activity() + return activity.incomingShareViewModelFactory.create(state) + } + } + + init { + observeRoomSummaries() + } + + private fun observeRoomSummaries() { + sessionObservableStore.observe() + .observeOn(AndroidSchedulers.mainThread()) + .switchMap { + it.orNull()?.rx()?.liveRoomSummaries() + ?: Observable.just(emptyList()) + } + .throttleLast(300, TimeUnit.MILLISECONDS) + .subscribe { + shareRoomListObservableStore.post(it) + } + .disposeOnClear() + } +} diff --git a/vector/src/main/java/im/vector/riotx/features/share/ShareRoomListObservableStore.kt b/vector/src/main/java/im/vector/riotx/features/share/ShareRoomListObservableStore.kt new file mode 100644 index 0000000000..c46ec42d64 --- /dev/null +++ b/vector/src/main/java/im/vector/riotx/features/share/ShareRoomListObservableStore.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2019 New Vector Ltd + * + * 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 im.vector.riotx.features.share + +import im.vector.matrix.android.api.session.room.model.RoomSummary +import im.vector.riotx.core.utils.RxStore +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class ShareRoomListObservableStore @Inject constructor() : RxStore>()