VancedManager/app/src/main/java/com/vanced/manager/ui/dialogs/ManagerAccentColorDialog.kt

128 lines
4.6 KiB
Kotlin
Raw Normal View History

2020-10-31 19:45:39 +00:00
package com.vanced.manager.ui.dialogs
2020-12-18 19:58:54 +00:00
import android.content.DialogInterface
2020-12-14 21:18:58 +00:00
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
2020-10-31 19:45:39 +00:00
import android.os.Bundle
2020-12-15 07:32:47 +00:00
import android.text.Editable
import android.text.TextWatcher
2020-10-31 19:45:39 +00:00
import android.view.LayoutInflater
import android.view.ViewGroup
2020-12-14 21:18:58 +00:00
import android.widget.TextView
import android.widget.Toast
2020-10-31 19:45:39 +00:00
import androidx.preference.PreferenceManager.getDefaultSharedPreferences
2020-12-14 21:18:58 +00:00
import com.madrapps.pikolo.listeners.OnColorSelectionListener
import com.vanced.manager.R
import com.vanced.manager.core.ui.base.BindingDialogFragment
2020-10-31 19:45:39 +00:00
import com.vanced.manager.databinding.DialogManagerAccentColorBinding
import com.vanced.manager.utils.*
2021-02-03 18:24:28 +00:00
import com.vanced.manager.utils.AppUtils.log
2020-10-31 19:45:39 +00:00
2020-12-14 21:18:58 +00:00
class ManagerAccentColorDialog : BindingDialogFragment<DialogManagerAccentColorBinding>() {
companion object {
fun newInstance(): ManagerAccentColorDialog = ManagerAccentColorDialog().apply {
arguments = Bundle()
}
}
2020-10-31 19:45:39 +00:00
private val prefs by lazy { getDefaultSharedPreferences(requireActivity()) }
override fun binding(
2020-10-31 19:45:39 +00:00
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
) = DialogManagerAccentColorBinding.inflate(inflater, container, false)
override fun otherSetups() {
dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
bindData()
2020-12-18 19:58:54 +00:00
}
override fun onCancel(dialog: DialogInterface) {
super.onCancel(dialog)
2020-12-19 16:09:42 +00:00
mutableAccentColor.value = prefs.getInt("manager_accent_color", defAccentColor)
2020-10-31 19:45:39 +00:00
}
private fun bindData() {
with(binding) {
2020-12-19 16:09:42 +00:00
val accent = prefs.getInt("manager_accent_color", defAccentColor)
2020-12-15 07:32:47 +00:00
hexEdittext.apply {
setText(accent.toHex(), TextView.BufferType.EDITABLE)
addTextChangedListener(object : TextWatcher {
2021-04-19 15:57:14 +00:00
override fun beforeTextChanged(
s: CharSequence?,
start: Int,
count: Int,
after: Int
) {
}
2020-12-15 07:32:47 +00:00
2021-04-19 15:57:14 +00:00
override fun onTextChanged(
s: CharSequence?,
start: Int,
before: Int,
count: Int
) {
2020-12-15 07:32:47 +00:00
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
2021-04-19 15:57:14 +00:00
} catch (e: IllegalArgumentException) {
}
2020-12-15 07:32:47 +00:00
}
}
override fun afterTextChanged(s: Editable?) {}
})
}
2020-12-14 21:18:58 +00:00
accentPicker.apply {
setColor(accent)
setColorSelectionListener(object : OnColorSelectionListener {
override fun onColorSelected(color: Int) {
mutableAccentColor.value = color
hexEdittext.setText(color.toHex(), TextView.BufferType.EDITABLE)
}
2020-12-15 07:32:47 +00:00
override fun onColorSelectionEnd(color: Int) {}
2020-12-14 21:18:58 +00:00
2020-12-15 07:32:47 +00:00
override fun onColorSelectionStart(color: Int) {}
2020-12-14 21:18:58 +00:00
})
}
2020-12-15 08:23:27 +00:00
accentCancel.setOnClickListener {
mutableAccentColor.value = accent
dismiss()
}
accentSave.setOnClickListener {
2020-12-14 21:18:58 +00:00
try {
2020-12-15 07:32:47 +00:00
val colorFromEditText = Color.parseColor(hexEdittext.text.toString())
mutableAccentColor.value = colorFromEditText
prefs.managerAccent = colorFromEditText
2020-12-14 21:18:58 +00:00
} catch (e: IllegalArgumentException) {
2021-02-03 18:24:28 +00:00
log("VMTheme", getString(R.string.failed_accent))
2021-04-19 15:57:14 +00:00
Toast.makeText(
requireActivity(),
getString(R.string.failed_accent),
Toast.LENGTH_SHORT
).show()
2020-12-14 21:18:58 +00:00
return@setOnClickListener
}
2020-12-14 21:18:58 +00:00
dismiss()
}
accentReset.setOnClickListener {
prefs.managerAccent = defAccentColor
2020-12-14 21:18:58 +00:00
mutableAccentColor.value = defAccentColor
dismiss()
2020-10-31 19:45:39 +00:00
}
}
}
}