From d9290928883f93501ef2f345c169b5a89010cf7d Mon Sep 17 00:00:00 2001 From: HaliksaR Date: Mon, 16 Nov 2020 00:19:22 +0700 Subject: [PATCH] ManagerLanguageDialog migrate to view binding --- .../ui/dialogs/ManagerLanguageDialog.kt | 77 ++++++++++--------- .../dialog_installation_files_detected.xml | 2 +- .../res/layout/dialog_manager_language.xml | 54 ++++++------- 3 files changed, 63 insertions(+), 70 deletions(-) diff --git a/app/src/main/java/com/vanced/manager/ui/dialogs/ManagerLanguageDialog.kt b/app/src/main/java/com/vanced/manager/ui/dialogs/ManagerLanguageDialog.kt index 8592dd62..0df816f8 100644 --- a/app/src/main/java/com/vanced/manager/ui/dialogs/ManagerLanguageDialog.kt +++ b/app/src/main/java/com/vanced/manager/ui/dialogs/ManagerLanguageDialog.kt @@ -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() { + + 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(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(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 + } + } } \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_installation_files_detected.xml b/app/src/main/res/layout/dialog_installation_files_detected.xml index b6f2404f..4d293b55 100644 --- a/app/src/main/res/layout/dialog_installation_files_detected.xml +++ b/app/src/main/res/layout/dialog_installation_files_detected.xml @@ -1,6 +1,6 @@ diff --git a/app/src/main/res/layout/dialog_manager_language.xml b/app/src/main/res/layout/dialog_manager_language.xml index dee0e780..09e14045 100644 --- a/app/src/main/res/layout/dialog_manager_language.xml +++ b/app/src/main/res/layout/dialog_manager_language.xml @@ -1,38 +1,30 @@ - + - + - + + + android:layout_height="450dp"> - - - + android:layout_height="wrap_content"/> + - - - - - - - - - - - - - + + + \ No newline at end of file