A music player that connects to your cloud/distributed storage.
1module UI.Services.State exposing (..)
2
3import Browser.Navigation as Nav
4import Common
5import Http
6import LastFm
7import Notifications
8import Return exposing (andThen, return)
9import String.Ext as String
10import UI.Common.State exposing (showNotification)
11import UI.Types exposing (Manager, Msg(..))
12import UI.User.State.Export as User
13import Url
14
15
16
17-- 🔱
18
19
20connectLastFm : Manager
21connectLastFm model =
22 model.url
23 |> Common.urlOrigin
24 |> String.addSuffix "?action=authenticate/lastfm"
25 |> Url.percentEncode
26 |> String.append "&cb="
27 |> String.append
28 (String.append
29 "http://www.last.fm/api/auth/?api_key="
30 LastFm.apiKey
31 )
32 |> Nav.load
33 |> return model
34
35
36disconnectLastFm : Manager
37disconnectLastFm model =
38 User.saveSettings { model | lastFm = LastFm.disconnect model.lastFm }
39
40
41gotLastFmSession : Result Http.Error String -> Manager
42gotLastFmSession result model =
43 case result of
44 Err _ ->
45 showNotification
46 (Notifications.stickyError "Could not connect with Last.fm")
47 { model | lastFm = LastFm.failedToAuthenticate model.lastFm }
48
49 Ok sessionKey ->
50 { model | lastFm = LastFm.gotSessionKey sessionKey model.lastFm }
51 |> showNotification
52 (Notifications.success "Connected successfully with Last.fm")
53 |> andThen
54 User.saveSettings
55
56
57scrobble : { duration : Int, timestamp : Int, trackId : String } -> Manager
58scrobble { duration, timestamp, trackId } model =
59 case Maybe.map (.item >> .identifiedTrack) model.nowPlaying of
60 Just ( _, track ) ->
61 if trackId == track.id then
62 ( model
63 , LastFm.scrobble model.lastFm
64 { duration = duration
65 , msg = Bypass
66 , timestamp = timestamp
67 , track = track
68 }
69 )
70
71 else
72 Return.singleton model
73
74 Nothing ->
75 Return.singleton model