2022-06-11 16:34:33 +05:30
|
|
|
package com.github.libretube.dialogs
|
|
|
|
|
|
|
|
import android.app.Dialog
|
|
|
|
import android.os.Bundle
|
2022-06-11 19:25:25 +05:30
|
|
|
import android.util.Log
|
2022-06-11 16:34:33 +05:30
|
|
|
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
|
|
|
|
|
|
|
|
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)
|
|
|
|
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()
|
|
|
|
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()
|
|
|
|
}
|
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 19:25:25 +05:30
|
|
|
}
|