From 2727d91d244bffbbdc6cc843e689cebc76bc0602 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 26 Aug 2022 09:15:48 +0200 Subject: [PATCH 1/3] better dialog syntax --- .../libretube/dialogs/AddToPlaylistDialog.kt | 20 ++--- .../libretube/dialogs/CreatePlaylistDialog.kt | 42 +++++----- .../libretube/dialogs/CustomInstanceDialog.kt | 80 +++++++++---------- .../libretube/dialogs/DeleteAccountDialog.kt | 32 ++++---- .../libretube/dialogs/DownloadDialog.kt | 32 ++++---- .../github/libretube/dialogs/LoginDialog.kt | 55 ++++++------- .../github/libretube/dialogs/LogoutDialog.kt | 30 ++++--- .../libretube/dialogs/RequireRestartDialog.kt | 20 +++-- .../github/libretube/dialogs/ShareDialog.kt | 78 +++++++++--------- .../github/libretube/dialogs/UpdateDialog.kt | 38 +++++---- 10 files changed, 202 insertions(+), 225 deletions(-) diff --git a/app/src/main/java/com/github/libretube/dialogs/AddToPlaylistDialog.kt b/app/src/main/java/com/github/libretube/dialogs/AddToPlaylistDialog.kt index a74e80ee4..1e21cbdf4 100644 --- a/app/src/main/java/com/github/libretube/dialogs/AddToPlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/AddToPlaylistDialog.kt @@ -29,21 +29,17 @@ class AddToPlaylistDialog : DialogFragment() { private lateinit var token: String override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - return activity?.let { - videoId = arguments?.getString("videoId")!! - val builder = MaterialAlertDialogBuilder(it) - // Get the layout inflater - binding = DialogAddtoplaylistBinding.inflate(layoutInflater) + videoId = arguments?.getString("videoId")!! + binding = DialogAddtoplaylistBinding.inflate(layoutInflater) + binding.title.text = ThemeHelper.getStyledAppName(requireContext()) - token = PreferenceHelper.getToken() + token = PreferenceHelper.getToken() - if (token != "") fetchPlaylists() + if (token != "") fetchPlaylists() - binding.title.text = ThemeHelper.getStyledAppName(requireContext()) - - builder.setView(binding.root) - builder.create() - } ?: throw IllegalStateException("Activity cannot be null") + return MaterialAlertDialogBuilder(requireContext()) + .setView(binding.root) + .show() } private fun fetchPlaylists() { diff --git a/app/src/main/java/com/github/libretube/dialogs/CreatePlaylistDialog.kt b/app/src/main/java/com/github/libretube/dialogs/CreatePlaylistDialog.kt index c9320a346..66f6c10e8 100644 --- a/app/src/main/java/com/github/libretube/dialogs/CreatePlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/CreatePlaylistDialog.kt @@ -23,32 +23,30 @@ class CreatePlaylistDialog : DialogFragment() { private lateinit var binding: DialogCreatePlaylistBinding override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - return activity?.let { - val builder = MaterialAlertDialogBuilder(it) - binding = DialogCreatePlaylistBinding.inflate(layoutInflater) + binding = DialogCreatePlaylistBinding.inflate(layoutInflater) - binding.title.text = ThemeHelper.getStyledAppName(requireContext()) + binding.title.text = ThemeHelper.getStyledAppName(requireContext()) - binding.cancelButton.setOnClickListener { - dismiss() + binding.cancelButton.setOnClickListener { + dismiss() + } + + token = PreferenceHelper.getToken() + + binding.createNewPlaylist.setOnClickListener { + // avoid creating the same playlist multiple times by spamming the button + binding.createNewPlaylist.setOnClickListener(null) + val listName = binding.playlistName.text.toString() + if (listName != "") { + createPlaylist(listName) + } else { + Toast.makeText(context, R.string.emptyPlaylistName, Toast.LENGTH_LONG).show() } + } - token = PreferenceHelper.getToken() - - binding.createNewPlaylist.setOnClickListener { - // avoid creating the same playlist multiple times by spamming the button - binding.createNewPlaylist.setOnClickListener(null) - val listName = binding.playlistName.text.toString() - if (listName != "") { - createPlaylist(listName) - } else { - Toast.makeText(context, R.string.emptyPlaylistName, Toast.LENGTH_LONG).show() - } - } - - builder.setView(binding.root) - builder.create() - } ?: throw IllegalStateException("Activity cannot be null") + return MaterialAlertDialogBuilder(requireContext()) + .setView(binding.root) + .show() } private fun createPlaylist(name: String) { diff --git a/app/src/main/java/com/github/libretube/dialogs/CustomInstanceDialog.kt b/app/src/main/java/com/github/libretube/dialogs/CustomInstanceDialog.kt index 48b1f0039..05d509f9e 100644 --- a/app/src/main/java/com/github/libretube/dialogs/CustomInstanceDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/CustomInstanceDialog.kt @@ -16,59 +16,57 @@ class CustomInstanceDialog : DialogFragment() { private lateinit var binding: DialogCustomInstanceBinding override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - return activity?.let { - val builder = MaterialAlertDialogBuilder(it) - binding = DialogCustomInstanceBinding.inflate(layoutInflater) + binding = DialogCustomInstanceBinding.inflate(layoutInflater) - binding.cancel.setOnClickListener { - dismiss() - } + binding.cancel.setOnClickListener { + dismiss() + } - binding.addInstance.setOnClickListener { - val customInstance = CustomInstance( - name = binding.instanceName.text.toString(), - apiUrl = binding.instanceApiUrl.text.toString(), - frontendUrl = binding.instanceFrontendUrl.text.toString() - ) + binding.addInstance.setOnClickListener { + val customInstance = CustomInstance( + name = binding.instanceName.text.toString(), + apiUrl = binding.instanceApiUrl.text.toString(), + frontendUrl = binding.instanceFrontendUrl.text.toString() + ) - if ( - customInstance.name != "" && - customInstance.apiUrl != "" && - customInstance.frontendUrl != "" - ) { - try { - // check whether the URL is valid, otherwise catch - URL(customInstance.apiUrl).toURI() - URL(customInstance.frontendUrl).toURI() + if ( + customInstance.name != "" && + customInstance.apiUrl != "" && + customInstance.frontendUrl != "" + ) { + try { + // check whether the URL is valid, otherwise catch + URL(customInstance.apiUrl).toURI() + URL(customInstance.frontendUrl).toURI() - Thread { - DatabaseHolder.db.customInstanceDao().insertAll(customInstance) - }.start() + Thread { + DatabaseHolder.db.customInstanceDao().insertAll(customInstance) + }.start() - activity?.recreate() - dismiss() - } catch (e: Exception) { - // invalid URL - Toast.makeText( - context, - getString(R.string.invalid_url), - Toast.LENGTH_SHORT - ).show() - } - } else { - // at least one empty input + activity?.recreate() + dismiss() + } catch (e: Exception) { + // invalid URL Toast.makeText( context, - context?.getString(R.string.empty_instance), + getString(R.string.invalid_url), Toast.LENGTH_SHORT ).show() } + } else { + // at least one empty input + Toast.makeText( + context, + context?.getString(R.string.empty_instance), + Toast.LENGTH_SHORT + ).show() } + } - binding.title.text = ThemeHelper.getStyledAppName(requireContext()) + binding.title.text = ThemeHelper.getStyledAppName(requireContext()) - builder.setView(binding.root) - builder.create() - } ?: throw IllegalStateException("Activity cannot be null") + return MaterialAlertDialogBuilder(requireContext()) + .setView(binding.root) + .show() } } diff --git a/app/src/main/java/com/github/libretube/dialogs/DeleteAccountDialog.kt b/app/src/main/java/com/github/libretube/dialogs/DeleteAccountDialog.kt index 88dea1817..1cb5e039c 100644 --- a/app/src/main/java/com/github/libretube/dialogs/DeleteAccountDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/DeleteAccountDialog.kt @@ -19,27 +19,25 @@ class DeleteAccountDialog : DialogFragment() { private lateinit var binding: DialogDeleteAccountBinding override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - return activity?.let { - val builder = MaterialAlertDialogBuilder(it) - binding = DialogDeleteAccountBinding.inflate(layoutInflater) + binding = DialogDeleteAccountBinding.inflate(layoutInflater) - binding.cancelButton.setOnClickListener { - dialog?.dismiss() + binding.cancelButton.setOnClickListener { + dialog?.dismiss() + } + + binding.deleteAccountConfirm.setOnClickListener { + if (binding.deletePassword.text.toString() != "") { + deleteAccount(binding.deletePassword.text.toString()) + } else { + Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show() } + } - binding.deleteAccountConfirm.setOnClickListener { - if (binding.deletePassword.text.toString() != "") { - deleteAccount(binding.deletePassword.text.toString()) - } else { - Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show() - } - } + binding.title.text = ThemeHelper.getStyledAppName(requireContext()) - binding.title.text = ThemeHelper.getStyledAppName(requireContext()) - - builder.setView(binding.root) - builder.create() - } ?: throw IllegalStateException("Activity cannot be null") + return MaterialAlertDialogBuilder(requireContext()) + .setView(binding.root) + .show() } private fun deleteAccount(password: String) { diff --git a/app/src/main/java/com/github/libretube/dialogs/DownloadDialog.kt b/app/src/main/java/com/github/libretube/dialogs/DownloadDialog.kt index 17bd96864..099f2557d 100644 --- a/app/src/main/java/com/github/libretube/dialogs/DownloadDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/DownloadDialog.kt @@ -28,29 +28,27 @@ class DownloadDialog( private lateinit var binding: DialogDownloadBinding override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - return activity?.let { - val builder = MaterialAlertDialogBuilder(it) - binding = DialogDownloadBinding.inflate(layoutInflater) + binding = DialogDownloadBinding.inflate(layoutInflater) - fetchAvailableSources() + fetchAvailableSources() - PermissionHelper.requestReadWrite(requireActivity()) + PermissionHelper.requestReadWrite(requireActivity()) - binding.title.text = ThemeHelper.getStyledAppName(requireContext()) + binding.title.text = ThemeHelper.getStyledAppName(requireContext()) - binding.audioRadio.setOnClickListener { - binding.videoSpinner.visibility = View.GONE - binding.audioSpinner.visibility = View.VISIBLE - } + binding.audioRadio.setOnClickListener { + binding.videoSpinner.visibility = View.GONE + binding.audioSpinner.visibility = View.VISIBLE + } - binding.videoRadio.setOnClickListener { - binding.audioSpinner.visibility = View.GONE - binding.videoSpinner.visibility = View.VISIBLE - } + binding.videoRadio.setOnClickListener { + binding.audioSpinner.visibility = View.GONE + binding.videoSpinner.visibility = View.VISIBLE + } - builder.setView(binding.root) - builder.create() - } ?: throw IllegalStateException("Activity cannot be null") + return MaterialAlertDialogBuilder(requireContext()) + .setView(binding.root) + .show() } private fun fetchAvailableSources() { diff --git a/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt b/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt index 03ba1be99..dea8cc329 100644 --- a/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt @@ -21,40 +21,37 @@ class LoginDialog : DialogFragment() { private lateinit var binding: DialogLoginBinding override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - return activity?.let { - val builder = MaterialAlertDialogBuilder(it) - // Get the layout inflater - binding = DialogLoginBinding.inflate(layoutInflater) + binding = DialogLoginBinding.inflate(layoutInflater) - binding.login.setOnClickListener { - if (binding.username.text.toString() != "" && binding.password.text.toString() != "") { - val login = - Login(binding.username.text.toString(), binding.password.text.toString()) - login(login) - } else { - Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show() - } + binding.login.setOnClickListener { + if (binding.username.text.toString() != "" && binding.password.text.toString() != "") { + val login = + Login(binding.username.text.toString(), binding.password.text.toString()) + login(login) + } else { + Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show() } - binding.register.setOnClickListener { - if ( - binding.username.text.toString() != "" && - binding.password.text.toString() != "" - ) { - val login = Login( - binding.username.text.toString(), - binding.password.text.toString() - ) - register(login) - } else { - Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show() - } + } + binding.register.setOnClickListener { + if ( + binding.username.text.toString() != "" && + binding.password.text.toString() != "" + ) { + val login = Login( + binding.username.text.toString(), + binding.password.text.toString() + ) + register(login) + } else { + Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show() } + } - binding.title.text = ThemeHelper.getStyledAppName(requireContext()) + binding.title.text = ThemeHelper.getStyledAppName(requireContext()) - builder.setView(binding.root) - builder.create() - } ?: throw IllegalStateException("Activity cannot be null") + return MaterialAlertDialogBuilder(requireContext()) + .setView(binding.root) + .show() } private fun login(login: Login) { diff --git a/app/src/main/java/com/github/libretube/dialogs/LogoutDialog.kt b/app/src/main/java/com/github/libretube/dialogs/LogoutDialog.kt index 99a5e3356..c5dd117d7 100644 --- a/app/src/main/java/com/github/libretube/dialogs/LogoutDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/LogoutDialog.kt @@ -14,25 +14,23 @@ class LogoutDialog : DialogFragment() { private lateinit var binding: DialogLogoutBinding override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - return activity?.let { - val builder = MaterialAlertDialogBuilder(it) - binding = DialogLogoutBinding.inflate(layoutInflater) + binding = DialogLogoutBinding.inflate(layoutInflater) - val user = PreferenceHelper.getUsername() + val user = PreferenceHelper.getUsername() - binding.user.text = - binding.user.text.toString() + " (" + user + ")" - binding.logout.setOnClickListener { - Toast.makeText(context, R.string.loggedout, Toast.LENGTH_SHORT).show() - PreferenceHelper.setToken("") - dialog?.dismiss() - activity?.recreate() - } + binding.user.text = + binding.user.text.toString() + " (" + user + ")" + binding.logout.setOnClickListener { + Toast.makeText(context, R.string.loggedout, Toast.LENGTH_SHORT).show() + PreferenceHelper.setToken("") + dialog?.dismiss() + activity?.recreate() + } - binding.title.text = ThemeHelper.getStyledAppName(requireContext()) + binding.title.text = ThemeHelper.getStyledAppName(requireContext()) - builder.setView(binding.root) - builder.create() - } ?: throw IllegalStateException("Activity cannot be null") + return MaterialAlertDialogBuilder(requireContext()) + .setView(binding.root) + .show() } } diff --git a/app/src/main/java/com/github/libretube/dialogs/RequireRestartDialog.kt b/app/src/main/java/com/github/libretube/dialogs/RequireRestartDialog.kt index f22b89980..5c2f3faf0 100644 --- a/app/src/main/java/com/github/libretube/dialogs/RequireRestartDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/RequireRestartDialog.kt @@ -10,16 +10,14 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder class RequireRestartDialog : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - return activity?.let { - MaterialAlertDialogBuilder(requireContext()) - .setTitle(R.string.require_restart) - .setMessage(R.string.require_restart_message) - .setPositiveButton(R.string.okay) { _, _ -> - activity?.recreate() - ThemeHelper.restartMainActivity(requireContext()) - } - .setNegativeButton(R.string.cancel, null) - .create() - } ?: throw IllegalStateException("Activity cannot be null") + return MaterialAlertDialogBuilder(requireContext()) + .setTitle(R.string.require_restart) + .setMessage(R.string.require_restart_message) + .setPositiveButton(R.string.okay) { _, _ -> + activity?.recreate() + ThemeHelper.restartMainActivity(requireContext()) + } + .setNegativeButton(R.string.cancel, null) + .show() } } diff --git a/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt b/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt index 398239287..acd7b4d89 100644 --- a/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt @@ -21,49 +21,47 @@ class ShareDialog( ) : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - return activity?.let { - var shareOptions = arrayOf( - getString(R.string.piped), - getString(R.string.youtube) - ) - val instanceUrl = getCustomInstanceFrontendUrl() + var shareOptions = arrayOf( + getString(R.string.piped), + getString(R.string.youtube) + ) + val instanceUrl = getCustomInstanceFrontendUrl() - // add instanceUrl option if custom instance frontend url available - if (instanceUrl != "") shareOptions += getString(R.string.instance) + // add instanceUrl option if custom instance frontend url available + if (instanceUrl != "") shareOptions += getString(R.string.instance) - MaterialAlertDialogBuilder(requireContext()) - .setTitle(context?.getString(R.string.share)) - .setItems( - shareOptions - ) { _, which -> - val host = when (which) { - 0 -> PIPED_FRONTEND_URL - 1 -> YOUTUBE_FRONTEND_URL - // only available for custom instances - else -> instanceUrl - } - val path = if (!isPlaylist) "/watch?v=$id" else "/playlist?list=$id" - var url = "$host$path" - if (PreferenceHelper.getBoolean( - PreferenceKeys.SHARE_WITH_TIME_CODE, - true - ) - ) { - url += "?t=$position" - } - - val intent = Intent() - intent.apply { - action = Intent.ACTION_SEND - putExtra(Intent.EXTRA_TEXT, url) - type = "text/plain" - } - context?.startActivity( - Intent.createChooser(intent, context?.getString(R.string.shareTo)) - ) + return MaterialAlertDialogBuilder(requireContext()) + .setTitle(context?.getString(R.string.share)) + .setItems( + shareOptions + ) { _, which -> + val host = when (which) { + 0 -> PIPED_FRONTEND_URL + 1 -> YOUTUBE_FRONTEND_URL + // only available for custom instances + else -> instanceUrl } - .show() - } ?: throw IllegalStateException("Activity cannot be null") + val path = if (!isPlaylist) "/watch?v=$id" else "/playlist?list=$id" + var url = "$host$path" + if (PreferenceHelper.getBoolean( + PreferenceKeys.SHARE_WITH_TIME_CODE, + true + ) + ) { + url += "?t=$position" + } + + val intent = Intent() + intent.apply { + action = Intent.ACTION_SEND + putExtra(Intent.EXTRA_TEXT, url) + type = "text/plain" + } + context?.startActivity( + Intent.createChooser(intent, context?.getString(R.string.shareTo)) + ) + } + .show() } // get the frontend url if it's a custom instance diff --git a/app/src/main/java/com/github/libretube/dialogs/UpdateDialog.kt b/app/src/main/java/com/github/libretube/dialogs/UpdateDialog.kt index 715400bdd..4b3ed8d6c 100644 --- a/app/src/main/java/com/github/libretube/dialogs/UpdateDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/UpdateDialog.kt @@ -18,27 +18,25 @@ class UpdateDialog( ) : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - return activity?.let { - MaterialAlertDialogBuilder(requireContext()) - .setTitle(context?.getString(R.string.update_available, updateInfo.name)) - .setMessage(context?.getString(R.string.update_now)) - .setNegativeButton(R.string.cancel, null) - .setPositiveButton(context?.getString(R.string.okay)) { _, _ -> - val downloadUrl = getDownloadUrl(updateInfo) - Log.i("downloadUrl", downloadUrl.toString()) - if (downloadUrl != null) { - PermissionHelper.requestReadWrite(requireActivity()) - val intent = Intent(context, UpdateService::class.java) - intent.putExtra("downloadUrl", downloadUrl) - context?.startService(intent) - } else { - val uri = Uri.parse(updateInfo.html_url) - val intent = Intent(Intent.ACTION_VIEW).setData(uri) - startActivity(intent) - } + return MaterialAlertDialogBuilder(requireContext()) + .setTitle(context?.getString(R.string.update_available, updateInfo.name)) + .setMessage(context?.getString(R.string.update_now)) + .setNegativeButton(R.string.cancel, null) + .setPositiveButton(context?.getString(R.string.okay)) { _, _ -> + val downloadUrl = getDownloadUrl(updateInfo) + Log.i("downloadUrl", downloadUrl.toString()) + if (downloadUrl != null) { + PermissionHelper.requestReadWrite(requireActivity()) + val intent = Intent(context, UpdateService::class.java) + intent.putExtra("downloadUrl", downloadUrl) + context?.startService(intent) + } else { + val uri = Uri.parse(updateInfo.html_url) + val intent = Intent(Intent.ACTION_VIEW).setData(uri) + startActivity(intent) } - .create() - } ?: throw IllegalStateException("Activity cannot be null") + } + .show() } private fun getDownloadUrl(updateInfo: UpdateInfo): String? { From fdacc323838b74ccb7664826f7b85203902c117c Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 26 Aug 2022 09:17:43 +0200 Subject: [PATCH 2/3] hide add to playlist when not logged in --- .../com/github/libretube/dialogs/VideoOptionsDialog.kt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt b/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt index 9eb7e221b..0df687c03 100644 --- a/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt @@ -37,6 +37,13 @@ class VideoOptionsDialog( context?.getString(R.string.share) ) + // remove the add to playlist option if not logged in + if (PreferenceHelper.getToken() == "") { + optionsList.remove( + context?.getString(R.string.addToPlaylist) + + ) + } /** * Check whether the player is running by observing the notification */ From 58efe901d912a0186db24588fb6bcb99d34fcba6 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 26 Aug 2022 09:37:28 +0200 Subject: [PATCH 3/3] allow toggling share with time code in dialog --- .../github/libretube/dialogs/ShareDialog.kt | 20 ++++++++---- app/src/main/res/layout/dialog_share.xml | 31 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 +- 3 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 app/src/main/res/layout/dialog_share.xml diff --git a/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt b/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt index acd7b4d89..d34ef2d2d 100644 --- a/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt @@ -7,6 +7,7 @@ import androidx.fragment.app.DialogFragment import com.github.libretube.PIPED_FRONTEND_URL import com.github.libretube.R import com.github.libretube.YOUTUBE_FRONTEND_URL +import com.github.libretube.databinding.DialogShareBinding import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.obj.CustomInstance import com.github.libretube.extensions.await @@ -17,8 +18,9 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder class ShareDialog( private val id: String, private val isPlaylist: Boolean, - private val position: Long = 0L + private val position: Long? = null ) : DialogFragment() { + private var binding: DialogShareBinding? = null override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { var shareOptions = arrayOf( @@ -30,6 +32,14 @@ class ShareDialog( // add instanceUrl option if custom instance frontend url available if (instanceUrl != "") shareOptions += getString(R.string.instance) + if (position != null) { + binding = DialogShareBinding.inflate(layoutInflater) + binding!!.timeCodeSwitch.isChecked = PreferenceHelper.getBoolean( + PreferenceKeys.SHARE_WITH_TIME_CODE, + true + ) + } + return MaterialAlertDialogBuilder(requireContext()) .setTitle(context?.getString(R.string.share)) .setItems( @@ -43,11 +53,8 @@ class ShareDialog( } val path = if (!isPlaylist) "/watch?v=$id" else "/playlist?list=$id" var url = "$host$path" - if (PreferenceHelper.getBoolean( - PreferenceKeys.SHARE_WITH_TIME_CODE, - true - ) - ) { + + if (binding != null && binding!!.timeCodeSwitch.isChecked) { url += "?t=$position" } @@ -61,6 +68,7 @@ class ShareDialog( Intent.createChooser(intent, context?.getString(R.string.shareTo)) ) } + .setView(binding?.root) .show() } diff --git a/app/src/main/res/layout/dialog_share.xml b/app/src/main/res/layout/dialog_share.xml new file mode 100644 index 000000000..fe4005185 --- /dev/null +++ b/app/src/main/res/layout/dialog_share.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d33f0b529..f51fca128 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -283,7 +283,7 @@ Error Copied Download succeeded - Share with start time + Share with time code Export Subscriptions Skip buttons Show buttons to skip to the next or previous video.