VancedManager/app/src/main/java/com/vanced/manager/ui/fragments/VancedLanguageSelectionFrag...

71 lines
2.8 KiB
Kotlin
Raw Normal View History

package com.vanced.manager.ui.fragments
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
import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.Fragment
import androidx.navigation.findNavController
import androidx.preference.PreferenceManager
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-08-02 16:16:55 +00:00
import com.vanced.manager.utils.InternetTools.baseUrl
2020-08-03 09:01:25 +00:00
import com.vanced.manager.utils.JsonHelper.getJsonArray
2020-08-03 08:11:20 +00:00
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
2020-08-02 18:22:33 +00:00
import kotlinx.coroutines.launch
2020-08-02 16:16:55 +00:00
import kotlinx.coroutines.runBlocking
import java.util.*
2020-08-02 16:16:55 +00:00
class VancedLanguageSelectionFragment : Fragment() {
2020-08-03 09:01:25 +00:00
private val langs: MutableList<String?> = runBlocking { getJsonArray("${PreferenceManager.getDefaultSharedPreferences(activity).getString("install_url", baseUrl)}/vanced.json").string("langs").value }
2020-08-03 08:11:20 +00:00
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
2020-07-03 14:43:36 +00:00
activity?.title = getString(R.string.install)
return inflater.inflate(R.layout.fragment_vanced_language_selection, container, false)
}
2020-08-02 16:16:55 +00:00
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2020-08-03 08:11:20 +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-08-03 08:11:20 +00:00
val chosenLangs = mutableListOf("en")
2020-08-03 09:01:25 +00:00
for (lang in langs) {
if (view.findViewWithTag<MaterialCheckBox>(lang).isChecked) {
if (lang != null) {
2020-08-03 08:11:20 +00:00
chosenLangs.add(lang)
2020-08-02 16:16:55 +00:00
}
}
}
2020-08-03 08:11:20 +00:00
PreferenceManager.getDefaultSharedPreferences(activity).edit()?.putString("lang", chosenLangs.joinToString())?.apply()
view.findNavController().navigate(R.id.action_installTo_homeFragment)
2020-08-02 16:16:55 +00:00
}
}
2020-08-03 09:01:25 +00:00
2020-08-02 16:16:55 +00:00
2020-08-03 08:11:20 +00:00
private fun loadBoxes(ll: LinearLayout) = CoroutineScope(Dispatchers.Main).launch {
2020-08-03 09:01:25 +00:00
for (lang in langs) {
if (lang != null) {
2020-08-02 19:10:13 +00:00
val box: MaterialCheckBox = MaterialCheckBox(activity).apply {
2020-08-02 16:16:55 +00:00
tag = lang
text = Locale(lang).displayLanguage
textSize = 16F
typeface = activity?.let { ResourcesCompat.getFont(it, R.font.exo_bold) }
}
ll.addView(box, MATCH_PARENT, WRAP_CONTENT)
}
}
}
}