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

ManagerLanguageDialog migrate to view binding

This commit is contained in:
HaliksaR 2020-11-16 00:19:22 +07:00
parent a7545071a1
commit d929092888
3 changed files with 63 additions and 70 deletions

View file

@ -2,63 +2,64 @@ package com.vanced.manager.ui.dialogs
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.view.ViewGroup.LayoutParams.*
import androidx.core.content.edit
import androidx.databinding.DataBindingUtil
import androidx.preference.PreferenceManager.getDefaultSharedPreferences
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import androidx.preference.PreferenceManager.*
import com.google.android.material.radiobutton.MaterialRadioButton
import com.vanced.manager.BuildConfig.MANAGER_LANGUAGES
import com.vanced.manager.R
import com.vanced.manager.BuildConfig.*
import com.vanced.manager.databinding.DialogManagerLanguageBinding
import com.vanced.manager.ui.core.BindingBottomSheetDialogFragment
import com.vanced.manager.utils.Extensions.getCheckedButtonTag
import com.vanced.manager.utils.LanguageHelper.getLanguageFormat
class ManagerLanguageDialog : BottomSheetDialogFragment() {
class ManagerLanguageDialog : BindingBottomSheetDialogFragment<DialogManagerLanguageBinding>() {
companion object {
fun newInstance(): ManagerLanguageDialog = ManagerLanguageDialog().apply {
arguments = Bundle()
}
}
private lateinit var binding: DialogManagerLanguageBinding
private val prefs by lazy { getDefaultSharedPreferences(requireActivity()) }
override fun onCreateView(
override fun binding(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.dialog_manager_language, container, false)
return binding.root
) = DialogManagerLanguageBinding.inflate(inflater, container, false)
override fun otherSetups() {
bindData()
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
addRadioButtons()
val language = prefs.getString("manager_lang", "System Default")
view.findViewWithTag<MaterialRadioButton>(language).isChecked = true
binding.languageSave.setOnClickListener {
val newPref = binding.languageRadiogroup.getCheckedButtonTag()
if (language != newPref) {
prefs.edit { putString("manager_lang", newPref) }
dismiss()
requireActivity().recreate()
} else {
dismiss()
private fun bindData() {
with(binding) {
addRadioButtons().forEach { mrb ->
languageRadiogroup.addView(mrb, MATCH_PARENT, WRAP_CONTENT)
}
}
}
private fun addRadioButtons() {
requireActivity().runOnUiThread {
(arrayOf("System Default") + MANAGER_LANGUAGES).forEach { lang ->
val button = MaterialRadioButton(requireActivity()).apply {
text = getLanguageFormat(requireActivity(), lang)
textSize = 18f
tag = lang
val language = prefs.getString("manager_lang", "System Default")
root.findViewWithTag<MaterialRadioButton>(language).isChecked = true
languageSave.setOnClickListener {
val newPref = binding.languageRadiogroup.getCheckedButtonTag()
if (language != newPref) {
prefs.edit { putString("manager_lang", newPref) }
dismiss()
requireActivity().recreate()
} else {
dismiss()
}
binding.languageRadiogroup.addView(button, MATCH_PARENT, WRAP_CONTENT)
}
}
}
private fun addRadioButtons() =
(arrayOf("System Default") + MANAGER_LANGUAGES).map { lang ->
MaterialRadioButton(requireActivity()).apply {
text = getLanguageFormat(requireActivity(), lang)
textSize = 18f
tag = lang
}
}
}

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/BottomDialogCard">

View file

@ -1,38 +1,30 @@
<?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:text="@string/language_title"
style="@style/BottomDialogCardTitle"/>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_height="450dp">
<TextView
style="@style/BottomDialogCardTitle"
android:text="@string/language_title" />
<androidx.core.widget.NestedScrollView
<RadioGroup
android:id="@+id/language_radiogroup"
android:layout_width="match_parent"
android:layout_height="450dp">
android:layout_height="wrap_content"/>
</androidx.core.widget.NestedScrollView>
<RadioGroup
android:id="@+id/language_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RadioGroup>
</androidx.core.widget.NestedScrollView>
<com.google.android.material.button.MaterialButton
android:id="@+id/language_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/language_save"
android:text="@string/save"
style="@style/BottomDialogButtonStyle"/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>