diff --git a/twidere/src/main/java/org/mariotaku/twidere/view/ShortTimeView.java b/twidere/src/main/java/org/mariotaku/twidere/view/ShortTimeView.java deleted file mode 100644 index 33fc82680..000000000 --- a/twidere/src/main/java/org/mariotaku/twidere/view/ShortTimeView.java +++ /dev/null @@ -1,120 +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; - -import android.content.Context; -import android.os.Handler; -import android.os.SystemClock; -import android.support.v7.widget.AppCompatTextView; -import android.text.TextUtils; -import android.text.format.DateUtils; -import android.util.AttributeSet; - -import org.mariotaku.twidere.Constants; -import org.mariotaku.twidere.R; - -import java.lang.ref.WeakReference; - -import static org.mariotaku.twidere.util.Utils.formatSameDayTime; - -public class ShortTimeView extends AppCompatTextView implements Constants { - - private static final long TICKER_DURATION = 5000L; - - private final Runnable mTicker; - private boolean mShowAbsoluteTime; - private long mTime; - - public ShortTimeView(final Context context) { - this(context, null); - } - - public ShortTimeView(final Context context, final AttributeSet attrs) { - this(context, attrs, android.R.attr.textViewStyle); - } - - public ShortTimeView(final Context context, final AttributeSet attrs, final int defStyle) { - super(context, attrs, defStyle); - mTicker = new TickerRunnable(this); - } - - public void setShowAbsoluteTime(boolean showAbsoluteTime) { - mShowAbsoluteTime = showAbsoluteTime; - invalidateTime(); - } - - public void setTime(final long time) { - mTime = time; - invalidateTime(); - } - - @Override - protected void onAttachedToWindow() { - super.onAttachedToWindow(); - post(mTicker); - } - - @Override - protected void onDetachedFromWindow() { - removeCallbacks(mTicker); - super.onDetachedFromWindow(); - } - - private void invalidateTime() { - if (mShowAbsoluteTime) { - setTextIfChanged(formatSameDayTime(getContext(), mTime)); - } else { - final long current = System.currentTimeMillis(); - if (Math.abs(current - mTime) > 60 * 1000) { - setTextIfChanged(DateUtils.getRelativeTimeSpanString(mTime, System.currentTimeMillis(), - DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_ABBREV_ALL)); - } else { - setTextIfChanged(getContext().getString(R.string.just_now)); - } - } - } - - private void setTextIfChanged(CharSequence text) { - if (TextUtils.equals(text, getText())) return; - setText(text); - } - - private static class TickerRunnable implements Runnable { - - private final WeakReference mViewRef; - - private TickerRunnable(final ShortTimeView view) { - mViewRef = new WeakReference<>(view); - } - - @Override - public void run() { - final ShortTimeView view = mViewRef.get(); - if (view == null) return; - final Handler handler = view.getHandler(); - if (handler == null) return; - view.invalidateTime(); - final long now = SystemClock.uptimeMillis(); - final long next = now + TICKER_DURATION - now % TICKER_DURATION; - handler.postAtTime(this, next); - } - } - -} diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/BaseArrayAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/BaseArrayAdapter.kt index 9d8d3969b..e581e07bd 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/BaseArrayAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/BaseArrayAdapter.kt @@ -51,7 +51,7 @@ open class BaseArrayAdapter( final override val profileImageStyle: Int final override val textSize: Float final override val profileImageEnabled: Boolean - final override val isShowAbsoluteTime: Boolean + final override val showAbsoluteTime: Boolean val nameFirst: Boolean init { @@ -61,7 +61,7 @@ open class BaseArrayAdapter( profileImageStyle = preferences[profileImageStyleKey] textSize = preferences[textSizeKey].toFloat() profileImageEnabled = preferences[displayProfileImageKey] - isShowAbsoluteTime = preferences[showAbsoluteTimeKey] + showAbsoluteTime = preferences[showAbsoluteTimeKey] nameFirst = preferences[nameFirstKey] } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/BaseRecyclerViewAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/BaseRecyclerViewAdapter.kt index ecd1f6f41..7dac07e9b 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/BaseRecyclerViewAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/BaseRecyclerViewAdapter.kt @@ -56,7 +56,7 @@ abstract class BaseRecyclerViewAdapter( override final val profileImageStyle: Int override final val textSize: Float override final val profileImageEnabled: Boolean - override final val isShowAbsoluteTime: Boolean + override final val showAbsoluteTime: Boolean init { //noinspection unchecked @@ -64,7 +64,7 @@ abstract class BaseRecyclerViewAdapter( profileImageStyle = preferences[profileImageStyleKey] textSize = preferences[textSizeKey].toFloat() profileImageEnabled = preferences[displayProfileImageKey] - isShowAbsoluteTime = preferences[showAbsoluteTimeKey] + showAbsoluteTime = preferences[showAbsoluteTimeKey] } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/DummyItemAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/DummyItemAdapter.kt index cdfae3a5f..ff289ed0b 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/DummyItemAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/DummyItemAdapter.kt @@ -46,7 +46,7 @@ class DummyItemAdapter @JvmOverloads constructor( override var profileImageEnabled: Boolean = false override var sensitiveContentEnabled: Boolean = false override var mediaPreviewEnabled: Boolean = false - override var isShowAbsoluteTime: Boolean = false + override var showAbsoluteTime: Boolean = false override var friendshipClickListener: IUsersAdapter.FriendshipClickListener? = null override var requestClickListener: IUsersAdapter.RequestClickListener? = null override var statusClickListener: IStatusViewHolder.StatusClickListener? = null @@ -183,6 +183,6 @@ class DummyItemAdapter @JvmOverloads constructor( showCardActions = !preferences[hideCardActionsKey] linkHighlightingStyle = preferences[linkHighlightOptionKey] useStarsForLikes = preferences[iWantMyStarsBackKey] - isShowAbsoluteTime = preferences[showAbsoluteTimeKey] + showAbsoluteTime = preferences[showAbsoluteTimeKey] } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/iface/IContentAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/iface/IContentAdapter.kt index 8c47379d1..5098c63a9 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/iface/IContentAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/iface/IContentAdapter.kt @@ -47,5 +47,5 @@ interface IContentAdapter { val bidiFormatter: BidiFormatter - val isShowAbsoluteTime: Boolean + val showAbsoluteTime: Boolean } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/view/ShortTimeView.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/view/ShortTimeView.kt new file mode 100644 index 000000000..43de83793 --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/view/ShortTimeView.kt @@ -0,0 +1,104 @@ +/* + * 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 + +import android.content.Context +import android.os.Handler +import android.os.SystemClock +import android.support.v7.widget.AppCompatTextView +import android.text.TextUtils +import android.text.format.DateUtils +import android.util.AttributeSet + +import org.mariotaku.twidere.Constants +import org.mariotaku.twidere.R + +import java.lang.ref.WeakReference + +import org.mariotaku.twidere.util.Utils.formatSameDayTime + +class ShortTimeView( + context: Context, + attrs: AttributeSet? = null +) : AppCompatTextView(context, attrs, android.R.attr.textViewStyle), Constants { + + private val ticker = TickerRunnable(this) + + var showAbsoluteTime: Boolean = false + set(value) { + field = value + invalidateTime() + } + + var time: Long = 0 + set(value) { + field = value + invalidateTime() + } + + override fun onAttachedToWindow() { + super.onAttachedToWindow() + post(ticker) + } + + override fun onDetachedFromWindow() { + removeCallbacks(ticker) + super.onDetachedFromWindow() + } + + private fun invalidateTime() { + if (showAbsoluteTime) { + setTextIfChanged(formatSameDayTime(context, time)) + } else { + val current = System.currentTimeMillis() + if (Math.abs(current - time) > 60 * 1000) { + setTextIfChanged(DateUtils.getRelativeTimeSpanString(time, System.currentTimeMillis(), + DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_ABBREV_ALL)) + } else { + setTextIfChanged(context.getString(R.string.just_now)) + } + } + } + + private fun setTextIfChanged(text: CharSequence) { + if (TextUtils.equals(text, getText())) return + setText(text) + } + + private class TickerRunnable(view: ShortTimeView) : Runnable { + + private val viewRef = WeakReference(view) + + override fun run() { + val view = viewRef.get() ?: return + val handler = view.handler ?: return + view.invalidateTime() + val now = SystemClock.uptimeMillis() + val next = now + TICKER_DURATION - now % TICKER_DURATION + handler.postAtTime(this, next) + } + } + + companion object { + + private const val TICKER_DURATION = 5000L + } + +} diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/ActivityTitleSummaryViewHolder.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/ActivityTitleSummaryViewHolder.kt index bfe798f7c..f26a04437 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/ActivityTitleSummaryViewHolder.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/ActivityTitleSummaryViewHolder.kt @@ -82,7 +82,7 @@ class ActivityTitleSummaryViewHolder( val spacing = resources.getDimensionPixelSize(R.dimen.element_spacing_small) lp.leftMargin = spacing MarginLayoutParamsCompat.setMarginStart(lp, spacing) - timeView.setShowAbsoluteTime(adapter.isShowAbsoluteTime) + timeView.showAbsoluteTime = adapter.showAbsoluteTime } fun displayActivity(activity: ParcelableActivity) { @@ -100,7 +100,7 @@ class ActivityTitleSummaryViewHolder( titleView.text = message.title summaryView.text = message.summary summaryView.visibility = if (summaryView.length() > 0) View.VISIBLE else View.GONE - timeView.setTime(activity.timestamp) + timeView.time = activity.timestamp if (adapter.showAccountsColor) { itemContent.drawEnd(activity.account_color) } else { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/MessageEntryViewHolder.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/MessageEntryViewHolder.kt index d852edec4..45141b681 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/MessageEntryViewHolder.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/MessageEntryViewHolder.kt @@ -75,7 +75,7 @@ class MessageEntryViewHolder(private val adapter: MessageEntriesAdapter, itemVie nameView.setScreenName("@" + screenName) nameView.updateText(adapter.bidiFormatter) textView.text = toPlainText(cursor.getString(ConversationEntries.IDX_TEXT_UNESCAPED)) - timeView.setTime(timestamp) + timeView.time = timestamp if (isOutgoing) { timeView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_indicator_sent, 0) } else { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/StatusViewHolder.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/StatusViewHolder.kt index ef03eb77d..995624999 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/StatusViewHolder.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/StatusViewHolder.kt @@ -102,7 +102,7 @@ class StatusViewHolder(private val adapter: IStatusesAdapter<*>, itemView: View) linkify.applyAllLinks(text, null, -1, false, adapter.linkHighlightingStyle, true) textView.text = text } - timeView.setTime(System.currentTimeMillis()) + timeView.time = System.currentTimeMillis() val showCardActions = isCardActionsShown if (adapter.mediaPreviewEnabled) { mediaPreview.visibility = View.VISIBLE @@ -288,10 +288,10 @@ class StatusViewHolder(private val adapter: IStatusesAdapter<*>, itemView: View) } } - if (status.is_retweet) { - timeView.setTime(status.retweet_timestamp) + timeView.time = if (status.is_retweet) { + status.retweet_timestamp } else { - timeView.setTime(status.timestamp) + status.timestamp } nameView.setName(colorNameManager.getUserNickname(status.user_key, status.user_name)) @@ -496,7 +496,7 @@ class StatusViewHolder(private val adapter: IStatusesAdapter<*>, itemView: View) favoriteCountView.textColors.defaultColor, favColor, favStyle) drawable.mutate() favoriteIcon.setImageDrawable(drawable) - timeView.setShowAbsoluteTime(adapter.isShowAbsoluteTime) + timeView.showAbsoluteTime = adapter.showAbsoluteTime favoriteIcon.activatedColor = favColor }