LibreTube/app/src/main/java/com/github/libretube/dialogs/CustomInstanceDialog.kt

124 lines
4.9 KiB
Kotlin
Raw Normal View History

2022-06-11 16:34:33 +05:30
package com.github.libretube.dialogs
import android.app.Dialog
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import android.widget.Button
import android.widget.TextView
2022-06-11 19:25:25 +05:30
import android.widget.Toast
2022-06-11 16:34:33 +05:30
import androidx.core.text.HtmlCompat
import androidx.fragment.app.DialogFragment
2022-06-11 19:25:25 +05:30
import androidx.preference.PreferenceManager
2022-06-11 16:34:33 +05:30
import com.github.libretube.R
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText
2022-06-11 20:37:37 +05:30
import java.net.URL
2022-06-11 16:34:33 +05:30
class CustomInstanceDialog : DialogFragment() {
2022-06-11 19:25:25 +05:30
val TAG = "CustomInstanceDialog"
2022-06-11 16:34:33 +05:30
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val builder = MaterialAlertDialogBuilder(it)
val inflater = requireActivity().layoutInflater
val view: View = inflater.inflate(R.layout.dialog_custom_instance, null)
val instanceNameEditText = view.findViewById<TextInputEditText>(R.id.instanceName)
val instanceApiUrlEditText = view.findViewById<TextInputEditText>(R.id.instanceApiUrl)
2022-06-18 14:44:46 +05:30
val instanceFrontendUrlEditText = view.findViewById<TextInputEditText>(R.id.instanceFrontendUrl)
2022-06-11 16:34:33 +05:30
val addInstanceButton = view.findViewById<Button>(R.id.addInstance)
2022-06-11 19:25:25 +05:30
val cancelButton = view.findViewById<Button>(R.id.cancel)
cancelButton.setOnClickListener {
dismiss()
}
2022-06-11 16:34:33 +05:30
addInstanceButton.setOnClickListener {
2022-06-11 19:25:25 +05:30
val instanceName = instanceNameEditText.text.toString()
val instanceApiUrl = instanceApiUrlEditText.text.toString()
2022-06-18 14:44:46 +05:30
val instanceFrontendUrl = instanceFrontendUrlEditText.text.toString()
2022-06-11 19:25:25 +05:30
2022-06-18 14:44:46 +05:30
if (instanceName != "" && instanceApiUrl != "" && instanceFrontendUrl != "") {
2022-06-11 20:37:37 +05:30
try {
// check whether the URL is valid, otherwise catch
2022-06-18 14:44:46 +05:30
URL(instanceApiUrl).toURI()
URL(instanceFrontendUrl).toURI()
saveCustomInstance(instanceName, instanceApiUrl, instanceFrontendUrl)
2022-06-11 20:37:37 +05:30
activity?.recreate()
dismiss()
2022-06-11 19:25:25 +05:30
} catch (e: Exception) {
2022-06-11 20:37:37 +05:30
// invalid URL
2022-06-11 20:38:05 +05:30
Toast.makeText(
context, getString(R.string.invalid_url), Toast.LENGTH_SHORT
).show()
2022-06-11 19:25:25 +05:30
}
} else {
2022-06-11 20:37:37 +05:30
// at least one empty input
2022-06-11 19:25:25 +05:30
Toast.makeText(
context, context?.getString(R.string.empty_instance), Toast.LENGTH_SHORT
).show()
}
2022-06-11 16:34:33 +05:30
}
val typedValue = TypedValue()
this.requireActivity().theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true)
val hexColor = String.format("#%06X", (0xFFFFFF and typedValue.data))
val appName = HtmlCompat.fromHtml(
"Libre<span style='color:$hexColor';>Tube</span>",
HtmlCompat.FROM_HTML_MODE_COMPACT
)
view.findViewById<TextView>(R.id.title).text = appName
builder.setView(view)
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
2022-06-11 20:37:37 +05:30
2022-06-18 14:44:46 +05:30
private fun saveCustomInstance(
instanceName: String,
instanceApiUrl: String,
instanceFrontendApiUrl: String
) {
2022-06-11 20:37:37 +05:30
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()
}
2022-06-18 14:44:46 +05:30
// get the api urls of the other custom instances
2022-06-11 20:37:37 +05:30
var customInstancesUrls = try {
sharedPreferences
.getStringSet("custom_instances_url", HashSet())!!.toList()
} catch (e: Exception) {
emptyList()
}
2022-06-18 14:44:46 +05:30
// get the frontend urls of the other custom instances
var customInstancesFrontendUrls = try {
sharedPreferences
.getStringSet("custom_instances_url", HashSet())!!.toList()
} catch (e: Exception) {
emptyList()
}
2022-06-11 20:37:37 +05:30
// append new instance to the list
customInstancesNames += instanceName
customInstancesUrls += instanceApiUrl
2022-06-18 14:44:46 +05:30
customInstancesFrontendUrls += instanceFrontendApiUrl
2022-06-11 20:37:37 +05:30
// save them to the shared preferences
sharedPreferences.edit()
.putStringSet("custom_instances_name", HashSet(customInstancesNames))
.putStringSet("custom_instances_url", HashSet(customInstancesUrls))
2022-06-18 14:44:46 +05:30
.putStringSet("custom_instances_frontend_url", HashSet(customInstancesFrontendUrls))
2022-06-11 20:37:37 +05:30
.apply()
}
2022-06-11 19:25:25 +05:30
}