2022-06-25 21:25:03 +05:30
|
|
|
package com.github.libretube.dialogs
|
|
|
|
|
|
|
|
import android.app.Dialog
|
|
|
|
import android.os.Bundle
|
2022-06-26 00:56:37 +05:30
|
|
|
import android.util.Log
|
2022-06-25 21:25:03 +05:30
|
|
|
import android.util.TypedValue
|
|
|
|
import android.widget.Toast
|
|
|
|
import androidx.core.text.HtmlCompat
|
|
|
|
import androidx.fragment.app.DialogFragment
|
|
|
|
import androidx.lifecycle.lifecycleScope
|
|
|
|
import com.github.libretube.R
|
2022-07-01 13:49:00 +05:30
|
|
|
import com.github.libretube.databinding.DialogDeleteAccountBinding
|
2022-06-25 21:25:03 +05:30
|
|
|
import com.github.libretube.obj.DeleteUserRequest
|
2022-07-01 20:24:20 +05:30
|
|
|
import com.github.libretube.activities.requireMainActivityRestart
|
2022-06-26 14:25:05 +05:30
|
|
|
import com.github.libretube.util.PreferenceHelper
|
2022-06-25 21:25:03 +05:30
|
|
|
import com.github.libretube.util.RetrofitInstance
|
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
|
|
|
|
|
|
|
class DeleteAccountDialog : DialogFragment() {
|
|
|
|
private val TAG = "DeleteAccountDialog"
|
2022-07-01 13:49:00 +05:30
|
|
|
private lateinit var binding: DialogDeleteAccountBinding
|
|
|
|
|
2022-06-25 21:25:03 +05:30
|
|
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
|
|
|
return activity?.let {
|
|
|
|
val builder = MaterialAlertDialogBuilder(it)
|
2022-07-01 13:49:00 +05:30
|
|
|
binding = DialogDeleteAccountBinding.inflate(layoutInflater)
|
2022-06-25 21:41:11 +05:30
|
|
|
|
2022-07-01 13:49:00 +05:30
|
|
|
binding.cancelButton.setOnClickListener {
|
2022-06-25 21:41:11 +05:30
|
|
|
dialog?.dismiss()
|
|
|
|
}
|
2022-06-25 21:25:03 +05:30
|
|
|
|
2022-07-01 13:49:00 +05:30
|
|
|
binding.deleteAccountConfirm.setOnClickListener {
|
|
|
|
if (binding.deletePassword.text.toString() != "") {
|
|
|
|
deleteAccount(binding.deletePassword.text.toString())
|
2022-06-25 21:25:03 +05:30
|
|
|
} else {
|
|
|
|
Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-25 21:25:03 +05:30
|
|
|
|
2022-07-01 13:49:00 +05:30
|
|
|
builder.setView(binding.root)
|
2022-06-25 21:25:03 +05:30
|
|
|
builder.create()
|
|
|
|
} ?: throw IllegalStateException("Activity cannot be null")
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun deleteAccount(password: String) {
|
|
|
|
fun run() {
|
|
|
|
lifecycleScope.launchWhenCreated {
|
2022-06-26 14:25:05 +05:30
|
|
|
val token = PreferenceHelper.getToken(requireContext())
|
2022-06-25 21:25:03 +05:30
|
|
|
|
2022-06-26 13:31:43 +05:30
|
|
|
try {
|
2022-06-25 21:25:03 +05:30
|
|
|
RetrofitInstance.api.deleteAccount(token, DeleteUserRequest(password))
|
|
|
|
} catch (e: Exception) {
|
2022-06-26 00:56:37 +05:30
|
|
|
Log.e(TAG, e.toString())
|
|
|
|
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
|
2022-06-25 22:00:17 +05:30
|
|
|
return@launchWhenCreated
|
2022-06-25 21:25:03 +05:30
|
|
|
}
|
2022-06-26 13:31:43 +05:30
|
|
|
requireMainActivityRestart = true
|
2022-06-25 21:25:03 +05:30
|
|
|
Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show()
|
|
|
|
logout()
|
|
|
|
dialog?.dismiss()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
run()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun logout() {
|
2022-06-26 14:25:05 +05:30
|
|
|
PreferenceHelper.setToken(requireContext(), "")
|
2022-06-25 21:25:03 +05:30
|
|
|
}
|
|
|
|
}
|