0
0
Fork 0
mirror of https://github.com/YTVanced/VancedManager synced 2024-11-26 05:05:15 +00:00

AppVersionSelectorDialog and VancedThemeSelectorDialog migrate to view binding

This commit is contained in:
HaliksaR 2020-11-15 23:48:22 +07:00
parent a1d4cdbbe5
commit a3767e4c9e
5 changed files with 139 additions and 110 deletions

View file

@ -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(
companion object {
private const val TAG_VERSIONS = "TAG_VERSIONS"
private const val TAG_APP = "TAG_APP"
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 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?) {
super.onViewCreated(view, savedInstanceState)
loadBoxes()
val tag = view.findViewWithTag<MaterialRadioButton>(prefs.getString("${app}_version", "latest"))
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
}
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) }
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() {
requireActivity().runOnUiThread {
versions?.forEach { version ->
val rb = MaterialRadioButton(requireActivity()).apply {
private fun loadBoxes() =
arguments?.getStringArrayList(TAG_VERSIONS)?.map { version ->
MaterialRadioButton(requireActivity()).apply {
text = version
tag = version
textSize = 18f
}
binding.dialogRadiogroup.addView(rb, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
}
}
}
override fun onDismiss(dialog: DialogInterface) {
super.onDismiss(dialog)
if (app == "vanced")
if (arguments?.getString(TAG_APP) == "vanced") {
VancedPreferencesDialog().show(requireActivity())
else
} else {
MusicPreferencesDialog().show(requireActivity())
}
}
}

View file

@ -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()

View file

@ -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 {

View file

@ -12,57 +12,67 @@ 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"))
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
}
binding.dialogSave.setOnClickListener {
dialogSave.setOnClickListener {
val checkedTag = binding.dialogRadiogroup.getCheckedButtonTag()
if (checkedTag != null)
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 {
private fun loadButtons() = vanced.get()?.array<String>("themes")?.value?.map {theme ->
MaterialRadioButton(requireActivity()).apply {
text = theme.convertToAppTheme(requireActivity())
tag = theme
textSize = 18f
}
binding.dialogRadiogroup.addView(rb, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
}
}
}
override fun onDismiss(dialog: DialogInterface) {
super.onDismiss(dialog)
VancedPreferencesDialog().show(requireActivity())
}
}

View file

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<layout>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/BottomDialogCard">
<LinearLayout
@ -23,16 +22,11 @@
android:layout_height="wrap_content">
</RadioGroup>
</androidx.core.widget.NestedScrollView>
<com.google.android.material.button.MaterialButton
android:id="@+id/dialog_save"
style="@style/BottomDialogButtonStyle"
android:text="@string/save" />
android:text="@string/save"
style="@style/BottomDialogButtonStyle"/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</layout>