diff --git a/CHANGELOG.md b/CHANGELOG.md index a2e58cd3870..b2eedd876e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ ([#575](https://github.com/Automattic/pocket-casts-android/pull/575)). * Fixed miniplayer play icon animation on theme change ([#527](https://github.com/Automattic/pocket-casts-android/pull/527)). + * Fixed podcast date format + ([#477](https://github.com/Automattic/pocket-casts-android/pull/477)). 7.27 ----- diff --git a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/EpisodeRow.kt b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/EpisodeRow.kt index 9167f6002f7..3c72b69bc5a 100644 --- a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/EpisodeRow.kt +++ b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/EpisodeRow.kt @@ -73,7 +73,7 @@ class EpisodeRow @JvmOverloads constructor( durationPublishedSeparator.showIf(durationVisible) val published = value.published - publishedView.text = if (published == null) "" else dateFormatter.format(published, context.resources) + publishedView.text = if (published == null) "" else dateFormatter.format(published) imageLoader.loadSmallImage(episode?.podcast_uuid).into(imageView) playButton.setPlaying(isPlaying = value.isPlaying, animate = false) } diff --git a/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/view/podcast/UserEpisodeViewHolder.kt b/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/view/podcast/UserEpisodeViewHolder.kt index 365c075ec67..b7be931a5c5 100644 --- a/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/view/podcast/UserEpisodeViewHolder.kt +++ b/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/view/podcast/UserEpisodeViewHolder.kt @@ -121,7 +121,7 @@ class UserEpisodeViewHolder( binding.playButtonType = playButtonType binding.episode = episode binding.tintColor = tintColor - binding.publishedDate = dateFormatter.format(episode.publishedDate, context.resources) + binding.publishedDate = dateFormatter.format(episode.publishedDate) binding.playButton.listener = playButtonListener binding.executePendingBindings() diff --git a/modules/features/podcasts/src/main/res/layout/view_podcast_header_bottom.xml b/modules/features/podcasts/src/main/res/layout/view_podcast_header_bottom.xml index 8c12d1453b9..bd38375816f 100644 --- a/modules/features/podcasts/src/main/res/layout/view_podcast_header_bottom.xml +++ b/modules/features/podcasts/src/main/res/layout/view_podcast_header_bottom.xml @@ -192,7 +192,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" app:constraint_referenced_ids="next_image,next_text" - android:visibility="@{podcast.displayableNextEpisodeDate(context.resources) != null}" + android:visibility="@{podcast.displayableNextEpisodeDate(context) != null}" android:id="@+id/next_group" tools:ignore="MissingConstraints" /> @@ -220,7 +220,7 @@ android:textAppearance="?attr/textBody1" android:ellipsize="end" android:lines="1" - android:text="@{podcast.displayableNextEpisodeDate(context.resources)}" + android:text="@{podcast.displayableNextEpisodeDate(context)}" android:textColor="?attr/primary_text_01" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/next_image" diff --git a/modules/services/localization/src/main/java/au/com/shiftyjelly/pocketcasts/localization/helper/RelativeDateFormatter.kt b/modules/services/localization/src/main/java/au/com/shiftyjelly/pocketcasts/localization/helper/RelativeDateFormatter.kt index b5adb11a353..234ccc73d43 100644 --- a/modules/services/localization/src/main/java/au/com/shiftyjelly/pocketcasts/localization/helper/RelativeDateFormatter.kt +++ b/modules/services/localization/src/main/java/au/com/shiftyjelly/pocketcasts/localization/helper/RelativeDateFormatter.kt @@ -15,25 +15,21 @@ import java.util.Locale * Format the date in the following format * "Today" * "Yesterday" - * "Monday" if less than a week + * "Monday" if less than six days from now * "23 July" in the same year * "1 December 2017" in a previous year */ class RelativeDateFormatter(val context: Context) { - private val sevenDaysAgo: Calendar = Calendar.getInstance().apply { add(Calendar.DATE, -7) } + private val now: Calendar = Calendar.getInstance() + private val sixDaysFromNow: Calendar = Calendar.getInstance().apply { add(Calendar.DATE, 6) } private val yesterday: Calendar = Calendar.getInstance().apply { add(Calendar.DATE, -1) } private val currentYear: Int = Calendar.getInstance().get(Calendar.YEAR) private var relativeDateFormatter: RelativeDateTimeFormatter? = null - fun format(date: Date, resources: Resources): String { + fun format(date: Date): String { val calendar = Calendar.getInstance() calendar.time = date - val format = when { - calendar.get(Calendar.YEAR) != currentYear -> DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_SHOW_YEAR - calendar.after(sevenDaysAgo) -> DateUtils.FORMAT_SHOW_WEEKDAY - else -> DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_NO_YEAR - } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // Try to add "Today" and "Yesterday" @@ -43,12 +39,18 @@ class RelativeDateFormatter(val context: Context) { } } else if (Locale.getDefault().language == "en") { // if using an old Android version but using English then add Today and Yesterday - val alternativeString = formatCloseDaysOld(calendar, resources) + val alternativeString = formatCloseDaysOld(calendar, context.resources) if (alternativeString != null) { return alternativeString } } + val format = when { + calendar.get(Calendar.YEAR) != currentYear -> DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_SHOW_YEAR + calendar.after(now) && calendar.before(sixDaysFromNow) -> DateUtils.FORMAT_SHOW_WEEKDAY + else -> DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_NO_YEAR + } + return DateUtils.formatDateTime(context, calendar.timeInMillis, format) } diff --git a/modules/services/model/src/main/java/au/com/shiftyjelly/pocketcasts/models/entity/Podcast.kt b/modules/services/model/src/main/java/au/com/shiftyjelly/pocketcasts/models/entity/Podcast.kt index bf8e2905703..8741a5553c9 100644 --- a/modules/services/model/src/main/java/au/com/shiftyjelly/pocketcasts/models/entity/Podcast.kt +++ b/modules/services/model/src/main/java/au/com/shiftyjelly/pocketcasts/models/entity/Podcast.kt @@ -1,5 +1,6 @@ package au.com.shiftyjelly.pocketcasts.models.entity +import android.content.Context import android.content.res.Resources import android.graphics.Color import android.text.format.DateUtils @@ -8,14 +9,13 @@ import androidx.room.Embedded import androidx.room.Entity import androidx.room.Ignore import androidx.room.PrimaryKey +import au.com.shiftyjelly.pocketcasts.localization.helper.RelativeDateFormatter import au.com.shiftyjelly.pocketcasts.localization.helper.tryToLocalise import au.com.shiftyjelly.pocketcasts.models.to.Bundle import au.com.shiftyjelly.pocketcasts.models.to.PlaybackEffects import au.com.shiftyjelly.pocketcasts.models.to.PodcastGrouping import au.com.shiftyjelly.pocketcasts.models.type.EpisodesSortType import au.com.shiftyjelly.pocketcasts.models.type.TrimMode -import au.com.shiftyjelly.pocketcasts.utils.extensions.toLocalizedFormatLongStyle -import au.com.shiftyjelly.pocketcasts.utils.extensions.toLocalizedFormatPattern import java.io.Serializable import java.net.MalformedURLException import java.net.URL @@ -195,7 +195,7 @@ data class Podcast( } @Suppress("DEPRECATION") - fun displayableNextEpisodeDate(resources: Resources): String? { + fun displayableNextEpisodeDate(context: Context): String? { val expectedDate = estimatedNextEpisode ?: return null val expectedTime = expectedDate.time if (expectedTime <= 0) { @@ -204,15 +204,17 @@ data class Podcast( val sevenDaysInMs = 7 * DateUtils.DAY_IN_MILLIS val now = System.currentTimeMillis() val sevenDaysAgo = now - sevenDaysInMs - val sevenDaysFuture = now + sevenDaysInMs + + val resources = context.resources return when { expectedTime < sevenDaysAgo -> null DateUtils.isToday(expectedTime) -> resources.getString(LR.string.podcast_next_episode_today) DateUtils.isToday(expectedTime - DateUtils.DAY_IN_MILLIS) -> resources.getString(LR.string.podcast_next_episode_tomorrow) expectedTime in sevenDaysAgo..now -> resources.getString(LR.string.podcast_next_episode_any_day_now) - expectedTime < sevenDaysFuture -> resources.getString(LR.string.podcast_next_episode_value, expectedDate.toLocalizedFormatPattern("EEEE")) - expectedDate.year == Date().year -> resources.getString(LR.string.podcast_next_episode_value, expectedDate.toLocalizedFormatPattern("d MMMM")) - else -> resources.getString(LR.string.podcast_next_episode_value, expectedDate.toLocalizedFormatLongStyle()) + else -> { + val formattedDate = RelativeDateFormatter(context).format(expectedDate) + resources.getString(LR.string.podcast_next_episode_value, formattedDate) + } } } } diff --git a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/extensions/Playable.kt b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/extensions/Playable.kt index b6600e315b4..2f885d8f71f 100644 --- a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/extensions/Playable.kt +++ b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/extensions/Playable.kt @@ -17,7 +17,7 @@ import java.util.Locale fun Playable.getSummaryText(dateFormatter: RelativeDateFormatter, @ColorInt tintColor: Int, showDuration: Boolean, context: Context): Spannable { return when (this) { is Episode -> episodeSummaryText(episode = this, dateFormatter = dateFormatter, tintColor = tintColor, showDuration = showDuration, context = context) - is UserEpisode -> userEpisodeSummaryText(userEpisode = this, dateFormatter = dateFormatter, context = context) + is UserEpisode -> userEpisodeSummaryText(userEpisode = this, dateFormatter = dateFormatter) else -> "".toSpannable() } } @@ -44,7 +44,7 @@ private fun episodeSummaryText(episode: Episode, dateFormatter: RelativeDateForm val timeLeft = TimeHelper.getTimeLeft(episode.playedUpToMs, episode.durationMs.toLong(), episode.isInProgress, context) val duration = if (showDuration) " • ${timeLeft.text}" else "" - val text = "$startText${dateFormatter.format(episode.publishedDate, context.resources)}$duration".toSpannable() + val text = "$startText${dateFormatter.format(episode.publishedDate)}$duration".toSpannable() if (episode.episodeType != Episode.EpisodeType.Regular) { text[0, startText.replace(" • ", "").length] = ForegroundColorSpan(tintColor) } @@ -52,6 +52,6 @@ private fun episodeSummaryText(episode: Episode, dateFormatter: RelativeDateForm return text } -private fun userEpisodeSummaryText(userEpisode: UserEpisode, dateFormatter: RelativeDateFormatter, context: Context): Spannable { - return dateFormatter.format(userEpisode.publishedDate, context.resources).toSpannable() +private fun userEpisodeSummaryText(userEpisode: UserEpisode, dateFormatter: RelativeDateFormatter): Spannable { + return dateFormatter.format(userEpisode.publishedDate).toSpannable() }