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

80 lines
3.1 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
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
import com.github.libretube.R
2022-07-01 13:49:00 +05:30
import com.github.libretube.databinding.DialogCustomInstanceBinding
2022-06-26 15:41:10 +05:30
import com.github.libretube.obj.CustomInstance
import com.github.libretube.util.PreferenceHelper
2022-06-11 16:34:33 +05:30
import com.google.android.material.dialog.MaterialAlertDialogBuilder
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-07-01 13:49:00 +05:30
private lateinit var binding: DialogCustomInstanceBinding
2022-06-11 19:25:25 +05:30
2022-06-11 16:34:33 +05:30
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val builder = MaterialAlertDialogBuilder(it)
2022-07-01 13:49:00 +05:30
binding = DialogCustomInstanceBinding.inflate(layoutInflater)
2022-06-11 16:34:33 +05:30
2022-07-01 13:49:00 +05:30
binding.cancel.setOnClickListener {
2022-06-11 19:25:25 +05:30
dismiss()
}
2022-07-01 13:49:00 +05:30
binding.addInstance.setOnClickListener {
2022-06-26 15:41:10 +05:30
val customInstance = CustomInstance()
2022-07-01 13:49:00 +05:30
customInstance.name = binding.instanceName.text.toString()
customInstance.apiUrl = binding.instanceApiUrl.text.toString()
customInstance.frontendUrl = binding.instanceFrontendUrl.text.toString()
2022-06-26 15:41:10 +05:30
if (
customInstance.name != "" &&
customInstance.apiUrl != "" &&
customInstance.frontendUrl != ""
) {
2022-06-11 20:37:37 +05:30
try {
// check whether the URL is valid, otherwise catch
2022-06-26 15:41:10 +05:30
URL(customInstance.apiUrl).toURI()
URL(customInstance.frontendUrl).toURI()
2022-06-18 14:44:46 +05:30
2022-06-26 15:41:10 +05:30
PreferenceHelper.saveCustomInstance(requireContext(), customInstance)
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(
2022-06-24 20:56:36 +05:30
context,
getString(R.string.invalid_url),
Toast.LENGTH_SHORT
2022-06-11 20:38:05 +05:30
).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(
2022-06-24 20:56:36 +05:30
context,
context?.getString(R.string.empty_instance),
Toast.LENGTH_SHORT
2022-06-11 19:25:25 +05:30
).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
)
2022-07-01 13:49:00 +05:30
binding.title.text = appName
2022-06-11 16:34:33 +05:30
2022-07-01 13:49:00 +05:30
builder.setView(binding.root)
2022-06-11 16:34:33 +05:30
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
2022-06-11 19:25:25 +05:30
}