From ca29ee2b0b25142d3dbb7dc182b84038b16a38e7 Mon Sep 17 00:00:00 2001 From: Goooler Date: Thu, 2 Mar 2023 04:06:55 +0800 Subject: [PATCH] Use more orEmpty extensions (#3399) https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/or-empty.html https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/or-empty.html --- .../java/com/keylesspalace/tusky/AccountsInListFragment.kt | 2 +- .../main/java/com/keylesspalace/tusky/EditProfileActivity.kt | 2 +- .../keylesspalace/tusky/components/account/AccountActivity.kt | 4 ++-- .../tusky/components/conversation/ConversationEntity.kt | 2 +- .../tusky/components/preference/AccountPreferencesFragment.kt | 4 ++-- .../tusky/components/report/fragments/ReportNoteFragment.kt | 2 +- .../keylesspalace/tusky/components/search/SearchActivity.kt | 2 +- .../components/search/fragments/SearchStatusesFragment.kt | 2 +- .../tusky/components/trending/TrendingAdapter.kt | 2 +- .../main/java/com/keylesspalace/tusky/db/AccountManager.kt | 4 ++-- .../tusky/receiver/SendStatusBroadcastReceiver.kt | 4 ++-- app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.kt | 2 +- .../test/java/com/keylesspalace/tusky/util/LocaleUtilsTest.kt | 2 +- 13 files changed, 17 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/AccountsInListFragment.kt b/app/src/main/java/com/keylesspalace/tusky/AccountsInListFragment.kt index 6be4224a8..2235958c7 100644 --- a/app/src/main/java/com/keylesspalace/tusky/AccountsInListFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/AccountsInListFragment.kt @@ -114,7 +114,7 @@ class AccountsInListFragment : DialogFragment(), Injectable { binding.searchView.isSubmitButtonEnabled = true binding.searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener { override fun onQueryTextSubmit(query: String?): Boolean { - viewModel.search(query ?: "") + viewModel.search(query.orEmpty()) return true } diff --git a/app/src/main/java/com/keylesspalace/tusky/EditProfileActivity.kt b/app/src/main/java/com/keylesspalace/tusky/EditProfileActivity.kt index 369f69262..33d7a281a 100644 --- a/app/src/main/java/com/keylesspalace/tusky/EditProfileActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/EditProfileActivity.kt @@ -136,7 +136,7 @@ class EditProfileActivity : BaseActivity(), Injectable { binding.noteEditText.setText(me.source?.note) binding.lockedCheckBox.isChecked = me.locked - accountFieldEditAdapter.setFields(me.source?.fields ?: emptyList()) + accountFieldEditAdapter.setFields(me.source?.fields.orEmpty()) binding.addFieldButton.isVisible = (me.source?.fields?.size ?: 0) < maxAccountFields diff --git a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt index 1b0800207..cdb30608d 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/account/AccountActivity.kt @@ -463,8 +463,8 @@ class AccountActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvide val emojifiedNote = account.note.parseAsMastodonHtml().emojify(account.emojis, binding.accountNoteTextView, animateEmojis) setClickableText(binding.accountNoteTextView, emojifiedNote, emptyList(), null, this) - accountFieldAdapter.fields = account.fields ?: emptyList() - accountFieldAdapter.emojis = account.emojis ?: emptyList() + accountFieldAdapter.fields = account.fields.orEmpty() + accountFieldAdapter.emojis = account.emojis.orEmpty() accountFieldAdapter.notifyDataSetChanged() binding.accountLockedImageView.visible(account.locked) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationEntity.kt b/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationEntity.kt index 0a75654b1..c338a1c03 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationEntity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationEntity.kt @@ -145,7 +145,7 @@ fun TimelineAccount.toEntity() = username = username, displayName = name, avatar = avatar, - emojis = emojis ?: emptyList() + emojis = emojis.orEmpty() ) fun Status.toEntity( diff --git a/app/src/main/java/com/keylesspalace/tusky/components/preference/AccountPreferencesFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/preference/AccountPreferencesFragment.kt index a863db48a..61b759f1c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/preference/AccountPreferencesFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/preference/AccountPreferencesFragment.kt @@ -207,7 +207,7 @@ class AccountPreferencesFragment : PreferenceFragmentCompat(), Injectable { entryValues = (listOf("") + locales.map { it.language }).toTypedArray() key = PrefKeys.DEFAULT_POST_LANGUAGE icon = makeIcon(requireContext(), GoogleMaterial.Icon.gmd_translate, iconSize) - value = accountManager.activeAccount?.defaultPostLanguage ?: "" + value = accountManager.activeAccount?.defaultPostLanguage.orEmpty() isPersistent = false // This will be entirely server-driven setSummaryProvider { entry } @@ -339,7 +339,7 @@ class AccountPreferencesFragment : PreferenceFragmentCompat(), Injectable { it.defaultPostPrivacy = account.source?.privacy ?: Status.Visibility.PUBLIC it.defaultMediaSensitivity = account.source?.sensitive ?: false - it.defaultPostLanguage = language ?: "" + it.defaultPostLanguage = language.orEmpty() accountManager.saveAccount(it) } } else { diff --git a/app/src/main/java/com/keylesspalace/tusky/components/report/fragments/ReportNoteFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/report/fragments/ReportNoteFragment.kt index 56f812a05..74b40fb93 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/report/fragments/ReportNoteFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/report/fragments/ReportNoteFragment.kt @@ -54,7 +54,7 @@ class ReportNoteFragment : Fragment(R.layout.fragment_report_note), Injectable { private fun handleChanges() { binding.editNote.doAfterTextChanged { - viewModel.reportNote = it?.toString() ?: "" + viewModel.reportNote = it?.toString().orEmpty() } binding.checkIsNotifyRemote.setOnCheckedChangeListener { _, isChecked -> viewModel.isRemoteNotify = isChecked diff --git a/app/src/main/java/com/keylesspalace/tusky/components/search/SearchActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/search/SearchActivity.kt index 90bf04e36..ca60e95c4 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/search/SearchActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/search/SearchActivity.kt @@ -114,7 +114,7 @@ class SearchActivity : BottomSheetActivity(), HasAndroidInjector, MenuProvider { private fun handleIntent(intent: Intent) { if (Intent.ACTION_SEARCH == intent.action) { - viewModel.currentQuery = intent.getStringExtra(SearchManager.QUERY) ?: "" + viewModel.currentQuery = intent.getStringExtra(SearchManager.QUERY).orEmpty() viewModel.search(viewModel.currentQuery) } } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/search/fragments/SearchStatusesFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/search/fragments/SearchStatusesFragment.kt index 9973f3e8f..12aeaf81b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/search/fragments/SearchStatusesFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/search/fragments/SearchStatusesFragment.kt @@ -468,7 +468,7 @@ class SearchStatusesFragment : SearchFragment(), Status val intent = ComposeActivity.startIntent( requireContext(), ComposeOptions( - content = redraftStatus.text ?: "", + content = redraftStatus.text.orEmpty(), inReplyToId = redraftStatus.inReplyToId, visibility = redraftStatus.visibility, contentWarning = redraftStatus.spoilerText, diff --git a/app/src/main/java/com/keylesspalace/tusky/components/trending/TrendingAdapter.kt b/app/src/main/java/com/keylesspalace/tusky/components/trending/TrendingAdapter.kt index 73a327b77..3e6f03416 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/trending/TrendingAdapter.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/trending/TrendingAdapter.kt @@ -72,7 +72,7 @@ class TrendingAdapter( is TrendingViewData.Tag -> { val maxTrendingValue = currentList .flatMap { trendingViewData -> - trendingViewData.asTagOrNull()?.tag?.history ?: emptyList() + trendingViewData.asTagOrNull()?.tag?.history.orEmpty() } .mapNotNull { it.uses.toLongOrNull() } .maxOrNull() ?: 1 diff --git a/app/src/main/java/com/keylesspalace/tusky/db/AccountManager.kt b/app/src/main/java/com/keylesspalace/tusky/db/AccountManager.kt index bf8d414fc..eaa36bb72 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/AccountManager.kt +++ b/app/src/main/java/com/keylesspalace/tusky/db/AccountManager.kt @@ -153,9 +153,9 @@ class AccountManager @Inject constructor(db: AppDatabase) { it.displayName = account.name it.profilePictureUrl = account.avatar it.defaultPostPrivacy = account.source?.privacy ?: Status.Visibility.PUBLIC - it.defaultPostLanguage = account.source?.language ?: "" + it.defaultPostLanguage = account.source?.language.orEmpty() it.defaultMediaSensitivity = account.source?.sensitive ?: false - it.emojis = account.emojis ?: emptyList() + it.emojis = account.emojis.orEmpty() Log.d(TAG, "updateActiveAccount: saving account with id " + it.id) accountDao.insertOrReplace(it) diff --git a/app/src/main/java/com/keylesspalace/tusky/receiver/SendStatusBroadcastReceiver.kt b/app/src/main/java/com/keylesspalace/tusky/receiver/SendStatusBroadcastReceiver.kt index dbeff4068..0c1d0e37c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/receiver/SendStatusBroadcastReceiver.kt +++ b/app/src/main/java/com/keylesspalace/tusky/receiver/SendStatusBroadcastReceiver.kt @@ -49,8 +49,8 @@ class SendStatusBroadcastReceiver : BroadcastReceiver() { val senderFullName = intent.getStringExtra(NotificationHelper.KEY_SENDER_ACCOUNT_FULL_NAME) val citedStatusId = intent.getStringExtra(NotificationHelper.KEY_CITED_STATUS_ID) val visibility = intent.getSerializableExtra(NotificationHelper.KEY_VISIBILITY) as Status.Visibility - val spoiler = intent.getStringExtra(NotificationHelper.KEY_SPOILER) ?: "" - val mentions = intent.getStringArrayExtra(NotificationHelper.KEY_MENTIONS) ?: emptyArray() + val spoiler = intent.getStringExtra(NotificationHelper.KEY_SPOILER).orEmpty() + val mentions = intent.getStringArrayExtra(NotificationHelper.KEY_MENTIONS).orEmpty() val account = accountManager.getAccountById(senderId) diff --git a/app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.kt b/app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.kt index 7e23e45d4..0f6d502e1 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.kt +++ b/app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.kt @@ -86,7 +86,7 @@ fun markupHiddenUrls(context: Context, content: CharSequence): SpannableStringBu false } else { val text = spannableContent.subSequence(start, spannableContent.getSpanEnd(it)).toString() - .split(' ').lastOrNull() ?: "" + .split(' ').lastOrNull().orEmpty() var textDomain = getDomain(text) if (textDomain.isBlank()) { textDomain = getDomain("https://$text") diff --git a/app/src/test/java/com/keylesspalace/tusky/util/LocaleUtilsTest.kt b/app/src/test/java/com/keylesspalace/tusky/util/LocaleUtilsTest.kt index db287cca9..da4c48d55 100644 --- a/app/src/test/java/com/keylesspalace/tusky/util/LocaleUtilsTest.kt +++ b/app/src/test/java/com/keylesspalace/tusky/util/LocaleUtilsTest.kt @@ -73,7 +73,7 @@ class LocaleUtilsTest { clientId = null, clientSecret = null, isActive = true, - defaultPostLanguage = configuredLanguages[1] ?: "", + defaultPostLanguage = configuredLanguages[1].orEmpty(), ) ) }