Code refactor

This commit is contained in:
h4h13 2020-01-28 23:17:48 +05:30
parent fdb2ab300e
commit 08033d6a16
31 changed files with 196 additions and 147 deletions

View file

@ -21,10 +21,10 @@ import android.view.View
import code.name.monkey.appthemehelper.R
class ATEColorPreference @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
defStyleRes: Int = 0
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
defStyleRes: Int = 0
) : Preference(context, attrs, defStyleAttr, defStyleRes) {
private var mView: View? = null

View file

@ -7,14 +7,17 @@ import android.graphics.Paint
import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import android.graphics.drawable.Drawable
import androidx.core.content.ContextCompat
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import androidx.core.content.ContextCompat
import code.name.monkey.appthemehelper.R
import kotlin.math.min
class BorderCircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : FrameLayout(context, attrs, defStyleAttr) {
class BorderCircleView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
private val mCheck: Drawable? = ContextCompat.getDrawable(context, R.drawable.ate_check)
private val paint: Paint = Paint()
@ -49,14 +52,14 @@ class BorderCircleView @JvmOverloads constructor(context: Context, attrs: Attrib
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val widthMode = View.MeasureSpec.getMode(widthMeasureSpec)
val heightMode = View.MeasureSpec.getMode(heightMeasureSpec)
if (widthMode == View.MeasureSpec.EXACTLY && heightMode != View.MeasureSpec.EXACTLY) {
val width = View.MeasureSpec.getSize(widthMeasureSpec)
val widthMode = MeasureSpec.getMode(widthMeasureSpec)
val heightMode = MeasureSpec.getMode(heightMeasureSpec)
if (widthMode == MeasureSpec.EXACTLY && heightMode != MeasureSpec.EXACTLY) {
val width = MeasureSpec.getSize(widthMeasureSpec)
var height = width
if (heightMode == View.MeasureSpec.AT_MOST) {
height = Math.min(height, View.MeasureSpec.getSize(heightMeasureSpec))
if (heightMode == MeasureSpec.AT_MOST) {
height = min(height, MeasureSpec.getSize(heightMeasureSpec))
}
setMeasuredDimension(width, height)
} else {
@ -73,8 +76,18 @@ class BorderCircleView @JvmOverloads constructor(context: Context, attrs: Attrib
}
val circleCenter = (canvasSize - borderWidth * 2) / 2
canvas.drawCircle((circleCenter + borderWidth).toFloat(), (circleCenter + borderWidth).toFloat(), (canvasSize - borderWidth * 2) / 2 + borderWidth - 4.0f, paintBorder)
canvas.drawCircle((circleCenter + borderWidth).toFloat(), (circleCenter + borderWidth).toFloat(), (canvasSize - borderWidth * 2) / 2 - 4.0f, paint)
canvas.drawCircle(
(circleCenter + borderWidth).toFloat(),
(circleCenter + borderWidth).toFloat(),
(canvasSize - borderWidth * 2) / 2 + borderWidth - 4.0f,
paintBorder
)
canvas.drawCircle(
(circleCenter + borderWidth).toFloat(),
(circleCenter + borderWidth).toFloat(),
(canvasSize - borderWidth * 2) / 2 - 4.0f,
paint
)
if (isActivated) {
val offset = canvasSize / 2 - mCheck!!.intrinsicWidth / 2

View file

@ -25,9 +25,9 @@ import code.name.monkey.appthemehelper.ThemeStore
import code.name.monkey.appthemehelper.common.prefs.BorderCircleView
class ATEColorPreference @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : Preference(context, attrs, defStyleAttr) {
private var mView: View? = null

View file

@ -19,12 +19,7 @@ import android.util.AttributeSet
import androidx.preference.DialogPreference
open class ATEDialogPreference @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0, defStyleRes: Int = 0
) : DialogPreference(context, attrs, defStyleAttr, defStyleRes) {
init {
}
}
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0, defStyleRes: Int = 0
) : DialogPreference(context, attrs, defStyleAttr, defStyleRes)

View file

@ -20,10 +20,10 @@ import androidx.preference.EditTextPreference
import code.name.monkey.appthemehelper.R
class ATEEditTextPreference @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
defStyleRes: Int = 0
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
defStyleRes: Int = 0
) : EditTextPreference(context, attrs, defStyleAttr, defStyleRes) {
init {

View file

@ -16,9 +16,7 @@ package code.name.monkey.appthemehelper.common.prefs.supportv7;
import android.content.Context;
import android.util.AttributeSet;
import androidx.preference.ListPreference;
import code.name.monkey.appthemehelper.R;
public class ATEListPreference extends ListPreference {
@ -45,7 +43,8 @@ public class ATEListPreference extends ListPreference {
private void init(Context context, AttributeSet attrs) {
setLayoutResource(R.layout.ate_preference_custom_support);
if (getSummary() == null || getSummary().toString().trim().isEmpty())
if (getSummary() == null || getSummary().toString().trim().isEmpty()) {
setSummary("%s");
}
}
}

View file

@ -21,14 +21,13 @@ import androidx.preference.Preference
import code.name.monkey.appthemehelper.ThemeStore
class ATEPreference @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
defStyleRes: Int = 0
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
defStyleRes: Int = 0
) : Preference(context, attrs, defStyleAttr, defStyleRes) {
init {
icon?.setColorFilter(ThemeStore.textColorSecondary(context), PorterDuff.Mode.SRC_IN)
}
}

View file

@ -22,11 +22,12 @@ import androidx.preference.PreferenceViewHolder
import code.name.monkey.appthemehelper.ThemeStore
class ATEPreferenceCategory @JvmOverloads constructor(
context: Context?,
attrs: AttributeSet?,
defStyleAttr: Int = -1,
defStyleRes: Int = -1
context: Context?,
attrs: AttributeSet?,
defStyleAttr: Int = -1,
defStyleRes: Int = -1
) : PreferenceCategory(context, attrs, defStyleAttr, defStyleRes) {
override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder)
val title = holder.itemView.findViewById<TextView>(android.R.id.title)

View file

@ -14,18 +14,17 @@
package code.name.monkey.appthemehelper.common.prefs.supportv7
import androidx.fragment.app.DialogFragment
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import code.name.monkey.appthemehelper.common.prefs.supportv7.dialogs.ATEListPreferenceDialogFragmentCompat
import code.name.monkey.appthemehelper.common.prefs.supportv7.dialogs.ATEPreferenceDialogFragment
/**
* @author Karim Abou Zeid (kabouzeid)
*/
abstract class ATEPreferenceFragmentCompat : PreferenceFragmentCompat() {
override fun onDisplayPreferenceDialog(preference: Preference) {
if (callbackFragment is OnPreferenceDisplayDialogCallback) {
(callbackFragment as OnPreferenceDisplayDialogCallback).onPreferenceDisplayDialog(this, preference)

View file

@ -23,7 +23,12 @@ class ATESeekBarPreference : SeekBarPreference {
init()
}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(
context,
attrs,
defStyleAttr,
defStyleRes
) {
init()
}

View file

@ -21,9 +21,9 @@ import androidx.appcompat.widget.AppCompatTextView
import code.name.monkey.appthemehelper.ThemeStore
class ATEAccentTextView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr) {
init {

View file

@ -1,32 +0,0 @@
/*
* Copyright (c) 2019 Hemanth Savarala.
*
* Licensed under the GNU General Public License v3
*
* This is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/
package code.name.monkey.appthemehelper.common.views
import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatCheckBox
import code.name.monkey.appthemehelper.ATH
import code.name.monkey.appthemehelper.ThemeStore
class ATECheckBox @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : AppCompatCheckBox(context, attrs, defStyleAttr) {
init {
ATH.setTint(this, ThemeStore.accentColor(context))
}
}

View file

@ -16,14 +16,13 @@ package code.name.monkey.appthemehelper.common.views
import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import code.name.monkey.appthemehelper.ThemeStore
import com.google.android.material.textview.MaterialTextView
class ATEPrimaryTextView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : MaterialTextView(context, attrs, defStyleAttr) {
init {

View file

@ -20,9 +20,9 @@ import code.name.monkey.appthemehelper.ThemeStore
import com.google.android.material.textview.MaterialTextView
class ATESecondaryTextView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : MaterialTextView(context, attrs, defStyleAttr) {
init {

View file

@ -7,7 +7,6 @@ import androidx.appcompat.widget.SwitchCompat
import code.name.monkey.appthemehelper.ATH
import code.name.monkey.appthemehelper.ThemeStore
/**
* @author Aidan Follestad (afollestad)
*/

View file

@ -26,8 +26,12 @@ object ATHUtil {
try {
return Class.forName(clsName)
} catch (t: Throwable) {
throw IllegalStateException(String.format("%s is not in your class path! You must include the associated library.", clsName))
throw IllegalStateException(
String.format(
"%s is not in your class path! You must include the associated library.",
clsName
)
)
}
}
}

View file

@ -4,7 +4,6 @@ import android.graphics.Color
import androidx.annotation.ColorInt
import androidx.annotation.FloatRange
object ColorUtil {
fun desaturateColor(color: Int, ratio: Float): Int {
val hsv = FloatArray(3)
@ -14,6 +13,7 @@ object ColorUtil {
return Color.HSVToColor(hsv)
}
fun stripAlpha(@ColorInt color: Int): Int {
return -0x1000000 or color
}
@ -79,7 +79,6 @@ object ColorUtil {
return Color.argb(a.toInt(), r.toInt(), g.toInt(), b.toInt())
}
private fun getColorDarkness(@ColorInt color: Int): Double {
return if (color == Color.BLACK)
1.0
@ -104,9 +103,9 @@ object ColorUtil {
@ColorInt
fun getMixedColor(@ColorInt color1: Int, @ColorInt color2: Int): Int {
return Color.rgb(
(Color.red(color1) + Color.red(color2)) / 2,
(Color.green(color1) + Color.green(color2)) / 2,
(Color.blue(color1) + Color.blue(color2)) / 2
(Color.red(color1) + Color.red(color2)) / 2,
(Color.green(color1) + Color.green(color2)) / 2,
(Color.blue(color1) + Color.blue(color2)) / 2
)
}

View file

@ -5,7 +5,6 @@ import android.graphics.drawable.Drawable
import android.graphics.drawable.TransitionDrawable
import androidx.annotation.ColorInt
object DrawableUtil {
fun createTransitionDrawable(@ColorInt startColor: Int, @ColorInt endColor: Int): TransitionDrawable {

View file

@ -7,17 +7,19 @@ import com.afollestad.materialdialogs.internal.button.DialogActionButton
import com.google.android.material.button.MaterialButton
import com.google.android.material.textfield.TextInputLayout
object MaterialUtil {
@JvmOverloads
fun setTint(button: MaterialButton, background: Boolean = true,
color: Int = ThemeStore.accentColor(button.context)) {
fun setTint(
button: MaterialButton, background: Boolean = true,
color: Int = ThemeStore.accentColor(button.context)
) {
button.isAllCaps = false
val context = button.context
val colorState = ColorStateList.valueOf(color)
val textColor = ColorStateList.valueOf(MaterialValueHelper.getPrimaryTextColor(context, ColorUtil.isColorLight(color)))
val textColor =
ColorStateList.valueOf(MaterialValueHelper.getPrimaryTextColor(context, ColorUtil.isColorLight(color)))
if (background) {
@ -28,7 +30,6 @@ object MaterialUtil {
button.setTextColor(colorState)
button.iconTint = colorState
}
}
fun setTint(textInputLayout: TextInputLayout, background: Boolean = true) {
@ -44,14 +45,18 @@ object MaterialUtil {
textInputLayout.defaultHintTextColor = colorState
textInputLayout.isHintAnimationEnabled = true
}
}
fun setTint(button: DialogActionButton, color: Int = ThemeStore.accentColor(button.context), background: Boolean = true) {
fun setTint(
button: DialogActionButton,
color: Int = ThemeStore.accentColor(button.context),
background: Boolean = true
) {
val temp = button as AppCompatButton
val context = temp.context
val colorState = ColorStateList.valueOf(color)
val textColor = ColorStateList.valueOf(MaterialValueHelper.getPrimaryTextColor(context, ColorUtil.isColorLight(color)))
val textColor =
ColorStateList.valueOf(MaterialValueHelper.getPrimaryTextColor(context, ColorUtil.isColorLight(color)))
if (background) {
temp.backgroundTintList = colorState

View file

@ -7,7 +7,6 @@ import androidx.core.content.ContextCompat
import code.name.monkey.appthemehelper.R
object MaterialValueHelper {
@SuppressLint("PrivateResource")

View file

@ -12,30 +12,41 @@ import com.google.android.material.navigation.NavigationView
object NavigationViewUtil {
fun setItemIconColors(navigationView: NavigationView, @ColorInt normalColor: Int, @ColorInt selectedColor: Int) {
val iconSl = ColorStateList(arrayOf(intArrayOf(-android.R.attr.state_checked), intArrayOf(android.R.attr.state_checked)), intArrayOf(normalColor, selectedColor))
val iconSl = ColorStateList(
arrayOf(
intArrayOf(-android.R.attr.state_checked),
intArrayOf(android.R.attr.state_checked)
), intArrayOf(normalColor, selectedColor)
)
navigationView.itemIconTintList = iconSl
val drawable = navigationView.itemBackground
navigationView.itemBackground = TintHelper.createTintedDrawable(drawable, ColorUtil.withAlpha(ThemeStore.accentColor(navigationView.context), 0.2f))
navigationView.itemBackground = TintHelper.createTintedDrawable(
drawable,
ColorUtil.withAlpha(ThemeStore.accentColor(navigationView.context), 0.2f)
)
}
fun setItemTextColors(navigationView: NavigationView, @ColorInt normalColor: Int, @ColorInt selectedColor: Int) {
val textSl = ColorStateList(
arrayOf(intArrayOf(-android.R.attr.state_checked), intArrayOf(android.R.attr.state_checked)),
intArrayOf(normalColor, selectedColor))
arrayOf(intArrayOf(-android.R.attr.state_checked), intArrayOf(android.R.attr.state_checked)),
intArrayOf(normalColor, selectedColor)
)
navigationView.itemTextColor = textSl
}
fun setItemIconColors(bottomNavigationView: BottomNavigationView, @ColorInt normalColor: Int, @ColorInt selectedColor: Int) {
val iconSl = ColorStateList(
arrayOf(intArrayOf(-android.R.attr.state_checked), intArrayOf(android.R.attr.state_checked)),
intArrayOf(normalColor, selectedColor))
arrayOf(intArrayOf(-android.R.attr.state_checked), intArrayOf(android.R.attr.state_checked)),
intArrayOf(normalColor, selectedColor)
)
bottomNavigationView.itemIconTintList = iconSl
}
fun setItemTextColors(bottomNavigationView: BottomNavigationView, @ColorInt normalColor: Int, @ColorInt selectedColor: Int) {
val textSl = ColorStateList(
arrayOf(intArrayOf(-android.R.attr.state_checked), intArrayOf(android.R.attr.state_checked)),
intArrayOf(normalColor, selectedColor))
arrayOf(intArrayOf(-android.R.attr.state_checked), intArrayOf(android.R.attr.state_checked)),
intArrayOf(normalColor, selectedColor)
)
bottomNavigationView.itemTextColor = textSl
}
}

View file

@ -1,23 +0,0 @@
package code.name.monkey.appthemehelper.util
import android.content.res.ColorStateList
import androidx.annotation.ColorInt
import com.google.android.material.tabs.TabLayout
object TabLayoutUtil {
fun setTabIconColors(tabLayout: TabLayout?, @ColorInt normalColor: Int, @ColorInt selectedColor: Int) {
if (tabLayout == null)
return
val sl = ColorStateList(arrayOf(intArrayOf(-android.R.attr.state_selected), intArrayOf(android.R.attr.state_selected)),
intArrayOf(normalColor, selectedColor))
for (i in 0 until tabLayout.tabCount) {
val tab = tabLayout.getTabAt(i)
if (tab != null && tab.icon != null) {
tab.icon = TintHelper.createTintedDrawable(tab.icon, sl)
}
}
}
}

View file

@ -4,7 +4,6 @@ import android.graphics.drawable.Drawable
import android.view.View
import android.view.ViewTreeObserver
object ViewUtil {
fun removeOnGlobalLayoutListener(v: View, listener: ViewTreeObserver.OnGlobalLayoutListener) {
v.viewTreeObserver.removeOnGlobalLayoutListener(listener)