Created
June 3, 2019 23:36
-
-
Save glureau/21127dbcbb630336dc90c144ae502852 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************************************************************************************** | |
* Storage of AutoSizeConfiguration | |
* | |
* 1 - We cannot setTextSize without disabling autosize (or use Reflection but way more fragile...) | |
* 2 - Disabling autosize reset all values and attrs is not re-parsed so data is lost. | |
* Due to these 2 reasons, we need a way to keep track of the previous values to restore them when required. | |
* | |
* The solution is to store in a static map a weak reference of the view and an AutoSizeConfiguration data class. | |
* So if there is no data available (first usage or first view destroyed and recreated), | |
* we store the attributes in our data class, and if there is already a data class, we re-use them. | |
**********************************************************************************************************************/ | |
private val autoSizeConfigurations = mutableMapOf<WeakReference<AppCompatTextView>, AutoSizeConfiguration>() | |
private data class AutoSizeConfiguration(val minTextSize: Int, val maxTextSize: Int, val stepGranularity: Int) | |
@SuppressLint("RestrictedApi") | |
private fun AppCompatTextView.providesAutoSizeConfiguration(): AutoSizeConfiguration { | |
val previousConfigurations = autoSizeConfigurations.filterKeys { it.get() === this } | |
return if (previousConfigurations.isNotEmpty()) { | |
previousConfigurations.values.toList()[0] | |
} else { | |
AutoSizeConfiguration(autoSizeMinTextSize, autoSizeMaxTextSize, autoSizeStepGranularity) | |
.also { autoSizeConfigurations[WeakReference(this)] = it } | |
} | |
} | |
// Values copied from AppCompatTextViewAutoSizeHelper | |
// Default minimum size for auto-sizing text in scaled pixels. | |
private const val DEFAULT_AUTO_SIZE_MIN_TEXT_SIZE_IN_SP = 12f | |
// Default maximum size for auto-sizing text in scaled pixels. | |
private const val DEFAULT_AUTO_SIZE_MAX_TEXT_SIZE_IN_SP = 112f | |
// Default value for the step size in pixels. | |
private const val DEFAULT_AUTO_SIZE_GRANULARITY_IN_PX = 1 | |
private fun AppCompatTextView.safeAutoSizeMinTextSize(previousConfiguration: AutoSizeConfiguration): Int = | |
if (previousConfiguration.minTextSize != -1) previousConfiguration.minTextSize | |
else TypedValue.applyDimension( | |
TypedValue.COMPLEX_UNIT_SP, | |
DEFAULT_AUTO_SIZE_MIN_TEXT_SIZE_IN_SP, | |
resources.displayMetrics | |
).toInt() | |
private fun AppCompatTextView.safeAutoSizeMaxTextSize(previousConfiguration: AutoSizeConfiguration) = | |
if (previousConfiguration.maxTextSize != -1) previousConfiguration.maxTextSize | |
else TypedValue.applyDimension( | |
TypedValue.COMPLEX_UNIT_SP, | |
DEFAULT_AUTO_SIZE_MAX_TEXT_SIZE_IN_SP, | |
resources.displayMetrics | |
).toInt() | |
private fun safeAutoSizeStepGranularity(previousConfiguration: AutoSizeConfiguration) = | |
if (previousConfiguration.stepGranularity != -1) previousConfiguration.stepGranularity | |
else DEFAULT_AUTO_SIZE_GRANULARITY_IN_PX |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment