diff --git a/src/Applications/UI/Tracks.elm b/src/Applications/UI/Tracks.elm index 016f0381..a92126b3 100644 --- a/src/Applications/UI/Tracks.elm +++ b/src/Applications/UI/Tracks.elm @@ -14,6 +14,7 @@ import Html.Styled.Lazy exposing (..) import InfiniteList import Json.Decode as Json import Json.Encode +import List.Extra as List import Material.Icons.Action as Icons import Material.Icons.Av as Icons import Material.Icons.Content as Icons @@ -24,8 +25,9 @@ import Return2 as R2 import Return3 as R3 import Tachyons.Classes as T import Tracks exposing (..) -import Tracks.Collection exposing (..) +import Tracks.Collection as Collection exposing (..) import Tracks.Encoding as Encoding +import Tracks.Favourites as Favourites import UI.Kit import UI.Navigation exposing (..) import UI.Page exposing (Page) @@ -136,6 +138,14 @@ update msg model = ----------------------------------------- -- Favourites ----------------------------------------- + -- > Make a track a favourite, or remove it as a favourite + ToggleFavourite index -> + model.collection.harvested + |> List.getAt index + |> Maybe.map (toggleFavourite model) + |> Maybe.withDefault (R3.withNothing model) + + -- > Filter collection by favourites only {toggle} ToggleFavouritesOnly -> { model | favouritesOnly = not model.favouritesOnly } |> reviseCollection harvest @@ -241,6 +251,28 @@ reviseCollection collector model = +-- 📣 ░░ FAVOURITES + + +toggleFavourite : Model -> IdentifiedTrack -> R3D3 Model Msg Reply +toggleFavourite model ( i, t ) = + let + newFavourites = + Favourites.toggleInFavouritesList ( i, t ) model.favourites + + effect = + if model.favouritesOnly then + Collection.map (Favourites.toggleInTracksList t) >> harvest + + else + Collection.map (Favourites.toggleInTracksList t) + in + { model | favourites = newFavourites } + |> reviseCollection effect + |> N5.addReply SaveFavourites + + + -- 🗺 diff --git a/src/Applications/UI/Tracks/Core.elm b/src/Applications/UI/Tracks/Core.elm index b6e903af..5320593c 100644 --- a/src/Applications/UI/Tracks/Core.elm +++ b/src/Applications/UI/Tracks/Core.elm @@ -44,6 +44,7 @@ type Msg ----------------------------------------- -- Favourites ----------------------------------------- + | ToggleFavourite Int | ToggleFavouritesOnly ----------------------------------------- -- Search diff --git a/src/Applications/UI/Tracks/Scene/List.elm b/src/Applications/UI/Tracks/Scene/List.elm index a8b7e075..99893400 100644 --- a/src/Applications/UI/Tracks/Scene/List.elm +++ b/src/Applications/UI/Tracks/Scene/List.elm @@ -199,10 +199,12 @@ rowStyles idx { isNowPlaying } = -- COLUMNS -favouriteColumn : Bool -> Identifiers -> Html msg +favouriteColumn : Bool -> Identifiers -> Html Msg favouriteColumn favouritesOnly identifiers = brick - [ css (favouriteColumnStyles favouritesOnly identifiers) ] + [ css (favouriteColumnStyles favouritesOnly identifiers) + , onClick (ToggleFavourite identifiers.indexInList) + ] [ T.dtc, T.pl3, T.v_mid ] [ if identifiers.isFavourite then text "t" diff --git a/src/Library/Tracks/Favourites.elm b/src/Library/Tracks/Favourites.elm index 9226d53e..f86c2432 100644 --- a/src/Library/Tracks/Favourites.elm +++ b/src/Library/Tracks/Favourites.elm @@ -1,6 +1,7 @@ -module Tracks.Favourites exposing (match) +module Tracks.Favourites exposing (match, toggleInFavouritesList, toggleInTracksList) -import Tracks exposing (Favourite, Track) +import List.Extra as List +import Tracks exposing (Favourite, IdentifiedTrack, Track) @@ -21,3 +22,58 @@ match a b = ) in aa == ba && at == bt + + +toggleInTracksList : Track -> List IdentifiedTrack -> List IdentifiedTrack +toggleInTracksList track = + let + lartist = + lowercaseArtist track + + ltitle = + lowercaseTitle track + in + List.map + (\( i, t ) -> + if lowercaseArtist t == lartist && lowercaseTitle t == ltitle then + ( { i | isFavourite = not i.isFavourite }, t ) + + else + ( i, t ) + ) + + +toggleInFavouritesList : IdentifiedTrack -> List Favourite -> List Favourite +toggleInFavouritesList ( i, t ) favourites = + let + favourite = + { artist = t.tags.artist + , title = t.tags.title + } + in + case i.isFavourite of + True -> + -- Remove from list + List.filterNot + (match favourite) + favourites + + False -> + -- Add to list + List.append + favourites + [ favourite ] + + + +-- ⚗️ + + +lowercaseArtist : Track -> String +lowercaseArtist = + .tags >> .artist >> String.toLower + + +lowercaseTitle : Track -> String +lowercaseTitle = + .tags >> .title >> String.toLower