forked from
tokono.ma/diffuse
A music player that connects to your cloud/distributed storage.
1module UI.Playlists.Alfred exposing (create, select)
2
3import Alfred exposing (..)
4import Conditional exposing (ifThenElse)
5import Dict
6import Dict.Extra as Dict
7import List.Extra as List
8import Material.Icons.Round as Icons
9import Playlists exposing (..)
10import Tracks exposing (IdentifiedTrack)
11import UI.Types as UI
12
13
14
15-- CREATE
16
17
18create : { collectionMode : Bool } -> List IdentifiedTrack -> List Playlist -> Alfred UI.Msg
19create { collectionMode } tracks playlists =
20 let
21 index =
22 makeIndex playlists
23
24 subject =
25 ifThenElse collectionMode "collection" "playlist"
26 in
27 Alfred.create
28 { action = createAction collectionMode tracks
29 , index = index
30 , message =
31 if List.length tracks == 1 then
32 "Choose or create a " ++ subject ++ " to add this track to."
33
34 else
35 "Choose or create a " ++ subject ++ " to add these tracks to."
36 , operation = QueryOrMutation
37 }
38
39
40createAction : Bool -> List IdentifiedTrack -> Alfred.Action UI.Msg
41createAction collectionMode tracks ctx =
42 let
43 playlistTracks =
44 Tracks.toPlaylistTracks tracks
45 in
46 case ctx.result of
47 Just result ->
48 -- Add to playlist
49 --
50 case Alfred.stringValue result.value of
51 Just playlistName ->
52 [ UI.AddTracksToPlaylist
53 { collection = collectionMode
54 , playlistName = playlistName
55 , tracks = playlistTracks
56 }
57 ]
58
59 Nothing ->
60 []
61
62 Nothing ->
63 -- Create playlist,
64 -- if given a search term.
65 --
66 case ctx.searchTerm of
67 Just searchTerm ->
68 [ UI.AddTracksToPlaylist
69 { collection = collectionMode
70 , playlistName = searchTerm
71 , tracks = playlistTracks
72 }
73 ]
74
75 Nothing ->
76 []
77
78
79
80-- SELECT
81
82
83select : List Playlist -> Alfred UI.Msg
84select playlists =
85 let
86 index =
87 makeIndex playlists
88 in
89 Alfred.create
90 { action = selectAction playlists
91 , index = index
92 , message = "Select a playlist to play tracks from."
93 , operation = Query
94 }
95
96
97selectAction : List Playlist -> Alfred.Action UI.Msg
98selectAction playlists { result } =
99 case Maybe.andThen (\r -> List.find (.name >> Just >> (==) (stringValue r.value)) playlists) result of
100 Just playlist ->
101 [ UI.SelectPlaylist playlist ]
102
103 Nothing ->
104 []
105
106
107
108-- ㊙️
109
110
111makeIndex playlists =
112 playlists
113 |> Dict.groupBy
114 (\p ->
115 case ( p.autoGenerated, p.collection ) of
116 ( Just _, _ ) ->
117 "9 - AutoGenerated Directory Playlists"
118
119 ( Nothing, True ) ->
120 "1 - Your Collections"
121
122 ( Nothing, False ) ->
123 "2 - Your Playlists"
124 )
125 |> Dict.toList
126 |> List.map
127 (\( k, v ) ->
128 ( String.dropLeft 4 k
129 , v
130 |> List.uniqueBy .name
131 |> List.map
132 (\playlist ->
133 { icon = Just (Icons.queue_music 16)
134 , title = playlist.name
135 , value = Alfred.StringValue playlist.name
136 }
137 )
138 |> List.sortBy (.title >> String.toLower)
139 )
140 )
141 |> List.map
142 (\( k, v ) ->
143 { name = Just k, items = v }
144 )