cleaned up code

This commit is contained in:
Mariotaku Lee 2016-12-13 08:45:14 +08:00
parent e3a28a1571
commit d137d8846d
1 changed files with 45 additions and 44 deletions

View File

@ -107,10 +107,10 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
@Inject
lateinit var defaultFeatures: DefaultFeatures
private var locationManager: LocationManager? = null
private var currentTask: AsyncTask<Any, Any, *>? = null
private val supportMenuInflater by lazy { SupportMenuInflater(this) }
private lateinit var locationManager: LocationManager
private lateinit var itemTouchHelper: ItemTouchHelper
private val supportMenuInflater by lazy { SupportMenuInflater(this) }
private var currentTask: AsyncTask<Any, Any, *>? = null
private val backTimeoutRunnable = Runnable { navigateBackPressed = false }
@ -177,8 +177,8 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
override fun onBackPressed() {
if (currentTask != null && currentTask!!.status == AsyncTask.Status.RUNNING) return
if (!shouldSkipDraft && hasComposingStatus() ) {
if (currentTask?.status == AsyncTask.Status.RUNNING) return
if (!shouldSkipDraft && hasComposingStatus()) {
saveToDrafts()
Toast.makeText(this, R.string.status_saved_to_draft, Toast.LENGTH_SHORT).show()
shouldSkipDraft = true
@ -198,7 +198,7 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
}
private fun discardTweet() {
if (isFinishing || currentTask != null && currentTask!!.status == AsyncTask.Status.RUNNING) return
if (isFinishing || currentTask?.status == AsyncTask.Status.RUNNING) return
currentTask = AsyncTaskUtils.executeTask(DiscardTweetTask(this))
}
@ -238,7 +238,7 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
saveAccountSelection()
try {
if (locationListener != null) {
locationManager!!.removeUpdates(locationListener)
locationManager.removeUpdates(locationListener)
locationListener = null
}
} catch (ignore: SecurityException) {
@ -344,10 +344,10 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
intent.putExtra(EXTRA_NAME, DataStoreUtils.getAccountName(this, accountKey))
intent.putExtra(EXTRA_SCREEN_NAME, DataStoreUtils.getAccountScreenName(this, accountKey))
}
if (inReplyToStatus != null) {
intent.putExtra(EXTRA_IN_REPLY_TO_ID, inReplyToStatus!!.id)
intent.putExtra(EXTRA_IN_REPLY_TO_NAME, inReplyToStatus!!.user_name)
intent.putExtra(EXTRA_IN_REPLY_TO_SCREEN_NAME, inReplyToStatus!!.user_screen_name)
inReplyToStatus?.let {
intent.putExtra(EXTRA_IN_REPLY_TO_ID, it.id)
intent.putExtra(EXTRA_IN_REPLY_TO_NAME, it.user_name)
intent.putExtra(EXTRA_IN_REPLY_TO_SCREEN_NAME, it.user_screen_name)
}
startActivityForResult(intent, REQUEST_EXTENSION_COMPOSE)
} else if (INTENT_ACTION_EXTENSION_EDIT_IMAGE == action) {
@ -510,7 +510,7 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
LOCATION_VALUE_COORDINATE -> {
attachLocationChecked = true
attachPreciseLocationChecked = true
locationText!!.tag = null
locationText.tag = null
}
LOCATION_VALUE_PLACE -> {
attachLocationChecked = true
@ -525,7 +525,7 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
requestOrUpdateLocation()
} else if (locationListener != null) {
try {
locationManager!!.removeUpdates(locationListener)
locationManager.removeUpdates(locationListener)
locationListener = null
} catch (e: SecurityException) {
//Ignore
@ -841,10 +841,9 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
addMedia(Arrays.asList(*draft.media))
}
recentLocation = draft.location
if (draft.action_extras is UpdateStatusActionExtras) {
val extra = draft.action_extras as UpdateStatusActionExtras?
possiblySensitive = extra!!.isPossiblySensitive
inReplyToStatus = extra.inReplyToStatus
(draft.action_extras as? UpdateStatusActionExtras)?.let {
possiblySensitive = it.isPossiblySensitive
inReplyToStatus = it.inReplyToStatus
}
return true
}
@ -966,7 +965,7 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
val replyToName = userColorNameManager.getDisplayName(status, nameFirst)
replyLabel.text = getString(R.string.quote_name_text, replyToName, status.text_unescaped)
replyLabel.visibility = View.VISIBLE
replyLabelDivider!!.visibility = View.VISIBLE
replyLabelDivider.visibility = View.VISIBLE
}
private fun showReplyLabel(status: ParcelableStatus?) {
@ -977,12 +976,12 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
val replyToName = userColorNameManager.getDisplayName(status, nameFirst)
replyLabel.text = getString(R.string.reply_to_name_text, replyToName, status.text_unescaped)
replyLabel.visibility = View.VISIBLE
replyLabelDivider!!.visibility = View.VISIBLE
replyLabelDivider.visibility = View.VISIBLE
}
private fun hideLabel() {
replyLabel.visibility = View.GONE
replyLabelDivider!!.visibility = View.GONE
replyLabelDivider.visibility = View.GONE
}
private fun handleReplyIntent(status: ParcelableStatus?): Boolean {
@ -1170,9 +1169,9 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
if (location != null) {
val attachPreciseLocation = preferences.getBoolean(KEY_ATTACH_PRECISE_LOCATION)
if (attachPreciseLocation) {
locationText!!.text = ParcelableLocationUtils.getHumanReadableString(location, 3)
locationText.text = ParcelableLocationUtils.getHumanReadableString(location, 3)
} else {
if (locationText!!.tag == null || location != recentLocation) {
if (locationText.tag == null || location != recentLocation) {
val task = DisplayPlaceNameTask(this)
task.params = location
task.callback = locationText
@ -1180,7 +1179,7 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
}
}
} else {
locationText!!.setText(R.string.unknown_location)
locationText.setText(R.string.unknown_location)
}
recentLocation = location
}
@ -1204,14 +1203,14 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
} else {
criteria.accuracy = Criteria.ACCURACY_COARSE
}
val provider = locationManager!!.getBestProvider(criteria, true)
val provider = locationManager.getBestProvider(criteria, true)
if (provider != null) {
locationText!!.setText(R.string.getting_location)
locationText.setText(R.string.getting_location)
locationListener = ComposeLocationListener(this)
locationManager!!.requestLocationUpdates(provider, 0, 0f, locationListener)
locationManager.requestLocationUpdates(provider, 0, 0f, locationListener)
val location = Utils.getCachedLocation(this)
if (location != null) {
locationListener!!.onLocationChanged(location)
locationListener?.onLocationChanged(location)
}
} else {
Toast.makeText(this, R.string.cannot_get_location, Toast.LENGTH_SHORT).show()
@ -1241,13 +1240,13 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
private fun updateLocationState() {
val attachLocation = preferences.getBoolean(KEY_ATTACH_LOCATION)
locationIcon!!.isActivated = attachLocation
locationIcon.isActivated = attachLocation
if (!attachLocation) {
locationText!!.setText(R.string.no_location)
locationText.setText(R.string.no_location)
} else if (recentLocation != null) {
setRecentLocation(recentLocation)
} else {
locationText!!.setText(R.string.getting_location)
locationText.setText(R.string.getting_location)
}
}
@ -1628,16 +1627,17 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
}
override fun afterExecute(textView: TextView?, addresses: List<Address>?) {
textView!!
val preferences = context.preferences
val attachLocation = preferences.getBoolean(KEY_ATTACH_LOCATION)
val attachPreciseLocation = preferences.getBoolean(KEY_ATTACH_PRECISE_LOCATION)
if (attachLocation) {
if (attachPreciseLocation) {
val location = params
textView!!.text = ParcelableLocationUtils.getHumanReadableString(location, 3)
textView.text = ParcelableLocationUtils.getHumanReadableString(location, 3)
textView.tag = location
} else if (addresses == null || addresses.isEmpty()) {
val tag = textView!!.tag
val tag = textView.tag
if (tag is Address) {
textView.text = tag.locality
} else {
@ -1646,18 +1646,21 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
}
} else {
val address = addresses[0]
textView!!.tag = address
textView.tag = address
textView.text = address.locality
}
} else {
textView!!.setText(R.string.no_location)
textView.setText(R.string.no_location)
}
}
internal class NoAddress
}
internal class MediaPreviewAdapter(activity: ComposeActivity, val mDragStartListener: SimpleItemTouchHelperCallback.OnStartDragListener) : ArrayRecyclerAdapter<ParcelableMediaUpdate, MediaPreviewViewHolder>(activity), SimpleItemTouchHelperCallback.ItemTouchHelperAdapter {
internal class MediaPreviewAdapter(
activity: ComposeActivity,
val dragStartListener: SimpleItemTouchHelperCallback.OnStartDragListener
) : ArrayRecyclerAdapter<ParcelableMediaUpdate, MediaPreviewViewHolder>(activity), SimpleItemTouchHelperCallback.ItemTouchHelperAdapter {
val inflater: LayoutInflater
init {
@ -1666,7 +1669,7 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
}
fun onStartDrag(viewHolder: ViewHolder) {
mDragStartListener.onStartDrag(viewHolder)
dragStartListener.onStartDrag(viewHolder)
}
val asList: List<ParcelableMediaUpdate>
@ -1686,13 +1689,13 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
return MediaPreviewViewHolder(view)
}
override fun onViewAttachedToWindow(holder: MediaPreviewViewHolder?) {
override fun onViewAttachedToWindow(holder: MediaPreviewViewHolder) {
super.onViewAttachedToWindow(holder)
holder!!.adapter = this
holder.adapter = this
}
override fun onViewDetachedFromWindow(holder: MediaPreviewViewHolder?) {
holder!!.adapter = null
override fun onViewDetachedFromWindow(holder: MediaPreviewViewHolder) {
holder.adapter = null
super.onViewDetachedFromWindow(holder)
}
@ -1743,16 +1746,14 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
}
override fun onLongClick(v: View): Boolean {
if (adapter == null) return false
adapter!!.onStartDrag(this)
adapter?.onStartDrag(this)
return false
}
override fun onClick(v: View) {
if (adapter == null) return
when (v.id) {
R.id.remove -> {
adapter!!.remove(layoutPosition)
adapter?.remove(layoutPosition)
}
R.id.edit -> {
itemView.parent.showContextMenuForChild(itemView)