Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added dialog to change app layout settings #6840

Merged
merged 4 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/6506.wip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[App Layout] added dialog to configure app layout
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea using [App Layout]. I'll start doing this too

10 changes: 10 additions & 0 deletions vector/src/main/java/im/vector/app/features/home/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import im.vector.app.features.analytics.plan.MobileScreen
import im.vector.app.features.analytics.plan.ViewRoom
import im.vector.app.features.crypto.recover.SetupMode
import im.vector.app.features.disclaimer.showDisclaimerDialog
import im.vector.app.features.home.room.list.home.layout.HomeLayoutSettingBottomDialogFragment
import im.vector.app.features.matrixto.MatrixToBottomSheet
import im.vector.app.features.matrixto.OriginOfMatrixTo
import im.vector.app.features.navigation.Navigator
Expand Down Expand Up @@ -283,6 +284,11 @@ class HomeActivity :
.show(supportFragmentManager, "SPACE_SETTINGS")
}

private fun showLayoutSettings() {
HomeLayoutSettingBottomDialogFragment()
.show(supportFragmentManager, "LAYOUT_SETTINGS")
}

private fun openSpaceInvite(spaceId: String) {
SpaceInviteBottomSheet.newInstance(spaceId)
.show(supportFragmentManager, "SPACE_INVITE")
Expand Down Expand Up @@ -596,6 +602,10 @@ class HomeActivity :
navigator.openSettings(this)
true
}
R.id.menu_home_layout_settings -> {
showLayoutSettings()
true
}
R.id.menu_home_invite_friends -> {
launchInviteFriends()
true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2022 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.app.features.home.room.list.home

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import org.matrix.android.sdk.api.extensions.orFalse
import javax.inject.Inject

private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "layout_preferences")

class HomeLayoutPreferencesStore @Inject constructor(
private val context: Context
) {

private val areRecentsEnbabled = booleanPreferencesKey("SETTINGS_PREFERENCES_HOME_RECENTS")
private val areFiltersEnabled = booleanPreferencesKey("SETTINGS_PREFERENCES_HOME_FILTERS")
private val isAZOrderingEnabled = booleanPreferencesKey("SETTINGS_PREFERENCES_USE_AZ_ORDER")

val areRecentsEnabledFlow: Flow<Boolean> = context.dataStore.data
.map { preferences -> preferences[areRecentsEnbabled].orFalse() }
.distinctUntilChanged()

val areFiltersEnabledFlow: Flow<Boolean> = context.dataStore.data
.map { preferences -> preferences[areFiltersEnabled].orFalse() }
.distinctUntilChanged()

val isAZOrderingEnabledFlow: Flow<Boolean> = context.dataStore.data
.map { preferences -> preferences[isAZOrderingEnabled].orFalse() }
.distinctUntilChanged()

suspend fun setRecentsEnabled(isEnabled: Boolean) {
context.dataStore.edit { settings ->
settings[areRecentsEnbabled] = isEnabled
}
}

suspend fun setFiltersEnabled(isEnabled: Boolean) {
context.dataStore.edit { settings ->
settings[areFiltersEnabled] = isEnabled
}
}

suspend fun setAZOrderingEnabled(isEnabled: Boolean) {
context.dataStore.edit { settings ->
settings[isAZOrderingEnabled] = isEnabled
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ class HomeRoomListFragment @Inject constructor(
}.launchIn(lifecycleScope)

views.roomListView.adapter = concatAdapter

// we need to force scroll when recents/filter tabs are added to make them visible
concatAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
if (positionStart == 0) {
layoutManager.scrollToPosition(0)
}
}
})
}

private fun setupFabs() {
Expand Down Expand Up @@ -153,6 +162,9 @@ class HomeRoomListFragment @Inject constructor(
}

private fun setUpAdapters(sections: Set<HomeRoomSection>) {
concatAdapter.adapters.forEach {
concatAdapter.removeAdapter(it)
}
sections.forEach {
concatAdapter.addAdapter(getAdapterForData(it))
}
Expand Down Expand Up @@ -212,12 +224,11 @@ class HomeRoomListFragment @Inject constructor(
is HomeRoomSection.RoomSummaryData -> {
HomeFilteredRoomsController(
roomSummaryItemFactory,
showFilters = section.showFilters,
).also { controller ->
controller.listener = this
controller.onFilterChanged = ::onRoomFilterChanged
section.filtersData.onEach {
controller.submitFiltersData(it)
controller.submitFiltersData(it.getOrNull())
}.launchIn(lifecycleScope)
section.list.observe(viewLifecycleOwner) { list ->
controller.submitList(list)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
Expand All @@ -53,12 +54,14 @@ import org.matrix.android.sdk.api.session.room.model.Membership
import org.matrix.android.sdk.api.session.room.model.tag.RoomTag
import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams
import org.matrix.android.sdk.api.session.room.state.isPublic
import org.matrix.android.sdk.api.util.Optional
import org.matrix.android.sdk.flow.flow

class HomeRoomListViewModel @AssistedInject constructor(
@Assisted initialState: HomeRoomListViewState,
private val session: Session,
private val spaceStateHandler: SpaceStateHandler,
private val preferencesStore: HomeLayoutPreferencesStore,
) : VectorViewModel<HomeRoomListViewState, HomeRoomListAction, HomeRoomListViewEvents>(initialState) {

@AssistedFactory
Expand All @@ -82,17 +85,30 @@ class HomeRoomListViewModel @AssistedInject constructor(

init {
configureSections()
observePreferences()
}

private fun configureSections() {
private fun observePreferences() {
preferencesStore.areRecentsEnabledFlow.onEach {
configureSections()
}.launchIn(viewModelScope)

preferencesStore.isAZOrderingEnabledFlow.onEach {
configureSections()
}.launchIn(viewModelScope)
}

private fun configureSections() = viewModelScope.launch {
val newSections = mutableSetOf<HomeRoomSection>()

newSections.add(getRecentRoomsSection())
newSections.add(getFilteredRoomsSection())
val areSettingsEnabled = preferencesStore.areRecentsEnabledFlow.first()

viewModelScope.launch {
_sections.emit(newSections)
if (areSettingsEnabled) {
newSections.add(getRecentRoomsSection())
}
newSections.add(getFilteredRoomsSection())

_sections.emit(newSections)

setState {
copy(state = StateView.State.Content)
Expand All @@ -111,13 +127,17 @@ class HomeRoomListViewModel @AssistedInject constructor(
)
}

private fun getFilteredRoomsSection(): HomeRoomSection.RoomSummaryData {
private suspend fun getFilteredRoomsSection(): HomeRoomSection.RoomSummaryData {
val builder = RoomSummaryQueryParams.Builder().also {
it.memberships = listOf(Membership.JOIN)
}

val params = getFilteredQueryParams(HomeRoomFilter.ALL, builder.build())
val sortOrder = RoomSortOrder.ACTIVITY // #6506
val sortOrder = if (preferencesStore.isAZOrderingEnabledFlow.first()) {
RoomSortOrder.NAME
} else {
RoomSortOrder.ACTIVITY
}

val liveResults = session.roomService().getFilteredPagedRoomSummariesLive(
params,
Expand All @@ -135,19 +155,18 @@ class HomeRoomListViewModel @AssistedInject constructor(
.onEach { selectedSpaceOption ->
val selectedSpace = selectedSpaceOption.orNull()
liveResults.queryParams = liveResults.queryParams.copy(
spaceFilter = selectedSpace?.roomId.toActiveSpaceOrNoFilter()
spaceFilter = selectedSpace?.roomId.toActiveSpaceOrNoFilter()
)
}.launchIn(viewModelScope)

return HomeRoomSection.RoomSummaryData(
list = liveResults.livePagedList,
showFilters = true, // #6506
filtersData = getFiltersDataFlow()
)
}

private fun getFiltersDataFlow(): SharedFlow<List<HomeRoomFilter>> {
val flow = MutableSharedFlow<List<HomeRoomFilter>>(replay = 1)
private fun getFiltersDataFlow(): SharedFlow<Optional<List<HomeRoomFilter>>> {
val flow = MutableSharedFlow<Optional<List<HomeRoomFilter>>>(replay = 1)

val favouritesFlow = session.flow()
.liveRoomSummaries(
Expand All @@ -168,25 +187,28 @@ class HomeRoomListViewModel @AssistedInject constructor(
.map { it.isNotEmpty() }
.distinctUntilChanged()

favouritesFlow.combine(dmsFLow) { hasFavourite, hasDm ->
hasFavourite to hasDm
}.onEach { (hasFavourite, hasDm) ->
val filtersData = mutableListOf(
HomeRoomFilter.ALL,
HomeRoomFilter.UNREADS
)
if (hasFavourite) {
filtersData.add(
HomeRoomFilter.FAVOURITES
)
}
if (hasDm) {
filtersData.add(
HomeRoomFilter.PEOPlE
combine(favouritesFlow, dmsFLow, preferencesStore.areFiltersEnabledFlow) { hasFavourite, hasDm, areFiltersEnabled ->
Triple(hasFavourite, hasDm, areFiltersEnabled)
}.onEach { (hasFavourite, hasDm, areFiltersEnabled) ->
if (areFiltersEnabled) {
val filtersData = mutableListOf(
HomeRoomFilter.ALL,
HomeRoomFilter.UNREADS
)
if (hasFavourite) {
filtersData.add(
HomeRoomFilter.FAVOURITES
)
}
if (hasDm) {
filtersData.add(
HomeRoomFilter.PEOPlE
)
}
flow.emit(Optional.from(filtersData))
} else {
flow.emit(Optional.empty())
}

flow.emit(filtersData)
}.launchIn(viewModelScope)

return flow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import androidx.paging.PagedList
import im.vector.app.features.home.room.list.home.filter.HomeRoomFilter
import kotlinx.coroutines.flow.SharedFlow
import org.matrix.android.sdk.api.session.room.model.RoomSummary
import org.matrix.android.sdk.api.util.Optional

sealed class HomeRoomSection {
data class RoomSummaryData(
val list: LiveData<PagedList<RoomSummary>>,
val showFilters: Boolean,
val filtersData: SharedFlow<List<HomeRoomFilter>>
val filtersData: SharedFlow<Optional<List<HomeRoomFilter>>>,
) : HomeRoomSection()

data class RecentRoomsData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import org.matrix.android.sdk.api.session.room.model.RoomSummary

class HomeFilteredRoomsController(
private val roomSummaryItemFactory: RoomSummaryItemFactory,
private val showFilters: Boolean,
) : PagedListEpoxyController<RoomSummary>(
// Important it must match the PageList builder notify Looper
modelBuildingHandler = createUIHandler()
Expand All @@ -48,7 +47,7 @@ class HomeFilteredRoomsController(

override fun addModels(models: List<EpoxyModel<*>>) {
val host = this
if (showFilters) {
if (host.filtersData != null) {
roomFilterHeaderItem {
id("filter_header")
filtersData(host.filtersData)
Expand All @@ -58,7 +57,7 @@ class HomeFilteredRoomsController(
super.addModels(models)
}

fun submitFiltersData(data: List<HomeRoomFilter>) {
fun submitFiltersData(data: List<HomeRoomFilter>?) {
this.filtersData = data
requestForcedModelBuild()
}
Expand Down
Loading