@@ -8,7 +8,6 @@ import android.view.ViewGroup
88import android.view.ViewTreeObserver
99import android.widget.ImageView
1010import android.widget.TextView
11- import androidx.annotation.StringRes
1211import androidx.core.text.BidiFormatter
1312import androidx.core.text.trimmedLength
1413import androidx.core.view.isVisible
@@ -26,11 +25,15 @@ import org.wordpress.android.ui.comments.CommentUtils
2625import org.wordpress.android.ui.notifications.NotificationsListViewModel
2726import org.wordpress.android.ui.notifications.blocks.NoteBlockClickableSpan
2827import org.wordpress.android.ui.notifications.utils.NotificationsUtilsWrapper
28+ import org.wordpress.android.util.DateUtils.isSameDay
2929import org.wordpress.android.util.GravatarUtils
3030import org.wordpress.android.util.RtlUtils
3131import org.wordpress.android.util.extensions.getColorFromAttribute
3232import org.wordpress.android.util.image.ImageManager
3333import org.wordpress.android.util.image.ImageType
34+ import java.text.DateFormat
35+ import java.util.Calendar
36+ import java.util.Date
3437import javax.inject.Inject
3538import 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