1
0
mirror of https://github.com/TwidereProject/Twidere-Android synced 2025-02-08 15:58:40 +01:00

Optimize variable declarations

This commit is contained in:
TacoTheDank 2020-06-08 17:09:07 -04:00
parent 50804ff533
commit a9f5129d95
17 changed files with 22 additions and 44 deletions

View File

@ -13,8 +13,7 @@ class AssistLauncherActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val prefs = getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE)
val composeNowAction = prefs.getString(KEY_COMPOSE_NOW_ACTION, VALUE_COMPOSE_NOW_ACTION_COMPOSE)
val action = when (composeNowAction) {
val action = when (prefs.getString(KEY_COMPOSE_NOW_ACTION, VALUE_COMPOSE_NOW_ACTION_COMPOSE)) {
VALUE_COMPOSE_NOW_ACTION_TAKE_PHOTO -> INTENT_ACTION_COMPOSE_TAKE_PHOTO
VALUE_COMPOSE_NOW_ACTION_PICK_IMAGE -> INTENT_ACTION_COMPOSE_PICK_IMAGE
else -> INTENT_ACTION_COMPOSE

View File

@ -807,8 +807,7 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
private fun extensionIntentItemSelected(item: MenuItem) {
val intent = item.intent ?: return
try {
val action = intent.action
when (action) {
when (intent.action) {
INTENT_ACTION_EXTENSION_COMPOSE -> {
val accountKeys = accountsAdapter.selectedAccountKeys
intent.putExtra(EXTRA_TEXT, ParseUtils.parseString(editText.text))

View File

@ -499,8 +499,7 @@ class MediaViewerActivity : BaseActivity(), IMediaViewerActivity, MediaSwipeClos
private fun saveToStorage() {
val fileInfo = getCurrentCacheFileInfo(saveToStoragePosition) ?: return
val type = (fileInfo as? CacheProvider.CacheFileTypeSupport)?.cacheFileType
val pubDir = when (type) {
val pubDir = when ((fileInfo as? CacheProvider.CacheFileTypeSupport)?.cacheFileType) {
CacheFileType.VIDEO -> {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)

View File

@ -95,8 +95,7 @@ class UserListSelectorActivity : BaseActivity(),
listView.setOnScrollListener(handler)
listView.setOnTouchListener(handler.touchListener)
listView.onItemClickListener = OnItemClickListener { view, _, position, _ ->
val item = view.getItemAtPosition(position)
when (item) {
when (val item = view.getItemAtPosition(position)) {
is ParcelableUserList -> {
val data = Intent()
data.putExtra(EXTRA_USER_LIST, item)

View File

@ -150,8 +150,7 @@ class MessagesConversationAdapter(
}
override fun getItemViewType(position: Int): Int {
val countIndex = itemCounts.getItemCountIndex(position)
when (countIndex) {
when (val countIndex = itemCounts.getItemCountIndex(position)) {
ITEM_START_MESSAGE -> when (getMessage(position, reuse = true).message_type) {
MessageType.STICKER -> {
return ITEM_TYPE_STICKER_MESSAGE

View File

@ -77,8 +77,7 @@ class MessagesEntriesAdapter(
}
override fun getItemViewType(position: Int): Int {
val countIndex = itemCounts.getItemCountIndex(position)
when (countIndex) {
when (val countIndex = itemCounts.getItemCountIndex(position)) {
0 -> return ITEM_TYPE_MESSAGE_ENTRY
1 -> return ITEM_VIEW_TYPE_LOAD_INDICATOR
else -> throw UnsupportedCountIndexException(countIndex, position)

View File

@ -152,8 +152,7 @@ class ParcelableActivitiesAdapter(
}
override fun getItemId(position: Int): Long {
val countIndex = itemCounts.getItemCountIndex(position)
when (countIndex) {
when (val countIndex = itemCounts.getItemCountIndex(position)) {
ITEM_INDEX_ACTIVITY -> {
return getRowId(position, false)
}
@ -245,14 +244,12 @@ class ParcelableActivitiesAdapter(
}
override fun getItemViewType(position: Int): Int {
val countIndex = getItemCountIndex(position)
when (countIndex) {
when (val countIndex = getItemCountIndex(position)) {
ITEM_INDEX_ACTIVITY -> {
if (isGapItem(position)) {
return ITEM_VIEW_TYPE_GAP
}
val action = getAction(position)
when (action) {
when (getAction(position)) {
Activity.Action.MENTION, Activity.Action.QUOTE, Activity.Action.REPLY -> {
return ITEM_VIEW_TYPE_STATUS
}

View File

@ -350,8 +350,7 @@ abstract class ParcelableStatusesAdapter(
if (position == 0 && ILoadMoreSupportAdapter.START in loadMoreIndicatorPosition) {
return ITEM_VIEW_TYPE_LOAD_INDICATOR
}
val countIndex = getItemCountIndex(position)
when (countIndex) {
when (val countIndex = getItemCountIndex(position)) {
ITEM_INDEX_LOAD_START_INDICATOR, ITEM_INDEX_LOAD_END_INDICATOR -> {
return ITEM_VIEW_TYPE_LOAD_INDICATOR
}

View File

@ -89,8 +89,7 @@ class SelectableUsersAdapter(
}
override fun getItemViewType(position: Int): Int {
val countIndex = itemCounts.getItemCountIndex(position)
when (countIndex) {
when (val countIndex = itemCounts.getItemCountIndex(position)) {
ITEM_TYPE_START_INDICATOR, ITEM_TYPE_END_INDICATOR -> return ITEM_VIEW_TYPE_LOAD_INDICATOR
ITEM_TYPE_USER -> return ITEM_VIEW_TYPE_USER
else -> throw UnsupportedCountIndexException(countIndex, position)
@ -99,8 +98,7 @@ class SelectableUsersAdapter(
}
override fun getItemId(position: Int): Long {
val countIndex = itemCounts.getItemCountIndex(position)
return when (countIndex) {
return when (val countIndex = itemCounts.getItemCountIndex(position)) {
ITEM_TYPE_START_INDICATOR, ITEM_TYPE_END_INDICATOR -> (countIndex.toLong() shl 32)
ITEM_TYPE_USER -> (countIndex.toLong() shl 32) or getUser(position).hashCode().toLong()
else -> throw UnsupportedCountIndexException(countIndex, position)

View File

@ -583,8 +583,7 @@ class MessageConversationInfoFragment : BaseFragment(), IToolBarSupportFragment,
}
override fun getItemViewType(position: Int): Int {
val countIndex = itemCounts.getItemCountIndex(position)
when (countIndex) {
when (val countIndex = itemCounts.getItemCountIndex(position)) {
ITEM_INDEX_TOP_SPACE -> return VIEW_TYPE_TOP_SPACE
ITEM_INDEX_HEADER -> return VIEW_TYPE_HEADER
ITEM_INDEX_ITEM -> return VIEW_TYPE_USER
@ -595,8 +594,7 @@ class MessageConversationInfoFragment : BaseFragment(), IToolBarSupportFragment,
}
override fun getItemId(position: Int): Long {
val countIndex = itemCounts.getItemCountIndex(position)
when (countIndex) {
when (val countIndex = itemCounts.getItemCountIndex(position)) {
ITEM_INDEX_ITEM -> {
val user = getUser(position)!!
return (countIndex.toLong() shl 32) or user.hashCode().toLong()
@ -805,8 +803,7 @@ class MessageConversationInfoFragment : BaseFragment(), IToolBarSupportFragment,
val position = parent.getChildLayoutPosition(view)
if (position < 0) return
val itemCounts = adapter.itemCounts
val countIndex = itemCounts.getItemCountIndex(position)
when (countIndex) {
when (val countIndex = itemCounts.getItemCountIndex(position)) {
ConversationInfoAdapter.ITEM_INDEX_TOP_SPACE,
ConversationInfoAdapter.ITEM_INDEX_SPACE,
ConversationInfoAdapter.ITEM_INDEX_ADD_USER -> {

View File

@ -943,8 +943,7 @@ class StatusFragment : BaseFragment(), LoaderCallbacks<SingleResponse<Parcelable
override fun isDividerEnabled(childPos: Int): Boolean {
if (childPos >= statusAdapter.itemCount || childPos < 0) return false
val itemType = statusAdapter.getItemType(childPos)
when (itemType) {
when (statusAdapter.getItemType(childPos)) {
StatusDetailsAdapter.ITEM_IDX_REPLY_LOAD_MORE, StatusDetailsAdapter.ITEM_IDX_REPLY_ERROR,
StatusDetailsAdapter.ITEM_IDX_SPACE -> return false
}

View File

@ -197,8 +197,7 @@ abstract class AbsRequestStatusesLoader(
}
protected open fun List<ParcelableStatus>.foundInPagination(): Boolean {
val pagination = this@AbsRequestStatusesLoader.pagination
return when (pagination) {
return when (val pagination = this@AbsRequestStatusesLoader.pagination) {
is SinceMaxPagination -> return any { it.id == pagination.maxId }
else -> false
}

View File

@ -39,8 +39,7 @@ data class UpdateStatus(
is UpdateStatusTask.UploaderNotFoundException ->
return "extension not found"
else -> {
val cause = ex.cause
when (cause) {
when (val cause = ex.cause) {
is UpdateStatusTask.ExtensionVersionMismatchException ->
return "extension version mismatch"
is IOException ->

View File

@ -25,8 +25,7 @@ object ParcelableStatusUpdateUtils {
statusUpdate.text = draft.text
statusUpdate.location = draft.location
statusUpdate.media = draft.media
val actionExtras = draft.action_extras
when (actionExtras) {
when (val actionExtras = draft.action_extras) {
is UpdateStatusActionExtras -> {
statusUpdate.in_reply_to_status = actionExtras.inReplyToStatus
statusUpdate.is_possibly_sensitive = actionExtras.isPossiblySensitive

View File

@ -46,8 +46,7 @@ class CacheProvider : ContentProvider() {
if (metadata != null) {
return metadata.contentType
}
val type = uri.getQueryParameter(QUERY_PARAM_TYPE)
when (type) {
when (uri.getQueryParameter(QUERY_PARAM_TYPE)) {
CacheFileType.IMAGE -> {
val file = fileCache.get(getCacheKey(uri)) ?: return null
return BitmapFactory.Options().apply {

View File

@ -343,8 +343,7 @@ class TwidereDataProvider : ContentProvider(), LazyLoadCallback {
}
private fun deleteInternal(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int {
val tableId = DataStoreUtils.getTableId(uri)
when (tableId) {
when (val tableId = DataStoreUtils.getTableId(uri)) {
VIRTUAL_TABLE_ID_DRAFTS_NOTIFICATIONS -> {
notificationManager.cancel(uri.toString(), NOTIFICATION_ID_DRAFTS)
return 1

View File

@ -92,8 +92,7 @@ abstract class AbsMessageViewHolder(itemView: View, val adapter: MessagesConvers
}
open fun setMessageContentGravity(view: View, outgoing: Boolean) {
val lp = view.layoutParams
when (lp) {
when (val lp = view.layoutParams) {
is FrameLayout.LayoutParams -> {
lp.gravity = if (outgoing) GravityCompat.END else GravityCompat.START
}