@@ -8,7 +8,6 @@ import android.view.ViewGroup
8
8
import android.view.ViewTreeObserver
9
9
import android.widget.ImageView
10
10
import android.widget.TextView
11
- import androidx.annotation.StringRes
12
11
import androidx.core.text.BidiFormatter
13
12
import androidx.core.text.trimmedLength
14
13
import androidx.core.view.isVisible
@@ -26,11 +25,15 @@ import org.wordpress.android.ui.comments.CommentUtils
26
25
import org.wordpress.android.ui.notifications.NotificationsListViewModel
27
26
import org.wordpress.android.ui.notifications.blocks.NoteBlockClickableSpan
28
27
import org.wordpress.android.ui.notifications.utils.NotificationsUtilsWrapper
28
+ import org.wordpress.android.util.DateUtils.isSameDay
29
29
import org.wordpress.android.util.GravatarUtils
30
30
import org.wordpress.android.util.RtlUtils
31
31
import org.wordpress.android.util.extensions.getColorFromAttribute
32
32
import org.wordpress.android.util.image.ImageManager
33
33
import org.wordpress.android.util.image.ImageType
34
+ import java.text.DateFormat
35
+ import java.util.Calendar
36
+ import java.util.Date
34
37
import javax.inject.Inject
35
38
import kotlin.math.roundToInt
36
39
@@ -141,25 +144,27 @@ class NoteViewHolder(
141
144
binding.root.context.getString(if (liked) R .string.mnu_comment_liked else R .string.reader_label_like)
142
145
}
143
146
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
160
156
}
161
157
}
162
158
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
+
163
168
fun bindSubject (note : Note ) {
164
169
// Subject is stored in db as html to preserve text formatting
165
170
var noteSubjectSpanned: Spanned = note.getFormattedSubject(notificationsUtilsWrapper)
@@ -270,3 +275,21 @@ class NoteViewHolder(
270
275
private val Note .timeGroup
271
276
get() = Note .getTimeGroupForTimestamp(timestamp)
272
277
}
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