diff --git a/vector/src/main/java/im/vector/riotx/core/di/ViewModelModule.kt b/vector/src/main/java/im/vector/riotx/core/di/ViewModelModule.kt index 37abde20b8..534a346a1c 100644 --- a/vector/src/main/java/im/vector/riotx/core/di/ViewModelModule.kt +++ b/vector/src/main/java/im/vector/riotx/core/di/ViewModelModule.kt @@ -57,9 +57,15 @@ import im.vector.riotx.features.workers.signout.SignOutViewModel interface ViewModelModule { + /** + * ViewModels with @IntoMap will be injected by this factory + */ @Binds fun bindViewModelFactory(factory: VectorViewModelFactory): ViewModelProvider.Factory + /** + * Below are bindings for the androidx view models (which extend ViewModel). Will be converted to MvRx ViewModel in the future. + */ @Binds @IntoMap @ViewModelKey(SignOutViewModel::class) @@ -110,6 +116,10 @@ interface ViewModelModule { @ViewModelKey(ConfigurationViewModel::class) fun bindConfigurationViewModel(viewModel: ConfigurationViewModel): ViewModel + /** + * Below are bindings for the MvRx view models (which extend VectorViewModel). Will be the only usage in the future. + */ + @Binds fun bindHomeActivityViewModelFactory(factory: HomeActivityViewModel_AssistedFactory): HomeActivityViewModel.Factory diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/filtered/FilteredRoomFooterItem.kt b/vector/src/main/java/im/vector/riotx/features/home/room/filtered/FilteredRoomFooterItem.kt index 0e916a0ea6..777f0d3266 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/filtered/FilteredRoomFooterItem.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/filtered/FilteredRoomFooterItem.kt @@ -30,10 +30,13 @@ abstract class FilteredRoomFooterItem : VectorEpoxyModel + if (publicRoomsFilter.text.toString() != state.currentFilter) { + // For initial filter + publicRoomsFilter.setText(state.currentFilter) + } + // Populate list with Epoxy publicRoomsController.setData(state) } diff --git a/vector/src/main/java/im/vector/riotx/features/roomdirectory/PublicRoomsViewState.kt b/vector/src/main/java/im/vector/riotx/features/roomdirectory/PublicRoomsViewState.kt index 0ad1ac267f..549d81cc76 100644 --- a/vector/src/main/java/im/vector/riotx/features/roomdirectory/PublicRoomsViewState.kt +++ b/vector/src/main/java/im/vector/riotx/features/roomdirectory/PublicRoomsViewState.kt @@ -22,6 +22,8 @@ import com.airbnb.mvrx.Uninitialized import im.vector.matrix.android.api.session.room.model.roomdirectory.PublicRoom data class PublicRoomsViewState( + // The current filter + val currentFilter: String = "", // Store cumul of pagination result val publicRooms: List = emptyList(), // Current pagination request diff --git a/vector/src/main/java/im/vector/riotx/features/roomdirectory/RoomDirectoryActivity.kt b/vector/src/main/java/im/vector/riotx/features/roomdirectory/RoomDirectoryActivity.kt index d708de9478..d3cefad293 100644 --- a/vector/src/main/java/im/vector/riotx/features/roomdirectory/RoomDirectoryActivity.kt +++ b/vector/src/main/java/im/vector/riotx/features/roomdirectory/RoomDirectoryActivity.kt @@ -16,8 +16,11 @@ package im.vector.riotx.features.roomdirectory +import android.content.Context +import android.content.Intent import android.os.Bundle import androidx.lifecycle.ViewModelProviders +import com.airbnb.mvrx.viewModel import im.vector.riotx.R import im.vector.riotx.core.di.ScreenComponent import im.vector.riotx.core.extensions.addFragment @@ -25,6 +28,7 @@ import im.vector.riotx.core.extensions.addFragmentToBackstack import im.vector.riotx.core.extensions.observeEvent import im.vector.riotx.core.platform.VectorBaseActivity import im.vector.riotx.features.roomdirectory.createroom.CreateRoomFragment +import im.vector.riotx.features.roomdirectory.createroom.CreateRoomViewModel import im.vector.riotx.features.roomdirectory.picker.RoomDirectoryPickerFragment import javax.inject.Inject @@ -39,7 +43,10 @@ class RoomDirectoryActivity : VectorBaseActivity() { } + @Inject lateinit var createRoomViewModelFactory: CreateRoomViewModel.Factory @Inject lateinit var roomDirectoryViewModelFactory: RoomDirectoryViewModel.Factory + private val roomDirectoryViewModel: RoomDirectoryViewModel by viewModel() + private val createRoomViewModel: CreateRoomViewModel by viewModel() private lateinit var navigationViewModel: RoomDirectoryNavigationViewModel override fun getLayoutRes() = R.layout.activity_simple @@ -51,6 +58,11 @@ class RoomDirectoryActivity : VectorBaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) navigationViewModel = ViewModelProviders.of(this, viewModelFactory).get(RoomDirectoryNavigationViewModel::class.java) + + if (isFirstCreation()) { + roomDirectoryViewModel.filterWith(intent?.getStringExtra(INITIAL_FILTER) ?: "") + } + navigationViewModel.navigateTo.observeEvent(this) { navigation -> when (navigation) { is Navigation.Back -> onBackPressed() @@ -59,6 +71,11 @@ class RoomDirectoryActivity : VectorBaseActivity() { is Navigation.Close -> finish() } } + + roomDirectoryViewModel.selectSubscribe(this, PublicRoomsViewState::currentFilter) { currentFilter -> + // Transmit the filter to the createRoomViewModel + createRoomViewModel.setName(currentFilter) + } } override fun initUiAndData() { @@ -67,4 +84,13 @@ class RoomDirectoryActivity : VectorBaseActivity() { } } + companion object { + private const val INITIAL_FILTER = "INITIAL_FILTER" + + fun getIntent(context: Context, initialFilter: String = ""): Intent { + val intent = Intent(context, RoomDirectoryActivity::class.java) + intent.putExtra(INITIAL_FILTER, initialFilter) + return intent + } + } } \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotx/features/roomdirectory/RoomDirectoryViewModel.kt b/vector/src/main/java/im/vector/riotx/features/roomdirectory/RoomDirectoryViewModel.kt index b34618e240..c47e8bbdbf 100644 --- a/vector/src/main/java/im/vector/riotx/features/roomdirectory/RoomDirectoryViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/roomdirectory/RoomDirectoryViewModel.kt @@ -59,9 +59,6 @@ class RoomDirectoryViewModel @AssistedInject constructor(@Assisted initialState: get() = _joinRoomErrorLiveData - // TODO Store in ViewState? - private var currentFilter: String = "" - private var since: String? = null private var currentTask: Cancelable? = null @@ -70,9 +67,6 @@ class RoomDirectoryViewModel @AssistedInject constructor(@Assisted initialState: private var roomDirectoryData = RoomDirectoryData() init { - // Load with empty filter - load() - setState { copy( roomDirectoryDisplayName = roomDirectoryData.displayName @@ -115,24 +109,20 @@ class RoomDirectoryViewModel @AssistedInject constructor(@Assisted initialState: this.roomDirectoryData = roomDirectoryData - reset() - load() + reset("") + load("") } - fun filterWith(filter: String) { - if (currentFilter == filter) { - return + fun filterWith(filter: String) = withState { state -> + if (state.currentFilter != filter) { + currentTask?.cancel() + + reset(filter) + load(filter) } - - currentTask?.cancel() - - currentFilter = filter - - reset() - load() } - private fun reset() { + private fun reset(newFilter: String) { // Reset since token since = null @@ -141,12 +131,13 @@ class RoomDirectoryViewModel @AssistedInject constructor(@Assisted initialState: publicRooms = emptyList(), asyncPublicRoomsRequest = Loading(), hasMore = false, - roomDirectoryDisplayName = roomDirectoryData.displayName + roomDirectoryDisplayName = roomDirectoryData.displayName, + currentFilter = newFilter ) } } - fun loadMore() { + fun loadMore() = withState { state -> if (currentTask == null) { setState { copy( @@ -154,15 +145,15 @@ class RoomDirectoryViewModel @AssistedInject constructor(@Assisted initialState: ) } - load() + load(state.currentFilter) } } - private fun load() { + private fun load(filter: String) { currentTask = session.getPublicRooms(roomDirectoryData.homeServer, PublicRoomsParams( limit = PUBLIC_ROOMS_LIMIT, - filter = PublicRoomsFilter(searchTerm = currentFilter), + filter = PublicRoomsFilter(searchTerm = filter), includeAllNetworks = roomDirectoryData.includeAllNetworks, since = since, thirdPartyInstanceId = roomDirectoryData.thirdPartyInstanceId diff --git a/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomActivity.kt b/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomActivity.kt index ffc342d8a4..d6cca2b665 100644 --- a/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomActivity.kt +++ b/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomActivity.kt @@ -21,6 +21,7 @@ import android.content.Intent import android.os.Bundle import androidx.appcompat.widget.Toolbar import androidx.lifecycle.ViewModelProviders +import com.airbnb.mvrx.viewModel import im.vector.riotx.R import im.vector.riotx.core.di.ScreenComponent import im.vector.riotx.core.extensions.addFragment @@ -29,12 +30,16 @@ import im.vector.riotx.core.platform.ToolbarConfigurable import im.vector.riotx.core.platform.VectorBaseActivity import im.vector.riotx.features.roomdirectory.RoomDirectoryActivity import im.vector.riotx.features.roomdirectory.RoomDirectoryNavigationViewModel +import javax.inject.Inject /** * Simple container for [CreateRoomFragment] */ class CreateRoomActivity : VectorBaseActivity(), ToolbarConfigurable { + @Inject lateinit var createRoomViewModelFactory: CreateRoomViewModel.Factory + private val createRoomViewModel: CreateRoomViewModel by viewModel() + private lateinit var navigationViewModel: RoomDirectoryNavigationViewModel override fun getLayoutRes() = R.layout.activity_simple @@ -46,6 +51,8 @@ class CreateRoomActivity : VectorBaseActivity(), ToolbarConfigurable { override fun initUiAndData() { if (isFirstCreation()) { addFragment(CreateRoomFragment(), R.id.simpleFragmentContainer) + + createRoomViewModel.setName(intent?.getStringExtra(INITIAL_NAME) ?: "") } } @@ -58,14 +65,19 @@ class CreateRoomActivity : VectorBaseActivity(), ToolbarConfigurable { navigationViewModel = ViewModelProviders.of(this, viewModelFactory).get(RoomDirectoryNavigationViewModel::class.java) navigationViewModel.navigateTo.observeEvent(this) { navigation -> when (navigation) { - is RoomDirectoryActivity.Navigation.Back -> finish() + is RoomDirectoryActivity.Navigation.Back, + is RoomDirectoryActivity.Navigation.Close -> finish() } } } companion object { - fun getIntent(context: Context): Intent { - return Intent(context, CreateRoomActivity::class.java) + private const val INITIAL_NAME = "INITIAL_NAME" + + fun getIntent(context: Context, initialName: String = ""): Intent { + return Intent(context, CreateRoomActivity::class.java).apply { + putExtra(INITIAL_NAME, initialName) + } } } diff --git a/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomFragment.kt b/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomFragment.kt index 28dc761a92..3a0079015b 100644 --- a/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomFragment.kt @@ -21,7 +21,7 @@ import android.view.MenuItem import androidx.lifecycle.ViewModelProviders import androidx.recyclerview.widget.LinearLayoutManager import com.airbnb.mvrx.Success -import com.airbnb.mvrx.fragmentViewModel +import com.airbnb.mvrx.activityViewModel import com.airbnb.mvrx.withState import im.vector.riotx.R import im.vector.riotx.core.di.ScreenComponent @@ -35,9 +35,8 @@ import javax.inject.Inject class CreateRoomFragment : VectorBaseFragment(), CreateRoomController.Listener { private lateinit var navigationViewModel: RoomDirectoryNavigationViewModel - private val viewModel: CreateRoomViewModel by fragmentViewModel() + private val viewModel: CreateRoomViewModel by activityViewModel() @Inject lateinit var createRoomController: CreateRoomController - @Inject lateinit var createRoomViewModelFactory: CreateRoomViewModel.Factory override fun getLayoutResId() = R.layout.fragment_create_room diff --git a/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomViewModel.kt b/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomViewModel.kt index c6a4ff1797..f0ee01152b 100644 --- a/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomViewModel.kt @@ -16,6 +16,7 @@ package im.vector.riotx.features.roomdirectory.createroom +import androidx.fragment.app.FragmentActivity import com.airbnb.mvrx.* import com.squareup.inject.assisted.Assisted import com.squareup.inject.assisted.AssistedInject @@ -25,6 +26,7 @@ import im.vector.matrix.android.api.session.room.model.RoomDirectoryVisibility import im.vector.matrix.android.api.session.room.model.create.CreateRoomParams import im.vector.matrix.android.api.session.room.model.create.CreateRoomPreset import im.vector.riotx.core.platform.VectorViewModel +import im.vector.riotx.features.roomdirectory.RoomDirectoryActivity class CreateRoomViewModel @AssistedInject constructor(@Assisted initialState: CreateRoomViewState, private val session: Session @@ -39,8 +41,13 @@ class CreateRoomViewModel @AssistedInject constructor(@Assisted initialState: Cr @JvmStatic override fun create(viewModelContext: ViewModelContext, state: CreateRoomViewState): CreateRoomViewModel? { - val fragment: CreateRoomFragment = (viewModelContext as FragmentViewModelContext).fragment() - return fragment.createRoomViewModelFactory.create(state) + val activity: FragmentActivity = (viewModelContext as ActivityViewModelContext).activity() + + return when (activity) { + is CreateRoomActivity -> activity.createRoomViewModelFactory.create(state) + is RoomDirectoryActivity -> activity.createRoomViewModelFactory.create(state) + else -> throw IllegalStateException("Wrong activity") + } } } diff --git a/vector/src/main/res/layout/activity_filtered_rooms.xml b/vector/src/main/res/layout/activity_filtered_rooms.xml index 9cced16788..4e26bc0e26 100644 --- a/vector/src/main/res/layout/activity_filtered_rooms.xml +++ b/vector/src/main/res/layout/activity_filtered_rooms.xml @@ -14,6 +14,7 @@ android:layout_width="0dp" android:layout_height="?attr/actionBarSize" android:elevation="4dp" + app:contentInsetStart="0dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> diff --git a/vector/src/main/res/layout/fragment_create_room.xml b/vector/src/main/res/layout/fragment_create_room.xml index f5eeeb31a0..301d5dee12 100644 --- a/vector/src/main/res/layout/fragment_create_room.xml +++ b/vector/src/main/res/layout/fragment_create_room.xml @@ -11,10 +11,10 @@ diff --git a/vector/src/main/res/layout/fragment_home_detail.xml b/vector/src/main/res/layout/fragment_home_detail.xml index 5ee80314d3..13d3fea803 100644 --- a/vector/src/main/res/layout/fragment_home_detail.xml +++ b/vector/src/main/res/layout/fragment_home_detail.xml @@ -8,10 +8,10 @@ diff --git a/vector/src/main/res/layout/fragment_public_rooms.xml b/vector/src/main/res/layout/fragment_public_rooms.xml index 2ba74a75a3..99fbd89611 100644 --- a/vector/src/main/res/layout/fragment_public_rooms.xml +++ b/vector/src/main/res/layout/fragment_public_rooms.xml @@ -23,11 +23,11 @@ diff --git a/vector/src/main/res/layout/fragment_room_list.xml b/vector/src/main/res/layout/fragment_room_list.xml index d4b6bd0f4c..c3fa4530e2 100644 --- a/vector/src/main/res/layout/fragment_room_list.xml +++ b/vector/src/main/res/layout/fragment_room_list.xml @@ -8,7 +8,7 @@ android:background="?riotx_header_panel_background"> diff --git a/vector/src/main/res/layout/fragment_room_preview_no_preview.xml b/vector/src/main/res/layout/fragment_room_preview_no_preview.xml index d6a154da34..ee8f87fc02 100644 --- a/vector/src/main/res/layout/fragment_room_preview_no_preview.xml +++ b/vector/src/main/res/layout/fragment_room_preview_no_preview.xml @@ -13,10 +13,10 @@ + android:elevation="4dp"> @style/Vector.Toolbar.Title @style/Vector.Toolbar.SubTitle ?riotx_background + 0dp