From 589188e27dcbdf383087d76c4defde1ad6a57653 Mon Sep 17 00:00:00 2001 From: kyori Date: Mon, 20 Aug 2018 22:51:30 +0900 Subject: [PATCH] Prevent creating new object in setCreatedAt --- .../tusky/adapter/NotificationsAdapter.java | 18 ++++++++---------- .../tusky/adapter/StatusBaseViewHolder.java | 18 ++++++++---------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/NotificationsAdapter.java b/app/src/main/java/com/keylesspalace/tusky/adapter/NotificationsAdapter.java index 248aa047e..8c2eb221b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/NotificationsAdapter.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/NotificationsAdapter.java @@ -328,8 +328,8 @@ public class NotificationsAdapter extends RecyclerView.Adapter { private StatusViewData.Concrete statusViewData; private boolean useAbsoluteTime; - private SimpleDateFormat sdf; - private boolean passed1day; + private SimpleDateFormat shortSdf; + private SimpleDateFormat longSdf; StatusNotificationViewHolder(View itemView, boolean useAbsoluteTime) { super(itemView); @@ -355,8 +355,8 @@ public class NotificationsAdapter extends RecyclerView.Adapter { contentWarningButton.setOnCheckedChangeListener(this); this.useAbsoluteTime = useAbsoluteTime; - sdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault()); - passed1day = false; + shortSdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault()); + longSdf = new SimpleDateFormat("MM/dd HH:mm:ss", Locale.getDefault()); } private void showNotificationContent(boolean show) { @@ -385,13 +385,11 @@ public class NotificationsAdapter extends RecyclerView.Adapter { if (useAbsoluteTime) { String time; if (createdAt != null) { - if (!passed1day) { - if (new Date().getTime() - createdAt.getTime() > 86400000L) { - passed1day = true; - sdf = new SimpleDateFormat("MM/dd HH:mm:ss", Locale.getDefault()); - } + if (System.currentTimeMillis() - createdAt.getTime() > 86400000L) { + time = longSdf.format(createdAt); + } else { + time = shortSdf.format(createdAt); } - time = sdf.format(createdAt); } else { time = "??:??:??"; } diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java index d3e0772b9..2d7f0fa77 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java @@ -67,8 +67,8 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { TextView contentWarningDescription; private boolean useAbsoluteTime; - private SimpleDateFormat sdf; - private boolean passed1day; + private SimpleDateFormat shortSdf; + private SimpleDateFormat longSdf; StatusBaseViewHolder(View itemView, boolean useAbsoluteTime) { super(itemView); @@ -99,8 +99,8 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { contentWarningButton = itemView.findViewById(R.id.status_content_warning_button); this.useAbsoluteTime = useAbsoluteTime; - sdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault()); - passed1day = false; + shortSdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault()); + longSdf = new SimpleDateFormat("MM/dd HH:mm:ss", Locale.getDefault()); } protected abstract int getMediaPreviewHeight(Context context); @@ -139,13 +139,11 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { if (useAbsoluteTime) { String time; if (createdAt != null) { - if (!passed1day) { - if (new Date().getTime() - createdAt.getTime() > 86400000L) { - passed1day = true; - sdf = new SimpleDateFormat("MM/dd HH:mm:ss", Locale.getDefault()); - } + if (System.currentTimeMillis() - createdAt.getTime() > 86400000L) { + time = longSdf.format(createdAt); + } else { + time = shortSdf.format(createdAt); } - time = sdf.format(createdAt); } else { time = "??:??:??"; }