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

json array improvements

This commit is contained in:
X1nto 2020-08-03 13:01:25 +04:00
parent ecaf591100
commit e77314c658
2 changed files with 17 additions and 25 deletions

View file

@ -11,14 +11,11 @@ import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.Fragment
import androidx.navigation.findNavController
import androidx.preference.PreferenceManager
import com.beust.klaxon.JsonObject
import com.beust.klaxon.Parser
import com.github.kittinunf.fuel.coroutines.awaitString
import com.github.kittinunf.fuel.httpGet
import com.google.android.material.button.MaterialButton
import com.google.android.material.checkbox.MaterialCheckBox
import com.vanced.manager.R
import com.vanced.manager.utils.InternetTools.baseUrl
import com.vanced.manager.utils.JsonHelper.getJsonArray
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -27,7 +24,7 @@ import java.util.*
class VancedLanguageSelectionFragment : Fragment() {
private val langs: Array<String>? = runBlocking { getLangs() }
private val langs: MutableList<String?> = runBlocking { getJsonArray("${PreferenceManager.getDefaultSharedPreferences(activity).getString("install_url", baseUrl)}/vanced.json").string("langs").value }
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
@ -42,9 +39,9 @@ class VancedLanguageSelectionFragment : Fragment() {
loadBoxes(view.findViewById(R.id.lang_button_ll))
view.findViewById<MaterialButton>(R.id.vanced_install_finish).setOnClickListener {
val chosenLangs = mutableListOf("en")
if (langs != null) {
for (lang in langs) {
if (view.findViewWithTag<MaterialCheckBox>(lang).isChecked) {
if (lang != null) {
chosenLangs.add(lang)
}
}
@ -54,18 +51,11 @@ class VancedLanguageSelectionFragment : Fragment() {
}
}
private suspend fun getLangs(): Array<String>? {
val langObj = Parser.default().parse(
StringBuilder(
"https://${PreferenceManager.getDefaultSharedPreferences(activity).getString("install_url", baseUrl)}/vanced.json".httpGet().awaitString()
)
) as JsonObject
return langObj.array<String>("langs")?.toTypedArray()
}
private fun loadBoxes(ll: LinearLayout) = CoroutineScope(Dispatchers.Main).launch {
if (langs != null) {
for (lang in langs) {
if (lang != null) {
val box: MaterialCheckBox = MaterialCheckBox(activity).apply {
tag = lang
text = Locale(lang).displayLanguage

View file

@ -1,5 +1,6 @@
package com.vanced.manager.utils
import com.beust.klaxon.JsonArray
import com.beust.klaxon.JsonObject
import com.beust.klaxon.Parser
import com.github.kittinunf.fuel.coroutines.awaitString
@ -7,13 +8,14 @@ import com.github.kittinunf.fuel.httpGet
object JsonHelper {
suspend fun getJson(url: String): JsonObject {
val result = url.httpGet().awaitString()
suspend fun getJson(url: String): JsonObject =
Parser.default().parse(
StringBuilder(url.httpGet().awaitString())
) as JsonObject
val parser: Parser = Parser.default()
val stringBuilder: StringBuilder = StringBuilder(result)
return parser.parse(stringBuilder) as JsonObject
}
suspend fun getJsonArray(url: String): JsonArray<*> =
Parser.default().parse(
StringBuilder(url.httpGet().awaitString())
) as JsonArray<*>
}