0
0
Fork 0
mirror of https://github.com/YTVanced/VancedManager synced 2024-11-17 16:55:10 +00:00

improved hex input field

This commit is contained in:
X1nto 2020-12-15 11:32:47 +04:00
parent cc72b1c7d1
commit 15b289e83e
2 changed files with 35 additions and 21 deletions

View file

@ -2,6 +2,8 @@ package com.vanced.manager.ui.dialogs
import android.graphics.Color import android.graphics.Color
import android.os.Bundle import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
@ -14,6 +16,7 @@ import com.vanced.manager.R
import com.vanced.manager.core.ui.base.BindingDialogFragment import com.vanced.manager.core.ui.base.BindingDialogFragment
import com.vanced.manager.databinding.DialogManagerAccentColorBinding import com.vanced.manager.databinding.DialogManagerAccentColorBinding
import com.vanced.manager.utils.Extensions.toHex import com.vanced.manager.utils.Extensions.toHex
import com.vanced.manager.utils.ThemeHelper.accentColor
import com.vanced.manager.utils.ThemeHelper.defAccentColor import com.vanced.manager.utils.ThemeHelper.defAccentColor
import com.vanced.manager.utils.ThemeHelper.mutableAccentColor import com.vanced.manager.utils.ThemeHelper.mutableAccentColor
@ -41,7 +44,31 @@ class ManagerAccentColorDialog : BindingDialogFragment<DialogManagerAccentColorB
private fun bindData() { private fun bindData() {
with(binding) { with(binding) {
val accent = prefs.getInt("manager_accent", defAccentColor) val accent = prefs.getInt("manager_accent", defAccentColor)
hexEdittext.setText(accent.toHex(), TextView.BufferType.EDITABLE) hexEdittext.apply {
setText(accent.toHex(), TextView.BufferType.EDITABLE)
addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (length() == 0) {
setText("#")
setSelection(1)
}
if (accentColor.value?.toHex() != text.toString() && length() == 7) {
try {
val colorFromEditText = Color.parseColor(text.toString())
accentPicker.setColor(colorFromEditText)
mutableAccentColor.value = colorFromEditText
} catch (e: java.lang.IllegalArgumentException) { }
}
}
override fun afterTextChanged(s: Editable?) {}
})
}
accentPicker.apply { accentPicker.apply {
setColor(accent) setColor(accent)
setColorSelectionListener(object : OnColorSelectionListener { setColorSelectionListener(object : OnColorSelectionListener {
@ -50,21 +77,17 @@ class ManagerAccentColorDialog : BindingDialogFragment<DialogManagerAccentColorB
hexEdittext.setText(color.toHex(), TextView.BufferType.EDITABLE) hexEdittext.setText(color.toHex(), TextView.BufferType.EDITABLE)
} }
override fun onColorSelectionEnd(color: Int) { override fun onColorSelectionEnd(color: Int) {}
return
}
override fun onColorSelectionStart(color: Int) { override fun onColorSelectionStart(color: Int) {}
return
}
}) })
} }
accentSave.setOnClickListener { accentSave.setOnClickListener {
try { try {
val colorFromEdittext = Color.parseColor(hexEdittext.text.toString()) val colorFromEditText = Color.parseColor(hexEdittext.text.toString())
mutableAccentColor.value = colorFromEdittext mutableAccentColor.value = colorFromEditText
prefs.edit { putInt("manager_accent", colorFromEdittext) } prefs.edit { putInt("manager_accent", colorFromEditText) }
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
Log.d("VMTheme", getString(R.string.failed_accent)) Log.d("VMTheme", getString(R.string.failed_accent))
Toast.makeText(requireActivity(), getString(R.string.failed_accent), Toast.LENGTH_SHORT).show() Toast.makeText(requireActivity(), getString(R.string.failed_accent), Toast.LENGTH_SHORT).show()
@ -73,10 +96,6 @@ class ManagerAccentColorDialog : BindingDialogFragment<DialogManagerAccentColorB
dismiss() dismiss()
} }
accentSave.setOnClickListener {
mutableAccentColor.value = accent
dismiss()
}
accentReset.setOnClickListener { accentReset.setOnClickListener {
prefs.edit { putInt("manager_accent", defAccentColor) } prefs.edit { putInt("manager_accent", defAccentColor) }
mutableAccentColor.value = defAccentColor mutableAccentColor.value = defAccentColor

View file

@ -20,7 +20,8 @@
<com.google.android.material.textfield.TextInputEditText <com.google.android.material.textfield.TextInputEditText
android:id="@+id/hex_edittext" android:id="@+id/hex_edittext"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" /> android:layout_height="wrap_content"
android:maxLength="7"/>
<RelativeLayout <RelativeLayout
android:layout_width="match_parent" android:layout_width="match_parent"
@ -33,12 +34,6 @@
android:layout_alignParentStart="true" android:layout_alignParentStart="true"
android:text="@string/reset" /> android:text="@string/reset" />
<com.vanced.manager.ui.core.ThemedOutlinedMaterialButton
android:id="@+id/accent_cancel"
style="@style/OutlinedButtonStyle"
android:layout_alignEnd="@id/accent_save"
android:text="@string/cancel" />
<com.vanced.manager.ui.core.ThemedMaterialButton <com.vanced.manager.ui.core.ThemedMaterialButton
android:id="@+id/accent_save" android:id="@+id/accent_save"
style="@style/ButtonStyle" style="@style/ButtonStyle"