Skip to content

Commit d167720

Browse files
committed
fix: Group notifications older then 2 days by date
- Replace broad time groups (Older than 2 days) with specific dates - Show calendar dates like 'December 15' for better timeline readability - Maintain 'Today'/'Yesterday' labels for consistency - Use localized date formatting with DateFormat.getDateInstance()
1 parent 6f020b3 commit d167720

File tree

1 file changed

+40
-17
lines changed
  • WordPress/src/main/java/org/wordpress/android/ui/notifications/adapters

1 file changed

+40
-17
lines changed

WordPress/src/main/java/org/wordpress/android/ui/notifications/adapters/NoteViewHolder.kt

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import android.view.ViewGroup
88
import android.view.ViewTreeObserver
99
import android.widget.ImageView
1010
import android.widget.TextView
11-
import androidx.annotation.StringRes
1211
import androidx.core.text.BidiFormatter
1312
import androidx.core.text.trimmedLength
1413
import androidx.core.view.isVisible
@@ -26,11 +25,15 @@ import org.wordpress.android.ui.comments.CommentUtils
2625
import org.wordpress.android.ui.notifications.NotificationsListViewModel
2726
import org.wordpress.android.ui.notifications.blocks.NoteBlockClickableSpan
2827
import org.wordpress.android.ui.notifications.utils.NotificationsUtilsWrapper
28+
import org.wordpress.android.util.DateUtils.isSameDay
2929
import org.wordpress.android.util.GravatarUtils
3030
import org.wordpress.android.util.RtlUtils
3131
import org.wordpress.android.util.extensions.getColorFromAttribute
3232
import org.wordpress.android.util.image.ImageManager
3333
import org.wordpress.android.util.image.ImageType
34+
import java.text.DateFormat
35+
import java.util.Calendar
36+
import java.util.Date
3437
import javax.inject.Inject
3538
import kotlin.math.roundToInt
3639

@@ -141,25 +144,27 @@ class NoteViewHolder(
141144
binding.root.context.getString(if (liked) R.string.mnu_comment_liked else R.string.reader_label_like)
142145
}
143146

144-
@StringRes
145-
private fun timeGroupHeaderText(note: Note, previousNote: Note?) =
146-
previousNote?.timeGroup.let { previousTimeGroup ->
147-
val timeGroup = note.timeGroup
148-
if (previousTimeGroup?.let { it == timeGroup } == true) {
149-
// If the previous time group exists and is the same, we don't need a new one
150-
null
151-
} else {
152-
// Otherwise, we create a new one
153-
when (timeGroup) {
154-
Note.NoteTimeGroup.GROUP_TODAY -> R.string.stats_timeframe_today
155-
Note.NoteTimeGroup.GROUP_YESTERDAY -> R.string.stats_timeframe_yesterday
156-
Note.NoteTimeGroup.GROUP_OLDER_TWO_DAYS -> R.string.older_two_days
157-
Note.NoteTimeGroup.GROUP_OLDER_WEEK -> R.string.older_last_week
158-
Note.NoteTimeGroup.GROUP_OLDER_MONTH -> R.string.older_month
159-
}
147+
private fun timeGroupHeaderText(note: Note, previousNote: Note?): String? {
148+
val noteDate = Date(note.timestamp * MILLISECOND)
149+
150+
// If we have a previous note, check if it's from the same calendar day
151+
previousNote?.let { prevNote ->
152+
val prevNoteDate = Date(prevNote.timestamp * MILLISECOND)
153+
if (isSameDay(noteDate, prevNoteDate)) {
154+
// Same day as previous note, don't show a header
155+
return null
160156
}
161157
}
162158

159+
return when (note.timeGroup) {
160+
Note.NoteTimeGroup.GROUP_TODAY -> binding.root.context.getString(R.string.stats_timeframe_today)
161+
Note.NoteTimeGroup.GROUP_YESTERDAY -> binding.root.context.getString(R.string.stats_timeframe_yesterday)
162+
Note.NoteTimeGroup.GROUP_OLDER_TWO_DAYS,
163+
Note.NoteTimeGroup.GROUP_OLDER_WEEK,
164+
Note.NoteTimeGroup.GROUP_OLDER_MONTH -> timeGroupHeaderDate(noteDate)
165+
}
166+
}
167+
163168
fun bindSubject(note: Note) {
164169
// Subject is stored in db as html to preserve text formatting
165170
var noteSubjectSpanned: Spanned = note.getFormattedSubject(notificationsUtilsWrapper)
@@ -270,3 +275,21 @@ class NoteViewHolder(
270275
private val Note.timeGroup
271276
get() = Note.getTimeGroupForTimestamp(timestamp)
272277
}
278+
279+
private const val MILLISECOND = 1000
280+
281+
/**
282+
* Get formatted date string for display in notification headers
283+
*/
284+
private fun timeGroupHeaderDate(date: Date): String {
285+
val calendar = Calendar.getInstance()
286+
val currentYear = calendar.get(Calendar.YEAR)
287+
calendar.time = date
288+
val noteYear = calendar.get(Calendar.YEAR)
289+
290+
val text = DateFormat.getDateInstance(DateFormat.MEDIUM).format(date)
291+
return when (noteYear) {
292+
currentYear -> text.replace(", $noteYear", "")
293+
else -> text
294+
}
295+
}

0 commit comments

Comments
 (0)