diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/SignInActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/SignInActivity.kt index 6e1286f9f..2bc2eda33 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/SignInActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/SignInActivity.kt @@ -432,8 +432,11 @@ class SignInActivity : BaseActivity(), OnClickListener, TextWatcher, APIEditorDi }.alwaysUi { val activity = weakThis.get() ?: return@alwaysUi if (activity.isFinishing) return@alwaysUi - val df = weakDf.get() ?: supportFragmentManager.findFragmentByTag(FRAGMENT_TAG_LOADING_DEFAULT_FEATURES) as? DialogFragment - df?.dismiss() + activity.executeAfterFragmentResumed { activity -> + val fm = activity.supportFragmentManager + val df = weakDf.get() ?: fm.findFragmentByTag(FRAGMENT_TAG_LOADING_DEFAULT_FEATURES) as? DialogFragment + df?.dismiss() + } } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/MessagesEntriesFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/MessagesEntriesFragment.kt index 79a4b729e..4fac73dfb 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/MessagesEntriesFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/MessagesEntriesFragment.kt @@ -1,27 +1,24 @@ package org.mariotaku.twidere.fragment -import android.accounts.AccountManager import android.content.Context import android.os.Bundle import android.support.v4.app.LoaderManager import android.support.v4.content.Loader +import com.squareup.otto.Subscribe import org.mariotaku.kpreferences.get -import org.mariotaku.ktextension.toNulls import org.mariotaku.sqliteqb.library.OrderBy import org.mariotaku.twidere.R import org.mariotaku.twidere.adapter.MessagesEntriesAdapter import org.mariotaku.twidere.adapter.iface.ILoadMoreSupportAdapter -import org.mariotaku.twidere.annotation.AccountType import org.mariotaku.twidere.constant.newDocumentApiKey import org.mariotaku.twidere.extension.model.user import org.mariotaku.twidere.loader.ObjectCursorLoader import org.mariotaku.twidere.model.ParcelableMessageConversation import org.mariotaku.twidere.model.ParcelableMessageConversationCursorIndices -import org.mariotaku.twidere.model.SimpleRefreshTaskParam import org.mariotaku.twidere.model.UserKey -import org.mariotaku.twidere.model.util.AccountUtils -import org.mariotaku.twidere.provider.TwidereDataStore.Messages +import org.mariotaku.twidere.model.event.GetMessagesTaskEvent import org.mariotaku.twidere.provider.TwidereDataStore.Messages.Conversations +import org.mariotaku.twidere.task.GetMessagesTask import org.mariotaku.twidere.util.DataStoreUtils import org.mariotaku.twidere.util.ErrorInfoStore import org.mariotaku.twidere.util.IntentUtils @@ -45,6 +42,16 @@ class MessagesEntriesFragment : AbsContentListRecyclerViewFragment?> { val loader = ObjectCursorLoader(context, ParcelableMessageConversationCursorIndices::class.java) loader.uri = Conversations.CONTENT_URI @@ -69,37 +76,8 @@ class MessagesEntriesFragment : AbsContentListRecyclerViewFragment by lazy { - this@MessagesEntriesFragment.accountKeys - } - - override val sinceIds: Array? - get() { - val result = arrayOfNulls(accountKeys.size) - val hasSinceAccountKeys = accounts.mapNotNull { account -> - when (account?.type) { - AccountType.FANFOU -> { - return@mapNotNull null - } - } - return@mapNotNull account?.key - }.toTypedArray() - val incomingIds = DataStoreUtils.getMessageIds(context, Messages.CONTENT_URI, - hasSinceAccountKeys.toNulls(), false) - val outgoingIds = DataStoreUtils.getMessageIds(context, Messages.CONTENT_URI, - hasSinceAccountKeys.toNulls(), true) - loop@ for (idx in 0..accountKeys.lastIndex) { - - } - return result - } - override val hasSinceIds: Boolean = true - override val hasMaxIds: Boolean = false + twitterWrapper.getMessagesAsync(GetMessagesTask.RefreshNewTaskParam(context) { + this@MessagesEntriesFragment.accountKeys }) return true } @@ -119,6 +97,13 @@ class MessagesEntriesFragment : AbsContentListRecyclerViewFragment 0) { @@ -135,4 +120,6 @@ class MessagesEntriesFragment : AbsContentListRecyclerViewFragment Unit)?, result: Unit) { callback?.invoke(true) + bus.post(GetMessagesTaskEvent(Messages.CONTENT_URI, false, null)) } private fun getMessages(microBlog: MicroBlog, details: AccountDetails, param: RefreshTaskParam, index: Int): GetMessagesData { @@ -86,7 +90,7 @@ class GetMessagesTask(context: Context) : BaseAbstractTask, val messages: Collection ) + + class RefreshNewTaskParam( + val context: Context, + val getAccountKeys: () -> Array + ) : SimpleRefreshTaskParam() { + + private val accounts by lazy { + AccountUtils.getAllAccountDetails(AccountManager.get(context), accountKeys, false) + } + + override val accountKeys: Array + get() = getAccountKeys() + + override val sinceIds: Array? + get() { + val keys = accounts.map { account -> + when (account?.type) { + AccountType.FANFOU -> { + return@map null + } + } + return@map account?.key + }.toTypedArray() + val incomingIds = DataStoreUtils.getNewestMessageIds(context, Messages.CONTENT_URI, + keys, false) + val outgoingIds = DataStoreUtils.getNewestMessageIds(context, Messages.CONTENT_URI, + keys, true) + return incomingIds + outgoingIds + } + + override val hasSinceIds: Boolean = true + override val hasMaxIds: Boolean = false + } } 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 387d687ed..9a9ae99b9 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/DataStoreUtils.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/DataStoreUtils.kt @@ -165,13 +165,8 @@ object DataStoreUtils { OrderBy(SQLFunctions.MAX(Statuses.STATUS_TIMESTAMP)), null, null) } - fun getMessageIds(context: Context, uri: Uri, accountKeys: Array, outgoing: Boolean): Array { - val having: Expression - if (outgoing) { - having = Expression.equals(Messages.IS_OUTGOING, 1) - } else { - having = Expression.notEquals(Messages.IS_OUTGOING, 1) - } + fun getNewestMessageIds(context: Context, uri: Uri, accountKeys: Array, outgoing: Boolean): Array { + val having: Expression = Expression.equals(Messages.IS_OUTGOING, if (outgoing) 1 else 0) return getStringFieldArray(context, uri, accountKeys, Messages.ACCOUNT_KEY, Messages.MESSAGE_ID, OrderBy(SQLFunctions.MAX(Messages.LOCAL_TIMESTAMP)), having, null) } @@ -439,27 +434,27 @@ object DataStoreUtils { .from(Tables(table, Filters.Sources.TABLE_NAME)) .where(Expression.or( Expression.likeRaw(Column(Table(table), Activities.STATUS_SOURCE), - "'%>'||" + Filters.Sources.TABLE_NAME + "." + Filters.Sources.VALUE + "||'%'"), + "'%>'||${Filters.Sources.TABLE_NAME}.${Filters.Sources.VALUE}||'%'"), Expression.likeRaw(Column(Table(table), Activities.STATUS_QUOTE_SOURCE), - "'%>'||" + Filters.Sources.TABLE_NAME + "." + Filters.Sources.VALUE + "||'%'") + "'%>'||${Filters.Sources.TABLE_NAME}.${Filters.Sources.VALUE}||'%'") )) .union() .select(Columns(Column(Table(table), Activities._ID))) .from(Tables(table, Filters.Keywords.TABLE_NAME)) .where(Expression.or( Expression.likeRaw(Column(Table(table), Activities.STATUS_TEXT_PLAIN), - "'%'||" + Filters.Keywords.TABLE_NAME + "." + Filters.Keywords.VALUE + "||'%'"), + "'%'||${Filters.Keywords.TABLE_NAME}.${Filters.Keywords.VALUE}||'%'"), Expression.likeRaw(Column(Table(table), Activities.STATUS_QUOTE_TEXT_PLAIN), - "'%'||" + Filters.Keywords.TABLE_NAME + "." + Filters.Keywords.VALUE + "||'%'") + "'%'||${Filters.Keywords.TABLE_NAME}.${Filters.Keywords.VALUE}||'%'") )) .union() .select(Columns(Column(Table(table), Activities._ID))) .from(Tables(table, Filters.Links.TABLE_NAME)) .where(Expression.or( Expression.likeRaw(Column(Table(table), Activities.STATUS_SPANS), - "'%'||" + Filters.Links.TABLE_NAME + "." + Filters.Links.VALUE + "||'%'"), + "'%'||${Filters.Links.TABLE_NAME}.${Filters.Links.VALUE}||'%'"), Expression.likeRaw(Column(Table(table), Activities.STATUS_QUOTE_SPANS), - "'%'||" + Filters.Links.TABLE_NAME + "." + Filters.Links.VALUE + "||'%'") + "'%'||${Filters.Links.TABLE_NAME}.${Filters.Links.VALUE}||'%'") )) val filterExpression = Expression.or( Expression.notIn(Column(Table(table), Activities._ID), filteredIdsQueryBuilder.build()), @@ -626,36 +621,36 @@ object DataStoreUtils { } @SuppressLint("Recycle") - private fun getFieldArray(context: Context, uri: Uri, - keys: Array, keyField: String, - valueField: String, sortExpression: OrderBy?, - extraHaving: Expression?, extraHavingArgs: Array?, - creator: FieldArrayCreator): T { + private fun getFieldArray( + context: Context, uri: Uri, + keys: Array, keyField: String, + valueField: String, sortExpression: OrderBy?, + extraWhere: Expression?, extraWhereArgs: Array?, + creator: FieldArrayCreator + ): T { val resolver = context.contentResolver val resultArray = creator.newArray(keys.size) val nonNullKeys = keys.mapNotNull { it?.toString() }.toTypedArray() val tableName = getTableNameByUri(uri) ?: throw NullPointerException() - val having: Expression - if (extraHaving != null) { - having = Expression.and(extraHaving, Expression.inArgs(keyField, nonNullKeys.size)) + val having = Expression.inArgs(keyField, nonNullKeys.size) + val bindingArgs: Array + if (extraWhereArgs != null) { + bindingArgs = extraWhereArgs + nonNullKeys } else { - having = Expression.inArgs(keyField, nonNullKeys.size) - } - val havingArgs: Array - if (extraHavingArgs != null) { - havingArgs = extraHavingArgs + nonNullKeys - } else { - havingArgs = nonNullKeys + bindingArgs = nonNullKeys } val builder = SQLQueryBuilder.select(Columns(keyField, valueField)) - .from(Table(tableName)) - .groupBy(Column(keyField)) - .having(having) + builder.from(Table(tableName)) + if (extraWhere != null) { + builder.where(extraWhere) + } + builder.groupBy(Column(keyField)) + builder.having(having) if (sortExpression != null) { builder.orderBy(sortExpression) } val rawUri = Uri.withAppendedPath(TwidereDataStore.CONTENT_URI_RAW_QUERY, builder.buildSQL()) - resolver.query(rawUri, null, null, havingArgs, null)?.useCursor { cur -> + resolver.query(rawUri, null, null, bindingArgs, null)?.useCursor { cur -> cur.moveToFirst() while (!cur.isAfterLast) { val string = cur.getString(0) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/TaskServiceRunner.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/TaskServiceRunner.kt index c8028dbbb..3e82bf29a 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/TaskServiceRunner.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/TaskServiceRunner.kt @@ -70,8 +70,10 @@ class TaskServiceRunner( } ACTION_REFRESH_DIRECT_MESSAGES -> { val task = GetMessagesTask(context) - task.params = AutoRefreshTaskParam(context, AccountPreferences::isAutoRefreshDirectMessagesEnabled) { accountKeys -> - arrayOfNulls(accountKeys.size) + task.params = GetMessagesTask.RefreshNewTaskParam(context) { + AccountPreferences.getAccountPreferences(context, DataStoreUtils.getAccountKeys(context)).filter { + it.isAutoRefreshEnabled && it.isAutoRefreshDirectMessagesEnabled + }.map(AccountPreferences::getAccountKey).toTypedArray() } return task }