Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/fragment/AbsStatusesFragment.kt

622 lines
26 KiB
Kotlin
Raw Normal View History

2016-06-29 15:47:52 +02:00
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.fragment
2016-12-18 06:21:24 +01:00
import android.accounts.AccountManager
import android.app.Activity
2016-06-29 15:47:52 +02:00
import android.content.Context
import android.content.Intent
import android.graphics.Rect
import android.os.Bundle
import android.support.v4.app.Fragment
2016-06-29 15:47:52 +02:00
import android.support.v4.app.LoaderManager.LoaderCallbacks
import android.support.v4.content.Loader
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.RecyclerView.OnScrollListener
import android.view.*
import com.squareup.otto.Subscribe
import edu.tsinghua.hotmobi.HotMobiLogger
import edu.tsinghua.hotmobi.model.MediaEvent
import kotlinx.android.synthetic.main.fragment_content_recyclerview.*
2016-12-16 01:38:37 +01:00
import org.mariotaku.kpreferences.get
import org.mariotaku.ktextension.*
2016-06-29 15:47:52 +02:00
import org.mariotaku.twidere.R
import org.mariotaku.twidere.TwidereConstants
import org.mariotaku.twidere.activity.AccountSelectorActivity
2016-06-29 15:47:52 +02:00
import org.mariotaku.twidere.adapter.ParcelableStatusesAdapter
import org.mariotaku.twidere.adapter.decorator.DividerItemDecoration
import org.mariotaku.twidere.adapter.iface.ILoadMoreSupportAdapter
import org.mariotaku.twidere.annotation.ReadPositionTag
import org.mariotaku.twidere.annotation.Referral
import org.mariotaku.twidere.constant.*
2016-08-19 16:25:27 +02:00
import org.mariotaku.twidere.constant.IntentConstants.*
import org.mariotaku.twidere.constant.KeyboardShortcutConstants.*
2016-12-29 06:50:18 +01:00
import org.mariotaku.twidere.extension.model.getAccountType
2016-06-29 15:47:52 +02:00
import org.mariotaku.twidere.graphic.like.LikeAnimationDrawable
import org.mariotaku.twidere.loader.iface.IExtendedLoader
import org.mariotaku.twidere.model.*
2016-12-18 06:21:24 +01:00
import org.mariotaku.twidere.model.analyzer.Share
2017-02-09 09:00:12 +01:00
import org.mariotaku.twidere.model.event.StatusListChangedEvent
2016-12-18 06:21:24 +01:00
import org.mariotaku.twidere.model.util.AccountUtils
2016-06-29 15:47:52 +02:00
import org.mariotaku.twidere.util.*
import org.mariotaku.twidere.util.KeyboardShortcutsHandler.KeyboardShortcutCallback
import org.mariotaku.twidere.util.imageloader.PauseRecyclerViewOnScrollListener
import org.mariotaku.twidere.view.ExtendedRecyclerView
import org.mariotaku.twidere.view.holder.GapViewHolder
import org.mariotaku.twidere.view.holder.StatusViewHolder
import org.mariotaku.twidere.view.holder.iface.IStatusViewHolder
/**
* Created by mariotaku on 14/11/5.
*/
abstract class AbsStatusesFragment : AbsContentListRecyclerViewFragment<ParcelableStatusesAdapter>(),
2016-08-19 16:25:27 +02:00
LoaderCallbacks<List<ParcelableStatus>?>, IStatusViewHolder.StatusClickListener,
KeyboardShortcutCallback {
2016-06-29 15:47:52 +02:00
2016-12-16 01:38:37 +01:00
private lateinit var statusesBusCallback: Any
private lateinit var navigationHelper: RecyclerViewNavigationHelper
private var pauseOnScrollListener: OnScrollListener? = null
var loaderInitialized: Boolean = false
private set
2016-06-29 15:47:52 +02:00
private val onScrollListener = object : OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
val layoutManager = layoutManager
2016-09-01 04:43:59 +02:00
saveReadPosition(layoutManager.findFirstVisibleItemPosition())
2016-06-29 15:47:52 +02:00
}
}
}
2016-08-19 16:25:27 +02:00
protected abstract val timelineType: String
protected abstract val accountKeys: Array<UserKey>
protected var adapterData: List<ParcelableStatus>?
get() = adapter.getData()
2016-08-19 16:25:27 +02:00
set(data) {
adapter.setData(data)
2016-08-19 16:25:27 +02:00
}
@ReadPositionTag
2016-08-19 16:25:27 +02:00
protected open val readPositionTag: String?
get() = null
2016-08-19 16:25:27 +02:00
protected open val readPositionTagWithArguments: String?
get() = readPositionTag
protected open val useSortIdAsReadPosition: Boolean = true
/**
* Used for 'restore position' feature
*/
protected open val currentReadPositionTag: String?
get() = if (readPositionTag == null || tabId < 0) null else "${readPositionTag}_${tabId}_current"
2016-08-19 16:25:27 +02:00
override val extraContentPadding: Rect
get() {
val paddingVertical = resources.getDimensionPixelSize(R.dimen.element_spacing_small)
return Rect(0, paddingVertical, 0, paddingVertical)
}
val shouldInitLoader: Boolean
get() = (parentFragment as? StatusesFragmentDelegate)?.shouldInitLoader ?: true
2016-06-29 15:47:52 +02:00
2016-08-19 16:25:27 +02:00
// Fragment life cycles
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
2016-12-16 01:38:37 +01:00
statusesBusCallback = createMessageBusCallback()
scrollListener.reversed = preferences[readFromBottomKey]
2016-08-19 16:25:27 +02:00
adapter.statusClickListener = this
registerForContextMenu(recyclerView)
navigationHelper = RecyclerViewNavigationHelper(recyclerView, layoutManager, adapter, this)
2016-08-19 16:25:27 +02:00
pauseOnScrollListener = PauseRecyclerViewOnScrollListener(adapter.mediaLoader.imageLoader, false, true)
if (shouldInitLoader) {
initLoaderIfNeeded()
}
showProgress()
}
override fun onStart() {
super.onStart()
recyclerView.addOnScrollListener(onScrollListener)
recyclerView.addOnScrollListener(pauseOnScrollListener)
bus.register(statusesBusCallback)
}
override fun onStop() {
bus.unregister(statusesBusCallback)
recyclerView.removeOnScrollListener(pauseOnScrollListener)
recyclerView.removeOnScrollListener(onScrollListener)
if (userVisibleHint) {
saveReadPosition()
}
super.onStop()
}
override fun onDestroy() {
adapter.statusClickListener = null
2016-08-19 16:25:27 +02:00
super.onDestroy()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
REQUEST_FAVORITE_SELECT_ACCOUNT, REQUEST_RETWEET_SELECT_ACCOUNT -> {
handleActionActivityResult(this, requestCode, resultCode, data)
}
}
}
2016-06-29 15:47:52 +02:00
abstract fun getStatuses(param: RefreshTaskParam): Boolean
override fun handleKeyboardShortcutSingle(handler: KeyboardShortcutsHandler, keyCode: Int, event: KeyEvent, metaState: Int): Boolean {
2016-08-19 16:25:27 +02:00
var action = handler.getKeyAction(CONTEXT_TAG_NAVIGATION, keyCode, event, metaState)
if (ACTION_NAVIGATION_REFRESH == action) {
2016-06-29 15:47:52 +02:00
triggerRefresh()
return true
}
val focusedChild = RecyclerViewUtils.findRecyclerViewChild(recyclerView,
layoutManager.focusedChild)
var position = -1
if (focusedChild != null && focusedChild.parent === recyclerView) {
position = recyclerView.getChildLayoutPosition(focusedChild)
}
if (position != -1) {
val status = adapter.getStatus(position) ?: return false
2016-06-29 15:47:52 +02:00
if (keyCode == KeyEvent.KEYCODE_ENTER) {
IntentUtils.openStatus(activity, status, null)
return true
}
if (action == null) {
2016-08-19 16:25:27 +02:00
action = handler.getKeyAction(CONTEXT_TAG_STATUS, keyCode, event, metaState)
2016-06-29 15:47:52 +02:00
}
if (action == null) return false
when (action) {
2016-08-19 16:25:27 +02:00
ACTION_STATUS_REPLY -> {
val intent = Intent(INTENT_ACTION_REPLY)
intent.putExtra(EXTRA_STATUS, status)
2016-06-29 15:47:52 +02:00
startActivity(intent)
return true
}
2016-08-19 16:25:27 +02:00
ACTION_STATUS_RETWEET -> {
2016-08-18 15:02:49 +02:00
executeAfterFragmentResumed {
RetweetQuoteDialogFragment.show(fragmentManager, status)
}
2016-06-29 15:47:52 +02:00
return true
}
2016-08-19 16:25:27 +02:00
ACTION_STATUS_FAVORITE -> {
2016-06-29 15:47:52 +02:00
val twitter = twitterWrapper
if (status.is_favorite) {
twitter.destroyFavoriteAsync(status.account_key, status.id)
} else {
val holder = recyclerView.findViewHolderForLayoutPosition(position) as StatusViewHolder
holder.playLikeAnimation(DefaultOnLikedListener(twitter, status))
}
return true
}
}
}
2016-12-16 01:38:37 +01:00
return navigationHelper.handleKeyboardShortcutSingle(handler, keyCode, event, metaState)
2016-06-29 15:47:52 +02:00
}
override fun isKeyboardShortcutHandled(handler: KeyboardShortcutsHandler, keyCode: Int, event: KeyEvent, metaState: Int): Boolean {
2016-08-19 16:25:27 +02:00
var action = handler.getKeyAction(CONTEXT_TAG_NAVIGATION, keyCode, event, metaState)
if (ACTION_NAVIGATION_REFRESH == action) {
2016-06-29 15:47:52 +02:00
return true
}
if (action == null) {
2016-08-19 16:25:27 +02:00
action = handler.getKeyAction(CONTEXT_TAG_STATUS, keyCode, event, metaState)
2016-06-29 15:47:52 +02:00
}
if (action == null) return false
when (action) {
2016-08-19 16:25:27 +02:00
ACTION_STATUS_REPLY, ACTION_STATUS_RETWEET, ACTION_STATUS_FAVORITE -> return true
2016-06-29 15:47:52 +02:00
}
2016-12-16 01:38:37 +01:00
return navigationHelper.isKeyboardShortcutHandled(handler, keyCode, event, metaState)
2016-06-29 15:47:52 +02:00
}
override fun handleKeyboardShortcutRepeat(handler: KeyboardShortcutsHandler, keyCode: Int, repeatCount: Int,
2017-02-13 17:44:56 +01:00
event: KeyEvent, metaState: Int): Boolean {
2016-12-16 01:38:37 +01:00
return navigationHelper.handleKeyboardShortcutRepeat(handler, keyCode, repeatCount, event, metaState)
2016-06-29 15:47:52 +02:00
}
override fun onCreateLoader(id: Int, args: Bundle): Loader<List<ParcelableStatus>?> {
2016-08-19 16:25:27 +02:00
val fromUser = args.getBoolean(EXTRA_FROM_USER)
args.remove(EXTRA_FROM_USER)
2016-06-29 15:47:52 +02:00
return onCreateStatusesLoader(activity, args, fromUser)
}
2016-08-23 03:46:14 +02:00
override fun setUserVisibleHint(isVisibleToUser: Boolean) {
if (userVisibleHint && !isVisibleToUser && host != null) {
saveReadPosition()
}
super.setUserVisibleHint(isVisibleToUser)
}
/**
* Statuses loaded, update adapter data & restore load position
*
* Steps:
* 1. Save current read position if not first load (adapter data is not empty)
* 1.1 If readFromBottom is true, save position on screen bottom
* 2. Change adapter data
* 3. Restore adapter data
* 3.1 If lastVisible was last item, keep lastVisibleItem position (load more)
* 3.2 Else, if readFromBottom is true:
* 3.1.1 If position was first, keep lastVisibleItem position (pull refresh)
* 3.1.2 Else, keep lastVisibleItem position
* 3.2 If readFromBottom is false:
* 3.2.1 If position was first, set new position to 0 (pull refresh)
* 3.2.2 Else, keep firstVisibleItem position (gap clicked)
*/
2016-06-29 15:47:52 +02:00
override fun onLoadFinished(loader: Loader<List<ParcelableStatus>?>, data: List<ParcelableStatus>?) {
val rememberPosition = preferences[rememberPositionKey]
val readPositionTag = currentReadPositionTag
val readFromBottom = preferences[readFromBottomKey]
val firstLoad = adapterData.isNullOrEmpty()
var lastReadId: Long = -1
var lastReadViewTop: Int = 0
var loadMore = false
var wasAtTop = false
// 1. Save current read position if not first load
if (!firstLoad) {
val firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition()
val lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition()
wasAtTop = firstVisibleItemPosition == 0
val statusRange = rangeOfSize(adapter.statusStartIndex, adapter.statusCount - 1)
2017-02-02 15:20:14 +01:00
val lastReadPosition = if (loadMore || readFromBottom) {
lastVisibleItemPosition
} else {
firstVisibleItemPosition
2017-01-31 05:04:36 +01:00
}.coerceInOr(statusRange, -1)
lastReadId = if (useSortIdAsReadPosition) {
adapter.getStatusSortId(lastReadPosition)
} else {
adapter.getStatusPositionKey(lastReadPosition)
2016-06-29 15:47:52 +02:00
}
lastReadViewTop = layoutManager.findViewByPosition(lastReadPosition)?.top ?: 0
2017-01-31 05:04:36 +01:00
loadMore = statusRange.endInclusive >= 0 && lastVisibleItemPosition >= statusRange.endInclusive
} else if (rememberPosition && readPositionTag != null) {
lastReadId = readStateManager.getPosition(readPositionTag)
lastReadViewTop = 0
2016-06-29 15:47:52 +02:00
}
// 2. Change adapter data
adapterData = data
2016-07-03 08:42:28 +02:00
refreshEnabled = true
var restorePosition = -1
2016-08-13 16:04:31 +02:00
if (loader !is IExtendedLoader || loader.fromUser) {
2016-06-29 15:47:52 +02:00
if (hasMoreData(data)) {
adapter.loadMoreSupportedPosition = ILoadMoreSupportAdapter.END
onHasMoreDataChanged(true)
} else {
adapter.loadMoreSupportedPosition = ILoadMoreSupportAdapter.NONE
onHasMoreDataChanged(false)
}
restorePosition = if (useSortIdAsReadPosition) {
adapter.findPositionBySortId(lastReadId)
} else {
adapter.findPositionByPositionKey(lastReadId)
2016-06-29 15:47:52 +02:00
}
} else {
onHasMoreDataChanged(false)
}
2017-02-02 15:20:14 +01:00
if (loadMore) {
restorePosition += 1
restorePosition.coerceInOr(0 until layoutManager.itemCount, -1)
}
if (restorePosition != -1 && adapter.isStatus(restorePosition) && (loadMore || !wasAtTop
|| readFromBottom || (rememberPosition && firstLoad))) {
if (layoutManager.height == 0) {
// RecyclerView has not currently laid out, ignore padding.
layoutManager.scrollToPositionWithOffset(restorePosition, lastReadViewTop)
} else {
layoutManager.scrollToPositionWithOffset(restorePosition, lastReadViewTop - layoutManager.paddingTop)
}
}
2016-06-29 15:47:52 +02:00
if (loader is IExtendedLoader) {
2016-08-13 16:04:31 +02:00
loader.fromUser = false
2016-06-29 15:47:52 +02:00
}
onStatusesLoaded(loader, data)
}
override fun onLoaderReset(loader: Loader<List<ParcelableStatus>?>) {
if (loader is IExtendedLoader) {
2016-08-13 16:04:31 +02:00
loader.fromUser = false
2016-06-29 15:47:52 +02:00
}
}
2016-08-19 16:25:27 +02:00
2016-06-29 15:47:52 +02:00
override fun onGapClick(holder: GapViewHolder, position: Int) {
val adapter = this.adapter
2016-12-06 04:08:56 +01:00
val status = adapter.getStatus(position) ?: return
2017-01-26 16:15:05 +01:00
DebugLog.v(TwidereConstants.LOGTAG, "Load activity gap " + status)
2016-12-06 04:08:56 +01:00
adapter.addGapLoadingId(ObjectId(status.account_key, status.id))
2016-06-29 15:47:52 +02:00
val accountIds = arrayOf(status.account_key)
2016-09-09 05:58:26 +02:00
val maxIds = arrayOf<String?>(status.id)
2016-06-29 15:47:52 +02:00
val maxSortIds = longArrayOf(status.sort_id)
2017-02-10 12:55:00 +01:00
getStatuses(BaseRefreshTaskParam(accountKeys = accountIds, maxIds = maxIds, sinceIds = null,
maxSortIds = maxSortIds, sinceSortIds = null))
2016-06-29 15:47:52 +02:00
}
override fun onMediaClick(holder: IStatusViewHolder, view: View, media: ParcelableMedia, statusPosition: Int) {
val status = adapter.getStatus(statusPosition) ?: return
2017-01-20 15:08:42 +01:00
IntentUtils.openMedia(activity, status, media, preferences[newDocumentApiKey],
preferences[displaySensitiveContentsKey])
2016-06-29 15:47:52 +02:00
// BEGIN HotMobi
val event = MediaEvent.create(activity, status, media, timelineType,
adapter.mediaPreviewEnabled)
HotMobiLogger.getInstance(activity).log(status.account_key, event)
// END HotMobi
}
override fun onItemActionClick(holder: RecyclerView.ViewHolder, id: Int, position: Int) {
val status = adapter.getStatus(position) ?: return
2017-02-13 17:44:56 +01:00
handleActionClick(holder as StatusViewHolder, status, id)
}
override fun onItemActionLongClick(holder: RecyclerView.ViewHolder, id: Int, position: Int): Boolean {
val status = adapter.getStatus(position) ?: return false
return handleActionLongClick(this, status, adapter.getItemId(position), id)
2016-06-29 15:47:52 +02:00
}
override fun createItemDecoration(context: Context, recyclerView: RecyclerView, layoutManager: LinearLayoutManager): RecyclerView.ItemDecoration? {
2016-12-08 15:56:21 +01:00
val itemDecoration = DividerItemDecoration(context, (recyclerView.layoutManager as LinearLayoutManager).orientation)
2016-06-29 15:47:52 +02:00
val res = context.resources
if (adapter.profileImageEnabled) {
2016-06-29 15:47:52 +02:00
val decorPaddingLeft = res.getDimensionPixelSize(R.dimen.element_spacing_normal) * 2 + res.getDimensionPixelSize(R.dimen.icon_size_status_profile_image)
itemDecoration.setPadding { position, rect ->
val itemViewType = adapter.getItemViewType(position)
var nextItemIsStatus = false
if (position < adapter.itemCount - 1) {
nextItemIsStatus = adapter.getItemViewType(position + 1) == ParcelableStatusesAdapter.ITEM_VIEW_TYPE_STATUS
}
if (nextItemIsStatus && itemViewType == ParcelableStatusesAdapter.ITEM_VIEW_TYPE_STATUS) {
rect.left = decorPaddingLeft
} else {
rect.left = 0
}
true
}
}
itemDecoration.setDecorationEndOffset(1)
return itemDecoration
}
protected fun saveReadPosition() {
2016-12-16 01:38:37 +01:00
saveReadPosition(layoutManager.findFirstVisibleItemPosition())
2016-06-29 15:47:52 +02:00
}
protected open fun onHasMoreDataChanged(hasMoreData: Boolean) {
}
override fun onStatusClick(holder: IStatusViewHolder, position: Int) {
2017-01-17 18:59:44 +01:00
val status = adapter.getStatus(position) ?: return
IntentUtils.openStatus(activity, status, null)
}
override fun onQuotedStatusClick(holder: IStatusViewHolder, position: Int) {
val status = adapter.getStatus(position) ?: return
2017-01-25 05:15:54 +01:00
val quotedId = status.quoted_id ?: return
IntentUtils.openStatus(activity, status.account_key, quotedId)
2016-06-29 15:47:52 +02:00
}
override fun onStatusLongClick(holder: IStatusViewHolder, position: Int): Boolean {
//TODO handle long click event
return true
}
override fun onItemMenuClick(holder: RecyclerView.ViewHolder, menuView: View, position: Int) {
if (activity == null) return
val view = layoutManager.findViewByPosition(position) ?: return
2016-06-29 15:47:52 +02:00
recyclerView.showContextMenuForChild(view)
}
override fun onUserProfileClick(holder: IStatusViewHolder, position: Int) {
2017-01-20 15:08:42 +01:00
val status = adapter.getStatus(position)!!
val intent = IntentUtils.userProfile(status.account_key, status.user_key,
2016-06-29 15:47:52 +02:00
status.user_screen_name, Referral.TIMELINE_STATUS,
status.extras.user_statusnet_profile_url)
2017-01-20 15:08:42 +01:00
IntentUtils.applyNewDocument(intent, preferences[newDocumentApiKey])
2016-06-29 15:47:52 +02:00
startActivity(intent)
}
override fun scrollToStart(): Boolean {
val result = super.scrollToStart()
if (result) {
saveReadPosition(0)
}
return result
}
2016-08-19 16:25:27 +02:00
fun initLoaderIfNeeded() {
if (isDetached || host == null || loaderInitialized) return
2016-06-29 15:47:52 +02:00
val loaderArgs = Bundle(arguments)
2016-08-19 16:25:27 +02:00
loaderArgs.putBoolean(EXTRA_FROM_USER, true)
2016-06-29 15:47:52 +02:00
loaderManager.initLoader(0, loaderArgs, this)
2016-08-19 16:25:27 +02:00
loaderInitialized = true
2016-06-29 15:47:52 +02:00
}
protected open fun createMessageBusCallback(): Any {
return StatusesBusCallback()
}
protected fun saveReadPosition(position: Int) {
if (host == null) return
2016-06-29 15:47:52 +02:00
if (position == RecyclerView.NO_POSITION) return
val status = adapter.getStatus(position) ?: return
2016-06-29 15:47:52 +02:00
val positionKey = if (status.position_key > 0) status.position_key else status.timestamp
readPositionTagWithArguments?.let {
2016-12-06 06:15:22 +01:00
accountKeys.map { accountKey -> Utils.getReadPositionTagWithAccount(it, accountKey) }
.forEach { readStateManager.setPosition(it, positionKey) }
}
currentReadPositionTag?.let {
readStateManager.setPosition(it, positionKey, true)
2016-06-29 15:47:52 +02:00
}
}
2016-08-19 16:25:27 +02:00
protected abstract fun hasMoreData(data: List<ParcelableStatus>?): Boolean
protected abstract fun onCreateStatusesLoader(context: Context, args: Bundle,
2017-02-13 17:44:56 +01:00
fromUser: Boolean): Loader<List<ParcelableStatus>?>
2016-08-19 16:25:27 +02:00
protected abstract fun onStatusesLoaded(loader: Loader<List<ParcelableStatus>?>, data: List<ParcelableStatus>?)
// Context Menu functions
2016-06-29 15:47:52 +02:00
override fun onCreateContextMenu(menu: ContextMenu, v: View, menuInfo: ContextMenu.ContextMenuInfo?) {
if (!userVisibleHint || menuInfo == null) return
val inflater = MenuInflater(context)
val contextMenuInfo = menuInfo as ExtendedRecyclerView.ContextMenuInfo?
val status = adapter.getStatus(contextMenuInfo!!.position)
2016-06-29 15:47:52 +02:00
inflater.inflate(R.menu.action_status, menu)
MenuUtils.setupForStatus(context, preferences, menu, status!!, twitterWrapper,
userColorNameManager)
2016-06-29 15:47:52 +02:00
}
2016-12-08 15:56:21 +01:00
override fun onContextItemSelected(item: MenuItem): Boolean {
2016-06-29 15:47:52 +02:00
if (!userVisibleHint) return false
2016-12-08 15:56:21 +01:00
val contextMenuInfo = item.menuInfo as ExtendedRecyclerView.ContextMenuInfo
val status = adapter.getStatus(contextMenuInfo.position) ?: return false
2016-06-29 15:47:52 +02:00
if (item.itemId == R.id.share) {
val shareIntent = Utils.createStatusShareIntent(activity, status)
val chooser = Intent.createChooser(shareIntent, getString(R.string.share_status))
startActivity(chooser)
2016-12-18 06:21:24 +01:00
val am = AccountManager.get(context)
val accountType = AccountUtils.findByAccountKey(am, status.account_key)?.getAccountType(am)
Analyzer.log(Share.status(accountType, status))
2016-06-29 15:47:52 +02:00
return true
}
return MenuUtils.handleStatusClick(activity, this, fragmentManager,
userColorNameManager, twitterWrapper, status, item)
}
class DefaultOnLikedListener(
private val twitter: AsyncTwitterWrapper,
private val status: ParcelableStatus,
private val accountKey: UserKey? = null
2016-06-29 15:47:52 +02:00
) : LikeAnimationDrawable.OnLikedListener {
override fun onLiked(): Boolean {
if (status.is_favorite) return false
twitter.createFavoriteAsync(accountKey ?: status.account_key, status)
2016-06-29 15:47:52 +02:00
return true
}
}
protected inner class StatusesBusCallback {
@Subscribe
fun notifyStatusListChanged(event: StatusListChangedEvent) {
adapter.notifyDataSetChanged()
2016-06-29 15:47:52 +02:00
}
}
2016-08-19 16:25:27 +02:00
interface StatusesFragmentDelegate {
val shouldInitLoader: Boolean
}
2016-06-29 15:47:52 +02:00
companion object {
const val REQUEST_FAVORITE_SELECT_ACCOUNT = 101
const val REQUEST_RETWEET_SELECT_ACCOUNT = 102
2016-06-29 15:47:52 +02:00
2017-02-13 17:44:56 +01:00
fun BaseFragment.handleActionClick(holder: StatusViewHolder, status: ParcelableStatus, id: Int) {
2016-06-29 15:47:52 +02:00
when (id) {
R.id.reply -> {
2016-08-19 16:25:27 +02:00
val intent = Intent(INTENT_ACTION_REPLY)
2016-06-29 15:47:52 +02:00
intent.`package` = context.packageName
2016-08-19 16:25:27 +02:00
intent.putExtra(EXTRA_STATUS, status)
2017-02-13 17:44:56 +01:00
startActivity(intent)
2016-06-29 15:47:52 +02:00
}
R.id.retweet -> {
2017-02-13 17:44:56 +01:00
executeAfterFragmentResumed { fragment ->
RetweetQuoteDialogFragment.show(fragment.childFragmentManager, status)
}
2016-06-29 15:47:52 +02:00
}
R.id.favorite -> {
if (status.is_favorite) {
2017-02-13 17:44:56 +01:00
twitterWrapper.destroyFavoriteAsync(status.account_key, status.id)
2016-06-29 15:47:52 +02:00
} else {
2017-02-13 17:44:56 +01:00
holder.playLikeAnimation(DefaultOnLikedListener(twitterWrapper, status))
2016-06-29 15:47:52 +02:00
}
}
}
}
fun handleActionLongClick(fragment: Fragment, status: ParcelableStatus, itemId: Long, id: Int): Boolean {
when (id) {
R.id.favorite -> {
val intent = selectAccountIntent(fragment.context, status, itemId)
fragment.startActivityForResult(intent, REQUEST_FAVORITE_SELECT_ACCOUNT)
return true
}
R.id.retweet -> {
val intent = selectAccountIntent(fragment.context, status, itemId)
fragment.startActivityForResult(intent, REQUEST_RETWEET_SELECT_ACCOUNT)
return true
}
}
return false
}
fun handleActionActivityResult(fragment: BaseFragment, requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
AbsStatusesFragment.REQUEST_FAVORITE_SELECT_ACCOUNT -> {
if (resultCode != Activity.RESULT_OK || data == null) return
val accountKey = data.getParcelableExtra<UserKey>(IntentConstants.EXTRA_ACCOUNT_KEY)
val extras = data.getBundleExtra(IntentConstants.EXTRA_EXTRAS)
val status = extras.getParcelable<ParcelableStatus>(IntentConstants.EXTRA_STATUS)
fragment.twitterWrapper.createFavoriteAsync(accountKey, status)
}
AbsStatusesFragment.REQUEST_RETWEET_SELECT_ACCOUNT -> {
if (resultCode != Activity.RESULT_OK || data == null) return
val accountKey = data.getParcelableExtra<UserKey>(IntentConstants.EXTRA_ACCOUNT_KEY)
val extras = data.getBundleExtra(IntentConstants.EXTRA_EXTRAS)
val status = extras.getParcelable<ParcelableStatus>(IntentConstants.EXTRA_STATUS)
RetweetQuoteDialogFragment.show(fragment.childFragmentManager, status, accountKey)
}
}
}
fun selectAccountIntent(context: Context, status: ParcelableStatus, itemId: Long): Intent {
val intent = Intent(context, AccountSelectorActivity::class.java)
intent.putExtra(EXTRA_SELECT_ONLY_ITEM_AUTOMATICALLY, true)
intent.putExtra(EXTRA_ACCOUNT_HOST, status.account_key.host)
intent.putExtra(EXTRA_SINGLE_SELECTION, true)
intent.putExtra(EXTRA_EXTRAS, Bundle {
this[EXTRA_STATUS] = status
this[EXTRA_ID] = itemId
})
return intent
}
2016-06-29 15:47:52 +02:00
}
}