rewrite custom instances

This commit is contained in:
Bnyro 2022-06-26 12:11:10 +02:00
parent 4e8039b0ad
commit 25fab2df40
7 changed files with 62 additions and 95 deletions

View File

@ -85,4 +85,5 @@ dependencies {
coreLibraryDesugaring libs.desugaring coreLibraryDesugaring libs.desugaring
implementation libs.cronet.embedded implementation libs.cronet.embedded
implementation libs.gson
} }

View File

@ -9,8 +9,9 @@ import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.core.text.HtmlCompat import androidx.core.text.HtmlCompat
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.preference.PreferenceManager
import com.github.libretube.R 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.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText import com.google.android.material.textfield.TextInputEditText
import java.net.URL import java.net.URL
@ -36,17 +37,22 @@ class CustomInstanceDialog : DialogFragment() {
} }
addInstanceButton.setOnClickListener { addInstanceButton.setOnClickListener {
val instanceName = instanceNameEditText.text.toString() val customInstance = CustomInstance()
val instanceApiUrl = instanceApiUrlEditText.text.toString() customInstance.name = instanceNameEditText.text.toString()
val instanceFrontendUrl = instanceFrontendUrlEditText.text.toString() customInstance.apiUrl = instanceApiUrlEditText.text.toString()
customInstance.frontendUrl = instanceFrontendUrlEditText.text.toString()
if (instanceName != "" && instanceApiUrl != "" && instanceFrontendUrl != "") { if (
customInstance.name != "" &&
customInstance.apiUrl != "" &&
customInstance.frontendUrl != ""
) {
try { try {
// check whether the URL is valid, otherwise catch // check whether the URL is valid, otherwise catch
URL(instanceApiUrl).toURI() URL(customInstance.apiUrl).toURI()
URL(instanceFrontendUrl).toURI() URL(customInstance.frontendUrl).toURI()
saveCustomInstance(instanceName, instanceApiUrl, instanceFrontendUrl) PreferenceHelper.saveCustomInstance(requireContext(), customInstance)
activity?.recreate() activity?.recreate()
dismiss() dismiss()
} catch (e: Exception) { } catch (e: Exception) {
@ -80,49 +86,4 @@ class CustomInstanceDialog : DialogFragment() {
builder.create() builder.create()
} ?: throw IllegalStateException("Activity cannot be null") } ?: 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()
}
} }

View File

@ -55,8 +55,6 @@ class ShareDialog(
// get the frontend url if it's a custom instance // get the frontend url if it's a custom instance
private fun getCustomInstanceFrontendUrl(): String { private fun getCustomInstanceFrontendUrl(): String {
val sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(requireContext())
val instancePref = PreferenceHelper.getString( val instancePref = PreferenceHelper.getString(
requireContext(), requireContext(),
"selectInstance", "selectInstance",
@ -64,27 +62,12 @@ class ShareDialog(
) )
// get the api urls of the other custom instances // get the api urls of the other custom instances
var customInstancesUrls = try { val customInstances = PreferenceHelper.getCustomInstances(requireContext())
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()
}
// return the custom instance frontend url if available // return the custom instance frontend url if available
return if (customInstancesUrls.contains(instancePref)) { customInstances.forEach { instance ->
val index = customInstancesUrls.indexOf(instancePref) if (instance.apiUrl == instancePref) return instance.apiUrl
return customInstancesFrontendUrls[index] }
} else { return ""
""
}
} }
} }

View File

@ -0,0 +1,7 @@
package com.github.libretube.obj
class CustomInstance(
var name: String = "",
var apiUrl: String = "",
var frontendUrl: String = ""
)

View File

@ -135,9 +135,7 @@ class InstanceSettings : PreferenceFragmentCompat() {
val clearCustomInstances = findPreference<Preference>("clearCustomInstances") val clearCustomInstances = findPreference<Preference>("clearCustomInstances")
clearCustomInstances?.setOnPreferenceClickListener { clearCustomInstances?.setOnPreferenceClickListener {
PreferenceHelper.removePreference(requireContext(), "custom_instances_name") PreferenceHelper.removePreference(requireContext(), "customInstances")
PreferenceHelper.removePreference(requireContext(), "custom_instances_url")
PreferenceHelper.removePreference(requireContext(), "custom_instances_frontend_url")
activity?.recreate() activity?.recreate()
true true
} }
@ -216,27 +214,15 @@ class InstanceSettings : PreferenceFragmentCompat() {
} }
private fun initCustomInstances() { private fun initCustomInstances() {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) val customInstances = PreferenceHelper.getCustomInstances(requireContext())
// get the names of the custom instances var instanceNames = resources.getStringArray(R.array.instances)
val customInstancesNames = try { var instanceValues = resources.getStringArray(R.array.instancesValue)
sharedPreferences customInstances.forEach{instance ->
.getStringSet("custom_instances_name", HashSet())!!.toList() instanceNames += instance.name
} catch (e: Exception) { instanceValues += instance.apiUrl
emptyList()
} }
// 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 // add custom instances to the list preference
val instance = findPreference<ListPreference>("selectInstance") val instance = findPreference<ListPreference>("selectInstance")
instance?.entries = instanceNames instance?.entries = instanceNames

View File

@ -3,6 +3,10 @@ package com.github.libretube.util
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import androidx.preference.PreferenceManager 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 { object PreferenceHelper {
fun setString(context: Context, key: String?, value: String?) { fun setString(context: Context, key: String?, value: String?) {
@ -81,6 +85,29 @@ object PreferenceHelper {
editor.putString("username", newValue) 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 { private fun getDefaultSharedPreferences(context: Context): SharedPreferences {
return PreferenceManager.getDefaultSharedPreferences(context) return PreferenceManager.getDefaultSharedPreferences(context)
} }

View File

@ -17,6 +17,7 @@ mobileffmpeg = "4.5.1.LTS"
desugaring = "1.1.5" desugaring = "1.1.5"
cronetEmbedded = "101.4951.41" cronetEmbedded = "101.4951.41"
leakcanary = "2.8.1" leakcanary = "2.8.1"
gson = "2.9.0"
[libraries] [libraries]
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" } 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" } 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" } cronet-embedded = { group = "org.chromium.net", name = "cronet-embedded", version.ref = "cronetEmbedded" }
square-leakcanary = { group = "com.squareup.leakcanary", name = "leakcanary-android", version.ref = "leakcanary" } square-leakcanary = { group = "com.squareup.leakcanary", name = "leakcanary-android", version.ref = "leakcanary" }
gson = { group = "com.google.code.gson", name="gson", version.ref = "gson"}