From b273bff738360c9eaceb4683e734ac0ecdaf5502 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sun, 23 May 2021 21:58:19 +0900 Subject: [PATCH] Replace some usages of Date with LocalDateTime. austinhuang0131/barinsta#1309 --- .../adapters/DirectItemsAdapter.java | 24 ++++++++++------- .../directmessages/DirectItemViewHolder.java | 6 +++-- .../awais/instagrabber/db/AppDatabase.java | 16 ++++++++---- .../instagrabber/db/entities/Favorite.java | 8 +++--- .../fragments/HashTagFragment.java | 4 +-- .../fragments/LocationFragment.java | 4 +-- .../fragments/main/ProfileFragment.java | 4 +-- .../responses/directmessages/DirectItem.kt | 11 +------- .../instagrabber/services/DMSyncService.java | 6 ++--- .../awais/instagrabber/utils/DateUtils.java | 26 +------------------ .../instagrabber/utils/ExportImportUtils.java | 10 ++++--- 11 files changed, 51 insertions(+), 68 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/adapters/DirectItemsAdapter.java b/app/src/main/java/awais/instagrabber/adapters/DirectItemsAdapter.java index 2673d787..444d4aa1 100644 --- a/app/src/main/java/awais/instagrabber/adapters/DirectItemsAdapter.java +++ b/app/src/main/java/awais/instagrabber/adapters/DirectItemsAdapter.java @@ -1,6 +1,5 @@ package awais.instagrabber.adapters; -import android.text.format.DateFormat; import android.view.LayoutInflater; import android.view.ViewGroup; @@ -13,8 +12,10 @@ import androidx.recyclerview.widget.AsyncListDiffer; import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.RecyclerView; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; import java.util.ArrayList; -import java.util.Date; import java.util.List; import java.util.Objects; import java.util.function.Function; @@ -57,7 +58,6 @@ import awais.instagrabber.repositories.responses.User; import awais.instagrabber.repositories.responses.directmessages.DirectItem; import awais.instagrabber.repositories.responses.directmessages.DirectItemStoryShare; import awais.instagrabber.repositories.responses.directmessages.DirectThread; -import awais.instagrabber.utils.DateUtils; public final class DirectItemsAdapter extends RecyclerView.Adapter { private static final String TAG = DirectItemsAdapter.class.getSimpleName(); @@ -292,12 +292,15 @@ public final class DirectItemsAdapter extends RecyclerView.Adapter sectionAndSort(final List list) { final List itemOrHeaders = new ArrayList<>(); - Date prevSectionDate = null; + LocalDate prevSectionDate = null; for (int i = 0; i < list.size(); i++) { final DirectItem item = list.get(i); - if (item == null) continue; + if (item == null || item.getDate() == null) continue; final DirectItemOrHeader prev = itemOrHeaders.isEmpty() ? null : itemOrHeaders.get(itemOrHeaders.size() - 1); - if (prev != null && prev.item != null && DateUtils.isSameDay(prev.item.getDate(), item.getDate())) { + if (prev != null + && prev.item != null + && prev.item.getDate() != null + && prev.item.getDate().toLocalDate().isEqual(item.getDate().toLocalDate())) { // just add item final DirectItemOrHeader itemOrHeader = new DirectItemOrHeader(); itemOrHeader.item = item; @@ -320,7 +323,7 @@ public final class DirectItemsAdapter extends RecyclerView.Adapter= 1) { rows = db.update(Favorite.TABLE_NAME, diff --git a/app/src/main/java/awais/instagrabber/db/entities/Favorite.java b/app/src/main/java/awais/instagrabber/db/entities/Favorite.java index 614ba167..69b2fa2d 100644 --- a/app/src/main/java/awais/instagrabber/db/entities/Favorite.java +++ b/app/src/main/java/awais/instagrabber/db/entities/Favorite.java @@ -6,7 +6,7 @@ import androidx.room.ColumnInfo; import androidx.room.Entity; import androidx.room.PrimaryKey; -import java.util.Date; +import java.time.LocalDateTime; import awais.instagrabber.models.enums.FavoriteType; @@ -37,14 +37,14 @@ public class Favorite { private final String picUrl; @ColumnInfo(name = COL_DATE_ADDED) - private final Date dateAdded; + private final LocalDateTime dateAdded; public Favorite(final int id, final String query, final FavoriteType type, final String displayName, final String picUrl, - final Date dateAdded) { + final LocalDateTime dateAdded) { this.id = id; this.query = query; this.type = type; @@ -73,7 +73,7 @@ public class Favorite { return picUrl; } - public Date getDateAdded() { + public LocalDateTime getDateAdded() { return dateAdded; } diff --git a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java index 617e4464..6aa2ae20 100644 --- a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java @@ -38,7 +38,7 @@ import com.google.android.material.snackbar.BaseTransientBottomBar; import com.google.android.material.snackbar.Snackbar; import com.google.common.collect.ImmutableList; -import java.util.Date; +import java.time.LocalDateTime; import java.util.List; import java.util.Set; @@ -531,7 +531,7 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe FavoriteType.HASHTAG, hashtagModel.getName(), "res:/" + R.drawable.ic_hashtag, - new Date() + LocalDateTime.now() ), new RepositoryCallback() { @Override public void onSuccess(final Void result) { diff --git a/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java b/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java index 371d87eb..8916fb38 100644 --- a/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/LocationFragment.java @@ -36,7 +36,7 @@ import com.google.android.material.snackbar.BaseTransientBottomBar; import com.google.android.material.snackbar.Snackbar; import com.google.common.collect.ImmutableList; -import java.util.Date; +import java.time.LocalDateTime; import java.util.List; import java.util.Set; @@ -539,7 +539,7 @@ public class LocationFragment extends Fragment implements SwipeRefreshLayout.OnR FavoriteType.LOCATION, locationModel.getName(), "res:/" + R.drawable.ic_location, - new Date() + LocalDateTime.now() ), new RepositoryCallback() { @Override public void onSuccess(final Void result) { diff --git a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java index 85268bcc..c6bb2d6b 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -45,8 +45,8 @@ import com.google.android.material.snackbar.BaseTransientBottomBar; import com.google.android.material.snackbar.Snackbar; import com.google.common.collect.ImmutableList; +import java.time.LocalDateTime; import java.util.Collections; -import java.util.Date; import java.util.List; import java.util.Objects; import java.util.Set; @@ -722,7 +722,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe FavoriteType.USER, profileModel.getFullName(), profileModel.getProfilePicUrl(), - new Date() + LocalDateTime.now() ), new RepositoryCallback() { @Override public void onSuccess(final Void result) { diff --git a/app/src/main/java/awais/instagrabber/repositories/responses/directmessages/DirectItem.kt b/app/src/main/java/awais/instagrabber/repositories/responses/directmessages/DirectItem.kt index c17fc2ed..daa759cf 100644 --- a/app/src/main/java/awais/instagrabber/repositories/responses/directmessages/DirectItem.kt +++ b/app/src/main/java/awais/instagrabber/repositories/responses/directmessages/DirectItem.kt @@ -8,7 +8,6 @@ import java.io.Serializable import java.time.Instant import java.time.LocalDateTime import java.time.ZoneId -import java.util.* data class DirectItem( var itemId: String? = null, @@ -40,9 +39,8 @@ data class DirectItem( val hideInThread: Int? = 0, val showForwardAttribution: Boolean = false ) : Cloneable, Serializable { - private var date: Date? = null var isPending = false - var localDateTime: LocalDateTime? = null + var date: LocalDateTime? = null get() { if (field == null) { field = Instant.ofEpochMilli(timestamp / 1000).atZone(ZoneId.systemDefault()).toLocalDateTime() @@ -60,13 +58,6 @@ data class DirectItem( date = null } - fun getDate(): Date? { - if (date == null) { - date = Date(timestamp / 1000) - } - return date - } - @Throws(CloneNotSupportedException::class) public override fun clone(): Any { return super.clone() diff --git a/app/src/main/java/awais/instagrabber/services/DMSyncService.java b/app/src/main/java/awais/instagrabber/services/DMSyncService.java index f885a559..63ae752a 100644 --- a/app/src/main/java/awais/instagrabber/services/DMSyncService.java +++ b/app/src/main/java/awais/instagrabber/services/DMSyncService.java @@ -112,7 +112,7 @@ public class DMSyncService extends LifecycleService { final DirectItem latestItem = unreadItems.get(unreadItems.size() - 1); lastNotifiedListBuilder.add(new DMLastNotified(0, unreadMessagesEntry.getKey(), - latestItem.getLocalDateTime(), + latestItem.getDate(), now)); } dmLastNotifiedRepository.insertOrUpdateDMLastNotified( @@ -145,8 +145,8 @@ public class DMSyncService extends LifecycleService { if (item.getUserId() == viewerId) break; // Reached a message from the viewer, it is assumed the viewer has read the next messages final boolean read = DMUtils.isRead(item, lastSeenAt, Collections.singletonList(viewerId)); if (read) break; - if (dmLastNotified != null && dmLastNotified.getLastNotifiedMsgTs() != null) { - if (count == 0 && DateUtils.isBeforeOrEqual(item.getLocalDateTime(), dmLastNotified.getLastNotifiedMsgTs())) { + if (dmLastNotified != null && dmLastNotified.getLastNotifiedMsgTs() != null && item.getDate() != null) { + if (count == 0 && DateUtils.isBeforeOrEqual(item.getDate(), dmLastNotified.getLastNotifiedMsgTs())) { // The first unread item has been notified and hence all subsequent items can be ignored // since the items are in desc timestamp order break; diff --git a/app/src/main/java/awais/instagrabber/utils/DateUtils.java b/app/src/main/java/awais/instagrabber/utils/DateUtils.java index 6f8fdcd1..473f4298 100644 --- a/app/src/main/java/awais/instagrabber/utils/DateUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/DateUtils.java @@ -4,39 +4,15 @@ import androidx.annotation.NonNull; import java.time.LocalDateTime; import java.util.Calendar; -import java.util.Date; import java.util.Locale; public final class DateUtils { - public static boolean isSameDay(final Date date1, final Date date2) { - if (date1 == null || date2 == null) { - return false; - } - Calendar calendar1 = Calendar.getInstance(); - calendar1.setTime(date1); - Calendar calendar2 = Calendar.getInstance(); - calendar2.setTime(date2); - return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR) - && calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH) - && calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH); - } - - public static Date dateAtZeroHours(@NonNull final Date date) { - final Calendar cal = Calendar.getInstance(); - cal.setTime(date); - cal.set(Calendar.HOUR_OF_DAY, 0); - cal.set(Calendar.MINUTE, 0); - cal.set(Calendar.SECOND, 0); - cal.set(Calendar.MILLISECOND, 0); - return cal.getTime(); - } - public static int getTimezoneOffset() { final Calendar calendar = Calendar.getInstance(Locale.getDefault()); return -(calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / (60 * 1000); } - public static boolean isBeforeOrEqual(final LocalDateTime localDateTime, final LocalDateTime comparedTo) { + public static boolean isBeforeOrEqual(@NonNull final LocalDateTime localDateTime, @NonNull final LocalDateTime comparedTo) { return localDateTime.isBefore(comparedTo) || localDateTime.isEqual(comparedTo); } } diff --git a/app/src/main/java/awais/instagrabber/utils/ExportImportUtils.java b/app/src/main/java/awais/instagrabber/utils/ExportImportUtils.java index 350ba9a3..16807994 100755 --- a/app/src/main/java/awais/instagrabber/utils/ExportImportUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/ExportImportUtils.java @@ -23,8 +23,10 @@ import org.json.JSONObject; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; import java.util.ArrayList; -import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -147,13 +149,15 @@ public final class ExportImportUtils { if (query == null || favoriteType == null) { continue; } + final long epochMillis = favsObject.getLong("d"); final Favorite favorite = new Favorite( 0, query, favoriteType, favsObject.optString("s"), favoriteType == FavoriteType.USER ? favsObject.optString("pic_url") : null, - new Date(favsObject.getLong("d"))); + LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMillis), ZoneId.systemDefault()) + ); // Log.d(TAG, "importJson: favoriteModel: " + favoriteModel); final FavoriteRepository favRepo = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context)); favRepo.getFavorite(query, favoriteType, new RepositoryCallback() { @@ -370,7 +374,7 @@ public final class ExportImportUtils { jsonObject.put("type", favorite.getType().toString()); jsonObject.put("s", favorite.getDisplayName()); jsonObject.put("pic_url", favorite.getPicUrl()); - jsonObject.put("d", favorite.getDateAdded().getTime()); + jsonObject.put("d", favorite.getDateAdded().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); jsonArray.put(jsonObject); } } catch (Exception e) {