From 04f72dfcb82586e7c516a540b83b4306ede6fd46 Mon Sep 17 00:00:00 2001 From: ganfra Date: Thu, 7 Nov 2019 15:19:12 +0100 Subject: [PATCH] Clean code after Benoit's review --- ...bleStore.kt => ActiveSessionDataSource.kt} | 4 +- .../java/im/vector/riotx/AppStateHandler.kt | 16 +++--- .../riotx/core/di/ActiveSessionHolder.kt | 4 +- .../vector/riotx/core/di/VectorComponent.kt | 16 +++--- .../bottomsheet/BottomSheetItemAction.kt | 12 +++- .../core/utils/{RxStore.kt => DataSource.kt} | 16 +++--- .../features/home/HomeDetailViewModel.kt | 6 +- .../features/home/HomeNavigationViewModel.kt | 8 +-- ...ableStore.kt => HomeRoomListDataSource.kt} | 4 +- .../CreateDirectRoomNavigationViewModel.kt | 8 +-- .../features/home/group/GroupListViewModel.kt | 2 +- ...oupStore.kt => SelectedGroupDataSource.kt} | 4 +- .../home/room/detail/RoomDetailFragment.kt | 8 +-- .../action/MessageActionsBottomSheet.kt | 4 +- ...nsStore.kt => MessageActionsDispatcher.kt} | 10 ++-- .../home/room/list/RoomListFragment.kt | 55 +++++++++++++------ .../home/room/list/RoomListViewEvents.kt | 26 +++++++++ .../home/room/list/RoomListViewModel.kt | 46 +++++++--------- .../room/list/RoomListViewModelFactory.kt | 10 ++-- .../room/list/actions/RoomListQuickActions.kt | 18 +----- .../RoomListQuickActionsEpoxyController.kt | 25 ++++----- .../list/actions/RoomListQuickActionsStore.kt | 8 +-- .../RoomDirectoryNavigationViewModel.kt | 8 +-- .../features/share/IncomingShareViewModel.kt | 6 +- ...bleStore.kt => ShareRoomListDataSource.kt} | 4 +- 25 files changed, 180 insertions(+), 148 deletions(-) rename vector/src/main/java/im/vector/riotx/{ActiveSessionObservableStore.kt => ActiveSessionDataSource.kt} (84%) rename vector/src/main/java/im/vector/riotx/core/utils/{RxStore.kt => DataSource.kt} (78%) rename vector/src/main/java/im/vector/riotx/features/home/{HomeRoomListObservableStore.kt => HomeRoomListDataSource.kt} (84%) rename vector/src/main/java/im/vector/riotx/features/home/group/{SelectedGroupStore.kt => SelectedGroupDataSource.kt} (83%) rename vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/{MessageActionsStore.kt => MessageActionsDispatcher.kt} (71%) create mode 100644 vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListViewEvents.kt rename vector/src/main/java/im/vector/riotx/features/share/{ShareRoomListObservableStore.kt => ShareRoomListDataSource.kt} (83%) diff --git a/vector/src/main/java/im/vector/riotx/ActiveSessionObservableStore.kt b/vector/src/main/java/im/vector/riotx/ActiveSessionDataSource.kt similarity index 84% rename from vector/src/main/java/im/vector/riotx/ActiveSessionObservableStore.kt rename to vector/src/main/java/im/vector/riotx/ActiveSessionDataSource.kt index cb89497577..4cafdb8d11 100644 --- a/vector/src/main/java/im/vector/riotx/ActiveSessionObservableStore.kt +++ b/vector/src/main/java/im/vector/riotx/ActiveSessionDataSource.kt @@ -19,9 +19,9 @@ package im.vector.riotx import arrow.core.Option import im.vector.matrix.android.api.session.Session -import im.vector.riotx.core.utils.BehaviorStore +import im.vector.riotx.core.utils.BehaviorDataSource import javax.inject.Inject import javax.inject.Singleton @Singleton -class ActiveSessionObservableStore @Inject constructor() : BehaviorStore>() +class ActiveSessionDataSource @Inject constructor() : BehaviorDataSource>() diff --git a/vector/src/main/java/im/vector/riotx/AppStateHandler.kt b/vector/src/main/java/im/vector/riotx/AppStateHandler.kt index 76cbb9ef94..7cb51b6373 100644 --- a/vector/src/main/java/im/vector/riotx/AppStateHandler.kt +++ b/vector/src/main/java/im/vector/riotx/AppStateHandler.kt @@ -23,9 +23,9 @@ import arrow.core.Option 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.features.home.HomeRoomListObservableStore +import im.vector.riotx.features.home.HomeRoomListDataSource 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.SelectedGroupDataSource import io.reactivex.Observable import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable @@ -41,9 +41,9 @@ import javax.inject.Singleton */ @Singleton class AppStateHandler @Inject constructor( - private val sessionObservableStore: ActiveSessionObservableStore, - private val homeRoomListObservableStore: HomeRoomListObservableStore, - private val selectedGroupStore: SelectedGroupStore) : LifecycleObserver { + private val sessionDataSource: ActiveSessionDataSource, + private val homeRoomListDataSource: HomeRoomListDataSource, + private val selectedGroupDataSource: SelectedGroupDataSource) : LifecycleObserver { private val compositeDisposable = CompositeDisposable() @@ -60,14 +60,14 @@ class AppStateHandler @Inject constructor( private fun observeRoomsAndGroup() { Observable .combineLatest, Option, List>( - sessionObservableStore.observe() + sessionDataSource.observe() .observeOn(AndroidSchedulers.mainThread()) .switchMap { it.orNull()?.rx()?.liveRoomSummaries() ?: Observable.just(emptyList()) } .throttleLast(300, TimeUnit.MILLISECONDS), - selectedGroupStore.observe(), + selectedGroupDataSource.observe(), BiFunction { rooms, selectedGroupOption -> val selectedGroup = selectedGroupOption.orNull() val filteredDirectRooms = rooms @@ -92,7 +92,7 @@ class AppStateHandler @Inject constructor( } ) .subscribe { - homeRoomListObservableStore.post(it) + homeRoomListDataSource.post(it) } .addTo(compositeDisposable) } diff --git a/vector/src/main/java/im/vector/riotx/core/di/ActiveSessionHolder.kt b/vector/src/main/java/im/vector/riotx/core/di/ActiveSessionHolder.kt index da3c041a1c..3eccb668ea 100644 --- a/vector/src/main/java/im/vector/riotx/core/di/ActiveSessionHolder.kt +++ b/vector/src/main/java/im/vector/riotx/core/di/ActiveSessionHolder.kt @@ -19,7 +19,7 @@ package im.vector.riotx.core.di import arrow.core.Option 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.ActiveSessionDataSource import im.vector.riotx.features.crypto.keysrequest.KeyRequestHandler import im.vector.riotx.features.crypto.verification.IncomingVerificationRequestHandler import java.util.concurrent.atomic.AtomicReference @@ -28,7 +28,7 @@ import javax.inject.Singleton @Singleton class ActiveSessionHolder @Inject constructor(private val authenticator: Authenticator, - private val sessionObservableStore: ActiveSessionObservableStore, + private val sessionObservableStore: ActiveSessionDataSource, private val keyRequestHandler: KeyRequestHandler, private val incomingVerificationRequestHandler: IncomingVerificationRequestHandler ) { 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 2dfbb5f799..d31955ce8e 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,7 +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.ActiveSessionDataSource import im.vector.riotx.EmojiCompatFontProvider import im.vector.riotx.EmojiCompatWrapper import im.vector.riotx.VectorApplication @@ -33,8 +33,8 @@ import im.vector.riotx.features.configuration.VectorConfiguration import im.vector.riotx.features.crypto.keysrequest.KeyRequestHandler import im.vector.riotx.features.crypto.verification.IncomingVerificationRequestHandler import im.vector.riotx.features.home.AvatarRenderer -import im.vector.riotx.features.home.HomeRoomListObservableStore -import im.vector.riotx.features.home.group.SelectedGroupStore +import im.vector.riotx.features.home.HomeRoomListDataSource +import im.vector.riotx.features.home.group.SelectedGroupDataSource import im.vector.riotx.features.html.EventHtmlRenderer import im.vector.riotx.features.navigation.Navigator import im.vector.riotx.features.notifications.* @@ -43,7 +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.share.ShareRoomListDataSource import im.vector.riotx.features.ui.UiStateRepository import javax.inject.Singleton @@ -85,13 +85,13 @@ interface VectorComponent { fun navigator(): Navigator - fun homeRoomListObservableStore(): HomeRoomListObservableStore + fun homeRoomListObservableStore(): HomeRoomListDataSource - fun shareRoomListObservableStore(): ShareRoomListObservableStore + fun shareRoomListObservableStore(): ShareRoomListDataSource - fun selectedGroupStore(): SelectedGroupStore + fun selectedGroupStore(): SelectedGroupDataSource - fun activeSessionObservableStore(): ActiveSessionObservableStore + fun activeSessionObservableStore(): ActiveSessionDataSource fun incomingVerificationRequestHandler(): IncomingVerificationRequestHandler diff --git a/vector/src/main/java/im/vector/riotx/core/epoxy/bottomsheet/BottomSheetItemAction.kt b/vector/src/main/java/im/vector/riotx/core/epoxy/bottomsheet/BottomSheetItemAction.kt index e0299f4e65..483650a434 100644 --- a/vector/src/main/java/im/vector/riotx/core/epoxy/bottomsheet/BottomSheetItemAction.kt +++ b/vector/src/main/java/im/vector/riotx/core/epoxy/bottomsheet/BottomSheetItemAction.kt @@ -16,6 +16,7 @@ */ package im.vector.riotx.core.epoxy.bottomsheet +import android.content.res.ColorStateList import android.view.View import android.widget.ImageView import android.widget.TextView @@ -24,6 +25,7 @@ import androidx.core.content.ContextCompat import androidx.core.graphics.drawable.DrawableCompat import androidx.core.view.isInvisible import androidx.core.view.isVisible +import androidx.core.widget.ImageViewCompat import com.airbnb.epoxy.EpoxyAttribute import com.airbnb.epoxy.EpoxyModelClass import im.vector.riotx.R @@ -51,6 +53,8 @@ abstract class BottomSheetItemAction : VectorEpoxyModel { +interface DataSource { fun observe(): Observable +} + +interface MutableDataSource : DataSource { fun post(value: T) } /** - * This store emits the most recent value it has observed and all subsequent observed values to each subscriber. + * This datasource emits the most recent value it has observed and all subsequent observed values to each subscriber. */ -open class BehaviorStore(private val defaultValue: T? = null) : RxStore { +open class BehaviorDataSource(private val defaultValue: T? = null) : MutableDataSource { private val storeRelay = createRelay() @@ -54,9 +54,9 @@ open class BehaviorStore(private val defaultValue: T? = null) : RxStore { } /** - * This store only emits all subsequent observed values to each subscriber. + * This datasource only emits all subsequent observed values to each subscriber. */ -open class PublishStore : RxStore { +open class PublishDataSource : MutableDataSource { private val storeRelay = PublishRelay.create() diff --git a/vector/src/main/java/im/vector/riotx/features/home/HomeDetailViewModel.kt b/vector/src/main/java/im/vector/riotx/features/home/HomeDetailViewModel.kt index c4dcd79ea0..585ecf126e 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/HomeDetailViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/HomeDetailViewModel.kt @@ -26,7 +26,7 @@ import im.vector.matrix.rx.rx import im.vector.riotx.core.di.HasScreenInjector import im.vector.riotx.core.platform.VectorViewModel import im.vector.riotx.core.resources.StringProvider -import im.vector.riotx.features.home.group.SelectedGroupStore +import im.vector.riotx.features.home.group.SelectedGroupDataSource import im.vector.riotx.features.home.room.list.RoomListFragment import im.vector.riotx.features.ui.UiStateRepository import io.reactivex.schedulers.Schedulers @@ -38,8 +38,8 @@ import io.reactivex.schedulers.Schedulers class HomeDetailViewModel @AssistedInject constructor(@Assisted initialState: HomeDetailViewState, private val session: Session, private val uiStateRepository: UiStateRepository, - private val selectedGroupStore: SelectedGroupStore, - private val homeRoomListStore: HomeRoomListObservableStore, + private val selectedGroupStore: SelectedGroupDataSource, + private val homeRoomListStore: HomeRoomListDataSource, private val stringProvider: StringProvider) : VectorViewModel(initialState) { diff --git a/vector/src/main/java/im/vector/riotx/features/home/HomeNavigationViewModel.kt b/vector/src/main/java/im/vector/riotx/features/home/HomeNavigationViewModel.kt index 122eee1833..81c340e400 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/HomeNavigationViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/HomeNavigationViewModel.kt @@ -17,8 +17,8 @@ package im.vector.riotx.features.home import androidx.lifecycle.ViewModel -import im.vector.riotx.core.utils.PublishStore -import im.vector.riotx.core.utils.RxStore +import im.vector.riotx.core.utils.PublishDataSource +import im.vector.riotx.core.utils.MutableDataSource -class HomeNavigationViewModel(private val store: RxStore = PublishStore()) - : ViewModel(), RxStore by store +class HomeNavigationViewModel(private val source: MutableDataSource = PublishDataSource()) + : ViewModel(), MutableDataSource by source diff --git a/vector/src/main/java/im/vector/riotx/features/home/HomeRoomListObservableStore.kt b/vector/src/main/java/im/vector/riotx/features/home/HomeRoomListDataSource.kt similarity index 84% rename from vector/src/main/java/im/vector/riotx/features/home/HomeRoomListObservableStore.kt rename to vector/src/main/java/im/vector/riotx/features/home/HomeRoomListDataSource.kt index 6b43af4bfc..c27a58d177 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/HomeRoomListObservableStore.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/HomeRoomListDataSource.kt @@ -17,9 +17,9 @@ package im.vector.riotx.features.home import im.vector.matrix.android.api.session.room.model.RoomSummary -import im.vector.riotx.core.utils.BehaviorStore +import im.vector.riotx.core.utils.BehaviorDataSource import javax.inject.Inject import javax.inject.Singleton @Singleton -class HomeRoomListObservableStore @Inject constructor() : BehaviorStore>() +class HomeRoomListDataSource @Inject constructor() : BehaviorDataSource>() diff --git a/vector/src/main/java/im/vector/riotx/features/home/createdirect/CreateDirectRoomNavigationViewModel.kt b/vector/src/main/java/im/vector/riotx/features/home/createdirect/CreateDirectRoomNavigationViewModel.kt index d120c306fb..5a84cf4258 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/createdirect/CreateDirectRoomNavigationViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/createdirect/CreateDirectRoomNavigationViewModel.kt @@ -17,8 +17,8 @@ package im.vector.riotx.features.home.createdirect import androidx.lifecycle.ViewModel -import im.vector.riotx.core.utils.PublishStore -import im.vector.riotx.core.utils.RxStore +import im.vector.riotx.core.utils.PublishDataSource +import im.vector.riotx.core.utils.MutableDataSource -class CreateDirectRoomNavigationViewModel(private val store: RxStore = PublishStore()) - : ViewModel(), RxStore by store +class CreateDirectRoomNavigationViewModel(private val dataSource: MutableDataSource = PublishDataSource()) + : ViewModel(), MutableDataSource by dataSource diff --git a/vector/src/main/java/im/vector/riotx/features/home/group/GroupListViewModel.kt b/vector/src/main/java/im/vector/riotx/features/home/group/GroupListViewModel.kt index ed547026ee..ac12113d66 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/group/GroupListViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/group/GroupListViewModel.kt @@ -39,7 +39,7 @@ import io.reactivex.functions.BiFunction const val ALL_COMMUNITIES_GROUP_ID = "ALL_COMMUNITIES_GROUP_ID" class GroupListViewModel @AssistedInject constructor(@Assisted initialState: GroupListViewState, - private val selectedGroupStore: SelectedGroupStore, + private val selectedGroupStore: SelectedGroupDataSource, private val session: Session, private val stringProvider: StringProvider ) : VectorViewModel(initialState) { diff --git a/vector/src/main/java/im/vector/riotx/features/home/group/SelectedGroupStore.kt b/vector/src/main/java/im/vector/riotx/features/home/group/SelectedGroupDataSource.kt similarity index 83% rename from vector/src/main/java/im/vector/riotx/features/home/group/SelectedGroupStore.kt rename to vector/src/main/java/im/vector/riotx/features/home/group/SelectedGroupDataSource.kt index a74f24c1d5..c7b36e1e7f 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/group/SelectedGroupStore.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/group/SelectedGroupDataSource.kt @@ -18,9 +18,9 @@ package im.vector.riotx.features.home.group import arrow.core.Option import im.vector.matrix.android.api.session.group.model.GroupSummary -import im.vector.riotx.core.utils.BehaviorStore +import im.vector.riotx.core.utils.BehaviorDataSource import javax.inject.Inject import javax.inject.Singleton @Singleton -class SelectedGroupStore @Inject constructor() : BehaviorStore>(Option.empty()) +class SelectedGroupDataSource @Inject constructor() : BehaviorDataSource>(Option.empty()) diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailFragment.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailFragment.kt index 4875c87ec0..f2f65e5cf0 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailFragment.kt @@ -99,7 +99,7 @@ import im.vector.riotx.features.home.room.detail.composer.TextComposerViewState import im.vector.riotx.features.home.room.detail.readreceipts.DisplayReadReceiptsBottomSheet import im.vector.riotx.features.home.room.detail.timeline.TimelineEventController import im.vector.riotx.features.home.room.detail.timeline.action.MessageActionsBottomSheet -import im.vector.riotx.features.home.room.detail.timeline.action.MessageActionsStore +import im.vector.riotx.features.home.room.detail.timeline.action.MessageActionsDispatcher import im.vector.riotx.features.home.room.detail.timeline.action.SimpleAction import im.vector.riotx.features.home.room.detail.timeline.edithistory.ViewEditHistoryBottomSheet import im.vector.riotx.features.home.room.detail.timeline.item.* @@ -200,7 +200,7 @@ class RoomDetailFragment : override fun getMenuRes() = R.menu.menu_timeline - private lateinit var messageActionsStore: MessageActionsStore + private lateinit var messageActionsDispatcher: MessageActionsDispatcher private lateinit var layoutManager: LinearLayoutManager private lateinit var attachmentsHelper: AttachmentsHelper private lateinit var keyboardStateUtils: KeyboardStateUtils @@ -217,7 +217,7 @@ class RoomDetailFragment : override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) - messageActionsStore = ViewModelProviders.of(requireActivity()).get(MessageActionsStore::class.java) + messageActionsDispatcher = ViewModelProviders.of(requireActivity()).get(MessageActionsDispatcher::class.java) attachmentsHelper = AttachmentsHelper.create(this, this).register() keyboardStateUtils = KeyboardStateUtils(requireActivity()) setupToolbar(roomToolbar) @@ -236,7 +236,7 @@ class RoomDetailFragment : val message = requireContext().getString(pair.first, *pair.second.toTypedArray()) showSnackWithMessage(message, Snackbar.LENGTH_LONG) } - messageActionsStore + messageActionsDispatcher .observe() .subscribe { handleActions(it) diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/MessageActionsBottomSheet.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/MessageActionsBottomSheet.kt index 2f0e3fab65..d0dc3de8f3 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/MessageActionsBottomSheet.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/MessageActionsBottomSheet.kt @@ -47,7 +47,7 @@ class MessageActionsBottomSheet : VectorBaseBottomSheetDialogFragment(), Message override val showExpanded = true - private lateinit var messageActionsStore: MessageActionsStore + private lateinit var messageActionsStore: MessageActionsDispatcher override fun injectWith(screenComponent: ScreenComponent) { screenComponent.inject(this) @@ -61,7 +61,7 @@ class MessageActionsBottomSheet : VectorBaseBottomSheetDialogFragment(), Message override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) - messageActionsStore = ViewModelProviders.of(requireActivity()).get(MessageActionsStore::class.java) + messageActionsStore = ViewModelProviders.of(requireActivity()).get(MessageActionsDispatcher::class.java) recyclerView.layoutManager = LinearLayoutManager(requireContext(), RecyclerView.VERTICAL, false) recyclerView.adapter = messageActionsEpoxyController.adapter // Disable item animation diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/MessageActionsStore.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/MessageActionsDispatcher.kt similarity index 71% rename from vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/MessageActionsStore.kt rename to vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/MessageActionsDispatcher.kt index 5c4c6fed80..a11e0c3689 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/MessageActionsStore.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/MessageActionsDispatcher.kt @@ -16,12 +16,12 @@ package im.vector.riotx.features.home.room.detail.timeline.action import androidx.lifecycle.ViewModel -import im.vector.riotx.core.utils.PublishStore -import im.vector.riotx.core.utils.RxStore +import im.vector.riotx.core.utils.PublishDataSource +import im.vector.riotx.core.utils.MutableDataSource /** * Activity shared view model to handle message actions */ -class MessageActionsStore constructor( - private val store: RxStore = PublishStore() -) : ViewModel(), RxStore by store +class MessageActionsDispatcher constructor( + private val dataSource: MutableDataSource = PublishDataSource() +) : ViewModel(), MutableDataSource by dataSource diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListFragment.kt b/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListFragment.kt index bcd640589c..0ee34f0679 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListFragment.kt @@ -21,6 +21,7 @@ import android.os.Parcelable import android.view.Menu import android.view.MenuItem import androidx.annotation.StringRes +import androidx.appcompat.app.AlertDialog import androidx.core.content.ContextCompat import androidx.core.view.isVisible import androidx.lifecycle.ViewModelProviders @@ -36,8 +37,6 @@ import im.vector.riotx.R import im.vector.riotx.core.di.ScreenComponent import im.vector.riotx.core.epoxy.LayoutManagerStateRestorer import im.vector.riotx.core.error.ErrorFormatter -import im.vector.riotx.core.extensions.observeEvent -import im.vector.riotx.core.extensions.observeEventFirstThrottle import im.vector.riotx.core.platform.OnBackPressed import im.vector.riotx.core.platform.StateView import im.vector.riotx.core.platform.VectorBaseFragment @@ -47,6 +46,7 @@ import im.vector.riotx.features.home.room.list.actions.RoomListQuickActionsStore import im.vector.riotx.features.home.room.list.widget.FabMenuView import im.vector.riotx.features.notifications.NotificationDrawerManager import im.vector.riotx.features.share.SharedData +import io.reactivex.android.schedulers.AndroidSchedulers import kotlinx.android.parcel.Parcelize import kotlinx.android.synthetic.main.fragment_room_list.* import javax.inject.Inject @@ -115,30 +115,42 @@ class RoomListFragment : VectorBaseFragment(), RoomSummaryController.Listener, O setupCreateRoomButton() setupRecyclerView() roomListViewModel.subscribe { renderState(it) } - roomListViewModel.openRoomLiveData.observeEventFirstThrottle(this, 800L) { - if (roomListParams.displayMode == DisplayMode.SHARE) { - val sharedData = roomListParams.sharedData ?: return@observeEventFirstThrottle - navigator.openRoomForSharing(requireActivity(), it, sharedData) - } else { - navigator.openRoom(requireActivity(), it) - } - } + + roomListViewModel.viewEvents + .observe() + .observeOn(AndroidSchedulers.mainThread()) + .subscribe { + when (it) { + is RoomListViewEvents.SelectRoom -> openSelectedRoom(it) + is RoomListViewEvents.Failure -> showError(it) + } + } + .disposeOnDestroy() createChatFabMenu.listener = this - roomListViewModel.invitationAnswerErrorLiveData.observeEvent(this) { throwable -> - vectorBaseActivity.coordinatorLayout?.let { - Snackbar.make(it, errorFormatter.toHumanReadable(throwable), Snackbar.LENGTH_SHORT) - .show() - } - } - quickActionsDispatcher .observe() .subscribe { handleQuickActions(it) } .disposeOnDestroy() } + private fun openSelectedRoom(event: RoomListViewEvents.SelectRoom) { + if (roomListParams.displayMode == DisplayMode.SHARE) { + val sharedData = roomListParams.sharedData ?: return + navigator.openRoomForSharing(requireActivity(), event.roomId, sharedData) + } else { + navigator.openRoom(requireActivity(), event.roomId) + } + } + + private fun showError(event: RoomListViewEvents.Failure) { + vectorBaseActivity.coordinatorLayout?.let { + Snackbar.make(it, errorFormatter.toHumanReadable(event.throwable), Snackbar.LENGTH_SHORT) + .show() + } + } + private fun setupCreateRoomButton() { when (roomListParams.displayMode) { DisplayMode.HOME -> createChatFabMenu.isVisible = true @@ -233,7 +245,14 @@ class RoomListFragment : VectorBaseFragment(), RoomSummaryController.Listener, O vectorBaseActivity.notImplemented("Opening room settings") } is RoomListQuickActions.Leave -> { - roomListViewModel.accept(RoomListActions.LeaveRoom(quickActions.roomId)) + AlertDialog.Builder(requireContext()) + .setTitle(R.string.room_participants_leave_prompt_title) + .setMessage(R.string.room_participants_leave_prompt_msg) + .setPositiveButton(R.string.leave) { _, _ -> + roomListViewModel.accept(RoomListActions.LeaveRoom(quickActions.roomId)) + } + .setNegativeButton(R.string.cancel, null) + .show() } } } diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListViewEvents.kt b/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListViewEvents.kt new file mode 100644 index 0000000000..1181236da2 --- /dev/null +++ b/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListViewEvents.kt @@ -0,0 +1,26 @@ +/* + * 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 + +/** + * Transient events for RoomList + */ +sealed class RoomListViewEvents { + data class Failure(val throwable: Throwable) : RoomListViewEvents() + data class SelectRoom(val roomId: String) : RoomListViewEvents() +} 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 31fa9d0309..ae8654d561 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 @@ -16,8 +16,6 @@ package im.vector.riotx.features.home.room.list -import androidx.lifecycle.LiveData -import androidx.lifecycle.MutableLiveData import com.airbnb.mvrx.FragmentViewModelContext import com.airbnb.mvrx.MvRxViewModelFactory import com.airbnb.mvrx.ViewModelContext @@ -26,17 +24,16 @@ import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.room.model.Membership import im.vector.matrix.android.api.session.room.model.RoomSummary 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.core.utils.RxStore +import im.vector.riotx.core.utils.DataSource +import im.vector.riotx.core.utils.PublishDataSource import io.reactivex.schedulers.Schedulers import timber.log.Timber import javax.inject.Inject class RoomListViewModel @Inject constructor(initialState: RoomListViewState, private val session: Session, - private val roomSummariesStore: RxStore>, + private val roomSummariesSource: DataSource>, private val alphabeticalRoomComparator: AlphabeticalRoomComparator, private val chronologicalRoomComparator: ChronologicalRoomComparator) : VectorViewModel(initialState) { @@ -57,13 +54,8 @@ class RoomListViewModel @Inject constructor(initialState: RoomListViewState, private val displayMode = initialState.displayMode private val roomListDisplayModeFilter = RoomListDisplayModeFilter(displayMode) - private val _openRoomLiveData = MutableLiveData>() - val openRoomLiveData: LiveData> - get() = _openRoomLiveData - - private val _invitationAnswerErrorLiveData = MutableLiveData>() - val invitationAnswerErrorLiveData: LiveData> - get() = _invitationAnswerErrorLiveData + private val _viewEvents = PublishDataSource() + val viewEvents: DataSource = _viewEvents init { observeRoomSummaries() @@ -85,7 +77,7 @@ class RoomListViewModel @Inject constructor(initialState: RoomListViewState, // PRIVATE METHODS ***************************************************************************** private fun handleSelectRoom(action: RoomListActions.SelectRoom) { - _openRoomLiveData.postLiveEvent(action.roomSummary.roomId) + _viewEvents.post(RoomListViewEvents.SelectRoom(action.roomSummary.roomId)) } private fun handleToggleCategory(action: RoomListActions.ToggleCategory) = setState { @@ -101,7 +93,7 @@ class RoomListViewModel @Inject constructor(initialState: RoomListViewState, } private fun observeRoomSummaries() { - roomSummariesStore + roomSummariesSource .observe() .observeOn(Schedulers.computation()) .map { @@ -111,7 +103,7 @@ class RoomListViewModel @Inject constructor(initialState: RoomListViewState, copy(asyncRooms = asyncRooms) } - roomSummariesStore + roomSummariesSource .observe() .observeOn(Schedulers.computation()) .map { buildRoomSummaries(it) } @@ -144,8 +136,7 @@ class RoomListViewModel @Inject constructor(initialState: RoomListViewState, override fun onFailure(failure: Throwable) { // Notify the user - _invitationAnswerErrorLiveData.postLiveEvent(failure) - + _viewEvents.post(RoomListViewEvents.Failure(failure)) setState { copy( joiningRoomsIds = joiningRoomsIds - roomId, @@ -182,8 +173,7 @@ class RoomListViewModel @Inject constructor(initialState: RoomListViewState, override fun onFailure(failure: Throwable) { // Notify the user - _invitationAnswerErrorLiveData.postLiveEvent(failure) - + _viewEvents.post(RoomListViewEvents.Failure(failure)) setState { copy( rejectingRoomsIds = rejectingRoomsIds - roomId, @@ -204,15 +194,19 @@ class RoomListViewModel @Inject constructor(initialState: RoomListViewState, } private fun handleChangeNotificationMode(action: RoomListActions.ChangeRoomNotificationState) { - session.getRoom(action.roomId)?.also { - it.setRoomNotificationState(action.notificationState, object : MatrixCallback {}) - } + session.getRoom(action.roomId)?.setRoomNotificationState(action.notificationState, object : MatrixCallback { + override fun onFailure(failure: Throwable) { + _viewEvents.post(RoomListViewEvents.Failure(failure)) + } + }) } private fun handleLeaveRoom(action: RoomListActions.LeaveRoom) { - session.getRoom(action.roomId)?.also { - it.leave(object : MatrixCallback {}) - } + session.getRoom(action.roomId)?.leave(object : MatrixCallback { + override fun onFailure(failure: Throwable) { + _viewEvents.post(RoomListViewEvents.Failure(failure)) + } + }) } private fun buildRoomSummaries(rooms: List): RoomSummaries { 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 index 5895aa4e52..897e72d811 100644 --- 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 @@ -17,14 +17,14 @@ 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 im.vector.riotx.features.home.HomeRoomListDataSource +import im.vector.riotx.features.share.ShareRoomListDataSource 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 homeRoomListDataSource: Provider, + private val shareRoomListDataSource: Provider, private val alphabeticalRoomComparator: Provider, private val chronologicalRoomComparator: Provider) : RoomListViewModel.Factory { @@ -32,7 +32,7 @@ class RoomListViewModelFactory @Inject constructor(private val session: Provider return RoomListViewModel( initialState, session.get(), - if (initialState.displayMode == RoomListFragment.DisplayMode.SHARE) shareRoomListObservableStore.get() else homeRoomListObservableStore.get(), + if (initialState.displayMode == RoomListFragment.DisplayMode.SHARE) shareRoomListDataSource.get() else homeRoomListDataSource.get(), alphabeticalRoomComparator.get(), chronologicalRoomComparator.get()) } diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/list/actions/RoomListQuickActions.kt b/vector/src/main/java/im/vector/riotx/features/home/room/list/actions/RoomListQuickActions.kt index e89b4ebcd0..86cd9043f3 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/list/actions/RoomListQuickActions.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/list/actions/RoomListQuickActions.kt @@ -20,7 +20,7 @@ import androidx.annotation.DrawableRes import androidx.annotation.StringRes import im.vector.riotx.R -sealed class RoomListQuickActions(@StringRes val titleRes: Int, @DrawableRes val iconResId: Int) { +sealed class RoomListQuickActions(@StringRes val titleRes: Int, @DrawableRes val iconResId: Int, val destructive: Boolean = false) { data class NotificationsAllNoisy(val roomId: String) : RoomListQuickActions( R.string.room_list_quick_actions_notifications_all_noisy, @@ -49,19 +49,7 @@ sealed class RoomListQuickActions(@StringRes val titleRes: Int, @DrawableRes val data class Leave(val roomId: String) : RoomListQuickActions( R.string.room_list_quick_actions_leave, - R.drawable.ic_room_actions_leave + R.drawable.ic_room_actions_leave, + true ) - - companion object { - fun all(roomId: String): List { - return listOf( - NotificationsAllNoisy(roomId), - NotificationsAll(roomId), - NotificationsMentionsOnly(roomId), - NotificationsMute(roomId), - Settings(roomId), - Leave(roomId) - ) - } - } } diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/list/actions/RoomListQuickActionsEpoxyController.kt b/vector/src/main/java/im/vector/riotx/features/home/room/list/actions/RoomListQuickActionsEpoxyController.kt index cc6abc42b1..70d0fc5f76 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/list/actions/RoomListQuickActionsEpoxyController.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/list/actions/RoomListQuickActionsEpoxyController.kt @@ -18,22 +18,16 @@ package im.vector.riotx.features.home.room.list.actions import android.view.View import com.airbnb.epoxy.TypedEpoxyController import im.vector.matrix.android.api.session.room.notification.RoomNotificationState -import im.vector.riotx.EmojiCompatFontProvider -import im.vector.riotx.core.date.VectorDateFormatter -import im.vector.riotx.core.epoxy.bottomsheet.BottomSheetItemAction_ +import im.vector.riotx.core.epoxy.bottomsheet.bottomSheetItemAction import im.vector.riotx.core.epoxy.bottomsheet.bottomSheetItemRoomPreview import im.vector.riotx.core.epoxy.bottomsheet.bottomSheetItemSeparator -import im.vector.riotx.core.resources.StringProvider import im.vector.riotx.features.home.AvatarRenderer import javax.inject.Inject /** * Epoxy controller for room list actions */ -class RoomListQuickActionsEpoxyController @Inject constructor(private val stringProvider: StringProvider, - private val avatarRenderer: AvatarRenderer, - private val dateFormatter: VectorDateFormatter, - private val fontProvider: EmojiCompatFontProvider) +class RoomListQuickActionsEpoxyController @Inject constructor(private val avatarRenderer: AvatarRenderer) : TypedEpoxyController() { var listener: Listener? = null @@ -78,13 +72,14 @@ class RoomListQuickActionsEpoxyController @Inject constructor(private val string is RoomListQuickActions.Settings, is RoomListQuickActions.Leave -> false } - return BottomSheetItemAction_() - .id("action_$index") - .selected(selected) - .iconRes(iconResId) - .textRes(titleRes) - .listener(View.OnClickListener { listener?.didSelectMenuAction(this) }) - .addTo(this@RoomListQuickActionsEpoxyController) + return bottomSheetItemAction { + id("action_$index") + selected(selected) + iconRes(iconResId) + textRes(titleRes) + destructive(this@toBottomSheetItem.destructive) + listener(View.OnClickListener { listener?.didSelectMenuAction(this@toBottomSheetItem) }) + } } interface Listener { diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/list/actions/RoomListQuickActionsStore.kt b/vector/src/main/java/im/vector/riotx/features/home/room/list/actions/RoomListQuickActionsStore.kt index ce0479ac92..35fe09dff8 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/list/actions/RoomListQuickActionsStore.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/list/actions/RoomListQuickActionsStore.kt @@ -17,12 +17,12 @@ package im.vector.riotx.features.home.room.list.actions import androidx.lifecycle.ViewModel -import im.vector.riotx.core.utils.PublishStore -import im.vector.riotx.core.utils.RxStore +import im.vector.riotx.core.utils.PublishDataSource +import im.vector.riotx.core.utils.MutableDataSource /** * Activity shared view model to handle room list quick actions */ class RoomListQuickActionsStore constructor( - private val store: RxStore = PublishStore() -) : ViewModel(), RxStore by store + private val store: MutableDataSource = PublishDataSource() +) : ViewModel(), MutableDataSource by store diff --git a/vector/src/main/java/im/vector/riotx/features/roomdirectory/RoomDirectoryNavigationViewModel.kt b/vector/src/main/java/im/vector/riotx/features/roomdirectory/RoomDirectoryNavigationViewModel.kt index cb26ff0c54..4e4230e07b 100644 --- a/vector/src/main/java/im/vector/riotx/features/roomdirectory/RoomDirectoryNavigationViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/roomdirectory/RoomDirectoryNavigationViewModel.kt @@ -17,8 +17,8 @@ package im.vector.riotx.features.roomdirectory import androidx.lifecycle.ViewModel -import im.vector.riotx.core.utils.PublishStore -import im.vector.riotx.core.utils.RxStore +import im.vector.riotx.core.utils.PublishDataSource +import im.vector.riotx.core.utils.MutableDataSource -class RoomDirectoryNavigationViewModel(private val store: RxStore = PublishStore()) - : ViewModel(), RxStore by store +class RoomDirectoryNavigationViewModel(private val source: MutableDataSource = PublishDataSource()) + : ViewModel(), MutableDataSource by source 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 index 51485ecbf9..9fba8541e3 100644 --- a/vector/src/main/java/im/vector/riotx/features/share/IncomingShareViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/share/IncomingShareViewModel.kt @@ -23,7 +23,7 @@ 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.ActiveSessionDataSource import im.vector.riotx.core.platform.VectorViewModel import io.reactivex.Observable import io.reactivex.android.schedulers.AndroidSchedulers @@ -35,8 +35,8 @@ 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) + private val sessionObservableStore: ActiveSessionDataSource, + private val shareRoomListObservableStore: ShareRoomListDataSource) : VectorViewModel(initialState) { @AssistedInject.Factory diff --git a/vector/src/main/java/im/vector/riotx/features/share/ShareRoomListObservableStore.kt b/vector/src/main/java/im/vector/riotx/features/share/ShareRoomListDataSource.kt similarity index 83% rename from vector/src/main/java/im/vector/riotx/features/share/ShareRoomListObservableStore.kt rename to vector/src/main/java/im/vector/riotx/features/share/ShareRoomListDataSource.kt index 32b0093b8f..b1b4d7b46e 100644 --- a/vector/src/main/java/im/vector/riotx/features/share/ShareRoomListObservableStore.kt +++ b/vector/src/main/java/im/vector/riotx/features/share/ShareRoomListDataSource.kt @@ -17,9 +17,9 @@ package im.vector.riotx.features.share import im.vector.matrix.android.api.session.room.model.RoomSummary -import im.vector.riotx.core.utils.BehaviorStore +import im.vector.riotx.core.utils.BehaviorDataSource import javax.inject.Inject import javax.inject.Singleton @Singleton -class ShareRoomListObservableStore @Inject constructor() : BehaviorStore>() +class ShareRoomListDataSource @Inject constructor() : BehaviorDataSource>()