LibreTube/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt

214 lines
8.2 KiB
Kotlin
Raw Normal View History

2022-06-11 15:23:09 +05:30
package com.github.libretube.preferences
import android.net.Uri
import android.os.Bundle
import android.widget.Toast
2022-07-21 20:29:06 +05:30
import androidx.activity.result.ActivityResultLauncher
2022-06-11 15:23:09 +05:30
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.preference.ListPreference
import androidx.preference.Preference
2022-07-15 02:02:42 +05:30
import androidx.preference.SwitchPreferenceCompat
2022-06-11 15:23:09 +05:30
import com.github.libretube.R
2022-07-01 20:24:20 +05:30
import com.github.libretube.activities.SettingsActivity
2022-08-14 13:25:28 +05:30
import com.github.libretube.api.RetrofitInstance
2022-08-14 13:29:05 +05:30
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.db.obj.CustomInstance
2022-06-11 16:34:33 +05:30
import com.github.libretube.dialogs.CustomInstanceDialog
2022-06-25 21:41:11 +05:30
import com.github.libretube.dialogs.DeleteAccountDialog
2022-06-11 15:23:09 +05:30
import com.github.libretube.dialogs.LoginDialog
2022-07-01 13:49:00 +05:30
import com.github.libretube.dialogs.LogoutDialog
2022-08-14 02:21:32 +05:30
import com.github.libretube.extensions.await
2022-08-06 14:39:28 +05:30
import com.github.libretube.util.ImportHelper
2022-07-21 20:29:06 +05:30
import com.github.libretube.util.PermissionHelper
2022-08-07 21:26:57 +05:30
import com.github.libretube.views.MaterialPreferenceFragment
2022-06-11 15:23:09 +05:30
2022-08-07 21:26:57 +05:30
class InstanceSettings : MaterialPreferenceFragment() {
2022-08-06 15:52:26 +05:30
/**
* result listeners for importing and exporting subscriptions
*/
private lateinit var getContent: ActivityResultLauncher<String>
2022-08-06 15:52:26 +05:30
private lateinit var createFile: ActivityResultLauncher<String>
2022-06-11 15:23:09 +05:30
override fun onCreate(savedInstanceState: Bundle?) {
getContent =
2022-08-06 15:52:26 +05:30
registerForActivityResult(
ActivityResultContracts.GetContent()
) { uri: Uri? ->
ImportHelper(requireActivity()).importSubscriptions(uri)
}
2022-08-06 15:52:26 +05:30
createFile = registerForActivityResult(
ActivityResultContracts.CreateDocument()
) { uri: Uri? ->
ImportHelper(requireActivity()).exportSubscriptions(uri)
}
super.onCreate(savedInstanceState)
}
2022-06-11 15:23:09 +05:30
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.instance_settings, rootKey)
2022-07-01 13:59:00 +05:30
val settingsActivity = activity as SettingsActivity
2022-07-03 15:43:38 +05:30
settingsActivity.changeTopBarText(getString(R.string.instance))
2022-06-11 15:23:09 +05:30
2022-07-17 21:48:39 +05:30
val instance = findPreference<ListPreference>(PreferenceKeys.FETCH_INSTANCE)
2022-06-11 19:25:25 +05:30
// fetchInstance()
2022-07-03 13:50:53 +05:30
initCustomInstances(instance!!)
instance.setOnPreferenceChangeListener { _, newValue ->
2022-06-11 15:23:09 +05:30
RetrofitInstance.url = newValue.toString()
2022-07-17 21:48:39 +05:30
if (!PreferenceHelper.getBoolean(PreferenceKeys.AUTH_INSTANCE_TOGGLE, false)) {
2022-07-03 13:50:53 +05:30
RetrofitInstance.authUrl = newValue.toString()
logout()
}
RetrofitInstance.lazyMgr.reset()
activity?.recreate()
2022-07-03 13:50:53 +05:30
true
}
2022-07-17 21:48:39 +05:30
val authInstance = findPreference<ListPreference>(PreferenceKeys.AUTH_INSTANCE)
2022-07-03 13:50:53 +05:30
initCustomInstances(authInstance!!)
// hide auth instance if option deselected
2022-07-17 21:48:39 +05:30
if (!PreferenceHelper.getBoolean(PreferenceKeys.AUTH_INSTANCE_TOGGLE, false)) {
2022-07-03 13:50:53 +05:30
authInstance.isVisible = false
}
authInstance.setOnPreferenceChangeListener { _, newValue ->
2022-07-03 15:09:25 +05:30
// save new auth url
RetrofitInstance.authUrl = newValue.toString()
2022-06-11 15:23:09 +05:30
RetrofitInstance.lazyMgr.reset()
2022-06-11 16:34:33 +05:30
logout()
activity?.recreate()
2022-06-11 16:34:33 +05:30
true
}
2022-07-18 23:06:21 +05:30
val authInstanceToggle =
findPreference<SwitchPreferenceCompat>(PreferenceKeys.AUTH_INSTANCE_TOGGLE)
2022-07-03 13:50:53 +05:30
authInstanceToggle?.setOnPreferenceChangeListener { _, newValue ->
authInstance.isVisible = newValue == true
logout()
2022-07-03 15:09:25 +05:30
// either use new auth url or the normal api url if auth instance disabled
2022-07-03 13:50:53 +05:30
RetrofitInstance.authUrl = if (newValue == false) RetrofitInstance.url
else authInstance.value
RetrofitInstance.lazyMgr.reset()
activity?.recreate()
2022-07-03 13:50:53 +05:30
true
}
2022-07-17 21:48:39 +05:30
val customInstance = findPreference<Preference>(PreferenceKeys.CUSTOM_INSTANCE)
2022-06-11 16:34:33 +05:30
customInstance?.setOnPreferenceClickListener {
val newFragment = CustomInstanceDialog()
2022-08-08 15:08:50 +05:30
newFragment.show(childFragmentManager, CustomInstanceDialog::class.java.name)
2022-06-11 15:23:09 +05:30
true
}
2022-07-17 21:48:39 +05:30
val clearCustomInstances = findPreference<Preference>(PreferenceKeys.CLEAR_CUSTOM_INSTANCES)
2022-06-11 19:25:25 +05:30
clearCustomInstances?.setOnPreferenceClickListener {
2022-08-14 02:21:32 +05:30
Thread {
2022-08-14 13:29:05 +05:30
DatabaseHolder.db.customInstanceDao().deleteAll()
2022-08-14 02:21:32 +05:30
}.await()
activity?.recreate()
2022-06-11 19:25:25 +05:30
true
}
2022-07-17 21:48:39 +05:30
val login = findPreference<Preference>(PreferenceKeys.LOGIN_REGISTER)
val token = PreferenceHelper.getToken()
2022-07-08 17:56:00 +05:30
if (token != "") login?.setTitle(R.string.logout)
2022-06-11 15:23:09 +05:30
login?.setOnPreferenceClickListener {
2022-07-01 13:49:00 +05:30
if (token == "") {
val newFragment = LoginDialog()
2022-08-08 15:08:50 +05:30
newFragment.show(childFragmentManager, LoginDialog::class.java.name)
2022-07-01 13:49:00 +05:30
} else {
val newFragment = LogoutDialog()
2022-08-08 15:08:50 +05:30
newFragment.show(childFragmentManager, LogoutDialog::class.java.name)
2022-07-01 13:49:00 +05:30
}
2022-06-11 15:23:09 +05:30
true
}
2022-07-17 21:48:39 +05:30
val deleteAccount = findPreference<Preference>(PreferenceKeys.DELETE_ACCOUNT)
2022-06-25 21:41:11 +05:30
deleteAccount?.setOnPreferenceClickListener {
2022-08-13 18:42:09 +05:30
if (PreferenceHelper.getToken() != "") {
2022-06-25 21:41:11 +05:30
val newFragment = DeleteAccountDialog()
2022-08-08 15:08:50 +05:30
newFragment.show(childFragmentManager, DeleteAccountDialog::class.java.name)
2022-06-25 21:41:11 +05:30
} else {
Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT).show()
}
true
}
2022-08-06 15:52:26 +05:30
val importSubscriptions = findPreference<Preference>(PreferenceKeys.IMPORT_SUBS)
importSubscriptions?.setOnPreferenceClickListener {
// check StorageAccess
val accessGranted =
2022-08-08 15:31:25 +05:30
PermissionHelper.isStoragePermissionGranted(requireActivity())
// import subscriptions
if (accessGranted) getContent.launch("*/*")
// request permissions if not granted
2022-08-08 15:31:25 +05:30
else PermissionHelper.requestReadWrite(requireActivity())
2022-06-11 15:23:09 +05:30
true
}
2022-08-06 15:52:26 +05:30
val exportSubscriptions = findPreference<Preference>(PreferenceKeys.EXPORT_SUBS)
exportSubscriptions?.setOnPreferenceClickListener {
2022-08-06 16:18:08 +05:30
createFile.launch("subscriptions.json")
2022-08-06 15:52:26 +05:30
true
}
2022-06-11 15:23:09 +05:30
}
2022-07-03 13:50:53 +05:30
private fun initCustomInstances(instancePref: ListPreference) {
2022-07-24 15:45:51 +05:30
lifecycleScope.launchWhenCreated {
2022-08-14 01:47:28 +05:30
var customInstances = listOf<CustomInstance>()
Thread {
2022-08-14 13:29:05 +05:30
customInstances = DatabaseHolder.db.customInstanceDao().getAll()
2022-08-14 02:21:32 +05:30
}.await()
2022-06-11 19:25:25 +05:30
2022-08-06 14:39:28 +05:30
val instanceNames = arrayListOf<String>()
val instanceValues = arrayListOf<String>()
2022-06-11 19:25:25 +05:30
2022-07-24 15:45:51 +05:30
// fetch official public instances
2022-06-11 19:25:25 +05:30
2022-06-11 15:23:09 +05:30
val response = try {
RetrofitInstance.api.getInstances("https://instances.tokhmi.xyz/")
} catch (e: Exception) {
2022-07-24 15:45:51 +05:30
e.printStackTrace()
emptyList()
2022-06-11 15:23:09 +05:30
}
2022-07-24 15:45:51 +05:30
response.forEach {
if (it.name != null && it.api_url != null) {
instanceNames += it.name!!
instanceValues += it.api_url!!
}
2022-06-11 15:23:09 +05:30
}
2022-06-11 19:25:25 +05:30
2022-07-24 15:45:51 +05:30
customInstances.forEach { instance ->
instanceNames += instance.name
instanceValues += instance.apiUrl
}
2022-06-11 19:25:25 +05:30
2022-06-11 15:23:09 +05:30
runOnUiThread {
2022-07-24 15:45:51 +05:30
// add custom instances to the list preference
instancePref.entries = instanceNames.toTypedArray()
instancePref.entryValues = instanceValues.toTypedArray()
instancePref.summaryProvider =
2022-06-11 15:23:09 +05:30
Preference.SummaryProvider<ListPreference> { preference ->
2022-07-24 15:45:51 +05:30
preference.entry
2022-06-11 15:23:09 +05:30
}
}
}
}
2022-07-24 15:45:51 +05:30
private fun logout() {
PreferenceHelper.setToken("")
Toast.makeText(context, getString(R.string.loggedout), Toast.LENGTH_SHORT).show()
}
2022-06-11 15:23:09 +05:30
private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
}