From 981cd693bfb4359d26991ced923f4024e7da33d9 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Mon, 24 May 2021 20:04:32 +0900 Subject: [PATCH] Remove the last usage of Date. austinhuang0131/barinsta#1309 --- .../DirectInboxItemViewHolder.java | 4 +-- .../awais/instagrabber/db/Converters.java | 11 ------- .../awais/instagrabber/utils/TextUtils.kt | 33 +++++++++---------- 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectInboxItemViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectInboxItemViewHolder.java index 2755cf32..2b0596b6 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectInboxItemViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectInboxItemViewHolder.java @@ -17,11 +17,9 @@ import java.util.List; import awais.instagrabber.R; import awais.instagrabber.adapters.DirectMessageInboxAdapter.OnItemClickListener; import awais.instagrabber.databinding.LayoutDmInboxItemBinding; -import awais.instagrabber.models.enums.MediaItemType; import awais.instagrabber.repositories.responses.User; import awais.instagrabber.repositories.responses.directmessages.DirectItem; import awais.instagrabber.repositories.responses.directmessages.DirectThread; -import awais.instagrabber.repositories.responses.directmessages.DirectThreadDirectStory; import awais.instagrabber.utils.DMUtils; import awais.instagrabber.utils.TextUtils; @@ -136,7 +134,7 @@ public final class DirectInboxItemViewHolder extends RecyclerView.ViewHolder { private void setDateTime(@NonNull final DirectItem item) { final long timestamp = item.getTimestamp() / 1000; - final String dateTimeString = TextUtils.getRelativeDateTimeString(itemView.getContext(), timestamp); + final String dateTimeString = TextUtils.getRelativeDateTimeString(timestamp); binding.tvDate.setText(dateTimeString); } diff --git a/app/src/main/java/awais/instagrabber/db/Converters.java b/app/src/main/java/awais/instagrabber/db/Converters.java index c852b920..f2f2c0fb 100644 --- a/app/src/main/java/awais/instagrabber/db/Converters.java +++ b/app/src/main/java/awais/instagrabber/db/Converters.java @@ -6,21 +6,10 @@ import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZoneOffset; -//import java.util.Date; import awais.instagrabber.models.enums.FavoriteType; public class Converters { -// @TypeConverter -// public static Date fromTimestamp(Long value) { -// return value == null ? null : new Date(value); -// } -// -// @TypeConverter -// public static Long dateToTimestamp(Date date) { -// return date == null ? null : date.getTime(); -// } - @TypeConverter public static FavoriteType fromFavoriteTypeString(String value) { try { diff --git a/app/src/main/java/awais/instagrabber/utils/TextUtils.kt b/app/src/main/java/awais/instagrabber/utils/TextUtils.kt index c4e71e2d..8da94d3e 100644 --- a/app/src/main/java/awais/instagrabber/utils/TextUtils.kt +++ b/app/src/main/java/awais/instagrabber/utils/TextUtils.kt @@ -1,14 +1,14 @@ package awais.instagrabber.utils -import android.content.Context -import android.text.format.DateFormat -import android.text.format.DateUtils import android.util.Patterns -import java.time.format.DateTimeFormatter +import java.time.Duration import java.time.Instant import java.time.LocalDateTime import java.time.ZoneId +import java.time.format.DateTimeFormatter +import java.time.format.FormatStyle import java.util.* +import kotlin.math.absoluteValue object TextUtils { var datetimeParser: DateTimeFormatter = DateTimeFormatter.ofPattern("") @@ -40,22 +40,19 @@ object TextUtils { } else String.format(Locale.ENGLISH, "%02d:%02d", min, sec) } - @JvmStatic - fun getRelativeDateTimeString(context: Context?, from: Long): String { - val now = Date() - val then = Date(from) - val days = daysBetween(from, now.time) - return if (days == 0) { - DateFormat.getTimeFormat(context).format(then) - } else DateFormat.getDateFormat(context).format(then) - } + private val timeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT) + private val dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT) - private fun daysBetween(d1: Long, d2: Long): Int { - return ((d2 - d1) / DateUtils.DAY_IN_MILLIS).toInt() + @JvmStatic + fun getRelativeDateTimeString(from: Long): String { + val now = LocalDateTime.now() + val then = LocalDateTime.ofInstant(Instant.ofEpochMilli(from), ZoneId.systemDefault()) + val days = Duration.between(now, then).toDays().absoluteValue + return then.format(if (days == 0L) timeFormatter else dateFormatter) } @JvmStatic - fun extractUrls(text: String?): List { + fun extractUrls(text: String): List { if (isEmpty(text)) return emptyList() val matcher = Patterns.WEB_URL.matcher(text) val urls: MutableList = ArrayList() @@ -87,8 +84,8 @@ object TextUtils { @JvmStatic fun epochSecondToString(epochSecond: Long): String { return LocalDateTime.ofInstant( - Instant.ofEpochSecond(epochSecond), - ZoneId.systemDefault() + Instant.ofEpochSecond(epochSecond), + ZoneId.systemDefault() ).format(datetimeParser) }