Added clear messages

This commit is contained in:
Mariotaku Lee 2017-03-26 00:18:17 +08:00
parent 355ee72a0f
commit 26bcfde9d2
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
7 changed files with 130 additions and 31 deletions

View File

@ -92,6 +92,7 @@ import org.mariotaku.twidere.model.util.AccountUtils
import org.mariotaku.twidere.provider.TwidereDataStore.Messages.Conversations
import org.mariotaku.twidere.task.twitter.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
@ -210,6 +211,7 @@ class MessageConversationInfoFragment : BaseFragment(), IToolBarSupportFragment,
}
override fun onPrepareOptionsMenu(menu: Menu) {
menu.setItemAvailability(R.id.clear_messages, true)
if (adapter.conversation?.conversation_extras_type == ExtrasType.TWITTER_OFFICIAL) {
menu.setItemAvailability(R.id.leave_conversation, true)
} else {
@ -224,6 +226,11 @@ class MessageConversationInfoFragment : BaseFragment(), IToolBarSupportFragment,
df.show(childFragmentManager, "destroy_conversation_confirm")
return true
}
R.id.clear_messages -> {
val df = ClearMessagesConfirmDialogFragment()
df.show(childFragmentManager, "clear_messages_confirm")
return true
}
}
return false
}
@ -294,6 +301,21 @@ class MessageConversationInfoFragment : BaseFragment(), IToolBarSupportFragment,
TaskStarter.execute(task)
}
private fun performClearMessages() {
ProgressDialogFragment.show(childFragmentManager, "clear_messages_progress")
val weakThis = WeakReference(this)
val task = ClearMessagesTask(context, accountKey, conversationId)
task.callback = callback@ { succeed ->
val f = weakThis.get() ?: return@callback
f.dismissDialogThen("clear_messages_progress") {
if (succeed) {
activity?.finish()
}
}
}
TaskStarter.execute(task)
}
private fun performAddParticipant(user: ParcelableUser) {
ProgressDialogFragment.show(childFragmentManager, "add_participant_progress")
val weakThis = WeakReference(this)
@ -744,6 +766,24 @@ class MessageConversationInfoFragment : BaseFragment(), IToolBarSupportFragment,
}
class ClearMessagesConfirmDialogFragment : BaseDialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(context)
builder.setMessage(R.string.message_clear_messages_confirm)
builder.setPositiveButton(R.string.action_clear_messages) { _, _ ->
(parentFragment as MessageConversationInfoFragment).performClearMessages()
}
builder.setNegativeButton(android.R.string.cancel, null)
val dialog = builder.create()
dialog.setOnShowListener {
it as AlertDialog
it.applyTheme()
}
return dialog
}
}
internal class ConversationInfoDecoration(
val adapter: ConversationInfoAdapter,
val typeSpacing: Int

View File

@ -69,10 +69,8 @@ import org.mariotaku.twidere.task.CreateFavoriteTask
import org.mariotaku.twidere.task.RetweetStatusTask
import org.mariotaku.twidere.task.twitter.UpdateStatusTask
import org.mariotaku.twidere.task.twitter.message.SendMessageTask
import org.mariotaku.twidere.util.NotificationManagerWrapper
import org.mariotaku.twidere.util.Utils
import org.mariotaku.twidere.util.deleteDrafts
import org.mariotaku.twidere.util.io.ContentLengthInputStream.ReadListener
import java.io.IOException
import java.util.concurrent.TimeUnit
@ -402,27 +400,6 @@ class LengthyOperationsService : BaseIntentService("lengthy_operations") {
handler.post { ManualTaskStarter.invokeAfterExecute(task, result) }
}
internal class MessageMediaUploadListener(private val context: Context, private val manager: NotificationManagerWrapper,
builder: NotificationCompat.Builder, private val message: String) : ReadListener {
var percent: Int = 0
private val builder: Builder
init {
this.builder = builder
}
override fun onRead(length: Long, position: Long) {
val percent = if (length > 0) (position * 100 / length).toInt() else 0
if (this.percent != percent) {
manager.notify(NOTIFICATION_ID_SEND_DIRECT_MESSAGE,
updateSendDirectMessageNotification(context, builder, percent, message))
}
this.percent = percent
}
}
companion object {
private val BULK_SIZE = (128 * 1024).toLong() // 128KiB

View File

@ -262,11 +262,7 @@ class UpdateStatusTask(
throw SchedulerNotFoundException("No scheduler found")
}
try {
controller.scheduleStatus(statusUpdate, pendingUpdate.overrideTexts, scheduleInfo)
} catch (e: ScheduleException) {
return UpdateStatusResult(e, draftId)
}
controller.scheduleStatus(statusUpdate, pendingUpdate.overrideTexts, scheduleInfo)
return UpdateStatusResult(pendingUpdate.length, draftId)
}

View File

@ -0,0 +1,80 @@
/*
* 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.ktextension.useCursor
import org.mariotaku.microblog.library.MicroBlog
import org.mariotaku.microblog.library.MicroBlogException
import org.mariotaku.sqliteqb.library.Expression
import org.mariotaku.twidere.extension.model.newMicroBlogInstance
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.ExceptionHandlingAbstractTask
import org.mariotaku.twidere.util.content.ContentResolverUtils
/**
* Created by mariotaku on 2017/2/16.
*/
class ClearMessagesTask(
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 messagesWhere = Expression.and(Expression.equalsArgs(Messages.ACCOUNT_KEY),
Expression.equalsArgs(Messages.CONVERSATION_ID)).sql
val messagesWhereArgs = arrayOf(accountKey.toString(), conversationId)
val projection = arrayOf(Messages.MESSAGE_ID)
val messageIds = mutableListOf<String>()
context.contentResolver.query(Messages.CONTENT_URI, projection, messagesWhere,
messagesWhereArgs, null)?.useCursor { cur ->
cur.moveToFirst()
while (!cur.isAfterLast) {
val messageId = cur.getString(0)
try {
if (DestroyMessageTask.performDestroyMessage(context, microBlog, account,
messageId)) {
messageIds.add(messageId)
}
} catch (e: MicroBlogException) {
// Ignore
}
cur.moveToNext()
}
}
ContentResolverUtils.bulkDelete(context.contentResolver, Messages.CONTENT_URI,
Messages.MESSAGE_ID, false, messageIds, messagesWhere, messagesWhereArgs)
return true
}
override fun afterExecute(callback: ((Boolean) -> Unit)?, result: Boolean?, exception: MicroBlogException?) {
callback?.invoke(result ?: false)
}
}

View File

@ -71,7 +71,7 @@ class DestroyMessageTask(
companion object {
private fun performDestroyMessage(context: Context, microBlog: MicroBlog,
internal fun performDestroyMessage(context: Context, microBlog: MicroBlog,
account: AccountDetails, messageId: String): Boolean {
when (account.type) {
AccountType.TWITTER -> {

View File

@ -21,6 +21,10 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/leave_conversation"
android:icon="@drawable/ic_action_delete"
android:icon="@drawable/ic_action_cancel"
android:title="@string/action_leave_conversation"/>
<item
android:id="@+id/clear_messages"
android:icon="@drawable/ic_action_delete"
android:title="@string/action_clear_messages"/>
</menu>

View File

@ -31,6 +31,7 @@
<string name="action_cancelling_like">cancelling like</string>
<string name="action_center">Center</string>
<string name="action_clear">Clear</string>
<string name="action_clear_messages">Clear messages</string>
<string name="action_compose">Compose</string>
<string name="action_compose_message_convert_to_status">Convert to tweet</string>
<string name="action_connect_to_buffer">Connect</string>
@ -592,6 +593,7 @@
<string name="message_api_url_format_help">[DOMAIN]: Twitter API domain.\nExample: https://[DOMAIN].twitter.com/ will be replaced to https://api.twitter.com/.</string>
<string name="message_auto_refresh_confirm">Enable auto refresh to get new tweets automatically?</string>
<string name="message_blocked_user">Blocked <xliff:g id="user">%s</xliff:g>.</string>
<string name="message_clear_messages_confirm">Clear all messages?</string>
<string name="message_conversation_created">Conversation created.</string>
<string name="message_destroy_conversation_confirm">Leave this conversation?</string>
<string name="message_direct_message_deleted">Direct message deleted.</string>
@ -628,6 +630,7 @@
<string name="message_toast_added_to_filter">Added to filter</string>
<string name="message_toast_added_user_to_list">Added <xliff:g id="user">%1$s</xliff:g> to list \"<xliff:g id="list">%2$s</xliff:g>\"</string>
<string name="message_toast_already_logged_in">You have already logged in</string>
<string name="message_toast_buffer_connection_failed">Unable to communicate with Buffer</string>
<string name="message_toast_cannot_get_location">Can\'t get your location</string>
<string name="message_toast_compose_write_storage_no_permission">Permission required to delete taken photo/video</string>
<string name="message_toast_duplicate_filter_rule">Rule duplicated</string>
@ -1255,5 +1258,4 @@
<string name="users_statuses">User\'s tweets</string>
<string name="vibration">Vibration</string>
<string name="message_toast_buffer_connection_failed">Unable to communicate with Buffer</string>
</resources>