Skip to content

Commit

Permalink
fix podcast ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
GrakovNe committed Nov 19, 2024
1 parent 6e63a81 commit 212512a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.grakovne.lissen.channel.audiobookshelf.podcast.converter

import org.grakovne.lissen.channel.audiobookshelf.common.model.MediaProgressResponse
import org.grakovne.lissen.channel.audiobookshelf.podcast.model.PodcastEpisodeResponse
import org.grakovne.lissen.channel.audiobookshelf.podcast.model.PodcastResponse
import org.grakovne.lissen.domain.BookChapter
import org.grakovne.lissen.domain.BookFile
import org.grakovne.lissen.domain.DetailedItem
import org.grakovne.lissen.domain.MediaProgress
import java.text.SimpleDateFormat
import java.util.Locale
import javax.inject.Inject
import javax.inject.Singleton

Expand All @@ -19,7 +22,7 @@ class PodcastResponseConverter @Inject constructor() {
val orderedEpisodes = item
.media
.episodes
?.sortedWith(compareBy({ it.season.safeToInt() }, { it.episode.safeToInt() }))
?.orderEpisode()

val filesAsChapters: List<BookChapter> =
orderedEpisodes
Expand Down Expand Up @@ -65,6 +68,20 @@ class PodcastResponseConverter @Inject constructor() {
}

companion object {
private val dateFormat = SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH)

private fun List<PodcastEpisodeResponse>.orderEpisode() =
this.sortedWith(
compareBy<PodcastEpisodeResponse> { item ->
try {
item.pubDate?.let { dateFormat.parse(it)?.time }
} catch (e: Exception) {
null
}
}
.thenBy { it.season.safeToInt() }
.thenBy { it.episode.safeToInt() }
)

private fun String?.safeToInt(): Int? {
val maybeNumber = this?.takeIf { it.isNotBlank() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ data class PodcastEpisodeResponse(
val id: String,
val season: String?,
val episode: String?,
val pubDate: String?,
val title: String,
val audioFile: PodcastAudioFileResponse
)
Expand Down

0 comments on commit 212512a

Please sign in to comment.