From 29ea05a0e5d90d69b54dcd96cbd925a411b66d44 Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Sun, 15 Sep 2019 09:10:07 +0200 Subject: [PATCH] Fix crash when status date is null (#1480) * Fix crash when status date is null * Fix crash when status date is null --- .../tusky/adapter/StatusBaseViewHolder.java | 35 ++++++++++++------- .../adapter/StatusDetailedViewHolder.java | 10 ++++-- 2 files changed, 30 insertions(+), 15 deletions(-) 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 6098263c7..4f95f6cfb 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java @@ -252,18 +252,25 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { } - protected void setCreatedAt(@NonNull Date createdAt) { + protected void setCreatedAt(Date createdAt) { if (useAbsoluteTime) { timestampInfo.setText(getAbsoluteTime(createdAt)); } else { - long then = createdAt.getTime(); - long now = System.currentTimeMillis(); - String readout = TimestampUtils.getRelativeTimeSpanString(timestampInfo.getContext(), then, now); - timestampInfo.setText(readout); + if(createdAt == null) { + timestampInfo.setText("?m"); + } else { + long then = createdAt.getTime(); + long now = System.currentTimeMillis(); + String readout = TimestampUtils.getRelativeTimeSpanString(timestampInfo.getContext(), then, now); + timestampInfo.setText(readout); + } } } - private String getAbsoluteTime(@NonNull Date createdAt) { + private String getAbsoluteTime(Date createdAt) { + if(createdAt == null) { + return "??:??:??"; + } if (DateUtils.isToday(createdAt.getTime())) { return shortSdf.format(createdAt); } else { @@ -271,18 +278,22 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { } } - private CharSequence getCreatedAtDescription(@NonNull Date createdAt) { + private CharSequence getCreatedAtDescription(Date createdAt) { if (useAbsoluteTime) { return getAbsoluteTime(createdAt); } else { /* This one is for screen-readers. Frequently, they would mispronounce timestamps like "17m" * as 17 meters instead of minutes. */ - long then = createdAt.getTime(); - long now = System.currentTimeMillis(); - return DateUtils.getRelativeTimeSpanString(then, now, - DateUtils.SECOND_IN_MILLIS, - DateUtils.FORMAT_ABBREV_RELATIVE); + if(createdAt == null) { + return "? minutes"; + } else { + long then = createdAt.getTime(); + long now = System.currentTimeMillis(); + return DateUtils.getRelativeTimeSpanString(then, now, + DateUtils.SECOND_IN_MILLIS, + DateUtils.FORMAT_ABBREV_RELATIVE); + } } } diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusDetailedViewHolder.java b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusDetailedViewHolder.java index 0b9ae3614..930a6ee3c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusDetailedViewHolder.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusDetailedViewHolder.java @@ -67,9 +67,13 @@ class StatusDetailedViewHolder extends StatusBaseViewHolder { } @Override - protected void setCreatedAt(@NonNull Date createdAt) { - DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.SHORT); - timestampInfo.setText(dateFormat.format(createdAt)); + protected void setCreatedAt(Date createdAt) { + if(createdAt == null) { + timestampInfo.setText(""); + } else { + DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.SHORT); + timestampInfo.setText(dateFormat.format(createdAt)); + } } private void setReblogAndFavCount(int reblogCount, int favCount, StatusActionListener listener) {