VancedManager/app/src/main/java/com/vanced/manager/ui/dialogs/VancedLanguageSelectionDial...

94 lines
3.7 KiB
Kotlin
Raw Normal View History

2020-10-31 19:45:39 +00:00
package com.vanced.manager.ui.dialogs
2020-08-03 09:54:59 +00:00
import android.content.Context
2020-10-31 19:45:39 +00:00
import android.content.DialogInterface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2020-08-02 16:16:55 +00:00
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.widget.LinearLayout
2020-10-31 19:45:39 +00:00
import android.widget.Toast
2020-11-05 16:35:49 +00:00
import androidx.core.content.edit
2020-08-02 16:16:55 +00:00
import androidx.core.content.res.ResourcesCompat
import androidx.lifecycle.coroutineScope
import androidx.lifecycle.lifecycleScope
2020-10-31 19:45:39 +00:00
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
2020-08-02 16:16:55 +00:00
import com.google.android.material.button.MaterialButton
2020-08-02 19:10:13 +00:00
import com.google.android.material.checkbox.MaterialCheckBox
import com.vanced.manager.R
2020-11-08 12:46:17 +00:00
import com.vanced.manager.utils.Extensions.show
import com.vanced.manager.utils.InternetTools.vanced
2020-10-31 19:45:39 +00:00
import com.vanced.manager.utils.LanguageHelper.getDefaultVancedLanguages
2020-09-06 10:33:04 +00:00
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
2020-08-02 16:16:55 +00:00
import java.util.*
import kotlin.coroutines.coroutineContext
2020-10-31 19:45:39 +00:00
class VancedLanguageSelectionDialog : BottomSheetDialogFragment() {
2020-08-03 10:25:40 +00:00
private lateinit var langs: MutableList<String>
2020-10-31 19:45:39 +00:00
private val prefs by lazy { requireActivity().getSharedPreferences("installPrefs", Context.MODE_PRIVATE) }
2020-08-03 08:11:20 +00:00
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
2020-11-08 12:46:17 +00:00
return inflater.inflate(R.layout.dialog_vanced_language_selection, container, false)
}
2020-08-02 16:16:55 +00:00
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
langs = vanced.get()?.array<String>("langs")?.value ?: mutableListOf("null")
2020-08-09 15:28:56 +00:00
loadBoxes(view.findViewById(R.id.lang_button_ll))
2020-08-02 16:16:55 +00:00
view.findViewById<MaterialButton>(R.id.vanced_install_finish).setOnClickListener {
2020-10-31 19:45:39 +00:00
val chosenLangs = mutableListOf<String>()
2020-08-03 11:39:26 +00:00
if (!langs.contains("null"))
2020-08-04 10:02:36 +00:00
langs.forEach { lang ->
2020-08-03 11:39:26 +00:00
if (view.findViewWithTag<MaterialCheckBox>(lang).isChecked) {
chosenLangs.add(lang)
}
2020-08-02 16:16:55 +00:00
}
2020-10-31 19:45:39 +00:00
if (chosenLangs.isEmpty()) {
Toast.makeText(requireActivity(), R.string.select_at_least_one_lang, Toast.LENGTH_SHORT).show()
return@setOnClickListener
2020-08-02 16:16:55 +00:00
}
2020-10-31 19:45:39 +00:00
2020-11-05 16:35:49 +00:00
prefs?.edit { putString("lang", chosenLangs.joinToString()) }
2020-10-31 19:45:39 +00:00
dismiss()
2020-08-02 16:16:55 +00:00
}
}
2020-08-09 15:28:56 +00:00
private fun loadBoxes(ll: LinearLayout) {
lifecycleScope.launch { // default Main //// But why is it here?
val langPrefs = prefs.getString("lang", getDefaultVancedLanguages())
2020-10-31 19:45:39 +00:00
if (this@VancedLanguageSelectionDialog::langs.isInitialized) {
2020-08-09 15:28:56 +00:00
if (!langs.contains("null")) {
langs.forEach { lang ->
val loc = Locale(lang)
val box: MaterialCheckBox = MaterialCheckBox(requireActivity()).apply {
tag = lang
isChecked = langPrefs?.contains(lang) ?: false
2020-08-09 15:28:56 +00:00
text = loc.getDisplayLanguage(loc).capitalize(Locale.ROOT)
textSize = 18F
typeface = ResourcesCompat.getFont(requireActivity(), R.font.exo_bold)
}
ll.addView(box, MATCH_PARENT, WRAP_CONTENT)
}
}
} else {
2020-08-09 15:28:56 +00:00
loadBoxes(ll)
}
2020-08-09 15:28:56 +00:00
}
}
2020-10-31 19:45:39 +00:00
override fun onDismiss(dialog: DialogInterface) {
super.onDismiss(dialog)
2020-11-08 12:46:17 +00:00
VancedPreferencesDialog().show(requireActivity())
2020-10-31 19:45:39 +00:00
}
}