From 3a60dd88c05d92dd90bb2c7f80a410969499b3ba Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Mon, 27 Mar 2017 13:46:04 +0800 Subject: [PATCH] fixed unix epoch converter --- .../model/ResponseList$$JsonObjectMapper.java | 8 +- .../util/UnixEpochMillisDateConverter.java | 26 ++--- .../util/UnixEpochSecondDateConverter.java | 26 ++--- .../model/util/ParcelableMediaUtils.java | 1 + .../twidere/view/holder/DraftViewHolder.java | 50 ---------- .../twidere/adapter/DraftsAdapter.kt | 60 ++--------- .../twidere/view/holder/DraftViewHolder.kt | 99 +++++++++++++++++++ .../src/main/res/layout/list_item_draft.xml | 33 +++---- twidere/src/main/res/values/strings.xml | 6 +- 9 files changed, 148 insertions(+), 161 deletions(-) delete mode 100644 twidere/src/main/java/org/mariotaku/twidere/view/holder/DraftViewHolder.java create mode 100644 twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/DraftViewHolder.kt diff --git a/twidere.component.common/src/main/java/org/mariotaku/microblog/library/twitter/model/ResponseList$$JsonObjectMapper.java b/twidere.component.common/src/main/java/org/mariotaku/microblog/library/twitter/model/ResponseList$$JsonObjectMapper.java index 522ea9bf1..419ca7c14 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/microblog/library/twitter/model/ResponseList$$JsonObjectMapper.java +++ b/twidere.component.common/src/main/java/org/mariotaku/microblog/library/twitter/model/ResponseList$$JsonObjectMapper.java @@ -46,7 +46,7 @@ public final class ResponseList$$JsonObjectMapper extends JsonMapper instance = new ResponseList<>(); while (jsonParser.nextToken() != JsonToken.END_OBJECT) { String fieldName = jsonParser.getCurrentName(); @@ -56,10 +56,10 @@ public final class ResponseList$$JsonObjectMapper extends JsonMapper(m84ClassJsonMapper.parseList(jsonParser)); } - return new ResponseList<>(m84ClassJsonMapper.parseList(jsonParser)); + jsonParser.skipChildren(); + throw new IOException("Unsupported object"); } @Override diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/util/UnixEpochMillisDateConverter.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/util/UnixEpochMillisDateConverter.java index 4dc6ede4c..aa4e7602f 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/util/UnixEpochMillisDateConverter.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/util/UnixEpochMillisDateConverter.java @@ -19,34 +19,24 @@ package org.mariotaku.twidere.model.util; -import com.bluelinelabs.logansquare.typeconverters.TypeConverter; -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; +import com.bluelinelabs.logansquare.typeconverters.LongBasedTypeConverter; -import java.io.IOException; import java.util.Date; /** * Created by mariotaku on 2017/3/25. */ -public class UnixEpochMillisDateConverter implements TypeConverter { +public class UnixEpochMillisDateConverter extends LongBasedTypeConverter { @Override - public Date parse(final JsonParser jsonParser) throws IOException { - long value = jsonParser.nextLongValue(-1); - return new Date(value); + public Date getFromLong(final long l) { + return new Date(l); } @Override - public void serialize(final Date object, final String fieldName, - final boolean writeFieldNameForObject, final JsonGenerator jsonGenerator) throws IOException { - if (writeFieldNameForObject) { - jsonGenerator.writeFieldName(fieldName); - } - if (object == null) { - jsonGenerator.writeNull(); - } else { - jsonGenerator.writeNumber(object.getTime()); - } + public long convertToLong(final Date object) { + if (object == null) return -1; + return object.getTime(); } + } diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/util/UnixEpochSecondDateConverter.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/util/UnixEpochSecondDateConverter.java index 659642ea0..b81a3780e 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/util/UnixEpochSecondDateConverter.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/util/UnixEpochSecondDateConverter.java @@ -19,11 +19,8 @@ package org.mariotaku.twidere.model.util; -import com.bluelinelabs.logansquare.typeconverters.TypeConverter; -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; +import com.bluelinelabs.logansquare.typeconverters.LongBasedTypeConverter; -import java.io.IOException; import java.util.Date; import java.util.concurrent.TimeUnit; @@ -31,23 +28,16 @@ import java.util.concurrent.TimeUnit; * Created by mariotaku on 2017/3/25. */ -public class UnixEpochSecondDateConverter implements TypeConverter { +public class UnixEpochSecondDateConverter extends LongBasedTypeConverter { @Override - public Date parse(final JsonParser jsonParser) throws IOException { - long value = jsonParser.nextLongValue(-1); - return new Date(TimeUnit.MILLISECONDS.toSeconds(value)); + public Date getFromLong(final long l) { + return new Date(TimeUnit.SECONDS.toMillis(l)); } @Override - public void serialize(final Date object, final String fieldName, - final boolean writeFieldNameForObject, final JsonGenerator jsonGenerator) throws IOException { - if (writeFieldNameForObject) { - jsonGenerator.writeFieldName(fieldName); - } - if (object == null) { - jsonGenerator.writeNull(); - } else { - jsonGenerator.writeNumber(TimeUnit.SECONDS.toMillis(object.getTime())); - } + public long convertToLong(final Date object) { + if (object == null) return -1; + return TimeUnit.MILLISECONDS.toSeconds(object.getTime()); } + } 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 c640258c4..f2d1e0848 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 @@ -253,6 +253,7 @@ public class ParcelableMediaUtils { return ParcelableMedia.Type.UNKNOWN; } + @NonNull public static ParcelableMedia image(final String url) { ParcelableMedia media = new ParcelableMedia(); media.type = ParcelableMedia.Type.IMAGE; diff --git a/twidere/src/main/java/org/mariotaku/twidere/view/holder/DraftViewHolder.java b/twidere/src/main/java/org/mariotaku/twidere/view/holder/DraftViewHolder.java deleted file mode 100644 index df5ad7392..000000000 --- a/twidere/src/main/java/org/mariotaku/twidere/view/holder/DraftViewHolder.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Twidere - Twitter client for Android - * - * Copyright (C) 2012-2014 Mariotaku Lee - * - * 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 . - */ - -package org.mariotaku.twidere.view.holder; - -import android.support.v7.widget.RecyclerView; -import android.view.View; -import android.widget.TextView; - -import org.mariotaku.twidere.R; -import org.mariotaku.twidere.view.CardMediaContainer; -import org.mariotaku.twidere.view.iface.IColorLabelView; - -public class DraftViewHolder extends RecyclerView.ViewHolder { - - public final IColorLabelView content; - public final TextView text; - public final TextView time; - public CardMediaContainer mediaPreviewContainer; - - public DraftViewHolder(final View itemView) { - super(itemView); - content = (IColorLabelView) itemView.findViewById(R.id.content); - text = (TextView) itemView.findViewById(R.id.text); - time = (TextView) itemView.findViewById(R.id.time); - mediaPreviewContainer = (CardMediaContainer) itemView.findViewById(R.id.media_preview_container); - } - - public void setTextSize(final float textSize) { - text.setTextSize(textSize); - time.setTextSize(textSize * 0.75f); - } - -} diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/DraftsAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/DraftsAdapter.kt index 7204c3994..8fa97afda 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/DraftsAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/DraftsAdapter.kt @@ -22,20 +22,15 @@ package org.mariotaku.twidere.adapter import android.content.Context import android.database.Cursor import android.support.v4.widget.SimpleCursorAdapter +import android.support.v7.widget.RecyclerViewAccessor import android.view.View import android.view.ViewGroup import com.bumptech.glide.RequestManager import org.mariotaku.kpreferences.get import org.mariotaku.library.objectcursor.ObjectCursor -import org.mariotaku.twidere.R import org.mariotaku.twidere.constant.mediaPreviewStyleKey -import org.mariotaku.twidere.extension.model.getActionName import org.mariotaku.twidere.model.Draft -import org.mariotaku.twidere.model.draft.StatusObjectExtras -import org.mariotaku.twidere.model.util.ParcelableMediaUtils -import org.mariotaku.twidere.util.DataStoreUtils import org.mariotaku.twidere.util.SharedPreferencesWrapper -import org.mariotaku.twidere.util.Utils import org.mariotaku.twidere.util.dagger.GeneralComponentHelper import org.mariotaku.twidere.view.holder.DraftViewHolder import javax.inject.Inject @@ -43,7 +38,7 @@ import javax.inject.Inject class DraftsAdapter( context: Context, val requestManager: RequestManager -) : SimpleCursorAdapter(context, R.layout.list_item_draft, null, arrayOfNulls(0), IntArray(0), 0) { +) : SimpleCursorAdapter(context, DraftViewHolder.layoutResource, null, emptyArray(), intArrayOf(), 0) { @Inject lateinit var preferences: SharedPreferencesWrapper @@ -55,6 +50,7 @@ class DraftsAdapter( field = value notifyDataSetChanged() } + private var indices: ObjectCursor.CursorIndices? = null init { @@ -63,59 +59,19 @@ class DraftsAdapter( } override fun bindView(view: View, context: Context, cursor: Cursor) { - val holder = view.tag as DraftViewHolder val draft = indices!!.newObject(cursor) - val accountKeys = draft.account_keys - val actionType: String = draft.action_type ?: Draft.Action.UPDATE_STATUS - val actionName = draft.getActionName(context) - var summaryText: String? = null - when (actionType) { - Draft.Action.SEND_DIRECT_MESSAGE, Draft.Action.SEND_DIRECT_MESSAGE_COMPAT, - Draft.Action.UPDATE_STATUS, Draft.Action.UPDATE_STATUS_COMPAT_1, - Draft.Action.UPDATE_STATUS_COMPAT_2, Draft.Action.REPLY, Draft.Action.QUOTE -> { - val media = ParcelableMediaUtils.fromMediaUpdates(draft.media) - holder.mediaPreviewContainer.visibility = View.VISIBLE - holder.mediaPreviewContainer.displayMedia(requestManager = requestManager, - media = media) - } - Draft.Action.FAVORITE, Draft.Action.RETWEET -> { - val extras = draft.action_extras as? StatusObjectExtras - if (extras != null) { - summaryText = extras.status.text_unescaped - } - holder.mediaPreviewContainer.visibility = View.GONE - } - else -> { - holder.mediaPreviewContainer.visibility = View.GONE - } - } - if (accountKeys != null) { - holder.content.drawEnd(*DataStoreUtils.getAccountColors(context, accountKeys)) - } else { - holder.content.drawEnd() - } - holder.setTextSize(textSize) - if (summaryText != null) { - holder.text.text = summaryText - } else if (draft.text.isNullOrEmpty()) { - holder.text.setText(R.string.empty_content) - } else { - holder.text.text = draft.text - } + val holder = view.tag as DraftViewHolder + RecyclerViewAccessor.setLayoutPosition(holder, cursor.position) - if (draft.timestamp > 0) { - val timeString = Utils.formatSameDayTime(context, draft.timestamp) - holder.time.text = context.getString(R.string.action_name_saved_at_time, actionName, timeString) - } else { - holder.time.text = actionName - } + holder.display(context, requestManager, draft) } - override fun newView(context: Context?, cursor: Cursor?, parent: ViewGroup): View { + override fun newView(context: Context, cursor: Cursor, parent: ViewGroup): View { val view = super.newView(context, cursor, parent) if (view.tag !is DraftViewHolder) { view.tag = DraftViewHolder(view).apply { + this.setTextSize(textSize) this.mediaPreviewContainer.style = mediaPreviewStyle } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/DraftViewHolder.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/DraftViewHolder.kt new file mode 100644 index 000000000..027ab2c58 --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/DraftViewHolder.kt @@ -0,0 +1,99 @@ +/* + * Twidere - Twitter client for Android + * + * Copyright (C) 2012-2017 Mariotaku Lee + * + * 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 . + */ + +package org.mariotaku.twidere.view.holder + +import android.content.Context +import android.support.v7.widget.RecyclerView +import android.view.View +import com.bumptech.glide.RequestManager +import kotlinx.android.synthetic.main.list_item_draft.view.* +import org.mariotaku.twidere.R +import org.mariotaku.twidere.extension.model.getActionName +import org.mariotaku.twidere.model.Draft +import org.mariotaku.twidere.model.draft.StatusObjectExtras +import org.mariotaku.twidere.model.util.ParcelableMediaUtils +import org.mariotaku.twidere.util.DataStoreUtils +import org.mariotaku.twidere.util.Utils + +class DraftViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { + + internal val contentView = itemView.content + internal val textView = itemView.text + internal val timeView = itemView.time + internal val mediaPreviewContainer = itemView.mediaPreviewContainer + + fun display(context: Context, requestManager: RequestManager, draft: Draft) { + val accountKeys = draft.account_keys + val actionType: String = draft.action_type ?: Draft.Action.UPDATE_STATUS + val actionName = draft.getActionName(context) + var summaryText: String? = null + when (actionType) { + Draft.Action.SEND_DIRECT_MESSAGE, Draft.Action.SEND_DIRECT_MESSAGE_COMPAT, + Draft.Action.UPDATE_STATUS, Draft.Action.UPDATE_STATUS_COMPAT_1, + Draft.Action.UPDATE_STATUS_COMPAT_2, Draft.Action.REPLY, Draft.Action.QUOTE -> { + val media = ParcelableMediaUtils.fromMediaUpdates(draft.media) + mediaPreviewContainer.visibility = View.VISIBLE + mediaPreviewContainer.displayMedia(requestManager = requestManager, + media = media) + } + Draft.Action.FAVORITE, Draft.Action.RETWEET -> { + val extras = draft.action_extras as? StatusObjectExtras + if (extras != null) { + summaryText = extras.status.text_unescaped + } + mediaPreviewContainer.visibility = View.GONE + } + else -> { + mediaPreviewContainer.visibility = View.GONE + } + } + if (accountKeys != null) { + contentView.drawEnd(*DataStoreUtils.getAccountColors(context, accountKeys)) + } else { + contentView.drawEnd() + } + if (summaryText != null) { + textView.text = summaryText + } else if (draft.text.isNullOrEmpty()) { + textView.setText(R.string.empty_content) + } else { + textView.text = draft.text + } + + if (draft.timestamp > 0) { + val timeString = Utils.formatSameDayTime(context, draft.timestamp) + timeView.text = context.getString(R.string.action_name_saved_at_time, actionName, timeString) + } else { + timeView.text = actionName + } + } + + fun setTextSize(textSize: Float) { + textView.textSize = textSize + timeView.textSize = textSize * 0.75f + } + + companion object { + + const val layoutResource = R.layout.list_item_draft + + } + +} diff --git a/twidere/src/main/res/layout/list_item_draft.xml b/twidere/src/main/res/layout/list_item_draft.xml index aaee42992..d5ae4b063 100644 --- a/twidere/src/main/res/layout/list_item_draft.xml +++ b/twidere/src/main/res/layout/list_item_draft.xml @@ -26,20 +26,30 @@ android:layout_height="wrap_content" android:background="?android:activatedBackgroundIndicator" android:descendantFocusability="blocksDescendants" + android:gravity="center_vertical" + android:minHeight="?android:listPreferredItemHeightSmall" android:orientation="vertical" android:padding="@dimen/element_spacing_normal" app:backgroundTint="?colorControlActivated" app:ignorePadding="true" tools:context=".adapter.DraftsAdapter"> + + @@ -47,27 +57,14 @@ - - + android:textColor="?android:textColorPrimary" + tools:text="12:00 April 1"/> Block blocking + Disconnect from Buffer Settings Cancel retweet @@ -516,6 +517,7 @@ Buffer accounts Twidere database provider Subscription + %1$s via %2$s Your coarse location Media Original tweet @@ -599,6 +601,7 @@ Leave this conversation? Direct message deleted. Direct message sent. + Unable to connect to Buffer Schedule media with Buffer is not yet supported Some account data are corrupted, Twidere will remove those accounts to prevent crash. This tweet will be sent as a message to %1$s, continue? Converting to a normal tweet uses an extra character. @@ -610,6 +613,7 @@ %1$s added %2$s %1$s left %1$s ยท %2$s + Buffer queue is empty Joined conversation. Twidere needs location permission for adding location to tweets. Twidere needs storage permission for saving media. @@ -1266,5 +1270,5 @@ User\'s tweets Vibration - Disconnect from Buffer + No time