Skip to content

Commit

Permalink
font scale setting screen (#6453)
Browse files Browse the repository at this point in the history
  • Loading branch information
fedrunov authored Jul 18, 2022
1 parent cdbc197 commit 79762d9
Show file tree
Hide file tree
Showing 38 changed files with 1,195 additions and 221 deletions.
1 change: 1 addition & 0 deletions changelog.d/5687.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adds settings screen to change app font scale or enable using system setting
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/color_primary_alpha25" android:state_checked="true" android:state_enabled="false" />
<item android:color="?colorPrimary" android:state_checked="true" android:state_enabled="true" />
<item android:color="?vctr_content_quaternary"/>
</selector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?vctr_content_quaternary" android:state_enabled="false" />
<item android:color="?vctr_content_primary"/>
</selector>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import androidx.test.espresso.matcher.ViewMatchers.withText
import com.adevinta.android.barista.interaction.BaristaClickInteractions.clickOn
import com.adevinta.android.barista.interaction.BaristaDialogInteractions.clickDialogNegativeButton
import im.vector.app.R
import im.vector.app.espresso.tools.waitUntilActivityVisible
import im.vector.app.espresso.tools.waitUntilViewVisible
import im.vector.app.features.settings.font.FontScaleSettingActivity

class SettingsPreferencesRobot {

Expand All @@ -32,6 +34,8 @@ class SettingsPreferencesRobot {
clickOn(R.string.settings_theme)
clickDialogNegativeButton()
clickOn(R.string.font_size)
clickDialogNegativeButton()
waitUntilActivityVisible<FontScaleSettingActivity> {
pressBack()
}
}
}
1 change: 1 addition & 0 deletions vector/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@
<activity android:name=".features.poll.create.CreatePollActivity" />
<activity android:name=".features.location.LocationSharingActivity" />
<activity android:name=".features.location.live.map.LocationLiveMapViewActivity" />
<activity android:name=".features.settings.font.FontScaleSettingActivity"/>

<!-- Services -->

Expand Down
6 changes: 6 additions & 0 deletions vector/src/main/java/im/vector/app/core/di/FragmentModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ import im.vector.app.features.settings.devtools.GossipingEventsPaperTrailFragmen
import im.vector.app.features.settings.devtools.IncomingKeyRequestListFragment
import im.vector.app.features.settings.devtools.KeyRequestsFragment
import im.vector.app.features.settings.devtools.OutgoingKeyRequestListFragment
import im.vector.app.features.settings.font.FontScaleSettingFragment
import im.vector.app.features.settings.homeserver.HomeserverSettingsFragment
import im.vector.app.features.settings.ignored.VectorSettingsIgnoredUsersFragment
import im.vector.app.features.settings.legals.LegalsFragment
Expand Down Expand Up @@ -586,6 +587,11 @@ interface FragmentModule {
@FragmentKey(HomeserverSettingsFragment::class)
fun bindHomeserverSettingsFragment(fragment: HomeserverSettingsFragment): Fragment

@Binds
@IntoMap
@FragmentKey(FontScaleSettingFragment::class)
fun bindFontScaleSettingFragment(fragment: FontScaleSettingFragment): Fragment

@Binds
@IntoMap
@FragmentKey(VectorSettingsPinFragment::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ import im.vector.app.features.settings.devtools.AccountDataViewModel
import im.vector.app.features.settings.devtools.GossipingEventsPaperTrailViewModel
import im.vector.app.features.settings.devtools.KeyRequestListViewModel
import im.vector.app.features.settings.devtools.KeyRequestViewModel
import im.vector.app.features.settings.font.FontScaleSettingViewModel
import im.vector.app.features.settings.homeserver.HomeserverSettingsViewModel
import im.vector.app.features.settings.ignored.IgnoredUsersViewModel
import im.vector.app.features.settings.legals.LegalsViewModel
Expand Down Expand Up @@ -606,4 +607,9 @@ interface MavericksViewModelModule {
@IntoMap
@MavericksViewModelKey(LocationLiveMapViewModel::class)
fun locationLiveMapViewModelFactory(factory: LocationLiveMapViewModel.Factory): MavericksAssistedViewModelFactory<*, *>

@Binds
@IntoMap
@MavericksViewModelKey(FontScaleSettingViewModel::class)
fun fontScaleSettingViewModelFactory(factory: FontScaleSettingViewModel.Factory): MavericksAssistedViewModelFactory<*, *>
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package im.vector.app.core.di

import javax.inject.Qualifier

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class DefaultPreferences

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class NamedGlobalScope
18 changes: 18 additions & 0 deletions vector/src/main/java/im/vector/app/core/di/SingletonModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.content.SharedPreferences
import android.content.res.Resources
import androidx.preference.PreferenceManager
import com.google.i18n.phonenumbers.PhoneNumberUtil
import dagger.Binds
import dagger.Module
Expand All @@ -37,6 +38,8 @@ import im.vector.app.core.error.ErrorFormatter
import im.vector.app.core.resources.BuildMeta
import im.vector.app.core.time.Clock
import im.vector.app.core.time.DefaultClock
import im.vector.app.core.utils.AndroidSystemSettingsProvider
import im.vector.app.core.utils.SystemSettingsProvider
import im.vector.app.features.analytics.AnalyticsConfig
import im.vector.app.features.analytics.AnalyticsTracker
import im.vector.app.features.analytics.VectorAnalytics
Expand All @@ -48,6 +51,8 @@ import im.vector.app.features.navigation.Navigator
import im.vector.app.features.pin.PinCodeStore
import im.vector.app.features.pin.SharedPrefPinCodeStore
import im.vector.app.features.room.VectorRoomDisplayNameFallbackProvider
import im.vector.app.features.settings.FontScalePreferences
import im.vector.app.features.settings.FontScalePreferencesImpl
import im.vector.app.features.settings.VectorPreferences
import im.vector.app.features.ui.SharedPreferencesUiStateRepository
import im.vector.app.features.ui.UiStateRepository
Expand Down Expand Up @@ -97,6 +102,12 @@ abstract class VectorBindModule {

@Binds
abstract fun bindEmojiSpanify(emojiCompatWrapper: EmojiCompatWrapper): EmojiSpanify

@Binds
abstract fun bindFontScale(fontScale: FontScalePreferencesImpl): FontScalePreferences

@Binds
abstract fun bindSystemSettingsProvide(provider: AndroidSystemSettingsProvider): SystemSettingsProvider
}

@InstallIn(SingletonComponent::class)
Expand Down Expand Up @@ -200,4 +211,11 @@ object VectorStaticModule {
@Provides
@Singleton
fun providesBuildMeta() = BuildMeta()

@Provides
@Singleton
@DefaultPreferences
fun providesDefaultSharedPreferences(context: Context): SharedPreferences {
return PreferenceManager.getDefaultSharedPreferences(context.applicationContext)
}
}
64 changes: 64 additions & 0 deletions vector/src/main/java/im/vector/app/core/epoxy/FontScaleItem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.core.epoxy

import android.util.TypedValue
import android.widget.CompoundButton
import android.widget.RadioButton
import android.widget.TextView
import com.airbnb.epoxy.EpoxyAttribute
import com.airbnb.epoxy.EpoxyModelClass
import im.vector.app.R
import im.vector.app.features.settings.FontScaleValue

@EpoxyModelClass
abstract class FontScaleItem : VectorEpoxyModel<FontScaleItem.Holder>(R.layout.item_font_scale) {

companion object {
const val MINIMAL_TEXT_SIZE_DP = 10f
}

@EpoxyAttribute var fontScale: FontScaleValue? = null
@EpoxyAttribute var selected: Boolean = true
@EpoxyAttribute var enabled: Boolean = true

@EpoxyAttribute(EpoxyAttribute.Option.DoNotHash)
var checkChangeListener: CompoundButton.OnCheckedChangeListener? = null

override fun bind(holder: Holder) {
super.bind(holder)
val context = holder.view.context
holder.textView.text = fontScale?.let {
context.resources.getString(it.nameResId)
}
val index = fontScale?.index ?: 0
holder.textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, MINIMAL_TEXT_SIZE_DP + index * 2)
holder.textView.isEnabled = enabled
holder.button.isChecked = selected
holder.button.isEnabled = enabled
holder.button.isClickable = enabled
holder.view.onClick {
holder.button.performClick()
}
holder.button.setOnCheckedChangeListener(checkChangeListener)
}

class Holder : VectorEpoxyHolder() {
val button by bind<RadioButton>(R.id.font_scale_radio_button)
val textView by bind<TextView>(R.id.font_scale_text)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.core.epoxy

import android.widget.TextView
import com.airbnb.epoxy.EpoxyAttribute
import com.airbnb.epoxy.EpoxyModelClass
import im.vector.app.R

@EpoxyModelClass
abstract class FontScaleSectionItem : VectorEpoxyModel<FontScaleSectionItem.Holder>(R.layout.item_font_scale_section) {

@EpoxyAttribute var sectionName: String = ""

override fun bind(holder: Holder) {
super.bind(holder)
holder.textView.text = sectionName
}

class Holder : VectorEpoxyHolder() {
val textView by bind<TextView>(R.id.font_scale_section_name)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.core.epoxy

import android.widget.CheckBox
import android.widget.CompoundButton
import com.airbnb.epoxy.EpoxyAttribute
import com.airbnb.epoxy.EpoxyModelClass
import im.vector.app.R

@EpoxyModelClass
abstract class FontScaleUseSystemSettingsItem : VectorEpoxyModel<FontScaleUseSystemSettingsItem.Holder>(R.layout.item_font_scale_system) {

@EpoxyAttribute var useSystemSettings: Boolean = true

@EpoxyAttribute(EpoxyAttribute.Option.DoNotHash)
var checkChangeListener: CompoundButton.OnCheckedChangeListener? = null

override fun bind(holder: Holder) {
super.bind(holder)
holder.checkBox.isChecked = useSystemSettings
holder.checkBox.setOnCheckedChangeListener(checkChangeListener)
holder.view.onClick {
holder.checkBox.performClick()
}
}

class Holder : VectorEpoxyHolder() {
val checkBox by bind<CheckBox>(R.id.font_scale_use_system_checkbox)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import androidx.viewbinding.ViewBinding
import com.airbnb.mvrx.MavericksView
import com.bumptech.glide.util.Util
Expand All @@ -66,6 +67,7 @@ import im.vector.app.core.extensions.restart
import im.vector.app.core.extensions.setTextOrHide
import im.vector.app.core.extensions.singletonEntryPoint
import im.vector.app.core.extensions.toMvRxBundle
import im.vector.app.core.utils.AndroidSystemSettingsProvider
import im.vector.app.core.utils.ToolbarConfig
import im.vector.app.core.utils.toast
import im.vector.app.features.MainActivity
Expand All @@ -82,7 +84,8 @@ import im.vector.app.features.rageshake.BugReportActivity
import im.vector.app.features.rageshake.BugReporter
import im.vector.app.features.rageshake.RageShake
import im.vector.app.features.session.SessionListener
import im.vector.app.features.settings.FontScale
import im.vector.app.features.settings.FontScalePreferences
import im.vector.app.features.settings.FontScalePreferencesImpl
import im.vector.app.features.settings.VectorPreferences
import im.vector.app.features.themes.ActivityOtherThemes
import im.vector.app.features.themes.ThemeUtils
Expand Down Expand Up @@ -154,6 +157,10 @@ abstract class VectorBaseActivity<VB : ViewBinding> : AppCompatActivity(), Maver

@Inject
lateinit var rageShake: RageShake

@Inject
lateinit var fontScalePreferences: FontScalePreferences

lateinit var navigator: Navigator
private set
private lateinit var fragmentFactory: FragmentFactory
Expand All @@ -172,7 +179,8 @@ abstract class VectorBaseActivity<VB : ViewBinding> : AppCompatActivity(), Maver
private val restorables = ArrayList<Restorable>()

override fun attachBaseContext(base: Context) {
val vectorConfiguration = VectorConfiguration(this)
val fontScalePreferences = FontScalePreferencesImpl(PreferenceManager.getDefaultSharedPreferences(base), AndroidSystemSettingsProvider(base))
val vectorConfiguration = VectorConfiguration(this, fontScalePreferences)
super.attachBaseContext(vectorConfiguration.getLocalisedContext(base))
}

Expand Down Expand Up @@ -285,7 +293,7 @@ abstract class VectorBaseActivity<VB : ViewBinding> : AppCompatActivity(), Maver
* This method has to be called for the font size setting be supported correctly.
*/
private fun applyFontSize() {
resources.configuration.fontScale = FontScale.getFontScaleValue(this).scale
resources.configuration.fontScale = fontScalePreferences.getResolvedFontScaleValue().scale

@Suppress("DEPRECATION")
resources.updateConfiguration(resources.configuration, resources.displayMetrics)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.core.utils

import android.content.Context
import android.provider.Settings
import javax.inject.Inject

/**
* A helper to get system settings.
*/
interface SystemSettingsProvider {

/**
* @return system setting for font scale
*/
fun getSystemFontScale(): Float
}

class AndroidSystemSettingsProvider @Inject constructor(
private val context: Context,
) : SystemSettingsProvider {

override fun getSystemFontScale(): Float {
return Settings.System.getFloat(context.contentResolver, Settings.System.FONT_SCALE)
}
}
Loading

0 comments on commit 79762d9

Please sign in to comment.