2017-02-15 09:11:11 +01:00
|
|
|
/*
|
|
|
|
* Twidere - Twitter client for Android
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012-2017 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.message
|
2016-07-08 09:52:32 +02:00
|
|
|
|
2017-02-09 14:43:51 +01:00
|
|
|
import android.content.Context
|
2017-02-15 07:55:18 +01:00
|
|
|
import android.content.Intent
|
2017-02-09 14:43:51 +01:00
|
|
|
import android.os.Bundle
|
2017-02-15 07:48:10 +01:00
|
|
|
import android.support.v4.app.LoaderManager.LoaderCallbacks
|
2017-02-09 14:43:51 +01:00
|
|
|
import android.support.v4.content.Loader
|
2017-02-10 18:03:40 +01:00
|
|
|
import com.squareup.otto.Subscribe
|
2017-02-09 14:43:51 +01:00
|
|
|
import org.mariotaku.kpreferences.get
|
2017-02-10 19:01:31 +01:00
|
|
|
import org.mariotaku.ktextension.toStringArray
|
|
|
|
import org.mariotaku.sqliteqb.library.Expression
|
2017-02-09 14:43:51 +01:00
|
|
|
import org.mariotaku.sqliteqb.library.OrderBy
|
|
|
|
import org.mariotaku.twidere.R
|
2017-02-15 07:55:18 +01:00
|
|
|
import org.mariotaku.twidere.TwidereConstants.EXTRA_ACCOUNT_KEYS
|
|
|
|
import org.mariotaku.twidere.TwidereConstants.REQUEST_SELECT_ACCOUNT
|
|
|
|
import org.mariotaku.twidere.activity.AccountSelectorActivity
|
2017-02-10 07:17:48 +01:00
|
|
|
import org.mariotaku.twidere.adapter.MessagesEntriesAdapter
|
2017-02-15 07:48:10 +01:00
|
|
|
import org.mariotaku.twidere.adapter.MessagesEntriesAdapter.MessageConversationClickListener
|
2017-02-10 12:55:00 +01:00
|
|
|
import org.mariotaku.twidere.adapter.iface.ILoadMoreSupportAdapter
|
2017-02-09 14:43:51 +01:00
|
|
|
import org.mariotaku.twidere.constant.newDocumentApiKey
|
|
|
|
import org.mariotaku.twidere.extension.model.user
|
2017-02-15 09:11:11 +01:00
|
|
|
import org.mariotaku.twidere.fragment.AbsContentListRecyclerViewFragment
|
2017-02-15 07:48:10 +01:00
|
|
|
import org.mariotaku.twidere.fragment.iface.IFloatingActionButtonFragment
|
|
|
|
import org.mariotaku.twidere.fragment.iface.IFloatingActionButtonFragment.ActionInfo
|
2017-02-09 14:43:51 +01:00
|
|
|
import org.mariotaku.twidere.loader.ObjectCursorLoader
|
|
|
|
import org.mariotaku.twidere.model.ParcelableMessageConversation
|
|
|
|
import org.mariotaku.twidere.model.ParcelableMessageConversationCursorIndices
|
|
|
|
import org.mariotaku.twidere.model.UserKey
|
2017-02-10 18:03:40 +01:00
|
|
|
import org.mariotaku.twidere.model.event.GetMessagesTaskEvent
|
2017-02-09 14:43:51 +01:00
|
|
|
import org.mariotaku.twidere.provider.TwidereDataStore.Messages.Conversations
|
2017-02-10 18:03:40 +01:00
|
|
|
import org.mariotaku.twidere.task.GetMessagesTask
|
2017-02-09 14:43:51 +01:00
|
|
|
import org.mariotaku.twidere.util.DataStoreUtils
|
|
|
|
import org.mariotaku.twidere.util.ErrorInfoStore
|
|
|
|
import org.mariotaku.twidere.util.IntentUtils
|
|
|
|
import org.mariotaku.twidere.util.Utils
|
2016-07-08 09:52:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mariotaku on 16/3/28.
|
|
|
|
*/
|
2017-02-10 07:17:48 +01:00
|
|
|
class MessagesEntriesFragment : AbsContentListRecyclerViewFragment<MessagesEntriesAdapter>(),
|
2017-02-15 07:48:10 +01:00
|
|
|
LoaderCallbacks<List<ParcelableMessageConversation>?>, MessageConversationClickListener,
|
|
|
|
IFloatingActionButtonFragment {
|
2017-02-09 14:43:51 +01:00
|
|
|
|
2017-02-10 19:01:31 +01:00
|
|
|
private val accountKeys: Array<UserKey> by lazy {
|
|
|
|
Utils.getAccountKeys(context, arguments) ?: DataStoreUtils.getActivatedAccountKeys(context)
|
|
|
|
}
|
2017-02-09 14:43:51 +01:00
|
|
|
|
|
|
|
private val errorInfoKey: String = ErrorInfoStore.KEY_DIRECT_MESSAGES
|
|
|
|
|
|
|
|
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
|
|
|
super.onActivityCreated(savedInstanceState)
|
|
|
|
adapter.listener = this
|
2017-02-10 12:55:00 +01:00
|
|
|
adapter.loadMoreSupportedPosition = ILoadMoreSupportAdapter.END
|
2017-02-09 14:43:51 +01:00
|
|
|
loaderManager.initLoader(0, null, this)
|
|
|
|
}
|
|
|
|
|
2017-02-10 18:03:40 +01:00
|
|
|
override fun onStart() {
|
|
|
|
super.onStart()
|
|
|
|
bus.register(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onStop() {
|
|
|
|
bus.unregister(this)
|
|
|
|
super.onStop()
|
|
|
|
}
|
|
|
|
|
2017-02-09 14:43:51 +01:00
|
|
|
override fun onCreateLoader(id: Int, args: Bundle?): Loader<List<ParcelableMessageConversation>?> {
|
|
|
|
val loader = ObjectCursorLoader(context, ParcelableMessageConversationCursorIndices::class.java)
|
|
|
|
loader.uri = Conversations.CONTENT_URI
|
2017-02-10 19:01:31 +01:00
|
|
|
loader.selection = Expression.inArgs(Conversations.ACCOUNT_KEY, accountKeys.size).sql
|
|
|
|
loader.selectionArgs = accountKeys.toStringArray()
|
2017-02-09 14:43:51 +01:00
|
|
|
loader.projection = Conversations.COLUMNS
|
2017-02-15 18:03:48 +01:00
|
|
|
loader.sortOrder = OrderBy(arrayOf(Conversations.LOCAL_TIMESTAMP,
|
|
|
|
Conversations.SORT_ID), booleanArrayOf(false, false)).sql
|
2017-02-09 14:43:51 +01:00
|
|
|
return loader
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onLoaderReset(loader: Loader<List<ParcelableMessageConversation>?>?) {
|
|
|
|
adapter.conversations = null
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onLoadFinished(loader: Loader<List<ParcelableMessageConversation>?>?, data: List<ParcelableMessageConversation>?) {
|
|
|
|
adapter.conversations = data
|
|
|
|
adapter.drawAccountColors = accountKeys.size > 1
|
2017-02-10 19:01:31 +01:00
|
|
|
setLoadMoreIndicatorPosition(ILoadMoreSupportAdapter.NONE)
|
2017-02-09 14:43:51 +01:00
|
|
|
showContentOrError()
|
|
|
|
}
|
|
|
|
|
2017-02-10 07:17:48 +01:00
|
|
|
override fun onCreateAdapter(context: Context): MessagesEntriesAdapter {
|
|
|
|
return MessagesEntriesAdapter(context)
|
2017-02-09 14:43:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun triggerRefresh(): Boolean {
|
|
|
|
super.triggerRefresh()
|
2017-02-14 17:26:48 +01:00
|
|
|
twitterWrapper.getMessagesAsync(object : GetMessagesTask.RefreshNewTaskParam(context) {
|
|
|
|
override val accountKeys: Array<UserKey> = this@MessagesEntriesFragment.accountKeys
|
2017-02-09 14:43:51 +01:00
|
|
|
})
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-02-10 12:55:00 +01:00
|
|
|
override fun onLoadMoreContents(position: Long) {
|
2017-02-10 19:01:31 +01:00
|
|
|
if (position != ILoadMoreSupportAdapter.END) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setLoadMoreIndicatorPosition(ILoadMoreSupportAdapter.END)
|
2017-02-14 17:26:48 +01:00
|
|
|
twitterWrapper.getMessagesAsync(object : GetMessagesTask.LoadMoreEntriesTaskParam(context) {
|
|
|
|
override val accountKeys: Array<UserKey> = this@MessagesEntriesFragment.accountKeys
|
2017-02-10 19:01:31 +01:00
|
|
|
})
|
2017-02-10 12:55:00 +01:00
|
|
|
}
|
|
|
|
|
2017-02-09 14:43:51 +01:00
|
|
|
override fun onConversationClick(position: Int) {
|
|
|
|
val conversation = adapter.getConversation(position) ?: return
|
|
|
|
IntentUtils.openMessageConversation(context, conversation.account_key, conversation.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onProfileImageClick(position: Int) {
|
|
|
|
val conversation = adapter.getConversation(position) ?: return
|
|
|
|
val user = conversation.user ?: return
|
|
|
|
IntentUtils.openUserProfile(context, user, preferences[newDocumentApiKey])
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:48:10 +01:00
|
|
|
override fun getActionInfo(tag: String): ActionInfo? {
|
|
|
|
return ActionInfo(R.drawable.ic_action_add, getString(R.string.new_direct_message))
|
|
|
|
}
|
|
|
|
|
2017-02-15 09:11:11 +01:00
|
|
|
override fun onActionClick(tag: String): Boolean {
|
2017-02-15 07:48:10 +01:00
|
|
|
val accountKey = accountKeys.singleOrNull() ?: run {
|
2017-02-15 07:55:18 +01:00
|
|
|
val selectIntent = Intent(context, AccountSelectorActivity::class.java)
|
|
|
|
selectIntent.putExtra(EXTRA_ACCOUNT_KEYS, accountKeys)
|
|
|
|
startActivityForResult(selectIntent, REQUEST_SELECT_ACCOUNT)
|
2017-02-15 09:11:11 +01:00
|
|
|
return true
|
2017-02-15 07:48:10 +01:00
|
|
|
}
|
|
|
|
startActivity(IntentUtils.newMessageConversation(accountKey))
|
2017-02-15 09:11:11 +01:00
|
|
|
return true
|
2017-02-15 07:48:10 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 18:03:40 +01:00
|
|
|
@Subscribe
|
|
|
|
fun onGetMessagesTaskEvent(event: GetMessagesTaskEvent) {
|
|
|
|
if (!event.running) {
|
|
|
|
refreshing = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 14:43:51 +01:00
|
|
|
private fun showContentOrError() {
|
|
|
|
val accountKeys = this.accountKeys
|
|
|
|
if (adapter.itemCount > 0) {
|
|
|
|
showContent()
|
|
|
|
} else if (accountKeys.isNotEmpty()) {
|
|
|
|
val errorInfo = ErrorInfoStore.getErrorInfo(context,
|
|
|
|
errorInfoStore[errorInfoKey, accountKeys[0]])
|
|
|
|
if (errorInfo != null) {
|
|
|
|
showEmpty(errorInfo.icon, errorInfo.message)
|
|
|
|
} else {
|
|
|
|
showEmpty(R.drawable.ic_info_refresh, getString(R.string.swipe_down_to_refresh))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
showError(R.drawable.ic_info_accounts, getString(R.string.message_toast_no_account_selected))
|
|
|
|
}
|
|
|
|
}
|
2017-02-10 18:03:40 +01:00
|
|
|
|
|
|
|
|
2017-02-09 14:43:51 +01:00
|
|
|
}
|