Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use same date format for next episode and previous #477

Merged
merged 5 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,14 @@ import java.util.Locale
class RelativeDateFormatter(val context: Context) {

private val sevenDaysAgo: Calendar = Calendar.getInstance().apply { add(Calendar.DATE, -7) }
private val sevenDaysFromNow: Calendar = Calendar.getInstance().apply { add(Calendar.DATE, 7) }
ashiagr marked this conversation as resolved.
Show resolved Hide resolved
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"
Expand All @@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also removed this resources param since there's already a field for context

if (alternativeString != null) {
return alternativeString
}
}

val format = when {
calendar.get(Calendar.YEAR) != currentYear -> DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_SHOW_YEAR
calendar.after(sevenDaysAgo) && calendar.before(sevenDaysFromNow) -> DateUtils.FORMAT_SHOW_WEEKDAY
else -> DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_NO_YEAR
}

return DateUtils.formatDateTime(context, calendar.timeInMillis, format)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -8,14 +9,14 @@ 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
Expand Down Expand Up @@ -195,7 +196,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) {
Expand All @@ -204,15 +205,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)
ashiagr marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand All @@ -44,14 +44,14 @@ 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)
}

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()
}