Skip to content

Commit

Permalink
Refactor audio duration to provide a string
Browse files Browse the repository at this point in the history
  • Loading branch information
Georges-GNM committed Dec 23, 2024
1 parent 8d5fb7f commit ecc274f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 53 deletions.
77 changes: 28 additions & 49 deletions common/app/model/PressedCard.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import model.CardStylePicker
import org.joda.time.DateTime
import com.gu.contentapi.client.model.v1.{Content, ElementType}

//case class AudioDuration(
// audioDurationMinutes: Option[collection.Seq[Int]],
// audioDurationSeconds: Option[collection.Seq[Int]],
//)

final case class PressedCard(
id: String,
cardStyle: CardStyle,
Expand All @@ -27,9 +22,7 @@ final case class PressedCard(
group: String,
isLive: Boolean,
galleryCount: Option[Int],
// audioDuration: AudioDuration,
audioDurationMinutes: Option[collection.Seq[Int]],
audioDurationSeconds: Option[collection.Seq[Int]],
audioDuration: Option[String],
) {
def withoutTrailText: PressedCard = copy(trailText = None)
}
Expand Down Expand Up @@ -58,46 +51,34 @@ object PressedCard {
)
}

// def getAudioDuration(fc: FaciaContent): AudioDuration = {
// def getAudioDurationMinutes(content: Content) =
// content.elements.map(_.flatMap(_.assets).flatMap(_.typeData.flatMap(_.durationMinutes)))
//
// def getAudioDurationSeconds(content: Content) =
// content.elements.map(_.flatMap(_.assets).flatMap(_.typeData.flatMap(_.durationSeconds)))
// val audioDurationMinutes = fold(fc)(
// curatedContent => getAudioDurationMinutes(curatedContent.content),
// supportingCuratedContent => getAudioDurationMinutes(supportingCuratedContent.content),
// _ => None,
// latestSnap => latestSnap.latestContent.flatMap(getAudioDurationMinutes),
// )
// val audioDurationSeconds = fold(fc)(
// curatedContent => getAudioDurationSeconds(curatedContent.content),
// supportingCuratedContent => getAudioDurationSeconds(supportingCuratedContent.content),
// _ => None,
// latestSnap => latestSnap.latestContent.flatMap(getAudioDurationSeconds),
// )
// val audioDuration = AudioDuration(audioDurationMinutes, audioDurationSeconds)
// audioDuration
//
// }
def getAudioDuration(fc: FaciaContent): Option[String] = {
def getMinutes(content: Content) =
content.elements.map(_.flatMap(_.assets).flatMap(_.typeData.flatMap(_.durationMinutes)))
def getSeconds(content: Content) =
content.elements.map(_.flatMap(_.assets).flatMap(_.typeData.flatMap(_.durationSeconds)))

def getAudioDurationMinutes(content: Content) =
content.elements.map(_.flatMap(_.assets).flatMap(_.typeData.flatMap(_.durationMinutes)))
def audioDurationMinutes(fc: FaciaContent) = fold(fc)(
curatedContent => getMinutes(curatedContent.content),
supportingCuratedContent => getMinutes(supportingCuratedContent.content),
_ => None,
latestSnap => latestSnap.latestContent.flatMap(getMinutes),
)

def getAudioDurationSeconds(content: Content) =
content.elements.map(_.flatMap(_.assets).flatMap(_.typeData.flatMap(_.durationSeconds)))
def audioDurationMinutes(fc: FaciaContent) = fold(fc)(
curatedContent => getAudioDurationMinutes(curatedContent.content),
supportingCuratedContent => getAudioDurationMinutes(supportingCuratedContent.content),
_ => None,
latestSnap => latestSnap.latestContent.flatMap(getAudioDurationMinutes),
)
def audioDurationSeconds(fc: FaciaContent) = fold(fc)(
curatedContent => getAudioDurationSeconds(curatedContent.content),
supportingCuratedContent => getAudioDurationSeconds(supportingCuratedContent.content),
_ => None,
latestSnap => latestSnap.latestContent.flatMap(getAudioDurationSeconds),
)
def audioDurationSeconds(fc: FaciaContent) = fold(fc)(
curatedContent => getSeconds(curatedContent.content),
supportingCuratedContent => getSeconds(supportingCuratedContent.content),
_ => None,
latestSnap => latestSnap.latestContent.flatMap(getSeconds),
)

val minutes = audioDurationMinutes(fc).get.headOption
val seconds = audioDurationSeconds(fc).get.headOption

if (minutes.isDefined || seconds.isDefined) {
val duration = minutes.toString + ":" + seconds.toString
Some(duration)
} else None
}

PressedCard(
id = FaciaContentUtils.id(content),
Expand All @@ -112,9 +93,7 @@ object PressedCard {
trailText = FaciaContentUtils.trailText(content),
starRating = FaciaContentUtils.starRating(content),
galleryCount = extractGalleryCount(content),
// audioDuration = getAudioDuration(content),
audioDurationMinutes = audioDurationMinutes(content),
audioDurationSeconds = audioDurationSeconds(content),
audioDuration = getAudioDuration(content),
)
}
}
3 changes: 1 addition & 2 deletions common/test/common/TrailsToShowcaseTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1395,8 +1395,7 @@ class TrailsToShowcaseTest extends AnyFlatSpec with Matchers with EitherValues {
isLive = true,
group = "",
galleryCount = None,
audioDurationMinutes = None,
audioDurationSeconds = None,
audioDuration = None,
)

val discussionSettings = PressedDiscussionSettings(
Expand Down
3 changes: 1 addition & 2 deletions common/test/common/facia/FixtureBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ object FixtureBuilder {
group = "0",
isLive = false,
galleryCount = None,
audioDurationMinutes = None,
audioDurationSeconds = None,
audioDuration = None,
)

def mkDiscussion(): PressedDiscussionSettings =
Expand Down

0 comments on commit ecc274f

Please sign in to comment.