convert all dialogs to viewbinding

This commit is contained in:
Bnyro 2022-07-01 10:19:00 +02:00
parent d296d3add3
commit 9ebd3d5f7e
9 changed files with 140 additions and 128 deletions

View File

@ -4,17 +4,14 @@ import android.app.Dialog
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.util.TypedValue import android.util.TypedValue
import android.view.View
import android.widget.ArrayAdapter import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.Spinner
import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.core.text.HtmlCompat import androidx.core.text.HtmlCompat
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.databinding.DialogAddtoplaylistBinding
import com.github.libretube.obj.PlaylistId import com.github.libretube.obj.PlaylistId
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.RetrofitInstance
@ -24,23 +21,22 @@ import java.io.IOException
class AddtoPlaylistDialog : DialogFragment() { class AddtoPlaylistDialog : DialogFragment() {
private val TAG = "AddToPlaylistDialog" private val TAG = "AddToPlaylistDialog"
private lateinit var binding: DialogAddtoplaylistBinding
private lateinit var videoId: String private lateinit var videoId: String
private lateinit var token: String private lateinit var token: String
private lateinit var spinner: Spinner
private lateinit var button: Button
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let { return activity?.let {
videoId = arguments?.getString("videoId")!! videoId = arguments?.getString("videoId")!!
val builder = MaterialAlertDialogBuilder(it) val builder = MaterialAlertDialogBuilder(it)
// Get the layout inflater // Get the layout inflater
val inflater = requireActivity().layoutInflater binding = DialogAddtoplaylistBinding.inflate(layoutInflater)
token = PreferenceHelper.getToken(requireContext()) token = PreferenceHelper.getToken(requireContext())
var view: View = inflater.inflate(R.layout.dialog_addtoplaylist, null)
spinner = view.findViewById(R.id.playlists_spinner) if (token != "") fetchPlaylists()
button = view.findViewById(R.id.addToPlaylist)
if (token != "") {
fetchPlaylists()
}
val typedValue = TypedValue() val typedValue = TypedValue()
this.requireActivity().theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true) this.requireActivity().theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true)
val hexColor = String.format("#%06X", (0xFFFFFF and typedValue.data)) val hexColor = String.format("#%06X", (0xFFFFFF and typedValue.data))
@ -48,9 +44,9 @@ class AddtoPlaylistDialog : DialogFragment() {
"Libre<span style='color:$hexColor';>Tube</span>", "Libre<span style='color:$hexColor';>Tube</span>",
HtmlCompat.FROM_HTML_MODE_COMPACT HtmlCompat.FROM_HTML_MODE_COMPACT
) )
view.findViewById<TextView>(R.id.title).text = appName binding.title.text = appName
builder.setView(view) builder.setView(binding.root)
builder.create() builder.create()
} ?: throw IllegalStateException("Activity cannot be null") } ?: throw IllegalStateException("Activity cannot be null")
} }
@ -80,10 +76,12 @@ class AddtoPlaylistDialog : DialogFragment() {
arrayAdapter.setDropDownViewResource( arrayAdapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item android.R.layout.simple_spinner_dropdown_item
) )
spinner.adapter = arrayAdapter binding.playlistsSpinner.adapter = arrayAdapter
runOnUiThread { runOnUiThread {
button.setOnClickListener { binding.addToPlaylist.setOnClickListener {
addToPlaylist(response[spinner.selectedItemPosition].id!!) addToPlaylist(
response[binding.playlistsSpinner.selectedItemPosition].id!!
)
} }
} }
} else { } else {

View File

@ -4,32 +4,29 @@ import android.app.Dialog
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.util.TypedValue import android.util.TypedValue
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.core.text.HtmlCompat import androidx.core.text.HtmlCompat
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.databinding.DialogCreatePlaylistBinding
import com.github.libretube.fragments.Library import com.github.libretube.fragments.Library
import com.github.libretube.obj.Playlists import com.github.libretube.obj.Playlists
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.RetrofitInstance
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException import java.io.IOException
class CreatePlaylistDialog : DialogFragment() { class CreatePlaylistDialog : DialogFragment() {
val TAG = "CreatePlaylistDialog" val TAG = "CreatePlaylistDialog"
private var token: String = "" private var token: String = ""
private lateinit var binding: DialogCreatePlaylistBinding
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let { return activity?.let {
val builder = MaterialAlertDialogBuilder(it) val builder = MaterialAlertDialogBuilder(it)
val inflater = requireActivity().layoutInflater binding = DialogCreatePlaylistBinding.inflate(layoutInflater)
val view: View = inflater.inflate(R.layout.dialog_create_playlist, null)
val typedValue = TypedValue() val typedValue = TypedValue()
this.requireActivity().theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true) this.requireActivity().theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true)
@ -38,21 +35,18 @@ class CreatePlaylistDialog : DialogFragment() {
"Libre<span style='color:$hexColor';>Tube</span>", "Libre<span style='color:$hexColor';>Tube</span>",
HtmlCompat.FROM_HTML_MODE_COMPACT HtmlCompat.FROM_HTML_MODE_COMPACT
) )
view.findViewById<TextView>(R.id.title).text = appName binding.title.text = appName
val cancelBtn = view.findViewById<Button>(R.id.cancel_button) binding.cancelButton.setOnClickListener {
cancelBtn.setOnClickListener {
dismiss() dismiss()
} }
token = PreferenceHelper.getToken(requireContext()) token = PreferenceHelper.getToken(requireContext())
val playlistName = view.findViewById<TextInputEditText>(R.id.playlist_name) binding.createNewPlaylist.setOnClickListener {
val createPlaylistBtn = view.findViewById<Button>(R.id.create_new_playlist)
createPlaylistBtn.setOnClickListener {
// avoid creating the same playlist multiple times by spamming the button // avoid creating the same playlist multiple times by spamming the button
createPlaylistBtn.setOnClickListener(null) binding.createNewPlaylist.setOnClickListener(null)
val listName = playlistName.text.toString() val listName = binding.playlistName.text.toString()
if (listName != "") { if (listName != "") {
createPlaylist(listName) createPlaylist(listName)
} else { } else {
@ -60,7 +54,7 @@ class CreatePlaylistDialog : DialogFragment() {
} }
} }
builder.setView(view) builder.setView(binding.root)
builder.create() builder.create()
} ?: throw IllegalStateException("Activity cannot be null") } ?: throw IllegalStateException("Activity cannot be null")
} }

View File

@ -3,44 +3,34 @@ package com.github.libretube.dialogs
import android.app.Dialog import android.app.Dialog
import android.os.Bundle import android.os.Bundle
import android.util.TypedValue import android.util.TypedValue
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.core.text.HtmlCompat import androidx.core.text.HtmlCompat
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.databinding.DialogCustomInstanceBinding
import com.github.libretube.obj.CustomInstance import com.github.libretube.obj.CustomInstance
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText
import java.net.URL import java.net.URL
class CustomInstanceDialog : DialogFragment() { class CustomInstanceDialog : DialogFragment() {
val TAG = "CustomInstanceDialog" val TAG = "CustomInstanceDialog"
private lateinit var binding: DialogCustomInstanceBinding
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let { return activity?.let {
val builder = MaterialAlertDialogBuilder(it) val builder = MaterialAlertDialogBuilder(it)
val inflater = requireActivity().layoutInflater binding = DialogCustomInstanceBinding.inflate(layoutInflater)
val view: View = inflater.inflate(R.layout.dialog_custom_instance, null)
val instanceNameEditText = view.findViewById<TextInputEditText>(R.id.instanceName) binding.cancel.setOnClickListener {
val instanceApiUrlEditText = view.findViewById<TextInputEditText>(R.id.instanceApiUrl)
val instanceFrontendUrlEditText = view
.findViewById<TextInputEditText>(R.id.instanceFrontendUrl)
val addInstanceButton = view.findViewById<Button>(R.id.addInstance)
val cancelButton = view.findViewById<Button>(R.id.cancel)
cancelButton.setOnClickListener {
dismiss() dismiss()
} }
addInstanceButton.setOnClickListener { binding.addInstance.setOnClickListener {
val customInstance = CustomInstance() val customInstance = CustomInstance()
customInstance.name = instanceNameEditText.text.toString() customInstance.name = binding.instanceName.text.toString()
customInstance.apiUrl = instanceApiUrlEditText.text.toString() customInstance.apiUrl = binding.instanceApiUrl.text.toString()
customInstance.frontendUrl = instanceFrontendUrlEditText.text.toString() customInstance.frontendUrl = binding.instanceFrontendUrl.text.toString()
if ( if (
customInstance.name != "" && customInstance.name != "" &&
@ -80,9 +70,9 @@ class CustomInstanceDialog : DialogFragment() {
"Libre<span style='color:$hexColor';>Tube</span>", "Libre<span style='color:$hexColor';>Tube</span>",
HtmlCompat.FROM_HTML_MODE_COMPACT HtmlCompat.FROM_HTML_MODE_COMPACT
) )
view.findViewById<TextView>(R.id.title).text = appName binding.title.text = appName
builder.setView(view) builder.setView(binding.root)
builder.create() builder.create()
} ?: throw IllegalStateException("Activity cannot be null") } ?: throw IllegalStateException("Activity cannot be null")
} }

View File

@ -4,14 +4,12 @@ import android.app.Dialog
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.util.TypedValue import android.util.TypedValue
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.core.text.HtmlCompat import androidx.core.text.HtmlCompat
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.databinding.DialogDeleteAccountBinding
import com.github.libretube.obj.DeleteUserRequest import com.github.libretube.obj.DeleteUserRequest
import com.github.libretube.requireMainActivityRestart import com.github.libretube.requireMainActivityRestart
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
@ -20,22 +18,20 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
class DeleteAccountDialog : DialogFragment() { class DeleteAccountDialog : DialogFragment() {
private val TAG = "DeleteAccountDialog" private val TAG = "DeleteAccountDialog"
lateinit var username: EditText private lateinit var binding: DialogDeleteAccountBinding
lateinit var password: EditText
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let { return activity?.let {
val builder = MaterialAlertDialogBuilder(it) val builder = MaterialAlertDialogBuilder(it)
val inflater = requireActivity().layoutInflater binding = DialogDeleteAccountBinding.inflate(layoutInflater)
val view = inflater.inflate(R.layout.dialog_delete_account, null)
view.findViewById<Button>(R.id.cancel_button).setOnClickListener { binding.cancelButton.setOnClickListener {
dialog?.dismiss() dialog?.dismiss()
} }
password = view.findViewById(R.id.delete_password) binding.deleteAccountConfirm.setOnClickListener {
view.findViewById<Button>(R.id.delete_account_confirm).setOnClickListener { if (binding.deletePassword.text.toString() != "") {
if (password.text.toString() != "") { deleteAccount(binding.deletePassword.text.toString())
deleteAccount(password.text.toString())
} else { } else {
Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show()
} }
@ -48,9 +44,9 @@ class DeleteAccountDialog : DialogFragment() {
"Libre<span style='color:$hexColor';>Tube</span>", "Libre<span style='color:$hexColor';>Tube</span>",
HtmlCompat.FROM_HTML_MODE_COMPACT HtmlCompat.FROM_HTML_MODE_COMPACT
) )
view.findViewById<TextView>(R.id.title).text = appName binding.title.text = appName
builder.setView(view) builder.setView(binding.root)
builder.create() builder.create()
} ?: throw IllegalStateException("Activity cannot be null") } ?: throw IllegalStateException("Activity cannot be null")
} }

View File

@ -9,22 +9,20 @@ import android.os.Bundle
import android.os.Environment import android.os.Environment
import android.util.Log import android.util.Log
import android.util.TypedValue import android.util.TypedValue
import android.view.View
import android.widget.ArrayAdapter import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.Spinner
import android.widget.TextView
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.core.text.HtmlCompat import androidx.core.text.HtmlCompat
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import com.github.libretube.MainActivity import com.github.libretube.MainActivity
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.databinding.DialogDownloadBinding
import com.github.libretube.obj.Streams import com.github.libretube.obj.Streams
import com.github.libretube.services.DownloadService import com.github.libretube.services.DownloadService
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
class DownloadDialog : DialogFragment() { class DownloadDialog : DialogFragment() {
private val TAG = "DownloadDialog" private val TAG = "DownloadDialog"
private lateinit var binding: DialogDownloadBinding
private lateinit var streams: Streams private lateinit var streams: Streams
private lateinit var videoId: String private lateinit var videoId: String
@ -37,9 +35,7 @@ class DownloadDialog : DialogFragment() {
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
val builder = MaterialAlertDialogBuilder(it) val builder = MaterialAlertDialogBuilder(it)
// Get the layout inflater binding = DialogDownloadBinding.inflate(layoutInflater)
val inflater = requireActivity().layoutInflater
var view: View = inflater.inflate(R.layout.dialog_download, null)
// request storage permissions if not granted yet // request storage permissions if not granted yet
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
@ -103,29 +99,27 @@ class DownloadDialog : DialogFragment() {
audioUrl.add(audio.url!!) audioUrl.add(audio.url!!)
} }
val videoSpinner = view.findViewById<Spinner>(R.id.video_spinner)
val videoArrayAdapter = ArrayAdapter( val videoArrayAdapter = ArrayAdapter(
requireContext(), requireContext(),
android.R.layout.simple_spinner_item, android.R.layout.simple_spinner_item,
vidName vidName
) )
videoArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) videoArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
videoSpinner.adapter = videoArrayAdapter binding.videoSpinner.adapter = videoArrayAdapter
videoSpinner.setSelection(1) binding.videoSpinner.setSelection(1)
val audioSpinner = view.findViewById<Spinner>(R.id.audio_spinner)
val audioArrayAdapter = ArrayAdapter( val audioArrayAdapter = ArrayAdapter(
requireContext(), requireContext(),
android.R.layout.simple_spinner_item, android.R.layout.simple_spinner_item,
audioName audioName
) )
audioArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) audioArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
audioSpinner.adapter = audioArrayAdapter binding.audioSpinner.adapter = audioArrayAdapter
audioSpinner.setSelection(1) binding.audioSpinner.setSelection(1)
view.findViewById<Button>(R.id.download).setOnClickListener { binding.download.setOnClickListener {
val selectedAudioUrl = audioUrl[audioSpinner.selectedItemPosition] val selectedAudioUrl = audioUrl[binding.audioSpinner.selectedItemPosition]
val selectedVideoUrl = vidUrl[videoSpinner.selectedItemPosition] val selectedVideoUrl = vidUrl[binding.videoSpinner.selectedItemPosition]
val intent = Intent(context, DownloadService::class.java) val intent = Intent(context, DownloadService::class.java)
intent.putExtra("videoId", videoId) intent.putExtra("videoId", videoId)
@ -143,9 +137,9 @@ class DownloadDialog : DialogFragment() {
"Libre<span style='color:$hexColor';>Tube</span>", "Libre<span style='color:$hexColor';>Tube</span>",
HtmlCompat.FROM_HTML_MODE_COMPACT HtmlCompat.FROM_HTML_MODE_COMPACT
) )
view.findViewById<TextView>(R.id.title).text = appName binding.title.text = appName
builder.setView(view) builder.setView(binding.root)
builder.create() builder.create()
} ?: throw IllegalStateException("Activity cannot be null") } ?: throw IllegalStateException("Activity cannot be null")
} }

View File

@ -4,15 +4,12 @@ import android.app.Dialog
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.util.TypedValue import android.util.TypedValue
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.core.text.HtmlCompat import androidx.core.text.HtmlCompat
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.databinding.DialogLoginBinding
import com.github.libretube.obj.Login import com.github.libretube.obj.Login
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.RetrofitInstance
@ -22,47 +19,36 @@ import java.io.IOException
class LoginDialog : DialogFragment() { class LoginDialog : DialogFragment() {
private val TAG = "LoginDialog" private val TAG = "LoginDialog"
lateinit var username: EditText private lateinit var binding: DialogLoginBinding
lateinit var password: EditText
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let { return activity?.let {
val builder = MaterialAlertDialogBuilder(it) val builder = MaterialAlertDialogBuilder(it)
// Get the layout inflater // Get the layout inflater
val inflater = requireActivity().layoutInflater binding = DialogLoginBinding.inflate(layoutInflater)
val token = PreferenceHelper.getToken(requireContext())
var view: View binding.login.setOnClickListener {
Log.e("dafaq", token!!) if (binding.username.text.toString() != "" && binding.password.text.toString() != "") {
if (token != "") { val login = Login(binding.username.text.toString(), binding.password.text.toString())
val user = PreferenceHelper.getUsername(requireContext())
view = inflater.inflate(R.layout.dialog_logout, null)
view.findViewById<TextView>(R.id.user).text =
view.findViewById<TextView>(R.id.user).text.toString() + " (" + user + ")"
view.findViewById<Button>(R.id.logout).setOnClickListener {
Toast.makeText(context, R.string.loggedout, Toast.LENGTH_SHORT).show()
PreferenceHelper.setToken(requireContext(), "")
dialog?.dismiss()
}
} else {
view = inflater.inflate(R.layout.dialog_login, null)
username = view.findViewById(R.id.username)
password = view.findViewById(R.id.password)
view.findViewById<Button>(R.id.login).setOnClickListener {
if (username.text.toString() != "" && password.text.toString() != "") {
val login = Login(username.text.toString(), password.text.toString())
login(login) login(login)
} else { } else {
Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show()
} }
} }
view.findViewById<Button>(R.id.register).setOnClickListener { binding.register.setOnClickListener {
if (username.text.toString() != "" && password.text.toString() != "") { if (
val login = Login(username.text.toString(), password.text.toString()) binding.username.text.toString() != "" &&
binding.password.text.toString() != ""
) {
val login = Login(
binding.username.text.toString(),
binding.password.text.toString()
)
register(login) register(login)
} else { } else {
Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show()
} }
} }
}
val typedValue = TypedValue() val typedValue = TypedValue()
this.requireActivity().theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true) this.requireActivity().theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true)
@ -71,9 +57,9 @@ class LoginDialog : DialogFragment() {
"Libre<span style='color:$hexColor';>Tube</span>", "Libre<span style='color:$hexColor';>Tube</span>",
HtmlCompat.FROM_HTML_MODE_COMPACT HtmlCompat.FROM_HTML_MODE_COMPACT
) )
view.findViewById<TextView>(R.id.title).text = appName binding.title.text = appName
builder.setView(view) builder.setView(binding.root)
builder.create() builder.create()
} ?: throw IllegalStateException("Activity cannot be null") } ?: throw IllegalStateException("Activity cannot be null")
} }

View File

@ -0,0 +1,46 @@
package com.github.libretube.dialogs
import android.app.Dialog
import android.os.Bundle
import android.util.TypedValue
import android.widget.Toast
import androidx.core.text.HtmlCompat
import androidx.fragment.app.DialogFragment
import com.github.libretube.R
import com.github.libretube.databinding.DialogLogoutBinding
import com.github.libretube.util.PreferenceHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder
class LogoutDialog : DialogFragment() {
private val TAG = "LogoutDialog"
private lateinit var binding: DialogLogoutBinding
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val builder = MaterialAlertDialogBuilder(it)
binding = DialogLogoutBinding.inflate(layoutInflater)
val user = PreferenceHelper.getUsername(requireContext())
binding.user.text =
binding.user.text.toString() + " (" + user + ")"
binding.logout.setOnClickListener {
Toast.makeText(context, R.string.loggedout, Toast.LENGTH_SHORT).show()
PreferenceHelper.setToken(requireContext(), "")
dialog?.dismiss()
}
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
)
binding.title.text = appName
builder.setView(binding.root)
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
}

View File

@ -22,6 +22,7 @@ import com.github.libretube.R
import com.github.libretube.dialogs.CustomInstanceDialog import com.github.libretube.dialogs.CustomInstanceDialog
import com.github.libretube.dialogs.DeleteAccountDialog import com.github.libretube.dialogs.DeleteAccountDialog
import com.github.libretube.dialogs.LoginDialog import com.github.libretube.dialogs.LoginDialog
import com.github.libretube.dialogs.LogoutDialog
import com.github.libretube.requireMainActivityRestart import com.github.libretube.requireMainActivityRestart
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.RetrofitInstance
@ -140,9 +141,15 @@ class InstanceSettings : PreferenceFragmentCompat() {
val login = findPreference<Preference>("login_register") val login = findPreference<Preference>("login_register")
login?.setOnPreferenceClickListener { login?.setOnPreferenceClickListener {
requireMainActivityRestart = true val token = PreferenceHelper.getToken(requireContext())
if (token == "") {
val newFragment = LoginDialog() val newFragment = LoginDialog()
newFragment.show(childFragmentManager, "Login") newFragment.show(childFragmentManager, "Login")
} else {
val newFragment = LogoutDialog()
newFragment.show(childFragmentManager, "Logout")
}
true true
} }

View File

@ -24,6 +24,7 @@
<Button <Button
android:id="@+id/logout" android:id="@+id/logout"
style="@style/CustomDialogButton" style="@style/CustomDialogButton"
android:layout_marginRight="16dp"
android:text="@string/logout" /> android:text="@string/logout" />
</LinearLayout> </LinearLayout>