Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
Add subtitlesTrackChanged event (#27)
Browse files Browse the repository at this point in the history
* Add `subtitlesTrackChanged` event

* Make label field optional

* Allow subtitle track field to be null
  • Loading branch information
tfausak authored May 31, 2023
1 parent ba66532 commit da64fb7
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 6 deletions.
1 change: 1 addition & 0 deletions elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"Jawa.Event.Seek",
"Jawa.Event.Seeked",
"Jawa.Event.SetupError",
"Jawa.Event.SubtitlesTrackChanged",
"Jawa.Event.SubtitlesTracks",
"Jawa.Event.Time",
"Jawa.Event.UserActive",
Expand Down
6 changes: 6 additions & 0 deletions src/Jawa/Event.elm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import Jawa.Event.Resize
import Jawa.Event.Seek
import Jawa.Event.Seeked
import Jawa.Event.SetupError
import Jawa.Event.SubtitlesTrackChanged
import Jawa.Event.SubtitlesTracks
import Jawa.Event.Time
import Jawa.Event.UserActive
Expand Down Expand Up @@ -93,6 +94,7 @@ type Event
| Seek Jawa.Event.Seek.Seek
| Seeked Jawa.Event.Seeked.Seeked
| SetupError Jawa.Event.SetupError.SetupError
| SubtitlesTrackChanged Jawa.Event.SubtitlesTrackChanged.SubtitlesTrackChanged
| SubtitlesTracks Jawa.Event.SubtitlesTracks.SubtitlesTracks
| Time Jawa.Event.Time.Time
| UserActive Jawa.Event.UserActive.UserActive
Expand Down Expand Up @@ -159,6 +161,7 @@ decoders =
, ( Jawa.Event.Seek.tag, Json.Decode.map Seek Jawa.Event.Seek.decoder )
, ( Jawa.Event.Seeked.tag, Json.Decode.map Seeked Jawa.Event.Seeked.decoder )
, ( Jawa.Event.SetupError.tag, Json.Decode.map SetupError Jawa.Event.SetupError.decoder )
, ( Jawa.Event.SubtitlesTrackChanged.tag, Json.Decode.map SubtitlesTrackChanged Jawa.Event.SubtitlesTrackChanged.decoder )
, ( Jawa.Event.SubtitlesTracks.tag, Json.Decode.map SubtitlesTracks Jawa.Event.SubtitlesTracks.decoder )
, ( Jawa.Event.Time.tag, Json.Decode.map Time Jawa.Event.Time.decoder )
, ( Jawa.Event.UserActive.tag, Json.Decode.map UserActive Jawa.Event.UserActive.decoder )
Expand Down Expand Up @@ -282,6 +285,9 @@ encode event =
SetupError x ->
encodeWith Jawa.Event.SetupError.tag Jawa.Event.SetupError.encode x

SubtitlesTrackChanged x ->
encodeWith Jawa.Event.SubtitlesTrackChanged.tag Jawa.Event.SubtitlesTrackChanged.encode x

SubtitlesTracks x ->
encodeWith Jawa.Event.SubtitlesTracks.tag Jawa.Event.SubtitlesTracks.encode x

Expand Down
45 changes: 45 additions & 0 deletions src/Jawa/Event/SubtitlesTrackChanged.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Jawa.Event.SubtitlesTrackChanged exposing (SubtitlesTrackChanged, decoder, encode, tag)

{-|
@docs SubtitlesTrackChanged, decoder, encode, tag
-}

import Jawa.SubtitleTrack
import Json.Decode
import Json.Encode


{-| This event is not documented.
-}
type alias SubtitlesTrackChanged =
{ currentTrack : Int
, tracks : List Jawa.SubtitleTrack.SubtitleTrack
}


{-| A JSON decoder.
-}
decoder : Json.Decode.Decoder SubtitlesTrackChanged
decoder =
Json.Decode.map2 SubtitlesTrackChanged
(Json.Decode.field "currentTrack" Json.Decode.int)
(Json.Decode.field "tracks" (Json.Decode.list Jawa.SubtitleTrack.decoder))


{-| A JSON encoder.
-}
encode : SubtitlesTrackChanged -> Json.Encode.Value
encode x =
Json.Encode.object
[ ( "currentTrack", Json.Encode.int x.currentTrack )
, ( "tracks", Json.Encode.list Jawa.SubtitleTrack.encode x.tracks )
]


{-| The tag that describes this type.
-}
tag : String
tag =
"subtitlesTrackChanged"
10 changes: 6 additions & 4 deletions src/Jawa/SubtitleTrack.elm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type alias SubtitleTrack =
, default : Bool
, id : String
, kind : Maybe String
, label : String
, label : Maybe String
, name : String
, subtitleTrack : Jawa.Metadata.Metadata
}
Expand All @@ -35,9 +35,11 @@ decoder =
(Json.Decode.field "default" Json.Decode.bool)
(Json.Decode.field "_id" Json.Decode.string)
(Json.Decode.Extra.optionalNullableField "kind" Json.Decode.string)
(Json.Decode.field "label" Json.Decode.string)
(Json.Decode.Extra.optionalNullableField "label" Json.Decode.string)
(Json.Decode.field "name" Json.Decode.string)
(Json.Decode.field "subtitleTrack" Jawa.Metadata.decoder)
(Json.Decode.Extra.optionalNullableField "subtitleTrack" Jawa.Metadata.decoder
|> Json.Decode.map (Maybe.withDefault (Jawa.Metadata.Metadata Json.Encode.null))
)


{-| A JSON encoder.
Expand All @@ -48,7 +50,7 @@ encode x =
, Just ( "default", Json.Encode.bool x.default )
, Just ( "_id", Json.Encode.string x.id )
, Maybe.map (Json.Encode.string >> Tuple.pair "kind") x.kind
, Just ( "label", Json.Encode.string x.label )
, Maybe.map (Json.Encode.string >> Tuple.pair "label") x.label
, Just ( "name", Json.Encode.string x.name )
, Just ( "subtitleTrack", Jawa.Metadata.encode x.subtitleTrack )
]
Expand Down
34 changes: 34 additions & 0 deletions tests/Jawa/Event/SubtitlesTrackChangedTest.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Jawa.Event.SubtitlesTrackChangedTest exposing
( fuzzer
, test
)

import Fuzz
import Jawa.Event.SubtitlesTrackChanged
import Jawa.Extra.Test
import Jawa.SubtitleTrackTest
import Test


test : Test.Test
test =
Test.describe "Jawa.Event.SubtitlesTrackChanged"
[ Jawa.Extra.Test.fuzzCodec "round trips" Jawa.Event.SubtitlesTrackChanged.decoder Jawa.Event.SubtitlesTrackChanged.encode fuzzer
, Jawa.Extra.Test.testCodec "works"
Jawa.Event.SubtitlesTrackChanged.decoder
Jawa.Event.SubtitlesTrackChanged.encode
""" {
"currentTrack": 0,
"tracks": []
} """
{ currentTrack = 0
, tracks = []
}
]


fuzzer : Fuzz.Fuzzer Jawa.Event.SubtitlesTrackChanged.SubtitlesTrackChanged
fuzzer =
Fuzz.map2 Jawa.Event.SubtitlesTrackChanged.SubtitlesTrackChanged
Fuzz.int
(Fuzz.listOfLengthBetween 0 2 Jawa.SubtitleTrackTest.fuzzer)
15 changes: 15 additions & 0 deletions tests/Jawa/EventTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Jawa.Event.ResizeTest
import Jawa.Event.SeekTest
import Jawa.Event.SeekedTest
import Jawa.Event.SetupErrorTest
import Jawa.Event.SubtitlesTrackChangedTest
import Jawa.Event.SubtitlesTracksTest
import Jawa.Event.TimeTest
import Jawa.Event.UserActiveTest
Expand Down Expand Up @@ -527,6 +528,19 @@ test =
, message = ""
}
)
, Jawa.Extra.Test.testCodec "works with subtitlesTrackChanged"
Jawa.Event.decoder
Jawa.Event.encode
""" {
"currentTrack": 0,
"tracks": [],
"type": "subtitlesTrackChanged"
} """
(Jawa.Event.SubtitlesTrackChanged
{ currentTrack = 0
, tracks = []
}
)
, Jawa.Extra.Test.testCodec "works with subtitlesTracks"
Jawa.Event.decoder
Jawa.Event.encode
Expand Down Expand Up @@ -670,6 +684,7 @@ fuzzer =
, Fuzz.map Jawa.Event.Seek Jawa.Event.SeekTest.fuzzer
, Fuzz.map Jawa.Event.Seeked Jawa.Event.SeekedTest.fuzzer
, Fuzz.map Jawa.Event.SetupError Jawa.Event.SetupErrorTest.fuzzer
, Fuzz.map Jawa.Event.SubtitlesTrackChanged Jawa.Event.SubtitlesTrackChangedTest.fuzzer
, Fuzz.map Jawa.Event.SubtitlesTracks Jawa.Event.SubtitlesTracksTest.fuzzer
, Fuzz.map Jawa.Event.Time Jawa.Event.TimeTest.fuzzer
, Fuzz.map Jawa.Event.UserActive Jawa.Event.UserActiveTest.fuzzer
Expand Down
4 changes: 2 additions & 2 deletions tests/Jawa/SubtitleTrackTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test =
, default = False
, id = "a"
, kind = Just "b"
, label = "c"
, label = Just "c"
, name = "d"
, subtitleTrack = Jawa.Metadata.Metadata (Json.Encode.object [])
}
Expand All @@ -46,6 +46,6 @@ fuzzer =
Fuzz.bool
Fuzz.string
(Fuzz.maybe Fuzz.string)
Fuzz.string
(Fuzz.maybe Fuzz.string)
Fuzz.string
Jawa.MetadataTest.fuzzer

0 comments on commit da64fb7

Please sign in to comment.