added custom instances setting

This commit is contained in:
Bnyro 2022-06-11 15:55:25 +02:00
parent 21b8e8bbc0
commit a8c48b34bb
5 changed files with 131 additions and 13 deletions

View File

@ -2,17 +2,22 @@ package com.github.libretube.dialogs
import android.app.Dialog import android.app.Dialog
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.util.TypedValue import android.util.TypedValue
import android.view.View import android.view.View
import android.widget.Button import android.widget.Button
import android.widget.TextView import android.widget.TextView
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.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
class CustomInstanceDialog : DialogFragment() { class CustomInstanceDialog : DialogFragment() {
val TAG = "CustomInstanceDialog"
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let { return activity?.let {
val builder = MaterialAlertDialogBuilder(it) val builder = MaterialAlertDialogBuilder(it)
@ -22,9 +27,50 @@ class CustomInstanceDialog : DialogFragment() {
val instanceNameEditText = view.findViewById<TextInputEditText>(R.id.instanceName) val instanceNameEditText = view.findViewById<TextInputEditText>(R.id.instanceName)
val instanceApiUrlEditText = view.findViewById<TextInputEditText>(R.id.instanceApiUrl) val instanceApiUrlEditText = view.findViewById<TextInputEditText>(R.id.instanceApiUrl)
val addInstanceButton = view.findViewById<Button>(R.id.addInstance) val addInstanceButton = view.findViewById<Button>(R.id.addInstance)
val cancelButton = view.findViewById<Button>(R.id.cancel)
cancelButton.setOnClickListener {
dismiss()
}
addInstanceButton.setOnClickListener { addInstanceButton.setOnClickListener {
val instanceName = instanceNameEditText.text val instanceName = instanceNameEditText.text.toString()
val instanceApiUrl = instanceApiUrlEditText.text val instanceApiUrl = instanceApiUrlEditText.text.toString()
if (instanceName != "" && instanceApiUrl != "") {
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 urls of the other custom instances
var customInstancesUrls = try {
sharedPreferences
.getStringSet("custom_instances_url", HashSet())!!.toList()
} catch (e: Exception) {
emptyList()
}
// append new instance to the list
customInstancesNames += instanceName
customInstancesUrls += instanceApiUrl
Log.e(TAG, customInstancesNames.toString())
// save them to the shared preferences
sharedPreferences.edit()
.putStringSet("custom_instances_name", HashSet(customInstancesNames))
.putStringSet("custom_instances_url", HashSet(customInstancesUrls))
.apply()
activity?.recreate()
dismiss()
} else {
Toast.makeText(
context, context?.getString(R.string.empty_instance), Toast.LENGTH_SHORT
).show()
}
} }
val typedValue = TypedValue() val typedValue = TypedValue()
@ -40,4 +86,4 @@ class CustomInstanceDialog : DialogFragment() {
builder.create() builder.create()
} ?: throw IllegalStateException("Activity cannot be null") } ?: throw IllegalStateException("Activity cannot be null")
} }
} }

View File

@ -13,25 +13,27 @@ import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityCompat.recreate
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.preference.EditTextPreference
import androidx.preference.ListPreference import androidx.preference.ListPreference
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.dialogs.CustomInstanceDialog import com.github.libretube.dialogs.CustomInstanceDialog
import com.github.libretube.dialogs.LoginDialog import com.github.libretube.dialogs.LoginDialog
import com.github.libretube.requireMainActivityRestart import com.github.libretube.requireMainActivityRestart
import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.RetrofitInstance
import org.chromium.base.ThreadUtils.runOnUiThread
import org.json.JSONObject
import org.json.JSONTokener
import retrofit2.HttpException
import java.io.IOException import java.io.IOException
import java.io.InputStream import java.io.InputStream
import java.util.zip.ZipEntry import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream import java.util.zip.ZipInputStream
import org.json.JSONObject
import org.json.JSONTokener
import retrofit2.HttpException
class InstanceSettings : PreferenceFragmentCompat() { class InstanceSettings : PreferenceFragmentCompat() {
val TAG = "InstanceSettings" val TAG = "InstanceSettings"
@ -113,8 +115,10 @@ class InstanceSettings : PreferenceFragmentCompat() {
topBarTextView?.text = getString(R.string.instance) topBarTextView?.text = getString(R.string.instance)
val instance = findPreference<ListPreference>("selectInstance") val instance = findPreference<ListPreference>("selectInstance")
fetchInstance() // fetchInstance()
initCustomInstances()
instance?.setOnPreferenceChangeListener { _, newValue -> instance?.setOnPreferenceChangeListener { _, newValue ->
requireMainActivityRestart = true
RetrofitInstance.url = newValue.toString() RetrofitInstance.url = newValue.toString()
RetrofitInstance.lazyMgr.reset() RetrofitInstance.lazyMgr.reset()
logout() logout()
@ -128,6 +132,17 @@ class InstanceSettings : PreferenceFragmentCompat() {
true true
} }
val clearCustomInstances = findPreference<Preference>("clearCustomInstances")
clearCustomInstances?.setOnPreferenceClickListener {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
sharedPreferences.edit()
.remove("custom_instances_name")
.remove("custom_instances_url")
.commit()
activity?.recreate()
true
}
val login = findPreference<Preference>("login_register") val login = findPreference<Preference>("login_register")
login?.setOnPreferenceClickListener { login?.setOnPreferenceClickListener {
requireMainActivityRestart = true requireMainActivityRestart = true
@ -190,6 +205,43 @@ class InstanceSettings : PreferenceFragmentCompat() {
} }
} }
private fun initCustomInstances() {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
// get the names of the custom instances
val customInstancesNames = try {
sharedPreferences
.getStringSet("custom_instances_name", HashSet())!!.toList()
} catch (e: Exception) {
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
val instance = findPreference<ListPreference>("selectInstance")
instance?.entries = instanceNames
instance?.entryValues = instanceValues
instance?.summaryProvider =
Preference.SummaryProvider<ListPreference> { preference ->
val text = preference.entry
if (TextUtils.isEmpty(text)) {
"kavin.rocks (Official)"
} else {
text
}
}
}
private fun logout() { private fun logout() {
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE) val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
val token = sharedPref?.getString("token", "") val token = sharedPref?.getString("token", "")
@ -223,10 +275,13 @@ class InstanceSettings : PreferenceFragmentCompat() {
listEntries.add(item.name!!) listEntries.add(item.name!!)
listEntryValues.add(item.api_url!!) listEntryValues.add(item.api_url!!)
} }
// add custom instances to the list
val entries = listEntries.toTypedArray<CharSequence>() val entries = listEntries.toTypedArray<CharSequence>()
val entryValues = listEntryValues.toTypedArray<CharSequence>() val entryValues = listEntryValues.toTypedArray<CharSequence>()
runOnUiThread { runOnUiThread {
val instance = findPreference<ListPreference>("officialInstance") val instance = findPreference<ListPreference>("selectInstance")
instance?.entries = entries instance?.entries = entries
instance?.entryValues = entryValues instance?.entryValues = entryValues
instance?.summaryProvider = instance?.summaryProvider =

View File

@ -2,17 +2,27 @@
<resources> <resources>
<string-array name="instances"> <string-array name="instances">
<item>kavin.rocks (Official)</item> <item>kavin.rocks (Official)</item>
<item>silkky.cloud</item>
<item>tokhmi.xyz</item> <item>tokhmi.xyz</item>
<item>moomoo.me</item> <item>moomoo.me</item>
<item>mint.lgbt</item> <item>mint.lgbt</item>
<item>il.ax</item>
<item>syncpundit.com</item>
<item>mha.fi</item>
<item>shimul.me</item>
<item>jae.fi</item>
<item>privacy.com.de</item>
</string-array> </string-array>
<string-array name="instancesValue"> <string-array name="instancesValue">
<item>https://pipedapi.kavin.rocks/</item> <item>https://pipedapi.kavin.rocks/</item>
<item>https://api.piped.silkky.cloud</item>
<item>https://pipedapi.tokhmi.xyz/</item> <item>https://pipedapi.tokhmi.xyz/</item>
<item>https://pipedapi.moomoo.me</item> <item>https://pipedapi.moomoo.me/</item>
<item>https://pa.mint.lgbt</item> <item>https://pa.mint.lgbt/</item>
<item>https://pa.il.ax/</item>
<item>https://pipedapi.syncpundit.com/</item>
<item>https://api-piped.mha.fi/</item>
<item>https://api-piped.shimul.me/</item>
<item>https://api.yt.jae.fi/</item>
<item>https://piped-api.privacy.com.de/</item>
</string-array> </string-array>
<string-array name="shareHostsList"> <string-array name="shareHostsList">
<item>youtube.com"</item> <item>youtube.com"</item>

View File

@ -166,4 +166,6 @@
<string name="instance_name">Instance name</string> <string name="instance_name">Instance name</string>
<string name="instance_api_url">Instance API url</string> <string name="instance_api_url">Instance API url</string>
<string name="addInstance">Add Instance</string> <string name="addInstance">Add Instance</string>
<string name="empty_instance">You have to fill in the name and the API url.</string>
<string name="clear_customInstances">Clear custom instances</string>
</resources> </resources>

View File

@ -18,6 +18,11 @@
app:summary="@string/customInstance_summary" app:summary="@string/customInstance_summary"
android:icon="@drawable/ic_add_instance" /> android:icon="@drawable/ic_add_instance" />
<Preference
app:key="clearCustomInstances"
app:title="@string/clear_customInstances"
android:icon="@drawable/ic_trash" />
<Preference <Preference
android:icon="@drawable/ic_login" android:icon="@drawable/ic_login"
android:summary="@string/notgmail" android:summary="@string/notgmail"