From 97107fa7771c89c146b2f86be4adee95b770bcce Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Mon, 20 Feb 2023 05:19:35 +0530 Subject: [PATCH] Use isEmpty() extension functions. --- .../github/libretube/api/PlaylistsHelper.kt | 2 +- .../libretube/ui/activities/MainActivity.kt | 13 ++++---- .../ui/dialogs/CreatePlaylistDialog.kt | 4 +-- .../ui/dialogs/DeleteAccountDialog.kt | 5 +-- .../libretube/ui/dialogs/LoginDialog.kt | 33 +++++++------------ .../ui/dialogs/RenamePlaylistDialog.kt | 11 +++---- .../libretube/ui/fragments/PlayerFragment.kt | 2 +- .../libretube/ui/fragments/SearchFragment.kt | 4 +-- .../libretube/workers/NotificationWorker.kt | 2 +- 9 files changed, 32 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt b/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt index e853634d6..cb70d77ff 100644 --- a/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt +++ b/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt @@ -104,7 +104,7 @@ object PlaylistsHelper { DatabaseHolder.Database.localPlaylistsDao().addPlaylistVideo(localPlaylistItem) val playlist = localPlaylist.playlist - if (playlist.thumbnailUrl == "") { + if (playlist.thumbnailUrl.isEmpty()) { // set the new playlist thumbnail URL localPlaylistItem.thumbnailUrl?.let { playlist.thumbnailUrl = it diff --git a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt index a857c7c97..ff0ea7808 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt @@ -291,13 +291,12 @@ class MainActivity : BaseActivity() { } // prevent malicious navigation when the search view is getting collapsed - if (navController.currentDestination?.id in listOf( - R.id.searchResultFragment, - R.id.channelFragment, - R.id.playlistFragment - ) && - (newText == null || newText == "") - ) { + val destIds = listOf( + R.id.searchResultFragment, + R.id.channelFragment, + R.id.playlistFragment + ) + if (navController.currentDestination?.id in destIds && newText.isNullOrEmpty()) { return false } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt index eb76d7f8a..d78b3de47 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt @@ -53,8 +53,8 @@ class CreatePlaylistDialog( 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 != "") { + val listName = binding.playlistName.text?.toString() + if (!listName.isNullOrEmpty()) { lifecycleScope.launch { requireDialog().hide() val playlistId = withContext(Dispatchers.IO) { diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/DeleteAccountDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/DeleteAccountDialog.kt index 951f33bbb..7be649dea 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/DeleteAccountDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/DeleteAccountDialog.kt @@ -27,8 +27,9 @@ class DeleteAccountDialog( } binding.deleteAccountConfirm.setOnClickListener { - if (binding.deletePassword.text.toString() != "") { - deleteAccount(binding.deletePassword.text.toString()) + val password = binding.deletePassword.text?.toString() + if (!password.isNullOrEmpty()) { + deleteAccount(password) } else { Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show() } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/LoginDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/LoginDialog.kt index fff4e3cef..1f6bca605 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/LoginDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/LoginDialog.kt @@ -28,35 +28,30 @@ class LoginDialog( binding = DialogLoginBinding.inflate(layoutInflater) binding.login.setOnClickListener { - if (isInsertionValid()) { - signIn( - binding.username.text.toString(), - binding.password.text.toString() - ) + val email = binding.username.text?.toString() + val password = binding.password.text?.toString() + + if (!email.isNullOrEmpty() && !password.isNullOrEmpty()) { + signIn(email, password) } else { Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show() } } binding.register.setOnClickListener { - if (isEmail(binding.username.text.toString())) { + val email = binding.username.text?.toString().orEmpty() + val password = binding.password.text?.toString().orEmpty() + + if (isEmail(email)) { MaterialAlertDialogBuilder(requireContext()) .setTitle(R.string.privacy_alert) .setMessage(R.string.username_email) .setNegativeButton(R.string.proceed) { _, _ -> - signIn( - binding.username.text.toString(), - binding.password.text.toString(), - true - ) + signIn(email, password, true) } .setPositiveButton(R.string.cancel, null) .show() - } else if (isInsertionValid()) { - signIn( - binding.username.text.toString(), - binding.password.text.toString(), - true - ) + } else if (email.isNotEmpty() && password.isNotEmpty()) { + signIn(email, password, true) } else { Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show() } @@ -67,10 +62,6 @@ class LoginDialog( .show() } - private fun isInsertionValid(): Boolean { - return binding.username.text.toString() != "" && binding.password.text.toString() != "" - } - private fun signIn(username: String, password: String, createNewAccount: Boolean = false) { val login = Login(username, password) lifecycleScope.launchWhenCreated { diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/RenamePlaylistDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/RenamePlaylistDialog.kt index e3125104b..47f0fdc66 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/RenamePlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/RenamePlaylistDialog.kt @@ -36,13 +36,10 @@ class RenamePlaylistDialog( .show() .apply { getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener { - val input = binding.input.text.toString() - if (input == "") { - Toast.makeText( - context, - R.string.emptyPlaylistName, - Toast.LENGTH_SHORT - ).show() + val input = binding.input.text?.toString() + if (input.isNullOrEmpty()) { + Toast.makeText(context, R.string.emptyPlaylistName, Toast.LENGTH_SHORT) + .show() return@setOnClickListener } if (input == currentPlaylistName) return@setOnClickListener diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 62a6a0df3..754ddffb0 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -1270,7 +1270,7 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions { private fun setStreamSource() { val defaultResolution = PlayerHelper.getDefaultResolution(requireContext()).replace("p", "") - if (defaultResolution != "") setPlayerResolution(defaultResolution.toInt()) + if (defaultResolution.isNotEmpty()) setPlayerResolution(defaultResolution.toInt()) if (!PreferenceHelper.getBoolean(PreferenceKeys.USE_HLS_OVER_DASH, false) && streams.videoStreams.isNotEmpty() diff --git a/app/src/main/java/com/github/libretube/ui/fragments/SearchFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/SearchFragment.kt index 6ad9b5b2e..57d90e6ef 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/SearchFragment.kt @@ -51,7 +51,7 @@ class SearchFragment : Fragment(R.layout.fragment_search) { // fetch the search or history binding.historyEmpty.visibility = View.GONE binding.suggestionsRecycler.visibility = View.VISIBLE - if (query == null || query == "") { + if (query.isNullOrEmpty()) { showHistory() } else { fetchSuggestions(query) @@ -71,7 +71,7 @@ class SearchFragment : Fragment(R.layout.fragment_search) { response.reversed(), (activity as MainActivity).searchView ) - if (isAdded && viewModel.searchQuery.value != "") { + if (isAdded && !viewModel.searchQuery.value.isNullOrEmpty()) { binding.suggestionsRecycler.adapter = suggestionsAdapter } } diff --git a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt index 004f20e6d..ee50523d3 100644 --- a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt +++ b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt @@ -99,7 +99,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : val latestFeedStreamId = videoFeed.firstOrNull()?.url?.toID() ?: return true // first time notifications are enabled or no new video available - if (lastSeenStreamId == "" || lastSeenStreamId == latestFeedStreamId) { + if (lastSeenStreamId.isEmpty() || lastSeenStreamId == latestFeedStreamId) { PreferenceHelper.setLatestVideoId(lastSeenStreamId) return true }