From 11efe9e76427b1212597dcf3a9f8520f7962a8c6 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 24 Jul 2022 12:15:51 +0200 Subject: [PATCH] fetch public instances again --- .../adapters/SearchHistoryAdapter.kt | 1 - .../github/libretube/obj/ChapterSegment.kt | 2 +- .../com/github/libretube/obj/SearchItem.kt | 2 +- .../java/com/github/libretube/obj/Segment.kt | 2 +- .../com/github/libretube/obj/StreamItem.kt | 2 +- .../java/com/github/libretube/obj/Subtitle.kt | 2 +- .../libretube/preferences/InstanceSettings.kt | 94 +++++++------------ .../github/libretube/update/UpdateChecker.kt | 1 - 8 files changed, 38 insertions(+), 68 deletions(-) diff --git a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt index 049ca4bf9..1f123083c 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt @@ -1,6 +1,5 @@ package com.github.libretube.adapters -import android.content.Context import android.view.LayoutInflater import android.view.ViewGroup import android.widget.EditText diff --git a/app/src/main/java/com/github/libretube/obj/ChapterSegment.kt b/app/src/main/java/com/github/libretube/obj/ChapterSegment.kt index e6bdf2eee..38120cc81 100644 --- a/app/src/main/java/com/github/libretube/obj/ChapterSegment.kt +++ b/app/src/main/java/com/github/libretube/obj/ChapterSegment.kt @@ -6,5 +6,5 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties data class ChapterSegment( var title: String? = null, var image: String? = null, - var start: Long? = null + var start: Long? = null ) diff --git a/app/src/main/java/com/github/libretube/obj/SearchItem.kt b/app/src/main/java/com/github/libretube/obj/SearchItem.kt index 50d6d6ec3..526ecc071 100644 --- a/app/src/main/java/com/github/libretube/obj/SearchItem.kt +++ b/app/src/main/java/com/github/libretube/obj/SearchItem.kt @@ -16,7 +16,7 @@ data class SearchItem( var uploadedDate: String? = null, var duration: Long? = null, var views: Long? = null, - var uploaderVerified: Boolean? = null, + var uploaderVerified: Boolean? = null, // Channel and Playlist attributes var name: String? = null, var description: String? = null, diff --git a/app/src/main/java/com/github/libretube/obj/Segment.kt b/app/src/main/java/com/github/libretube/obj/Segment.kt index 89d3f8fbb..b4eff6aec 100644 --- a/app/src/main/java/com/github/libretube/obj/Segment.kt +++ b/app/src/main/java/com/github/libretube/obj/Segment.kt @@ -7,4 +7,4 @@ data class Segment( val actionType: String? = null, val category: String? = null, val segment: List? = arrayListOf() -) \ No newline at end of file +) diff --git a/app/src/main/java/com/github/libretube/obj/StreamItem.kt b/app/src/main/java/com/github/libretube/obj/StreamItem.kt index 4670d80f7..b8eb2ee9e 100644 --- a/app/src/main/java/com/github/libretube/obj/StreamItem.kt +++ b/app/src/main/java/com/github/libretube/obj/StreamItem.kt @@ -16,4 +16,4 @@ data class StreamItem( var uploaderVerified: Boolean? = null, var uploaded: Long? = null, var shortDescription: String? = null -) \ No newline at end of file +) diff --git a/app/src/main/java/com/github/libretube/obj/Subtitle.kt b/app/src/main/java/com/github/libretube/obj/Subtitle.kt index 2aa66149f..1bcdc6957 100644 --- a/app/src/main/java/com/github/libretube/obj/Subtitle.kt +++ b/app/src/main/java/com/github/libretube/obj/Subtitle.kt @@ -9,4 +9,4 @@ data class Subtitle( val name: String? = null, val code: String? = null, val autoGenerated: Boolean? = null -) \ No newline at end of file +) diff --git a/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt b/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt index de570e60a..3bd2405ef 100644 --- a/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt @@ -4,7 +4,6 @@ import android.content.ContentResolver import android.content.Intent import android.net.Uri import android.os.Bundle -import android.text.TextUtils import android.util.Log import android.widget.Toast import androidx.activity.result.ActivityResultLauncher @@ -211,27 +210,43 @@ class InstanceSettings : PreferenceFragmentCompat() { } private fun initCustomInstances(instancePref: ListPreference) { - val customInstances = PreferenceHelper.getCustomInstances() + lifecycleScope.launchWhenCreated { + val customInstances = PreferenceHelper.getCustomInstances() - var instanceNames = resources.getStringArray(R.array.instances) - var instanceValues = resources.getStringArray(R.array.instancesValue) - customInstances.forEach { instance -> - instanceNames += instance.name - instanceValues += instance.apiUrl - } + var instanceNames = arrayListOf() + var instanceValues = arrayListOf() - // add custom instances to the list preference - instancePref.entries = instanceNames - instancePref.entryValues = instanceValues - instancePref.summaryProvider = - Preference.SummaryProvider { preference -> - val text = preference.entry - if (TextUtils.isEmpty(text)) { - "kavin.rocks (Official)" - } else { - text + // fetch official public instances + + val response = try { + RetrofitInstance.api.getInstances("https://instances.tokhmi.xyz/") + } catch (e: Exception) { + e.printStackTrace() + emptyList() + } + + response.forEach { + if (it.name != null && it.api_url != null) { + instanceNames += it.name!! + instanceValues += it.api_url!! } } + + customInstances.forEach { instance -> + instanceNames += instance.name + instanceValues += instance.apiUrl + } + + runOnUiThread { + // add custom instances to the list preference + instancePref.entries = instanceNames.toTypedArray() + instancePref.entryValues = instanceValues.toTypedArray() + instancePref.summaryProvider = + Preference.SummaryProvider { preference -> + preference.entry + } + } + } } private fun logout() { @@ -239,49 +254,6 @@ class InstanceSettings : PreferenceFragmentCompat() { Toast.makeText(context, getString(R.string.loggedout), Toast.LENGTH_SHORT).show() } - private fun fetchInstance() { - lifecycleScope.launchWhenCreated { - val response = try { - RetrofitInstance.api.getInstances("https://instances.tokhmi.xyz/") - } catch (e: IOException) { - println(e) - Log.e("settings", "IOException, you might not have internet connection") - return@launchWhenCreated - } catch (e: HttpException) { - Log.e("settings", "HttpException, unexpected response $e") - return@launchWhenCreated - } catch (e: Exception) { - Log.e("settings", e.toString()) - return@launchWhenCreated - } - val listEntries: MutableList = ArrayList() - val listEntryValues: MutableList = ArrayList() - for (item in response) { - listEntries.add(item.name!!) - listEntryValues.add(item.api_url!!) - } - - // add custom instances to the list - - val entries = listEntries.toTypedArray() - val entryValues = listEntryValues.toTypedArray() - runOnUiThread { - val instance = findPreference("selectInstance") - instance?.entries = entries - instance?.entryValues = entryValues - instance?.summaryProvider = - Preference.SummaryProvider { preference -> - val text = preference.entry - if (TextUtils.isEmpty(text)) { - "kavin.rocks (Official)" - } else { - text - } - } - } - } - } - private fun Fragment?.runOnUiThread(action: () -> Unit) { this ?: return if (!isAdded) return // Fragment not attached to an Activity diff --git a/app/src/main/java/com/github/libretube/update/UpdateChecker.kt b/app/src/main/java/com/github/libretube/update/UpdateChecker.kt index 00b01b69d..c47641120 100644 --- a/app/src/main/java/com/github/libretube/update/UpdateChecker.kt +++ b/app/src/main/java/com/github/libretube/update/UpdateChecker.kt @@ -1,6 +1,5 @@ package com.github.libretube.update -import android.util.Log import com.fasterxml.jackson.databind.ObjectMapper import com.github.libretube.GITHUB_API_URL import java.net.URL