From 59566d966b8a98161c268a6e9a748d7114d06516 Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Thu, 9 Feb 2017 13:14:54 +0800 Subject: [PATCH] moved classes --- .../model/ParcelableMessageConversation.java | 6 ++ .../twidere/provider/TwidereDataStore.java | 1 + .../java/org/mariotaku/twidere/Constants.java | 2 +- .../model/util/ParcelableCardEntityUtils.java | 96 ------------------- .../model/util/ParcelableMediaUtils.java | 12 ++- ...ParcelableMessageConversationExtensions.kt | 23 +++++ .../model/ParcelableMessageExtensions.kt | 10 ++ .../model/util/ParcelableCardEntityUtils.kt | 82 ++++++++++++++++ .../model/util/ParcelableStatusUtils.kt | 6 +- .../model/util}/ParcelableUserUtils.kt | 0 .../preference}/RefreshIntervalPreference.kt | 0 .../mariotaku/twidere/task/GetMessagesTask.kt | 73 ++++++++------ .../twidere/util}/ContentValuesCreator.kt | 0 13 files changed, 175 insertions(+), 136 deletions(-) delete mode 100644 twidere/src/main/java/org/mariotaku/twidere/model/util/ParcelableCardEntityUtils.java create mode 100644 twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableMessageConversationExtensions.kt create mode 100644 twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableMessageExtensions.kt create mode 100644 twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableCardEntityUtils.kt rename twidere/src/main/kotlin/{ => org/mariotaku/twidere/model/util}/ParcelableUserUtils.kt (100%) rename twidere/src/main/kotlin/{ => org/mariotaku/twidere/preference}/RefreshIntervalPreference.kt (100%) rename twidere/src/main/kotlin/{ => org/mariotaku/twidere/util}/ContentValuesCreator.kt (100%) diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMessageConversation.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMessageConversation.java index 1a19a33d1..4324b263c 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMessageConversation.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMessageConversation.java @@ -29,6 +29,9 @@ public class ParcelableMessageConversation { @CursorField(value = Conversations.MESSAGE_TIMESTAMP) public long message_timestamp; + @CursorField(value = Conversations.LOCAL_TIMESTAMP) + public long local_timestamp; + @CursorField(Conversations.TEXT_UNESCAPED) public String text_unescaped; @CursorField(value = Conversations.MEDIA, converter = LoganSquareCursorFieldConverter.class) @@ -36,6 +39,9 @@ public class ParcelableMessageConversation { @CursorField(value = Conversations.SPANS, converter = LoganSquareCursorFieldConverter.class) public SpanItem[] spans; + @CursorField(value = Conversations.EXTRAS) + public String extras; + @CursorField(value = Conversations.PARTICIPANTS, converter = LoganSquareCursorFieldConverter.class) public ParcelableUser[] participants; diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/provider/TwidereDataStore.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/provider/TwidereDataStore.java index 614d14da2..195bd626d 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/provider/TwidereDataStore.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/provider/TwidereDataStore.java @@ -378,6 +378,7 @@ public interface TwidereDataStore { String CONVERSATION_ID = "conversation_id"; String MESSAGE_TYPE = "message_type"; String MESSAGE_TIMESTAMP = "message_timestamp"; + String LOCAL_TIMESTAMP = "local_timestamp"; String TEXT_UNESCAPED = "text_unescaped"; String MEDIA = "media"; String SPANS = "spans"; diff --git a/twidere/src/main/java/org/mariotaku/twidere/Constants.java b/twidere/src/main/java/org/mariotaku/twidere/Constants.java index 2eb705b39..8201d4171 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/Constants.java +++ b/twidere/src/main/java/org/mariotaku/twidere/Constants.java @@ -34,7 +34,7 @@ import static org.mariotaku.twidere.annotation.PreferenceType.STRING; public interface Constants extends TwidereConstants { String DATABASES_NAME = "twidere.sqlite"; - int DATABASES_VERSION = 166; + int DATABASES_VERSION = 167; int EXTRA_FEATURES_NOTICE_VERSION = 0; diff --git a/twidere/src/main/java/org/mariotaku/twidere/model/util/ParcelableCardEntityUtils.java b/twidere/src/main/java/org/mariotaku/twidere/model/util/ParcelableCardEntityUtils.java deleted file mode 100644 index 43fc4ff3a..000000000 --- a/twidere/src/main/java/org/mariotaku/twidere/model/util/ParcelableCardEntityUtils.java +++ /dev/null @@ -1,96 +0,0 @@ -package org.mariotaku.twidere.model.util; - - -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.util.ArrayMap; - -import org.apache.commons.lang3.math.NumberUtils; -import org.mariotaku.microblog.library.twitter.model.CardEntity; -import org.mariotaku.microblog.library.twitter.util.ThreadLocalSimpleDateFormat; -import org.mariotaku.twidere.TwidereConstants; -import org.mariotaku.twidere.model.ParcelableCardEntity; -import org.mariotaku.twidere.model.UserKey; - -import java.text.DateFormat; -import java.text.ParseException; -import java.util.Date; -import java.util.Locale; -import java.util.Map; -import java.util.TimeZone; - -/** - * Created by mariotaku on 16/2/24. - */ -public class ParcelableCardEntityUtils implements TwidereConstants { - - static final DateFormat sISOFormat = new ThreadLocalSimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", - Locale.ENGLISH); - - static { - sISOFormat.setLenient(true); - sISOFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - } - - @Nullable - public static ParcelableCardEntity fromCardEntity(@Nullable CardEntity card, @Nullable UserKey accountKey) { - if (card == null) return null; - final ParcelableCardEntity obj = new ParcelableCardEntity(); - obj.name = card.getName(); - obj.url = card.getUrl(); - obj.users = ParcelableUserUtils.INSTANCE.fromUsers(card.getUsers(), accountKey); - obj.account_key = accountKey; - obj.values = from(card.getBindingValues()); - return obj; - } - - public static Map from(@Nullable Map bindingValues) { - if (bindingValues == null) return null; - final ArrayMap map = new ArrayMap<>(); - for (Map.Entry entry : bindingValues.entrySet()) { - map.put(entry.getKey(), new ParcelableCardEntity.ParcelableBindingValue(entry.getValue())); - } - return map; - } - - public static boolean getAsBoolean(@NonNull ParcelableCardEntity obj, @NonNull String key, boolean def) { - final ParcelableCardEntity.ParcelableBindingValue value = obj.getValue(key); - if (value == null) return def; - return Boolean.parseBoolean(value.value); - } - - public static String getAsString(@NonNull ParcelableCardEntity obj, @NonNull String key, String def) { - final ParcelableCardEntity.ParcelableBindingValue value = obj.getValue(key); - if (value == null) return def; - return value.value; - } - - public static String getString(@NonNull ParcelableCardEntity obj, @NonNull String key) { - final ParcelableCardEntity.ParcelableBindingValue value = obj.getValue(key); - if (value == null || !CardEntity.BindingValue.TYPE_STRING.equals(value.type)) return null; - return getAsString(obj, key, null); - } - - public static int getAsInteger(@NonNull ParcelableCardEntity obj, @NonNull String key, int def) { - final ParcelableCardEntity.ParcelableBindingValue value = obj.getValue(key); - if (value == null) return def; - return NumberUtils.toInt(value.value, def); - } - - public static long getAsLong(@NonNull ParcelableCardEntity obj, @NonNull String key, long def) { - final ParcelableCardEntity.ParcelableBindingValue value = obj.getValue(key); - if (value == null) return def; - return NumberUtils.toLong(value.value, def); - } - - public static Date getAsDate(@NonNull ParcelableCardEntity obj, @NonNull String key, Date def) { - final ParcelableCardEntity.ParcelableBindingValue value = obj.getValue(key); - if (value == null) return def; - try { - return sISOFormat.parse(value.value); - } catch (ParseException e) { - return def; - } - } - -} diff --git a/twidere/src/main/java/org/mariotaku/twidere/model/util/ParcelableMediaUtils.java b/twidere/src/main/java/org/mariotaku/twidere/model/util/ParcelableMediaUtils.java index d76b92840..1821772ee 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/model/util/ParcelableMediaUtils.java +++ b/twidere/src/main/java/org/mariotaku/twidere/model/util/ParcelableMediaUtils.java @@ -17,6 +17,7 @@ import org.mariotaku.microblog.library.twitter.model.UrlEntity; import org.mariotaku.twidere.model.ParcelableMedia; import org.mariotaku.twidere.model.ParcelableMediaUpdate; import org.mariotaku.twidere.model.ParcelableStatus; +import org.mariotaku.twidere.model.UserKey; import org.mariotaku.twidere.util.InternalTwitterContentUtils; import org.mariotaku.twidere.util.TwidereArrayUtils; import org.mariotaku.twidere.util.media.preview.PreviewMediaExtractor; @@ -97,11 +98,11 @@ public class ParcelableMediaUtils { } @Nullable - public static ParcelableMedia[] fromStatus(@NonNull final Status status) { + public static ParcelableMedia[] fromStatus(@NonNull final Status status, UserKey accountKey) { final ParcelableMedia[] fromEntities = fromEntities(status); final ParcelableMedia[] fromAttachments = fromAttachments(status); final ParcelableMedia[] fromCard = fromCard(status.getCard(), status.getUrlEntities(), - status.getMediaEntities(), status.getExtendedMediaEntities()); + status.getMediaEntities(), status.getExtendedMediaEntities(), accountKey); final ParcelableMedia[] fromPhoto = fromPhoto(status); final ParcelableMedia[] merged = new ParcelableMedia[fromCard.length + fromAttachments.length + fromEntities.length + fromPhoto.length]; @@ -150,13 +151,14 @@ public class ParcelableMediaUtils { private static ParcelableMedia[] fromCard(@Nullable CardEntity card, @Nullable UrlEntity[] urlEntities, @Nullable MediaEntity[] mediaEntities, - @Nullable MediaEntity[] extendedMediaEntities) { + @Nullable MediaEntity[] extendedMediaEntities, + UserKey accountKey) { if (card == null) return new ParcelableMedia[0]; final String name = card.getName(); if ("animated_gif".equals(name) || "player".equals(name)) { final ParcelableMedia media = new ParcelableMedia(); final CardEntity.BindingValue playerStreamUrl = card.getBindingValue("player_stream_url"); - media.card = ParcelableCardEntityUtils.fromCardEntity(card, null); + media.card = ParcelableCardEntityUtils.INSTANCE.fromCardEntity(card, accountKey); CardEntity.StringValue appUrlResolved = (CardEntity.StringValue) card.getBindingValue("app_url_resolved"); media.url = checkUrl(appUrlResolved) ? appUrlResolved.getValue() : card.getUrl(); if ("animated_gif".equals(name)) { @@ -193,7 +195,7 @@ public class ParcelableMediaUtils { final ParcelableMedia media = new ParcelableMedia(); media.url = card.getUrl(); - media.card = ParcelableCardEntityUtils.fromCardEntity(card, null); + media.card = ParcelableCardEntityUtils.INSTANCE.fromCardEntity(card, accountKey); media.type = ParcelableMedia.Type.IMAGE; media.media_url = ((CardEntity.ImageValue) photoImageFullSize).getUrl(); media.width = ((CardEntity.ImageValue) photoImageFullSize).getWidth(); diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableMessageConversationExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableMessageConversationExtensions.kt new file mode 100644 index 000000000..d6741f1df --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableMessageConversationExtensions.kt @@ -0,0 +1,23 @@ +package org.mariotaku.twidere.extension.model + +import org.mariotaku.twidere.model.ParcelableMessage +import org.mariotaku.twidere.model.ParcelableMessageConversation + +fun ParcelableMessageConversation.setFrom(message: ParcelableMessage) { + account_key = message.account_key + id = message.conversation_id + message_type = message.message_type + message_timestamp = message.message_timestamp + local_timestamp = message.local_timestamp + text_unescaped = message.text_unescaped + media = message.media + spans = message.spans + extras = message.extras + sender_key = message.sender_key + recipient_key = message.recipient_key + is_outgoing = message.is_outgoing + request_cursor = message.request_cursor +} + +val ParcelableMessageConversation.timestamp: Long + get() = if (message_timestamp > 0) message_timestamp else local_timestamp diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableMessageExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableMessageExtensions.kt new file mode 100644 index 000000000..9b38782c9 --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableMessageExtensions.kt @@ -0,0 +1,10 @@ +package org.mariotaku.twidere.extension.model + +import org.mariotaku.twidere.model.ParcelableMessage + +/** + * Created by mariotaku on 2017/2/9. + */ + +val ParcelableMessage.timestamp: Long + get() = if (message_timestamp > 0) message_timestamp else local_timestamp diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableCardEntityUtils.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableCardEntityUtils.kt new file mode 100644 index 000000000..3a2aa5ab6 --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableCardEntityUtils.kt @@ -0,0 +1,82 @@ +package org.mariotaku.twidere.model.util + + +import android.support.v4.util.ArrayMap +import org.apache.commons.lang3.math.NumberUtils +import org.mariotaku.microblog.library.twitter.model.CardEntity +import org.mariotaku.microblog.library.twitter.util.ThreadLocalSimpleDateFormat +import org.mariotaku.twidere.model.ParcelableCardEntity +import org.mariotaku.twidere.model.UserKey +import java.text.DateFormat +import java.text.ParseException +import java.util.* + +/** + * Created by mariotaku on 16/2/24. + */ +object ParcelableCardEntityUtils { + + internal val sISOFormat: DateFormat = ThreadLocalSimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", + Locale.ENGLISH) + + init { + sISOFormat.isLenient = true + sISOFormat.timeZone = TimeZone.getTimeZone("UTC") + } + + fun fromCardEntity(card: CardEntity?, accountKey: UserKey?): ParcelableCardEntity? { + if (card == null) return null + val obj = ParcelableCardEntity() + obj.name = card.name + obj.url = card.url + obj.users = ParcelableUserUtils.fromUsers(card.users, accountKey) + obj.account_key = accountKey + obj.values = from(card.bindingValues) + return obj + } + + fun from(bindingValues: Map?): Map? { + if (bindingValues == null) return null + val map = ArrayMap() + for ((key, value) in bindingValues) { + map.put(key, ParcelableCardEntity.ParcelableBindingValue(value)) + } + return map + } + + fun getAsBoolean(obj: ParcelableCardEntity, key: String, def: Boolean): Boolean { + val value = obj.getValue(key) ?: return def + return java.lang.Boolean.parseBoolean(value.value) + } + + fun getAsString(obj: ParcelableCardEntity, key: String, def: String?): String? { + return obj.getValue(key)?.value ?: return def + } + + fun getString(obj: ParcelableCardEntity, key: String): String? { + val value = obj.getValue(key) + if (value == null || CardEntity.BindingValue.TYPE_STRING != value.type) return null + return getAsString(obj, key, null) + } + + fun getAsInteger(obj: ParcelableCardEntity, key: String, def: Int): Int { + val value = obj.getValue(key) ?: return def + return NumberUtils.toInt(value.value, def) + } + + fun getAsLong(obj: ParcelableCardEntity, key: String, def: Long): Long { + val value = obj.getValue(key) ?: return def + return NumberUtils.toLong(value.value, def) + } + + fun getAsDate(obj: ParcelableCardEntity, key: String, def: Date): Date { + val value = obj.getValue(key) ?: return def + try { + return sISOFormat.parse(value.value) + } catch (e: ParseException) { + return def + } + + } + +} diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableStatusUtils.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableStatusUtils.kt index 9375a1be8..26ffe3584 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableStatusUtils.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableStatusUtils.kt @@ -4,13 +4,11 @@ import android.text.Spannable import android.text.Spanned import android.text.style.URLSpan import org.mariotaku.microblog.library.twitter.model.Status -import org.mariotaku.twidere.TwidereConstants.USER_TYPE_FANFOU_COM import org.mariotaku.twidere.model.* import org.mariotaku.twidere.model.ParcelableStatus.FilterFlags import org.mariotaku.twidere.util.HtmlSpanBuilder import org.mariotaku.twidere.util.InternalTwitterContentUtils import org.mariotaku.twidere.util.TwitterContentUtils -import org.mariotaku.twidere.util.UserColorNameManager import java.util.* /** @@ -102,7 +100,7 @@ object ParcelableStatusUtils { result.quoted_timestamp = quoted.createdAt.time result.quoted_source = quoted.source - result.quoted_media = ParcelableMediaUtils.fromStatus(quoted) + result.quoted_media = ParcelableMediaUtils.fromStatus(quoted, accountKey) result.quoted_user_key = UserKeyUtils.fromUser(quotedUser) result.quoted_user_name = quotedUser.name @@ -156,7 +154,7 @@ object ParcelableStatusUtils { result.extras.display_text_range = textWithIndices.range } - result.media = ParcelableMediaUtils.fromStatus(status) + result.media = ParcelableMediaUtils.fromStatus(status, accountKey) result.source = status.source result.location = getLocation(status) result.is_favorite = status.isFavorited diff --git a/twidere/src/main/kotlin/ParcelableUserUtils.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableUserUtils.kt similarity index 100% rename from twidere/src/main/kotlin/ParcelableUserUtils.kt rename to twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableUserUtils.kt diff --git a/twidere/src/main/kotlin/RefreshIntervalPreference.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/preference/RefreshIntervalPreference.kt similarity index 100% rename from twidere/src/main/kotlin/RefreshIntervalPreference.kt rename to twidere/src/main/kotlin/org/mariotaku/twidere/preference/RefreshIntervalPreference.kt diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/task/GetMessagesTask.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/task/GetMessagesTask.kt index 52e9e127f..c5cdc46a9 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/task/GetMessagesTask.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/task/GetMessagesTask.kt @@ -4,12 +4,13 @@ import android.accounts.AccountManager import android.content.Context import org.mariotaku.microblog.library.MicroBlog import org.mariotaku.microblog.library.MicroBlogException -import org.mariotaku.microblog.library.twitter.model.DirectMessage import org.mariotaku.microblog.library.twitter.model.Paging import org.mariotaku.microblog.library.twitter.model.User import org.mariotaku.twidere.TwidereConstants.LOGTAG import org.mariotaku.twidere.annotation.AccountType import org.mariotaku.twidere.extension.model.newMicroBlogInstance +import org.mariotaku.twidere.extension.model.setFrom +import org.mariotaku.twidere.extension.model.timestamp import org.mariotaku.twidere.model.* import org.mariotaku.twidere.model.util.AccountUtils.getAccountDetails import org.mariotaku.twidere.model.util.ParcelableMessageUtils @@ -67,33 +68,6 @@ class GetMessagesTask(context: Context) : BaseAbstractTask= 0) { - participants[index] = ParcelableUserUtils.fromUser(user, accountKey) - } else { - this.participants = participants + ParcelableUserUtils.fromUser(user, accountKey) - } - } - } - - fun MutableMap.addConversation(accountKey: UserKey, - message: ParcelableMessage, - dm: DirectMessage) { - val conversation = this[message.conversation_id] ?: run { - val obj = ParcelableMessageConversation() - this[message.conversation_id] = obj - return@run obj - } - conversation.addParticipant(accountKey, dm.recipient) - conversation.addParticipant(accountKey, dm.sender) - } - val accountKey = details.key val paging = Paging() val insertMessages = arrayListOf() @@ -101,12 +75,12 @@ class GetMessagesTask(context: Context) : BaseAbstractTask val message = ParcelableMessageUtils.incomingMessage(accountKey, dm) insertMessages.add(message) - conversations.addConversation(accountKey, message, dm) + conversations.addConversation(accountKey, message, dm.sender, dm.recipient) } microBlog.getSentDirectMessages(paging).forEach { dm -> val message = ParcelableMessageUtils.outgoingMessage(accountKey, dm) insertMessages.add(message) - conversations.addConversation(accountKey, message, dm) + conversations.addConversation(accountKey, message, dm.sender, dm.recipient) } return GetMessagesData(conversations.values, emptyList(), insertMessages) } @@ -115,9 +89,48 @@ class GetMessagesTask(context: Context) : BaseAbstractTask= 0) { + participants[index] = ParcelableUserUtils.fromUser(user, accountKey) + } else { + this.participants = participants + ParcelableUserUtils.fromUser(user, accountKey) + } + } + } + + private fun MutableMap.addConversation( + accountKey: UserKey, + message: ParcelableMessage, + vararg users: User + ) { + val conversation = this[message.conversation_id] ?: run { + val obj = ParcelableMessageConversation() + this[message.conversation_id] = obj + obj.setFrom(message) + return@run obj + } + if (message.timestamp > conversation.timestamp) { + conversation.setFrom(message) + } + users.forEach { user -> + conversation.addParticipant(accountKey, user) + } + } + + data class GetMessagesData( val insertConversations: Collection, val updateConversations: Collection, val insertMessages: Collection ) } + diff --git a/twidere/src/main/kotlin/ContentValuesCreator.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/ContentValuesCreator.kt similarity index 100% rename from twidere/src/main/kotlin/ContentValuesCreator.kt rename to twidere/src/main/kotlin/org/mariotaku/twidere/util/ContentValuesCreator.kt