diff --git a/presentation/src/main/java/common/util/TextViewStyler.kt b/presentation/src/main/java/common/util/TextViewStyler.kt index d56934020..c7bf34d06 100644 --- a/presentation/src/main/java/common/util/TextViewStyler.kt +++ b/presentation/src/main/java/common/util/TextViewStyler.kt @@ -22,6 +22,10 @@ import android.graphics.Typeface import android.util.AttributeSet import android.widget.TextView import com.moez.QKSMS.R +import common.util.TextViewStyler.Companion.SIZE_PRIMARY +import common.util.TextViewStyler.Companion.SIZE_SECONDARY +import common.util.TextViewStyler.Companion.SIZE_TERTIARY +import common.util.TextViewStyler.Companion.SIZE_TOOLBAR import common.util.extensions.getColorCompat import common.util.extensions.resolveThemeColor import common.widget.QkEditText @@ -48,69 +52,88 @@ class TextViewStyler @Inject constructor( const val SIZE_SECONDARY = 1 const val SIZE_TERTIARY = 2 const val SIZE_TOOLBAR = 3 - } - fun applyAttributes(textView: TextView, attrs: AttributeSet?) { - textView.run { - when (this) { - is QkTextView -> context.obtainStyledAttributes(attrs, R.styleable.QkTextView)?.run { - val colorAttr = getInt(R.styleable.QkTextView_textColor, -1) - val textSizeAttr = getInt(R.styleable.QkTextView_textSize, -1) - applyAttributes(textView, colorAttr, textSizeAttr) - recycle() + fun applyEditModeAttributes(textView: TextView, attrs: AttributeSet?) { + textView.run { + var colorAttr = 0 + var textSizeAttr = 0 + + when (this) { + is QkTextView -> context.obtainStyledAttributes(attrs, R.styleable.QkTextView)?.run { + colorAttr = getInt(R.styleable.QkTextView_textColor, -1) + textSizeAttr = getInt(R.styleable.QkTextView_textSize, -1) + recycle() + } + + is QkEditText -> context.obtainStyledAttributes(attrs, R.styleable.QkEditText)?.run { + colorAttr = getInt(R.styleable.QkEditText_textColor, -1) + textSizeAttr = getInt(R.styleable.QkEditText_textSize, -1) + recycle() + } + + else -> return } + setTextColor(when (colorAttr) { + COLOR_PRIMARY -> context.getColorCompat(R.color.textPrimary) + COLOR_SECONDARY -> context.getColorCompat(R.color.textSecondary) + COLOR_TERTIARY -> context.getColorCompat(R.color.textTertiary) + COLOR_PRIMARY_ON_THEME -> context.getColorCompat(R.color.textPrimaryDark) + COLOR_SECONDARY_ON_THEME -> context.getColorCompat(R.color.textSecondaryDark) + COLOR_TERTIARY_ON_THEME -> context.getColorCompat(R.color.textTertiaryDark) + COLOR_THEME -> context.getColorCompat(R.color.tools_theme) + else -> currentTextColor + }) - is QkEditText -> context.obtainStyledAttributes(attrs, R.styleable.QkEditText)?.run { - val colorAttr = getInt(R.styleable.QkEditText_textColor, -1) - val textSizeAttr = getInt(R.styleable.QkEditText_textSize, -1) - applyAttributes(textView, colorAttr, textSizeAttr) - recycle() + textSize = when (textSizeAttr) { + SIZE_PRIMARY -> 16f + SIZE_SECONDARY -> 14f + SIZE_TERTIARY -> 12f + SIZE_TOOLBAR -> 20f + else -> textSize / paint.density } } + } + } + + fun applyAttributes(textView: TextView, attrs: AttributeSet?) { + textView.run { + var colorAttr = 0 + var textSizeAttr = 0 if (!prefs.systemFont.get()) { fontProvider.getLato { lato -> setTypeface(lato, typeface?.style ?: Typeface.NORMAL) } } - } - } - private fun applyAttributes(textView: TextView, colorAttr: Int, textSizeAttr: Int) { - textView.run { - if (isInEditMode) { - setTextColor(context.getColorCompat(when (colorAttr) { - COLOR_PRIMARY -> R.color.textPrimary - COLOR_SECONDARY -> R.color.textSecondary - COLOR_TERTIARY -> R.color.textTertiary - COLOR_PRIMARY_ON_THEME -> R.color.textPrimaryDark - COLOR_SECONDARY_ON_THEME -> R.color.textSecondaryDark - COLOR_TERTIARY_ON_THEME -> R.color.textTertiaryDark - COLOR_THEME -> R.color.tools_theme - else -> R.color.textPrimary - })) + when (this) { + is QkTextView -> context.obtainStyledAttributes(attrs, R.styleable.QkTextView)?.run { + colorAttr = getInt(R.styleable.QkTextView_textColor, -1) + textSizeAttr = getInt(R.styleable.QkTextView_textSize, -1) + recycle() + } - textSize = when (textSizeAttr) { - SIZE_PRIMARY -> 16f - SIZE_SECONDARY -> 14f - SIZE_TERTIARY -> 12f - SIZE_TOOLBAR -> 20f - else -> textSize + is QkEditText -> context.obtainStyledAttributes(attrs, R.styleable.QkEditText)?.run { + colorAttr = getInt(R.styleable.QkEditText_textColor, -1) + textSizeAttr = getInt(R.styleable.QkEditText_textSize, -1) + recycle() } - } else { - setTextColor(when (colorAttr) { - COLOR_PRIMARY -> context.resolveThemeColor(android.R.attr.textColorPrimary) - COLOR_SECONDARY -> context.resolveThemeColor(android.R.attr.textColorSecondary) - COLOR_TERTIARY -> context.resolveThemeColor(android.R.attr.textColorTertiary) - COLOR_PRIMARY_ON_THEME -> colors.theme().textPrimary - COLOR_SECONDARY_ON_THEME -> colors.theme().textSecondary - COLOR_TERTIARY_ON_THEME -> colors.theme().textTertiary - COLOR_THEME -> colors.theme().theme - else -> currentTextColor - }) - setTextSize(textView, textSizeAttr) + else -> return } + + setTextColor(when (colorAttr) { + COLOR_PRIMARY -> context.resolveThemeColor(android.R.attr.textColorPrimary) + COLOR_SECONDARY -> context.resolveThemeColor(android.R.attr.textColorSecondary) + COLOR_TERTIARY -> context.resolveThemeColor(android.R.attr.textColorTertiary) + COLOR_PRIMARY_ON_THEME -> colors.theme().textPrimary + COLOR_SECONDARY_ON_THEME -> colors.theme().textSecondary + COLOR_TERTIARY_ON_THEME -> colors.theme().textTertiary + COLOR_THEME -> colors.theme().theme + else -> currentTextColor + }) + + setTextSize(textView, textSizeAttr) } } diff --git a/presentation/src/main/java/common/widget/QkEditText.kt b/presentation/src/main/java/common/widget/QkEditText.kt index f3baa1b7d..0259710f7 100644 --- a/presentation/src/main/java/common/widget/QkEditText.kt +++ b/presentation/src/main/java/common/widget/QkEditText.kt @@ -56,6 +56,8 @@ class QkEditText @JvmOverloads constructor(context: Context, attrs: AttributeSet if (!isInEditMode) { appComponent.inject(this) textViewStyler.applyAttributes(this, attrs) + } else { + TextViewStyler.applyEditModeAttributes(this, attrs) } } diff --git a/presentation/src/main/java/common/widget/QkTextView.kt b/presentation/src/main/java/common/widget/QkTextView.kt index c7f06202d..171b5c681 100644 --- a/presentation/src/main/java/common/widget/QkTextView.kt +++ b/presentation/src/main/java/common/widget/QkTextView.kt @@ -34,6 +34,8 @@ open class QkTextView @JvmOverloads constructor(context: Context, attrs: Attribu if (!isInEditMode) { appComponent.inject(this) textViewStyler.applyAttributes(this, attrs) + } else { + TextViewStyler.applyEditModeAttributes(this, attrs) } }