mirror of
https://github.com/YTVanced/VancedManager
synced 2024-11-22 11:15:10 +00:00
AppVersionSelectorDialog and VancedThemeSelectorDialog migrate to view binding
This commit is contained in:
parent
a1d4cdbbe5
commit
a3767e4c9e
5 changed files with 139 additions and 110 deletions
|
@ -2,72 +2,91 @@ package com.vanced.manager.ui.dialogs
|
|||
|
||||
import android.content.DialogInterface
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.content.edit
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
|
||||
import com.google.android.material.radiobutton.MaterialRadioButton
|
||||
import com.vanced.manager.R
|
||||
import com.vanced.manager.databinding.DialogBottomRadioButtonBinding
|
||||
import com.vanced.manager.ui.core.BindingBottomSheetDialogFragment
|
||||
import com.vanced.manager.utils.Extensions.getCheckedButtonTag
|
||||
import com.vanced.manager.utils.Extensions.getDefaultPrefs
|
||||
import com.vanced.manager.utils.Extensions.show
|
||||
|
||||
class AppVersionSelectorDialog(
|
||||
private val versions: List<String>?,
|
||||
private val app: String
|
||||
) : BottomSheetDialogFragment() {
|
||||
class AppVersionSelectorDialog : BindingBottomSheetDialogFragment<DialogBottomRadioButtonBinding>() {
|
||||
|
||||
private lateinit var binding: DialogBottomRadioButtonBinding
|
||||
private val prefs by lazy { requireActivity().getDefaultPrefs() }
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
binding = DataBindingUtil.inflate(inflater, R.layout.dialog_bottom_radio_button, container, false)
|
||||
return binding.root
|
||||
}
|
||||
companion object {
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
loadBoxes()
|
||||
val tag = view.findViewWithTag<MaterialRadioButton>(prefs.getString("${app}_version", "latest"))
|
||||
if (tag != null) {
|
||||
tag.isChecked = true
|
||||
}
|
||||
binding.dialogTitle.text = requireActivity().getString(R.string.version)
|
||||
binding.dialogSave.setOnClickListener {
|
||||
val checkedTag = binding.dialogRadiogroup.getCheckedButtonTag()
|
||||
if (checkedTag != null)
|
||||
prefs.edit { putString("${app}_version", checkedTag) }
|
||||
private const val TAG_VERSIONS = "TAG_VERSIONS"
|
||||
private const val TAG_APP = "TAG_APP"
|
||||
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadBoxes() {
|
||||
requireActivity().runOnUiThread {
|
||||
versions?.forEach { version ->
|
||||
val rb = MaterialRadioButton(requireActivity()).apply {
|
||||
text = version
|
||||
tag = version
|
||||
textSize = 18f
|
||||
}
|
||||
binding.dialogRadiogroup.addView(rb, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
||||
fun newInstance(
|
||||
versions: List<String>?,
|
||||
app: String
|
||||
): AppVersionSelectorDialog = AppVersionSelectorDialog().apply {
|
||||
arguments = Bundle().apply {
|
||||
val arrayList = arrayListOf<String>()
|
||||
versions?.let { arrayList.addAll(it) }
|
||||
putStringArrayList(TAG_VERSIONS, arrayList)
|
||||
putString(TAG_APP, app)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDismiss(dialog: DialogInterface) {
|
||||
super.onDismiss(dialog)
|
||||
if (app == "vanced")
|
||||
VancedPreferencesDialog().show(requireActivity())
|
||||
else
|
||||
MusicPreferencesDialog().show(requireActivity())
|
||||
override fun binding(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
) = DialogBottomRadioButtonBinding.inflate(inflater, container, false)
|
||||
|
||||
override fun otherSetups() {
|
||||
bindData()
|
||||
}
|
||||
|
||||
private fun bindData() {
|
||||
with(binding) {
|
||||
loadBoxes()?.forEach { mrb ->
|
||||
dialogRadiogroup.addView(
|
||||
mrb,
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
)
|
||||
}
|
||||
val tag = root.findViewWithTag<MaterialRadioButton>(
|
||||
prefs.getString("${arguments?.getString(TAG_APP)}_version", "latest")
|
||||
)
|
||||
if (tag != null) {
|
||||
tag.isChecked = true
|
||||
}
|
||||
dialogTitle.text = getString(R.string.version)
|
||||
dialogSave.setOnClickListener {
|
||||
val checkedTag = dialogRadiogroup.getCheckedButtonTag()
|
||||
if (checkedTag != null) {
|
||||
prefs.edit { putString("${arguments?.getString(TAG_APP)}_version", checkedTag) }
|
||||
}
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadBoxes() =
|
||||
arguments?.getStringArrayList(TAG_VERSIONS)?.map { version ->
|
||||
MaterialRadioButton(requireActivity()).apply {
|
||||
text = version
|
||||
tag = version
|
||||
textSize = 18f
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDismiss(dialog: DialogInterface) {
|
||||
super.onDismiss(dialog)
|
||||
if (arguments?.getString(TAG_APP) == "vanced") {
|
||||
VancedPreferencesDialog().show(requireActivity())
|
||||
} else {
|
||||
MusicPreferencesDialog().show(requireActivity())
|
||||
}
|
||||
}
|
||||
}
|
|
@ -37,7 +37,10 @@ class MusicPreferencesDialog : BottomSheetDialogFragment() {
|
|||
|
||||
binding.openVersionSelector.setOnClickListener {
|
||||
dismiss()
|
||||
AppVersionSelectorDialog(musicVersionsConv, "music").show(requireActivity())
|
||||
AppVersionSelectorDialog.newInstance(
|
||||
versions = musicVersionsConv,
|
||||
app = "music"
|
||||
).show(requireActivity())
|
||||
}
|
||||
binding.musicInstall.setOnClickListener {
|
||||
dismiss()
|
||||
|
|
|
@ -56,7 +56,10 @@ class VancedPreferencesDialog : BottomSheetDialogFragment() {
|
|||
|
||||
binding.openVersionSelector.setOnClickListener {
|
||||
dismiss()
|
||||
AppVersionSelectorDialog(vancedVersionsConv, "vanced").show(requireActivity())
|
||||
AppVersionSelectorDialog.newInstance(
|
||||
versions = vancedVersionsConv,
|
||||
app = "vanced"
|
||||
).show(requireActivity())
|
||||
}
|
||||
|
||||
binding.openLanguageSelector.setOnClickListener {
|
||||
|
|
|
@ -12,51 +12,62 @@ import com.google.android.material.bottomsheet.BottomSheetDialogFragment
|
|||
import com.google.android.material.radiobutton.MaterialRadioButton
|
||||
import com.vanced.manager.R
|
||||
import com.vanced.manager.databinding.DialogBottomRadioButtonBinding
|
||||
import com.vanced.manager.ui.core.BindingBottomSheetDialogFragment
|
||||
import com.vanced.manager.utils.Extensions.convertToAppTheme
|
||||
import com.vanced.manager.utils.Extensions.getCheckedButtonTag
|
||||
import com.vanced.manager.utils.Extensions.show
|
||||
import com.vanced.manager.utils.InternetTools.vanced
|
||||
|
||||
class VancedThemeSelectorDialog : BottomSheetDialogFragment() {
|
||||
class VancedThemeSelectorDialog : BindingBottomSheetDialogFragment<DialogBottomRadioButtonBinding>() {
|
||||
|
||||
companion object {
|
||||
|
||||
fun newInstance(): VancedThemeSelectorDialog = VancedThemeSelectorDialog().apply {
|
||||
arguments = Bundle()
|
||||
}
|
||||
}
|
||||
|
||||
private lateinit var binding: DialogBottomRadioButtonBinding
|
||||
private val prefs by lazy { requireActivity().getSharedPreferences("installPrefs", Context.MODE_PRIVATE) }
|
||||
|
||||
override fun onCreateView(
|
||||
override fun binding(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
binding = DataBindingUtil.inflate(inflater, R.layout.dialog_bottom_radio_button, container, false)
|
||||
return binding.root
|
||||
) = DialogBottomRadioButtonBinding.inflate(inflater, container, false)
|
||||
|
||||
override fun otherSetups() {
|
||||
bindData()
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
loadButtons()
|
||||
binding.dialogTitle.text = requireActivity().getString(R.string.theme)
|
||||
val tag = view.findViewWithTag<MaterialRadioButton>(prefs.getString("theme", "dark"))
|
||||
if (tag != null) {
|
||||
tag.isChecked = true
|
||||
}
|
||||
binding.dialogSave.setOnClickListener {
|
||||
val checkedTag = binding.dialogRadiogroup.getCheckedButtonTag()
|
||||
if (checkedTag != null)
|
||||
prefs.edit { putString("theme", checkedTag) }
|
||||
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadButtons() {
|
||||
requireActivity().runOnUiThread {
|
||||
vanced.get()?.array<String>("themes")?.value?.forEach { theme ->
|
||||
val rb = MaterialRadioButton(requireActivity()).apply {
|
||||
text = theme.convertToAppTheme(requireActivity())
|
||||
tag = theme
|
||||
textSize = 18f
|
||||
}
|
||||
binding.dialogRadiogroup.addView(rb, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
||||
private fun bindData() {
|
||||
with(binding) {
|
||||
loadButtons()?.forEach { mrb ->
|
||||
dialogRadiogroup.addView(
|
||||
mrb,
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
)
|
||||
}
|
||||
dialogTitle.text = requireActivity().getString(R.string.theme)
|
||||
val tag = root.findViewWithTag<MaterialRadioButton>(prefs.getString("theme", "dark"))
|
||||
if (tag != null) {
|
||||
tag.isChecked = true
|
||||
}
|
||||
dialogSave.setOnClickListener {
|
||||
val checkedTag = binding.dialogRadiogroup.getCheckedButtonTag()
|
||||
if (checkedTag != null) {
|
||||
prefs.edit { putString("theme", checkedTag) }
|
||||
}
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadButtons() = vanced.get()?.array<String>("themes")?.value?.map {theme ->
|
||||
MaterialRadioButton(requireActivity()).apply {
|
||||
text = theme.convertToAppTheme(requireActivity())
|
||||
tag = theme
|
||||
textSize = 18f
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -64,5 +75,4 @@ class VancedThemeSelectorDialog : BottomSheetDialogFragment() {
|
|||
super.onDismiss(dialog)
|
||||
VancedPreferencesDialog().show(requireActivity())
|
||||
}
|
||||
|
||||
}
|
|
@ -1,38 +1,32 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layout>
|
||||
<com.google.android.material.card.MaterialCardView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
style="@style/BottomDialogCard">
|
||||
|
||||
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
style="@style/BottomDialogCard">
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<LinearLayout
|
||||
<TextView
|
||||
android:id="@+id/dialog_title"
|
||||
style="@style/BottomDialogCardTitle"/>
|
||||
|
||||
<androidx.core.widget.NestedScrollView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/dialog_title"
|
||||
style="@style/BottomDialogCardTitle" />
|
||||
|
||||
<androidx.core.widget.NestedScrollView
|
||||
<RadioGroup
|
||||
android:id="@+id/dialog_radiogroup"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<RadioGroup
|
||||
android:id="@+id/dialog_radiogroup"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
</RadioGroup>
|
||||
</androidx.core.widget.NestedScrollView>
|
||||
|
||||
</RadioGroup>
|
||||
|
||||
</androidx.core.widget.NestedScrollView>
|
||||
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/dialog_save"
|
||||
style="@style/BottomDialogButtonStyle"
|
||||
android:text="@string/save" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
|
||||
</layout>
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/dialog_save"
|
||||
android:text="@string/save"
|
||||
style="@style/BottomDialogButtonStyle"/>
|
||||
</LinearLayout>
|
||||
</com.google.android.material.card.MaterialCardView>
|
Loading…
Reference in a new issue