0
0
Fork 0
mirror of https://github.com/YTVanced/VancedManager synced 2024-11-26 13:12:59 +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.content.DialogInterface
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.core.content.edit 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.google.android.material.radiobutton.MaterialRadioButton
import com.vanced.manager.R import com.vanced.manager.R
import com.vanced.manager.databinding.DialogBottomRadioButtonBinding 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.getCheckedButtonTag
import com.vanced.manager.utils.Extensions.getDefaultPrefs import com.vanced.manager.utils.Extensions.getDefaultPrefs
import com.vanced.manager.utils.Extensions.show import com.vanced.manager.utils.Extensions.show
class AppVersionSelectorDialog( class AppVersionSelectorDialog : BindingBottomSheetDialogFragment<DialogBottomRadioButtonBinding>() {
private val versions: List<String>?,
private val app: String
) : BottomSheetDialogFragment() {
private lateinit var binding: DialogBottomRadioButtonBinding
private val prefs by lazy { requireActivity().getDefaultPrefs() } private val prefs by lazy { requireActivity().getDefaultPrefs() }
override fun onCreateView( companion object {
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.dialog_bottom_radio_button, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { private const val TAG_VERSIONS = "TAG_VERSIONS"
super.onViewCreated(view, savedInstanceState) private const val TAG_APP = "TAG_APP"
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) }
dismiss() fun newInstance(
} versions: List<String>?,
} app: String
): AppVersionSelectorDialog = AppVersionSelectorDialog().apply {
private fun loadBoxes() { arguments = Bundle().apply {
requireActivity().runOnUiThread { val arrayList = arrayListOf<String>()
versions?.forEach { version -> versions?.let { arrayList.addAll(it) }
val rb = MaterialRadioButton(requireActivity()).apply { putStringArrayList(TAG_VERSIONS, arrayList)
text = version putString(TAG_APP, app)
tag = version
textSize = 18f
}
binding.dialogRadiogroup.addView(rb, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
} }
} }
} }
override fun onDismiss(dialog: DialogInterface) { override fun binding(
super.onDismiss(dialog) inflater: LayoutInflater,
if (app == "vanced") container: ViewGroup?,
VancedPreferencesDialog().show(requireActivity()) savedInstanceState: Bundle?
else ) = DialogBottomRadioButtonBinding.inflate(inflater, container, false)
MusicPreferencesDialog().show(requireActivity())
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())
}
}
} }

View file

@ -37,7 +37,10 @@ class MusicPreferencesDialog : BottomSheetDialogFragment() {
binding.openVersionSelector.setOnClickListener { binding.openVersionSelector.setOnClickListener {
dismiss() dismiss()
AppVersionSelectorDialog(musicVersionsConv, "music").show(requireActivity()) AppVersionSelectorDialog.newInstance(
versions = musicVersionsConv,
app = "music"
).show(requireActivity())
} }
binding.musicInstall.setOnClickListener { binding.musicInstall.setOnClickListener {
dismiss() dismiss()

View file

@ -56,7 +56,10 @@ class VancedPreferencesDialog : BottomSheetDialogFragment() {
binding.openVersionSelector.setOnClickListener { binding.openVersionSelector.setOnClickListener {
dismiss() dismiss()
AppVersionSelectorDialog(vancedVersionsConv, "vanced").show(requireActivity()) AppVersionSelectorDialog.newInstance(
versions = vancedVersionsConv,
app = "vanced"
).show(requireActivity())
} }
binding.openLanguageSelector.setOnClickListener { binding.openLanguageSelector.setOnClickListener {

View file

@ -12,51 +12,62 @@ import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.radiobutton.MaterialRadioButton import com.google.android.material.radiobutton.MaterialRadioButton
import com.vanced.manager.R import com.vanced.manager.R
import com.vanced.manager.databinding.DialogBottomRadioButtonBinding 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.convertToAppTheme
import com.vanced.manager.utils.Extensions.getCheckedButtonTag import com.vanced.manager.utils.Extensions.getCheckedButtonTag
import com.vanced.manager.utils.Extensions.show import com.vanced.manager.utils.Extensions.show
import com.vanced.manager.utils.InternetTools.vanced 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) } private val prefs by lazy { requireActivity().getSharedPreferences("installPrefs", Context.MODE_PRIVATE) }
override fun onCreateView( override fun binding(
inflater: LayoutInflater, inflater: LayoutInflater,
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ) = DialogBottomRadioButtonBinding.inflate(inflater, container, false)
binding = DataBindingUtil.inflate(inflater, R.layout.dialog_bottom_radio_button, container, false)
return binding.root override fun otherSetups() {
bindData()
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { private fun bindData() {
loadButtons() with(binding) {
binding.dialogTitle.text = requireActivity().getString(R.string.theme) loadButtons()?.forEach { mrb ->
val tag = view.findViewWithTag<MaterialRadioButton>(prefs.getString("theme", "dark")) dialogRadiogroup.addView(
if (tag != null) { mrb,
tag.isChecked = true ViewGroup.LayoutParams.MATCH_PARENT,
} ViewGroup.LayoutParams.WRAP_CONTENT
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)
} }
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) super.onDismiss(dialog)
VancedPreferencesDialog().show(requireActivity()) VancedPreferencesDialog().show(requireActivity())
} }
} }

View file

@ -1,38 +1,32 @@
<?xml version="1.0" encoding="utf-8"?> <?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" <LinearLayout
style="@style/BottomDialogCard"> 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_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content">
android:orientation="vertical">
<TextView <RadioGroup
android:id="@+id/dialog_title" android:id="@+id/dialog_radiogroup"
style="@style/BottomDialogCardTitle" />
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content">
<RadioGroup </RadioGroup>
android:id="@+id/dialog_radiogroup" </androidx.core.widget.NestedScrollView>
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RadioGroup> <com.google.android.material.button.MaterialButton
android:id="@+id/dialog_save"
</androidx.core.widget.NestedScrollView> android:text="@string/save"
style="@style/BottomDialogButtonStyle"/>
<com.google.android.material.button.MaterialButton </LinearLayout>
android:id="@+id/dialog_save" </com.google.android.material.card.MaterialCardView>
style="@style/BottomDialogButtonStyle"
android:text="@string/save" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</layout>