diff --git a/library/ui-strings/src/main/res/values/strings_sc.xml b/library/ui-strings/src/main/res/values/strings_sc.xml index 15f57952ed..148d267d8c 100644 --- a/library/ui-strings/src/main/res/values/strings_sc.xml +++ b/library/ui-strings/src/main/res/values/strings_sc.xml @@ -146,6 +146,8 @@ <string name="settings_enable_space_pager">Swipe chat list to switch space</string> <string name="settings_enable_space_pager_summary">Allow to switch between root spaces by swiping horizontally in the chat list</string> + <string name="settings_space_pager_prefer_space_1">Prefer lower root space</string> + <string name="settings_space_pager_prefer_space_1_summary">Prioritize showing spaces in the bottom space bar that you have moved to the top of your root space list</string> <string name="settings_notif_only_alert_once">Only alert once</string> <string name="settings_notif_only_alert_once_summary">Omit alert sounds for new messages if there is already an open notification for that chat</string> diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/home/spacebar/SpaceBarController.kt b/vector/src/main/java/im/vector/app/features/home/room/list/home/spacebar/SpaceBarController.kt index 8a1faa0fcb..f7440ea778 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/home/spacebar/SpaceBarController.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/home/spacebar/SpaceBarController.kt @@ -25,11 +25,13 @@ import com.airbnb.epoxy.Carousel import com.airbnb.epoxy.Carousel.SnapHelperFactory import com.airbnb.epoxy.EpoxyController import com.airbnb.epoxy.EpoxyModel +import com.airbnb.epoxy.ModelProp import com.airbnb.epoxy.ModelView import com.github.rubensousa.gravitysnaphelper.GravitySnapHelper import im.vector.app.core.resources.StringProvider import im.vector.app.features.home.AvatarRenderer import im.vector.app.features.home.room.list.UnreadCounterBadgeView +import im.vector.app.features.settings.VectorPreferences import org.matrix.android.sdk.api.session.room.model.RoomSummary import org.matrix.android.sdk.api.util.toMatrixItem import javax.inject.Inject @@ -39,6 +41,7 @@ import kotlin.math.min class SpaceBarController @Inject constructor( val stringProvider: StringProvider, private val avatarRenderer: AvatarRenderer, + private val vectorPreferences: VectorPreferences, ) : EpoxyController() { private var data: SpaceBarData = SpaceBarData() @@ -61,6 +64,7 @@ class SpaceBarController @Inject constructor( private fun addSpaces(host: SpaceBarController, spaces: List<RoomSummary?>, selectedSpace: RoomSummary?) { spaceBarCarousel { + enableSnap(host.vectorPreferences.preferSpecificSpacePagerSpace()) id("spaces_carousel") padding( Carousel.Padding( @@ -146,12 +150,21 @@ class SpaceBarController @Inject constructor( // Scroll to an element such that the new selection is roughly in the middle val firstVisible = lm.findFirstCompletelyVisibleItemPosition() val visibleRange = lm.findLastCompletelyVisibleItemPosition() - firstVisible + 1 - val overshoot = visibleRange/2 - val currentMiddle = firstVisible + overshoot - if (currentMiddle < position) { - effectivePosition = position + overshoot - } else if (currentMiddle > position) { - effectivePosition = position - overshoot + if (vectorPreferences.preferSpecificSpacePagerSpace()) { // scroll to smallest space possible + effectivePosition = when { + position < 1 -> 0 // show home only if it is selected + firstVisible < 1 && position <= visibleRange -> 1 + visibleRange // hide home + position > firstVisible + visibleRange -> position // make selection visible by scrolling right + else -> max(1, position - visibleRange) // make selection visible by scrolling left, as right as possible without scrolling to home + } + } else { // center current space + val overshoot = visibleRange / 2 + val currentMiddle = firstVisible + overshoot + if (currentMiddle < position) { + effectivePosition = position + overshoot + } else if (currentMiddle > position) { + effectivePosition = position - overshoot + } } // List limits effectivePosition = max(0, min(effectivePosition, lm.itemCount-1)) @@ -168,16 +181,19 @@ private inline fun <T> SpaceBarCarouselModelBuilder.withModelsFrom( } @ModelView(autoLayout = ModelView.Size.MATCH_WIDTH_WRAP_HEIGHT) -internal class SpaceBarCarousel(context: Context?) : Carousel(context) { +class SpaceBarCarousel(context: Context?) : Carousel(context) { + private var mEnableSnap: Boolean = false + @ModelProp // Couldn't get ModelProp to work without explicit setter + fun setEnableSnap(value: Boolean) { + mEnableSnap = value + } override fun getSnapHelperFactory(): SnapHelperFactory? { - return null // SpaceBarSnapHelperFactory() + return if (mEnableSnap) SpaceBarSnapHelperFactory() else null } } -/* internal class SpaceBarSnapHelperFactory: SnapHelperFactory() { override fun buildSnapHelper(context: Context?): SnapHelper { - return GravitySnapHelper(Gravity.CENTER) + return GravitySnapHelper(Gravity.START) } } - */ diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt b/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt index d2fb3493c8..0723e30e82 100755 --- a/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt @@ -252,6 +252,7 @@ class VectorPreferences @Inject constructor( private const val SETTINGS_JUMP_TO_BOTTOM_ON_SEND = "SETTINGS_JUMP_TO_BOTTOM_ON_SEND" private const val SETTINGS_SPACE_MEMBERS_IN_SPACE_ROOMS = "SETTINGS_SPACE_MEMBERS_IN_SPACE_ROOMS" private const val SETTINGS_ENABLE_SPACE_PAGER = "SETTINGS_ENABLE_SPACE_PAGER" + private const val SETTINGS_SPACE_PAGER_BAR_PREFER_SPACE = "SETTINGS_SPACE_PAGER_BAR_PREFER_SPACE" private const val SETTINGS_NOTIF_ONLY_ALERT_ONCE = "SETTINGS_NOTIF_ONLY_ALERT_ONCE" private const val SETTINGS_HIDE_CALL_BUTTONS = "SETTINGS_HIDE_CALL_BUTTONS" private const val SETTINGS_READ_RECEIPT_FOLLOWS_READ_MARKER = "SETTINGS_READ_RECEIPT_FOLLOWS_READ_MARKER" @@ -1223,6 +1224,11 @@ class VectorPreferences @Inject constructor( return defaultPrefs.getBoolean(SETTINGS_ENABLE_SPACE_PAGER, false) } + // SC addition + fun preferSpecificSpacePagerSpace(): Boolean { + return defaultPrefs.getBoolean(SETTINGS_SPACE_PAGER_BAR_PREFER_SPACE, false) + } + // SC addition fun onlyAlertOnce(): Boolean { return defaultPrefs.getBoolean(SETTINGS_NOTIF_ONLY_ALERT_ONCE, false) @@ -1292,6 +1298,7 @@ class VectorPreferences @Inject constructor( .putBoolean(SETTINGS_FOLLOW_SYSTEM_LOCALE, true) .putBoolean(SETTINGS_ENABLE_MEMBER_NAME_CLICK, false) .putBoolean(SETTINGS_CLEAR_HIGHLIGHT_ON_SCROLL, true) + .putBoolean(SETTINGS_SPACE_PAGER_BAR_PREFER_SPACE, true) .apply() } diff --git a/vector/src/main/res/xml/vector_settings_labs.xml b/vector/src/main/res/xml/vector_settings_labs.xml index 2971fb538a..0b2c6f67c7 100644 --- a/vector/src/main/res/xml/vector_settings_labs.xml +++ b/vector/src/main/res/xml/vector_settings_labs.xml @@ -67,6 +67,13 @@ android:title="@string/settings_enable_space_pager" android:summary="@string/settings_enable_space_pager_summary" /> + <im.vector.app.core.preference.VectorSwitchPreference + android:defaultValue="false" + android:key="SETTINGS_SPACE_PAGER_BAR_PREFER_SPACE" + android:dependency="SETTINGS_ENABLE_SPACE_PAGER" + android:title="@string/settings_space_pager_prefer_space_1" + android:summary="@string/settings_space_pager_prefer_space_1_summary" /> + <im.vector.app.core.preference.VectorSwitchPreference android:defaultValue="false" android:key="SETTINGS_SHOW_OPEN_ANONYMOUS"