A music player that connects to your cloud/distributed storage.
1module Brain.Other.State exposing (..)
2
3import Alien
4import Brain.Common.State as Common
5import Brain.Ports as Ports
6import Brain.Task.Ports
7import Brain.Types exposing (..)
8import Dict
9import Json.Decode as Json
10import List.Extra as List
11import Return exposing (return)
12import Return.Ext as Return
13import Sources exposing (Service(..))
14import Sources.Encoding
15import Sources.Refresh.AccessToken
16import Time
17
18
19
20-- 🔱
21
22
23refreshedAccessToken : Json.Value -> Manager
24refreshedAccessToken value model =
25 case Json.decodeValue Sources.Refresh.AccessToken.portArgumentsDecoder value of
26 Ok portArguments ->
27 case portArguments.service of
28 Google ->
29 model.hypaethralUserData.sources
30 |> List.find (.id >> (==) portArguments.sourceId)
31 |> Maybe.map
32 (\source ->
33 source.data
34 |> Dict.insert "accessToken" portArguments.accessToken
35 |> Dict.insert "expiresAt" (String.fromInt portArguments.expiresAt)
36 |> (\newData -> { source | data = newData })
37 )
38 |> Maybe.map
39 (\source ->
40 source
41 |> Sources.Encoding.encode
42 |> Alien.broadcast Alien.UpdateSourceData
43 |> Ports.toUI
44 )
45 |> Maybe.withDefault Cmd.none
46 |> return model
47
48 _ ->
49 Return.singleton model
50
51 Err err ->
52 Common.reportUI Alien.ToCache (Json.errorToString err) model
53
54
55setCurrentTime : Time.Posix -> Manager
56setCurrentTime time model =
57 Return.singleton { model | currentTime = time }
58
59
60{-| Save alien data to cache.
61-}
62toCache : Json.Value -> Manager
63toCache data =
64 case Json.decodeValue Alien.hostDecoder data of
65 Ok alienEvent ->
66 case Alien.tagFromString alienEvent.tag of
67 Just tag ->
68 alienEvent.data
69 |> Brain.Task.Ports.toCache tag
70 |> Common.attemptPortTask (always Bypass)
71 |> Return.communicate
72
73 Nothing ->
74 Common.reportUI Alien.ToCache "Failed to decode alien tag"
75
76 Err err ->
77 err
78 |> Json.errorToString
79 |> Common.reportUI Alien.ToCache