1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-12-23 05:16:58 +00:00

Remove the last usage of Date. austinhuang0131/barinsta#1309

This commit is contained in:
Ammar Githam 2021-05-24 20:04:32 +09:00
parent 7e00dcc1b7
commit 981cd693bf
3 changed files with 16 additions and 32 deletions

View File

@ -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);
}

View File

@ -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 {

View File

@ -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<String> {
fun extractUrls(text: String): List<String> {
if (isEmpty(text)) return emptyList()
val matcher = Patterns.WEB_URL.matcher(text)
val urls: MutableList<String> = 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)
}