moved classes
This commit is contained in:
parent
e05a4fdd82
commit
59566d966b
|
@ -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;
|
||||
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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<String, ParcelableCardEntity.ParcelableBindingValue> from(@Nullable Map<String, CardEntity.BindingValue> bindingValues) {
|
||||
if (bindingValues == null) return null;
|
||||
final ArrayMap<String, ParcelableCardEntity.ParcelableBindingValue> map = new ArrayMap<>();
|
||||
for (Map.Entry<String, CardEntity.BindingValue> 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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<String, CardEntity.BindingValue>?): Map<String, ParcelableCardEntity.ParcelableBindingValue>? {
|
||||
if (bindingValues == null) return null
|
||||
val map = ArrayMap<String, ParcelableCardEntity.ParcelableBindingValue>()
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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<RefreshTaskParam, Uni
|
|||
}
|
||||
|
||||
private fun getDefaultMessages(microBlog: MicroBlog, details: AccountDetails): GetMessagesData {
|
||||
fun ParcelableMessageConversation.addParticipant(accountKey: UserKey, user: User) {
|
||||
val userKey = UserKeyUtils.fromUser(user)
|
||||
val participants = this.participants
|
||||
if (participants == null) {
|
||||
this.participants = arrayOf(ParcelableUserUtils.fromUser(user, accountKey))
|
||||
} else {
|
||||
val index = participants.indexOfFirst { it.key == userKey }
|
||||
if (index >= 0) {
|
||||
participants[index] = ParcelableUserUtils.fromUser(user, accountKey)
|
||||
} else {
|
||||
this.participants = participants + ParcelableUserUtils.fromUser(user, accountKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun MutableMap<String, ParcelableMessageConversation>.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<ParcelableMessage>()
|
||||
|
@ -101,12 +75,12 @@ class GetMessagesTask(context: Context) : BaseAbstractTask<RefreshTaskParam, Uni
|
|||
microBlog.getDirectMessages(paging).forEach { dm ->
|
||||
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<RefreshTaskParam, Uni
|
|||
DebugLog.d(LOGTAG, data.toString())
|
||||
}
|
||||
|
||||
private fun ParcelableMessageConversation.addParticipant(
|
||||
accountKey: UserKey,
|
||||
user: User
|
||||
) {
|
||||
val userKey = UserKeyUtils.fromUser(user)
|
||||
val participants = this.participants
|
||||
if (participants == null) {
|
||||
this.participants = arrayOf(ParcelableUserUtils.fromUser(user, accountKey))
|
||||
} else {
|
||||
val index = participants.indexOfFirst { it.key == userKey }
|
||||
if (index >= 0) {
|
||||
participants[index] = ParcelableUserUtils.fromUser(user, accountKey)
|
||||
} else {
|
||||
this.participants = participants + ParcelableUserUtils.fromUser(user, accountKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun MutableMap<String, ParcelableMessageConversation>.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<ParcelableMessageConversation>,
|
||||
val updateConversations: Collection<ParcelableMessageConversation>,
|
||||
val insertMessages: Collection<ParcelableMessage>
|
||||
)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue