Browse Source

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

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
981cd693bf
  1. 4
      app/src/main/java/awais/instagrabber/adapters/viewholder/directmessages/DirectInboxItemViewHolder.java
  2. 11
      app/src/main/java/awais/instagrabber/db/Converters.java
  3. 33
      app/src/main/java/awais/instagrabber/utils/TextUtils.kt

4
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.R;
import awais.instagrabber.adapters.DirectMessageInboxAdapter.OnItemClickListener; import awais.instagrabber.adapters.DirectMessageInboxAdapter.OnItemClickListener;
import awais.instagrabber.databinding.LayoutDmInboxItemBinding; import awais.instagrabber.databinding.LayoutDmInboxItemBinding;
import awais.instagrabber.models.enums.MediaItemType;
import awais.instagrabber.repositories.responses.User; import awais.instagrabber.repositories.responses.User;
import awais.instagrabber.repositories.responses.directmessages.DirectItem; import awais.instagrabber.repositories.responses.directmessages.DirectItem;
import awais.instagrabber.repositories.responses.directmessages.DirectThread; import awais.instagrabber.repositories.responses.directmessages.DirectThread;
import awais.instagrabber.repositories.responses.directmessages.DirectThreadDirectStory;
import awais.instagrabber.utils.DMUtils; import awais.instagrabber.utils.DMUtils;
import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.TextUtils;
@ -136,7 +134,7 @@ public final class DirectInboxItemViewHolder extends RecyclerView.ViewHolder {
private void setDateTime(@NonNull final DirectItem item) { private void setDateTime(@NonNull final DirectItem item) {
final long timestamp = item.getTimestamp() / 1000; final long timestamp = item.getTimestamp() / 1000;
final String dateTimeString = TextUtils.getRelativeDateTimeString(itemView.getContext(), timestamp);
final String dateTimeString = TextUtils.getRelativeDateTimeString(timestamp);
binding.tvDate.setText(dateTimeString); binding.tvDate.setText(dateTimeString);
} }

11
app/src/main/java/awais/instagrabber/db/Converters.java

@ -6,21 +6,10 @@ import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZoneOffset; import java.time.ZoneOffset;
//import java.util.Date;
import awais.instagrabber.models.enums.FavoriteType; import awais.instagrabber.models.enums.FavoriteType;
public class Converters { 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 @TypeConverter
public static FavoriteType fromFavoriteTypeString(String value) { public static FavoriteType fromFavoriteTypeString(String value) {
try { try {

33
app/src/main/java/awais/instagrabber/utils/TextUtils.kt

@ -1,14 +1,14 @@
package awais.instagrabber.utils package awais.instagrabber.utils
import android.content.Context
import android.text.format.DateFormat
import android.text.format.DateUtils
import android.util.Patterns import android.util.Patterns
import java.time.format.DateTimeFormatter
import java.time.Duration
import java.time.Instant import java.time.Instant
import java.time.LocalDateTime import java.time.LocalDateTime
import java.time.ZoneId import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.util.* import java.util.*
import kotlin.math.absoluteValue
object TextUtils { object TextUtils {
var datetimeParser: DateTimeFormatter = DateTimeFormatter.ofPattern("") var datetimeParser: DateTimeFormatter = DateTimeFormatter.ofPattern("")
@ -40,22 +40,19 @@ object TextUtils {
} else String.format(Locale.ENGLISH, "%02d:%02d", min, sec) } 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 @JvmStatic
fun extractUrls(text: String?): List<String> {
fun extractUrls(text: String): List<String> {
if (isEmpty(text)) return emptyList() if (isEmpty(text)) return emptyList()
val matcher = Patterns.WEB_URL.matcher(text) val matcher = Patterns.WEB_URL.matcher(text)
val urls: MutableList<String> = ArrayList() val urls: MutableList<String> = ArrayList()
@ -87,8 +84,8 @@ object TextUtils {
@JvmStatic @JvmStatic
fun epochSecondToString(epochSecond: Long): String { fun epochSecondToString(epochSecond: Long): String {
return LocalDateTime.ofInstant( return LocalDateTime.ofInstant(
Instant.ofEpochSecond(epochSecond),
ZoneId.systemDefault()
Instant.ofEpochSecond(epochSecond),
ZoneId.systemDefault()
).format(datetimeParser) ).format(datetimeParser)
} }

Loading…
Cancel
Save