mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-27 15:30:31 +05:30
rewrite custom instances
This commit is contained in:
parent
4e8039b0ad
commit
25fab2df40
@ -85,4 +85,5 @@ dependencies {
|
||||
|
||||
coreLibraryDesugaring libs.desugaring
|
||||
implementation libs.cronet.embedded
|
||||
implementation libs.gson
|
||||
}
|
||||
|
@ -9,8 +9,9 @@ import android.widget.TextView
|
||||
import android.widget.Toast
|
||||
import androidx.core.text.HtmlCompat
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.github.libretube.R
|
||||
import com.github.libretube.obj.CustomInstance
|
||||
import com.github.libretube.util.PreferenceHelper
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import com.google.android.material.textfield.TextInputEditText
|
||||
import java.net.URL
|
||||
@ -36,17 +37,22 @@ class CustomInstanceDialog : DialogFragment() {
|
||||
}
|
||||
|
||||
addInstanceButton.setOnClickListener {
|
||||
val instanceName = instanceNameEditText.text.toString()
|
||||
val instanceApiUrl = instanceApiUrlEditText.text.toString()
|
||||
val instanceFrontendUrl = instanceFrontendUrlEditText.text.toString()
|
||||
val customInstance = CustomInstance()
|
||||
customInstance.name = instanceNameEditText.text.toString()
|
||||
customInstance.apiUrl = instanceApiUrlEditText.text.toString()
|
||||
customInstance.frontendUrl = instanceFrontendUrlEditText.text.toString()
|
||||
|
||||
if (instanceName != "" && instanceApiUrl != "" && instanceFrontendUrl != "") {
|
||||
if (
|
||||
customInstance.name != "" &&
|
||||
customInstance.apiUrl != "" &&
|
||||
customInstance.frontendUrl != ""
|
||||
) {
|
||||
try {
|
||||
// check whether the URL is valid, otherwise catch
|
||||
URL(instanceApiUrl).toURI()
|
||||
URL(instanceFrontendUrl).toURI()
|
||||
URL(customInstance.apiUrl).toURI()
|
||||
URL(customInstance.frontendUrl).toURI()
|
||||
|
||||
saveCustomInstance(instanceName, instanceApiUrl, instanceFrontendUrl)
|
||||
PreferenceHelper.saveCustomInstance(requireContext(), customInstance)
|
||||
activity?.recreate()
|
||||
dismiss()
|
||||
} catch (e: Exception) {
|
||||
@ -80,49 +86,4 @@ class CustomInstanceDialog : DialogFragment() {
|
||||
builder.create()
|
||||
} ?: throw IllegalStateException("Activity cannot be null")
|
||||
}
|
||||
|
||||
private fun saveCustomInstance(
|
||||
instanceName: String,
|
||||
instanceApiUrl: String,
|
||||
instanceFrontendApiUrl: String
|
||||
) {
|
||||
val sharedPreferences = PreferenceManager
|
||||
.getDefaultSharedPreferences(requireContext())
|
||||
|
||||
// get the names of the other custom instances
|
||||
var customInstancesNames = try {
|
||||
sharedPreferences
|
||||
.getStringSet("custom_instances_name", HashSet())!!.toList()
|
||||
} catch (e: Exception) {
|
||||
emptyList()
|
||||
}
|
||||
|
||||
// get the api urls of the other custom instances
|
||||
var customInstancesUrls = try {
|
||||
sharedPreferences
|
||||
.getStringSet("custom_instances_url", HashSet())!!.toList()
|
||||
} catch (e: Exception) {
|
||||
emptyList()
|
||||
}
|
||||
|
||||
// get the frontend urls of the other custom instances
|
||||
var customInstancesFrontendUrls = try {
|
||||
sharedPreferences
|
||||
.getStringSet("custom_instances_url", HashSet())!!.toList()
|
||||
} catch (e: Exception) {
|
||||
emptyList()
|
||||
}
|
||||
|
||||
// append new instance to the list
|
||||
customInstancesNames += instanceName
|
||||
customInstancesUrls += instanceApiUrl
|
||||
customInstancesFrontendUrls += instanceFrontendApiUrl
|
||||
|
||||
// save them to the shared preferences
|
||||
sharedPreferences.edit()
|
||||
.putStringSet("custom_instances_name", HashSet(customInstancesNames))
|
||||
.putStringSet("custom_instances_url", HashSet(customInstancesUrls))
|
||||
.putStringSet("custom_instances_frontend_url", HashSet(customInstancesFrontendUrls))
|
||||
.apply()
|
||||
}
|
||||
}
|
||||
|
@ -55,8 +55,6 @@ class ShareDialog(
|
||||
|
||||
// get the frontend url if it's a custom instance
|
||||
private fun getCustomInstanceFrontendUrl(): String {
|
||||
val sharedPreferences =
|
||||
PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||
val instancePref = PreferenceHelper.getString(
|
||||
requireContext(),
|
||||
"selectInstance",
|
||||
@ -64,27 +62,12 @@ class ShareDialog(
|
||||
)
|
||||
|
||||
// get the api urls of the other custom instances
|
||||
var customInstancesUrls = try {
|
||||
sharedPreferences
|
||||
.getStringSet("custom_instances_url", HashSet())!!.toList()
|
||||
} catch (e: Exception) {
|
||||
emptyList()
|
||||
}
|
||||
|
||||
// get the frontend urls of the other custom instances
|
||||
var customInstancesFrontendUrls = try {
|
||||
sharedPreferences
|
||||
.getStringSet("custom_instances_url", HashSet())!!.toList()
|
||||
} catch (e: Exception) {
|
||||
emptyList()
|
||||
}
|
||||
val customInstances = PreferenceHelper.getCustomInstances(requireContext())
|
||||
|
||||
// return the custom instance frontend url if available
|
||||
return if (customInstancesUrls.contains(instancePref)) {
|
||||
val index = customInstancesUrls.indexOf(instancePref)
|
||||
return customInstancesFrontendUrls[index]
|
||||
} else {
|
||||
""
|
||||
}
|
||||
customInstances.forEach { instance ->
|
||||
if (instance.apiUrl == instancePref) return instance.apiUrl
|
||||
}
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
package com.github.libretube.obj
|
||||
|
||||
class CustomInstance(
|
||||
var name: String = "",
|
||||
var apiUrl: String = "",
|
||||
var frontendUrl: String = ""
|
||||
)
|
@ -135,9 +135,7 @@ class InstanceSettings : PreferenceFragmentCompat() {
|
||||
|
||||
val clearCustomInstances = findPreference<Preference>("clearCustomInstances")
|
||||
clearCustomInstances?.setOnPreferenceClickListener {
|
||||
PreferenceHelper.removePreference(requireContext(), "custom_instances_name")
|
||||
PreferenceHelper.removePreference(requireContext(), "custom_instances_url")
|
||||
PreferenceHelper.removePreference(requireContext(), "custom_instances_frontend_url")
|
||||
PreferenceHelper.removePreference(requireContext(), "customInstances")
|
||||
activity?.recreate()
|
||||
true
|
||||
}
|
||||
@ -216,27 +214,15 @@ class InstanceSettings : PreferenceFragmentCompat() {
|
||||
}
|
||||
|
||||
private fun initCustomInstances() {
|
||||
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||
val customInstances = PreferenceHelper.getCustomInstances(requireContext())
|
||||
|
||||
// get the names of the custom instances
|
||||
val customInstancesNames = try {
|
||||
sharedPreferences
|
||||
.getStringSet("custom_instances_name", HashSet())!!.toList()
|
||||
} catch (e: Exception) {
|
||||
emptyList()
|
||||
var instanceNames = resources.getStringArray(R.array.instances)
|
||||
var instanceValues = resources.getStringArray(R.array.instancesValue)
|
||||
customInstances.forEach{instance ->
|
||||
instanceNames += instance.name
|
||||
instanceValues += instance.apiUrl
|
||||
}
|
||||
|
||||
// get the urls of the custom instances
|
||||
val customInstancesUrls = try {
|
||||
sharedPreferences
|
||||
.getStringSet("custom_instances_url", HashSet())!!.toList()
|
||||
} catch (e: Exception) {
|
||||
emptyList()
|
||||
}
|
||||
|
||||
val instanceNames = resources.getStringArray(R.array.instances) + customInstancesNames
|
||||
val instanceValues = resources.getStringArray(R.array.instancesValue) + customInstancesUrls
|
||||
|
||||
// add custom instances to the list preference
|
||||
val instance = findPreference<ListPreference>("selectInstance")
|
||||
instance?.entries = instanceNames
|
||||
|
@ -3,6 +3,10 @@ package com.github.libretube.util
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.github.libretube.obj.CustomInstance
|
||||
import com.google.common.reflect.TypeToken
|
||||
import com.google.gson.Gson
|
||||
import java.lang.reflect.Type
|
||||
|
||||
object PreferenceHelper {
|
||||
fun setString(context: Context, key: String?, value: String?) {
|
||||
@ -81,6 +85,29 @@ object PreferenceHelper {
|
||||
editor.putString("username", newValue)
|
||||
}
|
||||
|
||||
fun saveCustomInstance(context: Context, customInstance: CustomInstance) {
|
||||
val editor = getDefaultSharedPreferencesEditor(context)
|
||||
val gson = Gson()
|
||||
|
||||
val customInstancesList = getCustomInstances(context)
|
||||
customInstancesList += customInstance
|
||||
|
||||
val json = gson.toJson(customInstancesList)
|
||||
editor.putString("customInstances", json).commit()
|
||||
}
|
||||
|
||||
fun getCustomInstances(context: Context): ArrayList<CustomInstance> {
|
||||
val settings = getDefaultSharedPreferences(context)
|
||||
val gson = Gson()
|
||||
val json: String = settings.getString("customInstances", "")!!
|
||||
val type: Type = object : TypeToken<List<CustomInstance?>?>() {}.type
|
||||
return try {
|
||||
gson.fromJson(json, type)
|
||||
} catch (e: Exception) {
|
||||
arrayListOf()
|
||||
}
|
||||
}
|
||||
|
||||
private fun getDefaultSharedPreferences(context: Context): SharedPreferences {
|
||||
return PreferenceManager.getDefaultSharedPreferences(context)
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ mobileffmpeg = "4.5.1.LTS"
|
||||
desugaring = "1.1.5"
|
||||
cronetEmbedded = "101.4951.41"
|
||||
leakcanary = "2.8.1"
|
||||
gson = "2.9.0"
|
||||
|
||||
[libraries]
|
||||
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
|
||||
@ -41,3 +42,4 @@ desugaring = { group = "com.android.tools", name = "desugar_jdk_libs", version.r
|
||||
exoplayer-extension-cronet = { group = "com.google.android.exoplayer", name = "extension-cronet", version.ref = "exoplayer" }
|
||||
cronet-embedded = { group = "org.chromium.net", name = "cronet-embedded", version.ref = "cronetEmbedded" }
|
||||
square-leakcanary = { group = "com.squareup.leakcanary", name = "leakcanary-android", version.ref = "leakcanary" }
|
||||
gson = { group = "com.google.code.gson", name="gson", version.ref = "gson"}
|
Loading…
x
Reference in New Issue
Block a user