removed some AbsTask implementations

This commit is contained in:
Mariotaku Lee 2017-11-08 20:04:02 +08:00
parent 2f97b7ec36
commit 9fd3e0ee9a
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
6 changed files with 114 additions and 136 deletions

View File

@ -47,17 +47,16 @@ import kotlinx.android.synthetic.main.activity_home_content.view.*
import kotlinx.android.synthetic.main.fragment_messages_conversation_info.*
import kotlinx.android.synthetic.main.header_message_conversation_info.view.*
import kotlinx.android.synthetic.main.layout_toolbar_message_conversation_title.*
import nl.komponents.kovenant.combine.and
import nl.komponents.kovenant.task
import nl.komponents.kovenant.then
import nl.komponents.kovenant.ui.alwaysUi
import nl.komponents.kovenant.ui.successUi
import org.mariotaku.abstask.library.TaskStarter
import org.mariotaku.chameleon.Chameleon
import org.mariotaku.chameleon.ChameleonUtils
import org.mariotaku.kpreferences.get
import org.mariotaku.ktextension.mapToArray
import org.mariotaku.ktextension.setItemAvailability
import org.mariotaku.ktextension.setItemTitle
import org.mariotaku.ktextension.spannable
import org.mariotaku.ktextension.*
import org.mariotaku.library.objectcursor.ObjectCursor
import org.mariotaku.microblog.library.MicroBlog
import org.mariotaku.microblog.library.MicroBlogException
@ -90,11 +89,11 @@ import org.mariotaku.twidere.model.*
import org.mariotaku.twidere.model.ParcelableMessageConversation.ConversationType
import org.mariotaku.twidere.model.ParcelableMessageConversation.ExtrasType
import org.mariotaku.twidere.model.util.AccountUtils
import org.mariotaku.twidere.promise.MessageConversationPromises
import org.mariotaku.twidere.provider.TwidereDataStore.Messages.Conversations
import org.mariotaku.twidere.task.status.UpdateStatusTask
import org.mariotaku.twidere.task.twitter.message.AddParticipantsTask
import org.mariotaku.twidere.task.twitter.message.ClearMessagesTask
import org.mariotaku.twidere.task.twitter.message.DestroyConversationTask
import org.mariotaku.twidere.task.twitter.message.SetConversationNotificationDisabledTask
import org.mariotaku.twidere.util.IntentUtils
import org.mariotaku.twidere.view.holder.SimpleUserViewHolder
@ -266,7 +265,7 @@ class MessageConversationInfoFragment : BaseFragment(), IToolBarSupportFragment,
val name = data.getTitle(context, userColorNameManager, preferences[nameFirstKey]).first
val summary = data.getSubtitle(context)
@ImageShapeStyle val profileImageStyle = preferences[profileImageStyleKey]
@ImageShapeStyle val profileImageStyle: Int = preferences[profileImageStyleKey]
requestManager.loadProfileImage(context, data, profileImageStyle).into(conversationAvatar)
requestManager.loadProfileImage(context, data, profileImageStyle, 0f,
0f, ProfileImageSize.REASONABLY_SMALL).into(appBarIcon)
@ -297,19 +296,13 @@ class MessageConversationInfoFragment : BaseFragment(), IToolBarSupportFragment,
}
private fun performDestroyConversation() {
ProgressDialogFragment.show(childFragmentManager, "leave_conversation_progress")
val weakThis = WeakReference(this)
val task = DestroyConversationTask(context, accountKey, conversationId)
task.callback = callback@ { succeed ->
val f = weakThis.get() ?: return@callback
f.dismissDialogThen("leave_conversation_progress") {
if (succeed) {
activity?.setResult(RESULT_CLOSE)
activity?.finish()
}
}
val weakThis by weak(this)
showProgressDialog("leave_conversation_progress") and MessageConversationPromises.destroyConversation(context, accountKey, conversationId).successUi {
val f = weakThis ?: return@successUi
f.dismissProgressDialog("leave_conversation_progress")
f.activity?.setResult(RESULT_CLOSE)
f.activity?.finish()
}
TaskStarter.execute(task)
}
private fun performClearMessages() {

View File

@ -0,0 +1,91 @@
/*
* 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.promise
import android.accounts.AccountManager
import android.content.Context
import nl.komponents.kovenant.Promise
import nl.komponents.kovenant.task
import org.mariotaku.ktextension.weak
import org.mariotaku.microblog.library.MicroBlog
import org.mariotaku.microblog.library.MicroBlogException
import org.mariotaku.sqliteqb.library.Expression
import org.mariotaku.twidere.annotation.AccountType
import org.mariotaku.twidere.extension.model.isOfficial
import org.mariotaku.twidere.extension.model.newMicroBlogInstance
import org.mariotaku.twidere.model.AccountDetails
import org.mariotaku.twidere.model.ParcelableMessageConversation
import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.model.util.AccountUtils
import org.mariotaku.twidere.provider.TwidereDataStore.Messages
import org.mariotaku.twidere.task.twitter.message.ClearMessagesTask
import org.mariotaku.twidere.util.DataStoreUtils
object MessageConversationPromises {
fun destroyConversation(context: Context, accountKey: UserKey, conversationId: String): Promise<Boolean, Exception> {
val weakContext by weak(context)
return task {
val taskContext = weakContext ?: throw InterruptedException()
val account = AccountUtils.getAccountDetails(AccountManager.get(taskContext), accountKey, true) ?:
throw MicroBlogException("No account")
val conversation = DataStoreUtils.findMessageConversation(taskContext, accountKey, conversationId)
var deleteMessages = true
var deleteConversation = true
// Only perform real deletion when it's not temp conversation (stored locally)
if (conversation != null) when {
conversation.conversation_extras_type != ParcelableMessageConversation.ExtrasType.TWITTER_OFFICIAL -> {
deleteMessages = false
deleteConversation = ClearMessagesTask.clearMessages(taskContext, account, conversationId)
}
!conversation.is_temp -> if (!requestDestroyConversation(taskContext, account, conversationId)) {
return@task false
}
}
if (deleteMessages) {
val deleteMessageWhere = Expression.and(Expression.equalsArgs(Messages.ACCOUNT_KEY),
Expression.equalsArgs(Messages.CONVERSATION_ID)).sql
val deleteMessageWhereArgs = arrayOf(accountKey.toString(), conversationId)
taskContext.contentResolver.delete(Messages.CONTENT_URI, deleteMessageWhere, deleteMessageWhereArgs)
}
if (deleteConversation) {
val deleteConversationWhere = Expression.and(Expression.equalsArgs(Messages.Conversations.ACCOUNT_KEY),
Expression.equalsArgs(Messages.Conversations.CONVERSATION_ID)).sql
val deleteConversationWhereArgs = arrayOf(accountKey.toString(), conversationId)
taskContext.contentResolver.delete(Messages.Conversations.CONTENT_URI, deleteConversationWhere, deleteConversationWhereArgs)
}
return@task true
}
}
private fun requestDestroyConversation(context: Context, account: AccountDetails, conversationId: String): Boolean {
when (account.type) {
AccountType.TWITTER -> {
if (account.isOfficial(context)) {
val twitter = account.newMicroBlogInstance(context, MicroBlog::class.java)
return twitter.deleteDmConversation(conversationId).isSuccessful
}
}
}
return false
}
}

View File

@ -38,10 +38,9 @@ abstract class AbsFriendshipOperationTask(
val event = FriendshipTaskEvent(action, params.accountKey, params.userKey)
event.isFinished = true
if (result != null) {
val user = result
showSucceededMessage(params, user)
showSucceededMessage(params, result)
event.isSucceeded = true
event.user = user
event.user = result
} else if (exception != null) {
showErrorMessage(params, exception)
}

View File

@ -64,8 +64,8 @@ class AddParticipantsTask(
Thread.sleep(300L)
return true
}
val microBlog = account.newMicroBlogInstance(context, cls = MicroBlog::class.java)
val addData = requestAddParticipants(microBlog, account, conversation)
val addData = requestAddParticipants(account, conversation)
GetMessagesTask.storeMessages(context, addData, account, showNotification = false)
return true
}
@ -74,11 +74,12 @@ class AddParticipantsTask(
callback?.invoke(result ?: false)
}
private fun requestAddParticipants(microBlog: MicroBlog, account: AccountDetails, conversation: ParcelableMessageConversation?):
private fun requestAddParticipants(account: AccountDetails, conversation: ParcelableMessageConversation?):
GetMessagesTask.DatabaseUpdateData {
when (account.type) {
AccountType.TWITTER -> {
if (account.isOfficial(context)) {
val microBlog = account.newMicroBlogInstance(context, cls = MicroBlog::class.java)
val ids = participants.mapToArray { it.key.id }
val response = microBlog.addParticipants(conversationId, ids)
if (conversation != null) {

View File

@ -1,100 +0,0 @@
/*
* 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.task.twitter.message
import android.accounts.AccountManager
import android.content.Context
import org.mariotaku.microblog.library.MicroBlog
import org.mariotaku.microblog.library.MicroBlogException
import org.mariotaku.sqliteqb.library.Expression
import org.mariotaku.twidere.annotation.AccountType
import org.mariotaku.twidere.extension.model.isOfficial
import org.mariotaku.twidere.extension.model.newMicroBlogInstance
import org.mariotaku.twidere.model.AccountDetails
import org.mariotaku.twidere.model.ParcelableMessageConversation
import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.model.util.AccountUtils
import org.mariotaku.twidere.provider.TwidereDataStore.Messages
import org.mariotaku.twidere.provider.TwidereDataStore.Messages.Conversations
import org.mariotaku.twidere.task.ExceptionHandlingAbstractTask
import org.mariotaku.twidere.util.DataStoreUtils
/**
* Created by mariotaku on 2017/2/14.
*/
class DestroyConversationTask(
context: Context,
val accountKey: UserKey,
val conversationId: String
) : ExceptionHandlingAbstractTask<Unit?, Boolean, MicroBlogException, ((Boolean) -> Unit)?>(context) {
override val exceptionClass = MicroBlogException::class.java
override fun onExecute(params: Unit?): Boolean {
val account = AccountUtils.getAccountDetails(AccountManager.get(context), accountKey, true) ?:
throw MicroBlogException("No account")
val microBlog = account.newMicroBlogInstance(context, cls = MicroBlog::class.java)
val conversation = DataStoreUtils.findMessageConversation(context, accountKey, conversationId)
var deleteMessages = true
var deleteConversation = true
// Only perform real deletion when it's not temp conversation (stored locally)
if (conversation != null) when {
conversation.conversation_extras_type != ParcelableMessageConversation.ExtrasType.TWITTER_OFFICIAL -> {
deleteMessages = false
deleteConversation = ClearMessagesTask.clearMessages(context, account, conversationId)
}
!conversation.is_temp -> if (!requestDestroyConversation(microBlog, account)) {
return false
}
}
if (deleteMessages) {
val deleteMessageWhere = Expression.and(Expression.equalsArgs(Messages.ACCOUNT_KEY),
Expression.equalsArgs(Messages.CONVERSATION_ID)).sql
val deleteMessageWhereArgs = arrayOf(accountKey.toString(), conversationId)
context.contentResolver.delete(Messages.CONTENT_URI, deleteMessageWhere, deleteMessageWhereArgs)
}
if (deleteConversation) {
val deleteConversationWhere = Expression.and(Expression.equalsArgs(Conversations.ACCOUNT_KEY),
Expression.equalsArgs(Conversations.CONVERSATION_ID)).sql
val deleteConversationWhereArgs = arrayOf(accountKey.toString(), conversationId)
context.contentResolver.delete(Conversations.CONTENT_URI, deleteConversationWhere, deleteConversationWhereArgs)
}
return true
}
override fun afterExecute(callback: ((Boolean) -> Unit)?, result: Boolean?, exception: MicroBlogException?) {
callback?.invoke(result ?: false)
}
private fun requestDestroyConversation(microBlog: MicroBlog, account: AccountDetails): Boolean {
when (account.type) {
AccountType.TWITTER -> {
if (account.isOfficial(context)) {
return microBlog.deleteDmConversation(conversationId).isSuccessful
}
}
}
return false
}
}

View File

@ -26,6 +26,7 @@ import android.widget.Toast
import com.squareup.otto.Bus
import com.squareup.otto.Subscribe
import nl.komponents.kovenant.Promise
import nl.komponents.kovenant.task
import org.mariotaku.abstask.library.TaskStarter
import org.mariotaku.kpreferences.get
import org.mariotaku.ktextension.mapToArray
@ -371,19 +372,12 @@ class AsyncTwitterWrapper(
TaskStarter.execute(task)
}
fun setActivitiesAboutMeUnreadAsync(accountKeys: Array<UserKey>, cursor: Long) {
val task = object : ExceptionHandlingAbstractTask<Any?, Unit, MicroBlogException, Any?>(context) {
override val exceptionClass = MicroBlogException::class.java
override fun onExecute(params: Any?) {
for (accountKey in accountKeys) {
val microBlog = MicroBlogAPIFactory.getInstance(context, accountKey) ?: continue
if (!AccountUtils.isOfficial(context, accountKey)) continue
microBlog.setActivitiesAboutMeUnread(cursor)
}
}
fun setActivitiesAboutMeUnreadAsync(accountKeys: Array<UserKey>, cursor: Long) = task {
for (accountKey in accountKeys) {
val microBlog = MicroBlogAPIFactory.getInstance(context, accountKey) ?: continue
if (!AccountUtils.isOfficial(context, accountKey)) continue
microBlog.setActivitiesAboutMeUnread(cursor)
}
TaskStarter.execute(task)
}
fun addUpdatingRelationshipId(accountKey: UserKey, userKey: UserKey) {