Something went wrong. Try again.
A music player that connects to your cloud/distributed storage. forked from tokono.ma/diffuse
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345module UI.Tracks.Covers exposing (..)
import Base64import Conditional exposing (ifThenElse)import Dictimport Maybe.Extra as Maybeimport Tracks exposing (..)
-- 🔱
generate : SortBy -> Tracks.Collection -> CoverCollectiongenerate sortBy tracks = let groupFn = coverGroup sortBy
makeCoverFn = makeCover sortBy in tracks.arranged |> List.foldr (\identifiedTrack { covers, gathering } -> let group = groupFn identifiedTrack
( identifiers, track ) = identifiedTrack
{ artist, album } = track.tags in if group /= gathering.previousGroup then -- New group, make cover for previous group let collection = makeCoverFn gathering covers in { gathering = { acc = [ identifiedTrack ] , accIds = [ track.id ] , previousGroup = group , previousTrack = track
-- , currentAlbumSequence = Just ( identifiedTrack, 1 ) , largestAlbumSequence = Nothing
-- , currentAlbumFavsSequence = Just ( identifiedTrack, ifThenElse identifiers.isFavourite 1 0 ) , largestAlbumFavsSequence = Nothing
-- , currentArtistSequence = Just ( identifiedTrack, 1 ) , largestArtistSequence = Nothing } , covers = collection }
else -- Same group { gathering = { acc = identifiedTrack :: gathering.acc , accIds = track.id :: gathering.accIds , previousGroup = group , previousTrack = track
-- Album sequence ----------------- , currentAlbumSequence = if album /= gathering.previousTrack.tags.album then Just ( identifiedTrack, 1 )
else increaseSequence gathering.currentAlbumSequence
-- , largestAlbumSequence = if album /= gathering.previousTrack.tags.album then resolveLargestSequence gathering.currentAlbumSequence gathering.largestAlbumSequence
else gathering.largestAlbumSequence
-- Album favourites sequence ---------------------------- , currentAlbumFavsSequence = if album /= gathering.previousTrack.tags.album then Just ( identifiedTrack, ifThenElse identifiers.isFavourite 1 0 )
else if identifiers.isFavourite then increaseSequence gathering.currentAlbumFavsSequence
else gathering.currentAlbumFavsSequence
-- , largestAlbumFavsSequence = if album /= gathering.previousTrack.tags.album then resolveLargestSequence gathering.currentAlbumFavsSequence gathering.largestAlbumFavsSequence
else gathering.largestAlbumFavsSequence
-- Artist sequence ------------------ , currentArtistSequence = if artist /= gathering.previousTrack.tags.artist then Just ( identifiedTrack, 1 )
else increaseSequence gathering.currentArtistSequence
-- , largestArtistSequence = if artist /= gathering.previousTrack.tags.artist then resolveLargestSequence gathering.currentArtistSequence gathering.largestArtistSequence
else gathering.largestArtistSequence } , covers = covers } ) { covers = [] , gathering = { acc = [] , accIds = [] , previousGroup = "" , previousTrack = emptyTrack
-- , currentAlbumSequence = Nothing , largestAlbumSequence = Nothing , currentAlbumFavsSequence = Nothing , largestAlbumFavsSequence = Nothing , currentArtistSequence = Nothing , largestArtistSequence = Nothing } } |> (\{ covers, gathering } -> makeCoverFn gathering covers ) |> (\collection -> { arranged = collection, harvested = [] } )
harvest : Maybe Cover -> SortBy -> Tracks.Collection -> CoverCollection -> ( CoverCollection, Maybe Cover )harvest previouslySelectedCover sortBy tracks covers = let groupFn = coverGroup sortBy
( groups, tracksPerGroup ) = List.foldr (\identifiedTrack ( acc, dict ) -> let group = groupFn identifiedTrack in ( if Dict.member group dict == False then group :: acc
else acc -- , Dict.update group (Maybe.unwrap [ identifiedTrack ] ((::) identifiedTrack) >> Just) dict ) ) ( [], Dict.empty ) tracks.harvested in covers.arranged |> List.foldr (\cover ( acc, sel ) -> if List.member cover.group groups then let groupTracks = Maybe.withDefault [] (Dict.get cover.group tracksPerGroup)
trackIds = List.map (Tuple.second >> .id) groupTracks
harvestedCover = { cover | tracks = groupTracks, trackIds = trackIds } in case ( previouslySelectedCover, sel ) of ( Just pre, Nothing ) -> ( harvestedCover :: acc , if pre.key == harvestedCover.key then Just harvestedCover
else Nothing )
( Just _, Just s ) -> ( harvestedCover :: acc , Just s )
( Nothing, _ ) -> ( harvestedCover :: acc , Nothing )
else ( acc , sel ) ) ( [] , Nothing ) |> Tuple.mapFirst (\h -> { covers | harvested = h })
-- ⚗️
makeCover sortBy_ gathering collection = let closedGathering = { gathering | largestAlbumSequence = resolveLargestSequence gathering.currentAlbumSequence gathering.largestAlbumSequence
-- , largestAlbumFavsSequence = resolveLargestSequence gathering.currentAlbumFavsSequence gathering.largestAlbumFavsSequence
-- , largestArtistSequence = resolveLargestSequence gathering.currentArtistSequence gathering.largestArtistSequence } in case closedGathering.acc of [] -> collection
fallback :: _ -> makeCoverWithFallback sortBy_ closedGathering fallback :: collection
makeCoverWithFallback _ gathering fallback = let amountOfTracks = List.length gathering.accIds
group = gathering.previousGroup
identifiedTrack : IdentifiedTrack identifiedTrack = gathering.largestAlbumFavsSequence |> Maybe.orElse gathering.largestAlbumSequence |> Maybe.map Tuple.first |> Maybe.withDefault fallback
( _, track ) = identifiedTrack
( largestAlbumSequence, largestArtistSequence ) = ( Maybe.unwrap 0 Tuple.second gathering.largestAlbumSequence , Maybe.unwrap 0 Tuple.second gathering.largestArtistSequence )
( sameAlbum, sameArtist ) = ( largestAlbumSequence == amountOfTracks , largestArtistSequence == amountOfTracks )
isVariousArtists = False || (amountOfTracks > 4 && largestArtistSequence < 3) || (Maybe.map String.toLower track.tags.artist == Just "va") in { key = Base64.encode (coverKey isVariousArtists track) , identifiedTrackCover = identifiedTrack
-- , group = group , sameAlbum = sameAlbum , sameArtist = sameArtist
-- , trackIds = gathering.accIds , tracks = gathering.acc , variousArtists = isVariousArtists }
-- ⚗️ ░░ SEQUENCES
increaseSequence = Maybe.map (Tuple.mapSecond ((+) 1))
resolveLargestSequence curr state = case ( curr, state ) of ( Just ( _, c ), Just ( _, s ) ) -> ifThenElse (c > s) curr state
( Just _, Nothing ) -> curr
( Nothing, Just _ ) -> state
( Nothing, Nothing ) -> Nothing