compose code cleanup

This commit is contained in:
Mariotaku Lee 2017-04-14 15:05:51 +08:00
parent 0b27aad645
commit ec8267f8a5
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
4 changed files with 555 additions and 624 deletions

View File

@ -21,3 +21,7 @@ fun <T : Any> Array<T>.toNulls(): Array<T?> {
fun <T : Any> Array<T>.toStringArray(): Array<String> { fun <T : Any> Array<T>.toStringArray(): Array<String> {
return Array(size) { this[it].toString() } return Array(size) { this[it].toString() }
} }
inline fun <T : Any, reified R : Any> Array<T>.mapToArray(transform: (T) -> R): Array<R> {
return map(transform).toTypedArray()
}

View File

@ -31,9 +31,11 @@ import java.nio.charset.Charset
import java.util.* import java.util.*
/** val Draft.filename: String get() = "$unique_id_non_null.eml"
* Created by mariotaku on 2016/12/7.
*/ val Draft.unique_id_non_null: String
get() = unique_id ?: UUID.nameUUIDFromBytes(("$_id:$timestamp").toByteArray()).toString()
fun Draft.writeMimeMessageTo(context: Context, st: OutputStream) { fun Draft.writeMimeMessageTo(context: Context, st: OutputStream) {
val bodyFactory = StorageBodyFactory() val bodyFactory = StorageBodyFactory()
val storageProvider = bodyFactory.storageProvider val storageProvider = bodyFactory.storageProvider
@ -126,10 +128,15 @@ fun Draft.getActionName(context: Context): String? {
return null return null
} }
val Draft.filename: String get() = "$unique_id_non_null.eml" fun Draft.applyUpdateStatus(statusUpdate: ParcelableStatusUpdate) {
this.unique_id = statusUpdate.draft_unique_id ?: UUID.randomUUID().toString()
val Draft.unique_id_non_null: String this.account_keys = statusUpdate.accounts.map { it.key }.toTypedArray()
get() = unique_id ?: UUID.nameUUIDFromBytes(("$_id:$timestamp").toByteArray()).toString() this.text = statusUpdate.text
this.location = statusUpdate.location
this.media = statusUpdate.media
this.timestamp = System.currentTimeMillis()
this.action_extras = statusUpdate.draft_extras
}
private class DraftContentHandler(private val context: Context, private val draft: Draft) : SimpleContentHandler() { private class DraftContentHandler(private val context: Context, private val draft: Draft) : SimpleContentHandler() {
private val processingStack = Stack<SimpleContentHandler>() private val processingStack = Stack<SimpleContentHandler>()

View File

@ -33,10 +33,10 @@ import org.mariotaku.twidere.R
import org.mariotaku.twidere.TwidereConstants.* import org.mariotaku.twidere.TwidereConstants.*
import org.mariotaku.twidere.annotation.AccountType import org.mariotaku.twidere.annotation.AccountType
import org.mariotaku.twidere.app.TwidereApplication import org.mariotaku.twidere.app.TwidereApplication
import org.mariotaku.twidere.extension.model.applyUpdateStatus
import org.mariotaku.twidere.extension.model.mediaSizeLimit import org.mariotaku.twidere.extension.model.mediaSizeLimit
import org.mariotaku.twidere.extension.model.newMicroBlogInstance import org.mariotaku.twidere.extension.model.newMicroBlogInstance
import org.mariotaku.twidere.extension.model.textLimit import org.mariotaku.twidere.extension.model.textLimit
import org.mariotaku.twidere.extension.text.twitter.extractReplyTextAndMentions
import org.mariotaku.twidere.extension.text.twitter.getTweetLength import org.mariotaku.twidere.extension.text.twitter.getTweetLength
import org.mariotaku.twidere.model.* import org.mariotaku.twidere.model.*
import org.mariotaku.twidere.model.account.AccountExtras import org.mariotaku.twidere.model.account.AccountExtras
@ -137,31 +137,6 @@ class UpdateStatusTask(
return result return result
} }
private fun regulateReplyText(update: ParcelableStatusUpdate, pending: PendingStatusUpdate) {
if (update.draft_action != Draft.Action.REPLY) return
val inReplyTo = update.in_reply_to_status ?: return
for (i in 0 until pending.length) {
val details = update.accounts[i]
if (details.type != AccountType.TWITTER) continue
val (_, replyText, extraMentions, excludedMentions, replyToOriginalUser) =
extractor.extractReplyTextAndMentions(pending.overrideTexts[i], inReplyTo)
pending.overrideTexts[i] = replyText
val excludeReplyUserIds = excludedMentions.mapNotNull { mention ->
// Remove account mention that is not appeared in `extraMentions`
if (details.key == mention.key && extraMentions.none {
it.value.equals(mention.screen_name, ignoreCase = true)
}) return@mapNotNull null
return@mapNotNull mention.key.id
}.toTypedArray()
val replyToSelf = details.key == inReplyTo.user_key
// Fix status to at least make mentioned user know what status it is
if (!replyToOriginalUser && !replyToSelf && update.attachment_url == null) {
update.attachment_url = LinkCreator.getTwitterStatusLink(inReplyTo.user_screen_name,
inReplyTo.id).toString()
}
}
}
private fun deleteOrUpdateDraft(update: ParcelableStatusUpdate, result: UpdateStatusResult, private fun deleteOrUpdateDraft(update: ParcelableStatusUpdate, result: UpdateStatusResult,
draftId: Long) { draftId: Long) {
val where = Expression.equals(Drafts._ID, draftId).sql val where = Expression.equals(Drafts._ID, draftId).sql
@ -520,16 +495,11 @@ class UpdateStatusTask(
private fun saveDraft(statusUpdate: ParcelableStatusUpdate): Long { private fun saveDraft(statusUpdate: ParcelableStatusUpdate): Long {
return saveDraft(context, statusUpdate.draft_action ?: Draft.Action.UPDATE_STATUS) { return saveDraft(context, statusUpdate.draft_action ?: Draft.Action.UPDATE_STATUS) {
this.unique_id = statusUpdate.draft_unique_id ?: UUID.randomUUID().toString() applyUpdateStatus(statusUpdate)
this.account_keys = statusUpdate.accounts.map { it.key }.toTypedArray()
this.text = statusUpdate.text
this.location = statusUpdate.location
this.media = statusUpdate.media
this.timestamp = System.currentTimeMillis()
this.action_extras = statusUpdate.draft_extras
} }
} }
class PendingStatusUpdate internal constructor(val length: Int, defaultText: String) { class PendingStatusUpdate internal constructor(val length: Int, defaultText: String) {
internal constructor(statusUpdate: ParcelableStatusUpdate) : this(statusUpdate.accounts.size, internal constructor(statusUpdate: ParcelableStatusUpdate) : this(statusUpdate.accounts.size,
@ -996,17 +966,23 @@ class UpdateStatusTask(
val deleteAlways: List<MediaDeletionItem>? val deleteAlways: List<MediaDeletionItem>?
) )
fun saveDraft(context: Context, @Draft.Action action: String, config: Draft.() -> Unit): Long { inline fun createDraft(@Draft.Action action: String, config: Draft.() -> Unit): Draft {
val draft = Draft() val draft = Draft()
draft.action_type = action draft.action_type = action
draft.timestamp = System.currentTimeMillis() draft.timestamp = System.currentTimeMillis()
config(draft) config(draft)
return draft
}
fun saveDraft(context: Context, @Draft.Action action: String, config: Draft.() -> Unit): Long {
val draft = createDraft(action, config)
val resolver = context.contentResolver val resolver = context.contentResolver
val creator = ObjectCursor.valuesCreatorFrom(Draft::class.java) val creator = ObjectCursor.valuesCreatorFrom(Draft::class.java)
val draftUri = resolver.insert(Drafts.CONTENT_URI, creator.create(draft)) ?: return -1 val draftUri = resolver.insert(Drafts.CONTENT_URI, creator.create(draft)) ?: return -1
return draftUri.lastPathSegment.toLongOr(-1) return draftUri.lastPathSegment.toLongOr(-1)
} }
fun deleteDraft(context: Context, id: Long) { fun deleteDraft(context: Context, id: Long) {
val where = Expression.equals(Drafts._ID, id).sql val where = Expression.equals(Drafts._ID, id).sql
context.contentResolver.delete(Drafts.CONTENT_URI, where, null) context.contentResolver.delete(Drafts.CONTENT_URI, where, null)