From 1ac1c588247530fe062a4df3b058d96a9ef0374c Mon Sep 17 00:00:00 2001 From: TacoTheDank Date: Mon, 8 Jun 2020 19:09:31 -0400 Subject: [PATCH] Fold some if-thens --- .../twidere/activity/MediaViewerActivity.kt | 3 +-- .../activity/TrendsLocationSelectorActivity.kt | 15 +++------------ .../adapter/SimpleParcelableUsersAdapter.kt | 6 +----- .../extension/model/CredentialsExtensions.kt | 6 ++---- .../twidere/fragment/ColorPickerDialogFragment.kt | 7 ++----- .../twidere/fragment/TrendsSuggestionsFragment.kt | 3 +-- .../mariotaku/twidere/model/AccountPreferences.kt | 6 +----- .../mariotaku/twidere/provider/CacheProvider.kt | 3 +-- .../twidere/service/LengthyOperationsService.kt | 12 +++++------- .../twidere/task/twitter/GetStatusesTask.kt | 3 +-- .../org/mariotaku/twidere/util/DataStoreUtils.kt | 3 +-- .../twidere/util/ExternalThemeManager.kt | 7 +------ .../org/mariotaku/twidere/util/MenuUtils.kt | 8 ++++---- .../kotlin/org/mariotaku/twidere/util/Utils.kt | 6 ++---- .../org/mariotaku/twidere/util/net/TwidereDns.kt | 3 +-- .../twidere/util/theme/ThemeFunctions.kt | 3 +-- .../twidere/view/ActionIconThemedTextView.kt | 3 +-- .../premium/PromotionOfferViewController.kt | 3 +-- .../view/holder/status/DetailStatusViewHolder.kt | 7 ++----- 19 files changed, 32 insertions(+), 75 deletions(-) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/MediaViewerActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/MediaViewerActivity.kt index 47c8fa6ad..78a12544d 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/MediaViewerActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/MediaViewerActivity.kt @@ -466,8 +466,7 @@ class MediaViewerActivity : BaseActivity(), IMediaViewerActivity, MediaSwipeClos if (checkAllSelfPermissionsGranted(AndroidPermissions.WRITE_EXTERNAL_STORAGE)) { saveToStorage() } else { - val permissions: Array - permissions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + val permissions: Array = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { arrayOf(AndroidPermissions.WRITE_EXTERNAL_STORAGE, AndroidPermissions.READ_EXTERNAL_STORAGE) } else { arrayOf(AndroidPermissions.WRITE_EXTERNAL_STORAGE) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/TrendsLocationSelectorActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/TrendsLocationSelectorActivity.kt index 9e0e8baea..9ab2c7d63 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/TrendsLocationSelectorActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/TrendsLocationSelectorActivity.kt @@ -177,23 +177,14 @@ class TrendsLocationSelectorActivity : BaseActivity() { } override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup): View { - val view: View - view = if (convertView != null) { - convertView - } else { - inflater.inflate(android.R.layout.simple_expandable_list_item_1, parent, false) - } + val view: View = convertView ?: inflater.inflate(android.R.layout.simple_expandable_list_item_1, parent, false) view.findViewById(android.R.id.text1).text = getGroup(groupPosition).name return view } override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup): View { - val view: View - view = if (convertView != null) { - convertView - } else { - inflater.inflate(android.R.layout.simple_list_item_1, parent, false) - } + val view: View = + convertView ?: inflater.inflate(android.R.layout.simple_list_item_1, parent, false) val location = getChild(groupPosition, childPosition) val text1 = view.findViewById(android.R.id.text1) if (location.parentId == WORLDWIDE) { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/SimpleParcelableUsersAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/SimpleParcelableUsersAdapter.kt index 17115f183..5a9f32b82 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/SimpleParcelableUsersAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/SimpleParcelableUsersAdapter.kt @@ -36,11 +36,7 @@ class SimpleParcelableUsersAdapter( override fun getItemId(position: Int): Long { val item = getItem(position) - return if (item != null) { - item.hashCode().toLong() - } else { - -1 - } + return item?.hashCode()?.toLong() ?: -1 } override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/CredentialsExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/CredentialsExtensions.kt index 8e51cf3ca..786a0a574 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/CredentialsExtensions.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/CredentialsExtensions.kt @@ -63,9 +63,8 @@ fun Credentials.getAuthorization(cls: Class<*>?): Authorization { } fun Credentials.getEndpoint(cls: Class<*>): Endpoint { - val apiUrlFormat: String val noVersionSuffix = this.no_version_suffix - apiUrlFormat = if (!TextUtils.isEmpty(this.api_url_format)) { + val apiUrlFormat: String = if (!TextUtils.isEmpty(this.api_url_format)) { this.api_url_format } else { DEFAULT_TWITTER_API_URL_FORMAT @@ -113,8 +112,7 @@ fun Credentials.getEndpoint(cls: Class<*>): Endpoint { } val endpointUrl = MicroBlogAPIFactory.getApiUrl(apiUrlFormat, domain, versionSuffix) if (this is OAuthCredentials) { - val signEndpointUrl: String - signEndpointUrl = if (same_oauth_signing_url) { + val signEndpointUrl: String = if (same_oauth_signing_url) { endpointUrl } else { MicroBlogAPIFactory.getApiUrl(DEFAULT_TWITTER_API_URL_FORMAT, domain, versionSuffix) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/ColorPickerDialogFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/ColorPickerDialogFragment.kt index 0af296ceb..7633e296b 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/ColorPickerDialogFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/ColorPickerDialogFragment.kt @@ -61,11 +61,8 @@ class ColorPickerDialogFragment : BaseDialogFragment(), DialogInterface.OnClickL override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val color: Int val args = arguments - color = if (savedInstanceState != null) { - savedInstanceState.getInt(EXTRA_COLOR, Color.WHITE) - } else { - args!!.getInt(EXTRA_COLOR, Color.WHITE) - } + color = + savedInstanceState?.getInt(EXTRA_COLOR, Color.WHITE) ?: args!!.getInt(EXTRA_COLOR, Color.WHITE) val activity = activity val builder = AlertDialog.Builder(requireActivity()) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/TrendsSuggestionsFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/TrendsSuggestionsFragment.kt index 1f97a0441..51018938a 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/TrendsSuggestionsFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/TrendsSuggestionsFragment.kt @@ -86,8 +86,7 @@ class TrendsSuggestionsFragment : AbsContentListViewFragment(), L override fun onItemClick(view: AdapterView<*>, child: View, position: Int, id: Long) { if (multiSelectManager.isActive) return - val trend: String? - trend = if (view is ListView) { + val trend: String? = if (view is ListView) { adapter.getItem(position - view.headerViewsCount) } else { adapter.getItem(position) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/model/AccountPreferences.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/model/AccountPreferences.kt index c197f680a..cd3255b18 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/model/AccountPreferences.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/model/AccountPreferences.kt @@ -44,11 +44,7 @@ class AccountPreferences( val defaultNotificationLightColor: Int get() { val a = AccountUtils.getAccountDetails(AccountManager.get(context), accountKey, true) - return if (a != null) { - a.color - } else { - ContextCompat.getColor(context, R.color.branding_color) - } + return a?.color ?: ContextCompat.getColor(context, R.color.branding_color) } val directMessagesNotificationType: Int diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/provider/CacheProvider.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/provider/CacheProvider.kt index 7a26a2ea2..c2399bcfe 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/provider/CacheProvider.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/provider/CacheProvider.kt @@ -167,8 +167,7 @@ class CacheProvider : ContentProvider() { * Copied from ContentResolver.java */ private fun modeToMode(mode: String): Int { - val modeBits: Int - modeBits = if ("r" == mode) { + val modeBits: Int = if ("r" == mode) { ParcelFileDescriptor.MODE_READ_ONLY } else if ("w" == mode || "wt" == mode) { ParcelFileDescriptor.MODE_WRITE_ONLY or ParcelFileDescriptor.MODE_CREATE or ParcelFileDescriptor.MODE_TRUNCATE diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/service/LengthyOperationsService.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/service/LengthyOperationsService.kt index 3a4da1cbc..92e1292ed 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/service/LengthyOperationsService.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/service/LengthyOperationsService.kt @@ -216,15 +216,13 @@ class LengthyOperationsService : BaseIntentService("lengthy_operations") { private fun handleUpdateStatusIntent(intent: Intent) { val status = intent.getParcelableExtra(EXTRA_STATUS) - val statusParcelables = intent.getNullableTypedArrayExtra(EXTRA_STATUSES) val scheduleInfo = intent.getParcelableExtra(EXTRA_SCHEDULE_INFO) val statuses: Array - statuses = if (statusParcelables != null) { - statusParcelables - } else if (status != null) { - arrayOf(status) - } else - return + statuses = intent.getNullableTypedArrayExtra(EXTRA_STATUSES) + ?: if (status != null) { + arrayOf(status) + } else + return @Draft.Action val actionType = intent.getStringExtra(EXTRA_ACTION) statuses.forEach { it.draft_action = actionType } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetStatusesTask.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetStatusesTask.kt index ea2e4ea37..477c4f41b 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetStatusesTask.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetStatusesTask.kt @@ -228,8 +228,7 @@ abstract class GetStatusesTask( fun getPositionKey(timestamp: Long, sortId: Long, lastSortId: Long, sortDiff: Long, position: Int, count: Int): Long { if (sortDiff == 0L) return timestamp - val extraValue: Int - extraValue = if (sortDiff > 0) { + val extraValue: Int = if (sortDiff > 0) { // descent sorted by time count - 1 - position } else { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/DataStoreUtils.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/DataStoreUtils.kt index 9caec94ec..2a9179f84 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/DataStoreUtils.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/DataStoreUtils.kt @@ -309,8 +309,7 @@ object DataStoreUtils { } fun getAccountDisplayName(context: Context, accountKey: UserKey, nameFirst: Boolean): String? { - val name: String? - name = if (nameFirst) { + val name: String? = if (nameFirst) { getAccountName(context, accountKey) } else { "@${getAccountScreenName(context, accountKey)}" diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/ExternalThemeManager.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/ExternalThemeManager.kt index 892122798..a9fda06f3 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/ExternalThemeManager.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/ExternalThemeManager.kt @@ -46,12 +46,7 @@ class ExternalThemeManager(private val context: Context, private val preferences fun reloadEmojiPreferences() { val emojiComponentName = preferences.getString(KEY_EMOJI_SUPPORT, null) emojiPackageName = if (emojiComponentName != null) { - val componentName = ComponentName.unflattenFromString(emojiComponentName) - if (componentName != null) { - componentName.packageName - } else { - null - } + ComponentName.unflattenFromString(emojiComponentName)?.packageName } else { null } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/MenuUtils.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/MenuUtils.kt index 0080bffe9..0adf8b253 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/MenuUtils.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/MenuUtils.kt @@ -124,8 +124,8 @@ object MenuUtils { val retweetHighlight = ContextCompat.getColor(context, R.color.highlight_retweet) val favoriteHighlight = ContextCompat.getColor(context, R.color.highlight_favorite) val likeHighlight = ContextCompat.getColor(context, R.color.highlight_like) - val isMyRetweet: Boolean - isMyRetweet = if (RetweetStatusTask.isCreatingRetweet(status.account_key, status.id)) { + val isMyRetweet: Boolean = + if (RetweetStatusTask.isCreatingRetweet(status.account_key, status.id)) { true } else if (twitter.isDestroyingStatus(status.account_key, status.id)) { false @@ -164,8 +164,8 @@ object MenuUtils { } val favorite = menu.findItem(R.id.favorite) if (favorite != null) { - val isFavorite: Boolean - isFavorite = if (CreateFavoriteTask.isCreatingFavorite(status.account_key, status.id)) { + val isFavorite: Boolean = + if (CreateFavoriteTask.isCreatingFavorite(status.account_key, status.id)) { true } else if (DestroyFavoriteTask.isDestroyingFavorite(status.account_key, status.id)) { false diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/Utils.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/Utils.kt index 092299ded..b1b8eba69 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/Utils.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/Utils.kt @@ -456,8 +456,7 @@ object Utils { } fun getInsetsTopWithoutActionBarHeight(context: Context, top: Int): Int { - val actionBarHeight: Int - actionBarHeight = if (context is AppCompatActivity) { + val actionBarHeight: Int = if (context is AppCompatActivity) { getActionBarHeight(context.supportActionBar) } else if (context is Activity) { getActionBarHeight(context.actionBar) @@ -576,9 +575,8 @@ object Utils { * @param message String */ fun sendPebbleNotification(context: Context, title: String?, message: String) { - val appName: String - appName = if (title == null) { + val appName: String = if (title == null) { context.getString(R.string.app_name) } else { "${context.getString(R.string.app_name)} - $title" diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/net/TwidereDns.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/net/TwidereDns.kt index 91ef23304..46f073b11 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/net/TwidereDns.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/net/TwidereDns.kt @@ -303,8 +303,7 @@ class TwidereDns(val context: Context, private val preferences: SharedPreference @Throws(UnknownHostException::class) private fun addrFromRecord(name: String, r: Record): InetAddress { - val addr: InetAddress - addr = if (r is ARecord) { + val addr: InetAddress = if (r is ARecord) { r.address } else { (r as AAAARecord).address diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/theme/ThemeFunctions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/theme/ThemeFunctions.kt index 13343511c..c6e05b687 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/theme/ThemeFunctions.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/theme/ThemeFunctions.kt @@ -12,8 +12,7 @@ import org.mariotaku.twidere.util.ThemeUtils */ fun getCurrentThemeResource(context: Context, theme: String, fromThemeResource: Int = 0): Int { - val a: TypedArray - a = if (fromThemeResource == 0) { + val a: TypedArray = if (fromThemeResource == 0) { context.obtainStyledAttributes(R.styleable.TwidereTheme) } else { context.obtainStyledAttributes(fromThemeResource, R.styleable.TwidereTheme) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/view/ActionIconThemedTextView.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/view/ActionIconThemedTextView.kt index bf74f314e..c20f144e7 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/view/ActionIconThemedTextView.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/view/ActionIconThemedTextView.kt @@ -97,8 +97,7 @@ class ActionIconThemedTextView( for (d in TextViewSupport.getCompoundDrawablesRelative(this)) { if (d == null) continue d.mutate() - val color: Int - color = if (isActivated) { + val color: Int = if (isActivated) { activatedColor } else if (isEnabled) { defaultColor diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/view/controller/premium/PromotionOfferViewController.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/view/controller/premium/PromotionOfferViewController.kt index 7f77a1d0a..10cbd8b8e 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/view/controller/premium/PromotionOfferViewController.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/view/controller/premium/PromotionOfferViewController.kt @@ -53,7 +53,6 @@ class PromotionOfferViewController : PremiumDashboardActivity.ExtraFeatureViewCo private fun enablePromotions() { preferences[promotionsEnabledKey] = true - val activity = ChameleonUtils.getActivity(context) - activity?.recreate() + ChameleonUtils.getActivity(context)?.recreate() } } \ No newline at end of file diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/status/DetailStatusViewHolder.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/status/DetailStatusViewHolder.kt index ee20f557e..da647baeb 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/status/DetailStatusViewHolder.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/status/DetailStatusViewHolder.kt @@ -202,9 +202,7 @@ class DetailStatusViewHolder( itemView.profileContainer.drawStart(colorNameManager.getUserColor(status.user_key)) - val timestamp: Long - - timestamp = if (status.is_retweet) { + val timestamp: Long = if (status.is_retweet) { status.retweet_timestamp } else { status.timestamp @@ -736,8 +734,7 @@ class DetailStatusViewHolder( } fun displayCount(count: LabeledCount, hideNumbers: Boolean) { - val label: String - label = when (count.type) { + val label: String = when (count.type) { KEY_REPLY_COUNT -> { adapter.context.getString(R.string.replies) }