diff --git a/elm.json b/elm.json index 6af5b45f..2af24873 100644 --- a/elm.json +++ b/elm.json @@ -8,6 +8,7 @@ "dependencies": { "direct": { "FabienHenon/elm-infinite-list-view": "3.1.1", + "Fresheyeball/elm-return": "7.1.0", "Gizra/elm-all-set": "1.0.1", "Gizra/elm-debouncer": "2.0.0", "Herteby/enum": "1.0.0", diff --git a/src/Applications/UI.elm b/src/Applications/UI.elm index 823d7231..44e487f9 100644 --- a/src/Applications/UI.elm +++ b/src/Applications/UI.elm @@ -10,13 +10,12 @@ import Chunky exposing (..) import Common exposing (Switch(..)) import Conditional exposing (..) import ContextMenu exposing (ContextMenu) -import Coordinates exposing (Viewport) import Css exposing (url) import Css.Classes as C -import Debouncer.Basic as Debouncer exposing (Debouncer) -import Dict exposing (Dict) +import Debouncer.Basic as Debouncer +import Dict import Dict.Ext as Dict -import File exposing (File) +import File import File.Download import File.Select import Html exposing (Html, section) @@ -24,7 +23,6 @@ import Html.Attributes exposing (class, id, style) import Html.Events exposing (on, onClick) import Html.Events.Extra.Pointer as Pointer import Html.Lazy as Lazy -import Http import Json.Decode import Json.Encode import Keyboard @@ -49,6 +47,8 @@ import Time import Tracks import Tracks.Encoding as Tracks import UI.Alfred as Alfred +import UI.Audio.State as Audio +import UI.Audio.Types as Audio import UI.Authentication as Authentication import UI.Authentication.ContextMenu as Authentication import UI.Backdrop as Backdrop @@ -59,7 +59,7 @@ import UI.DnD as DnD import UI.Equalizer as Equalizer import UI.Navigation as Navigation import UI.Notifications -import UI.Page as Page exposing (Page) +import UI.Page as Page import UI.Playlists as Playlists import UI.Playlists.Alfred import UI.Playlists.ContextMenu as Playlists @@ -78,6 +78,7 @@ import UI.Svg.Elements import UI.Tracks as Tracks import UI.Tracks.ContextMenu as Tracks import UI.Tracks.Scene.List +import UI.Types exposing (..) import Url exposing (Protocol(..), Url) import Url.Ext as Url import User.Layer exposing (..) @@ -88,15 +89,6 @@ import User.Layer.Methods.RemoteStorage as RemoteStorage -- ⛩ -type alias Flags = - { darkMode : Bool - , initialTime : Int - , isOnline : Bool - , upgrade : Bool - , viewport : Viewport - } - - main : Program Flags Model Msg main = Browser.application @@ -113,55 +105,6 @@ main = -- 🌳 -type alias Model = - { contextMenu : Maybe (ContextMenu Reply) - , confirmation : Maybe String - , currentTime : Time.Posix - , darkMode : Bool - , debounce : Debouncer Msg Msg - , downloading : Maybe { notificationId : Int } - , focusedOnInput : Bool - , isDragging : Bool - , isLoading : Bool - , isOnline : Bool - , isTouchDevice : Bool - , isUpgrading : Bool - , lastFm : LastFm.Model - , navKey : Nav.Key - , notifications : UI.Notifications.Model - , page : Page - , pressedKeys : List Keyboard.Key - , processAutomatically : Bool - , url : Url - , viewport : Viewport - - ----------------------------------------- - -- Audio - ----------------------------------------- - , audioDuration : Float - , audioHasStalled : Bool - , audioIsLoading : Bool - , audioIsPlaying : Bool - , audioPosition : Float - - -- - , progress : Dict String Float - , rememberProgress : Bool - - ----------------------------------------- - -- Children - ----------------------------------------- - , alfred : Alfred.Model - , authentication : Authentication.Model - , backdrop : Backdrop.Model - , equalizer : Equalizer.Model - , queue : Queue.Model - , playlists : Playlists.Model - , sources : Sources.Model - , tracks : Tracks.Model - } - - init : Flags -> Url -> Nav.Key -> Return Model Msg init flags url key = let @@ -242,84 +185,6 @@ init flags url key = -- 📣 -type Msg - = Bypass - | Reply Reply - -- - | Blur - | Debounce (Debouncer.Msg Msg) - | DownloadTracksFinished - | FocusedOnInput - | HideOverlay - | KeyboardMsg Keyboard.Msg - | LoadEnclosedUserData Json.Decode.Value - | LoadHypaethralUserData Json.Decode.Value - | PreferredColorSchemaChanged { dark : Bool } - | RemoveQueueSelection - | RemoveTrackSelection - | ResizedWindow ( Int, Int ) - | ShowNotification (Notification Reply) - | SetCurrentTime Time.Posix - | SetIsOnline Bool - | SetIsTouchDevice Bool - | StoppedDragging - | ToggleLoadingScreen Switch - ----------------------------------------- - -- Audio - ----------------------------------------- - | NoteProgress { trackId : String, progress : Float } - | PlayPause - | SetAudioDuration Float - | SetAudioHasStalled Bool - | SetAudioIsLoading Bool - | SetAudioIsPlaying Bool - | SetAudioPosition Float - | Stop - ----------------------------------------- - -- Authentication - ----------------------------------------- - | AuthenticationBootFailure String - | MissingSecretKey Json.Decode.Value - | NotAuthenticated - | RemoteStorageWebfinger RemoteStorage.Attributes (Result Http.Error String) - ----------------------------------------- - -- Children - ----------------------------------------- - | AlfredMsg Alfred.Msg - | AuthenticationMsg Authentication.Msg - | BackdropMsg Backdrop.Msg - | EqualizerMsg Equalizer.Msg - | PlaylistsMsg Playlists.Msg - | QueueMsg Queue.Msg - | SourcesMsg Sources.Msg - | TracksMsg Tracks.Msg - ----------------------------------------- - -- Import / Export - ----------------------------------------- - | Import File - | ImportJson String - ----------------------------------------- - -- Last.fm - ----------------------------------------- - | GotLastFmSession (Result Http.Error String) - | Scrobble { duration : Int, timestamp : Int, trackId : String } - ----------------------------------------- - -- Page Transitions - ----------------------------------------- - | PageChanged Page - ----------------------------------------- - -- Tracks Cache - ----------------------------------------- - | FailedToStoreTracksInCache (List String) - | FinishedStoringTracksInCache (List String) - ----------------------------------------- - -- URL - ----------------------------------------- - | ChangeUrlUsingPage Page - | LinkClicked Browser.UrlRequest - | UrlChanged Url - - update : Msg -> Model -> Return Model Msg update msg model = case msg of @@ -569,75 +434,12 @@ update msg model = _ -> return notDragging - ToggleLoadingScreen On -> + UI.Types.ToggleLoadingScreen On -> return { model | isLoading = True } - ToggleLoadingScreen Off -> + UI.Types.ToggleLoadingScreen Off -> return { model | isLoading = False } - ----------------------------------------- - -- Audio - ----------------------------------------- - NoteProgress { trackId, progress } -> - let - updatedProgressTable = - if not model.rememberProgress then - model.progress - - else if progress > 0.975 then - Dict.remove trackId model.progress - - else - Dict.insert trackId progress model.progress - in - if model.rememberProgress then - translateReply - SaveProgress - { model | progress = updatedProgressTable } - - else - return model - - PlayPause -> - if Maybe.isNothing model.queue.activeItem then - update (QueueMsg Queue.Shift) model - - else if model.audioIsPlaying then - returnWithModel model (Ports.pause ()) - - else - returnWithModel model (Ports.play ()) - - SetAudioDuration duration -> - (case model.tracks.nowPlaying of - Just ( _, track ) -> - { duration = round duration - , msg = Bypass - , track = track - } - |> LastFm.nowPlaying model.lastFm - |> returnWithCommand - - Nothing -> - return - ) - { model | audioDuration = duration } - - SetAudioHasStalled hasStalled -> - return { model | audioHasStalled = hasStalled } - - SetAudioIsLoading isLoading -> - return { model | audioIsLoading = isLoading } - - SetAudioIsPlaying isPlaying -> - return { model | audioIsPlaying = isPlaying } - - SetAudioPosition position -> - return { model | audioPosition = position } - - Stop -> - returnWithModel model (Ports.pause ()) - ----------------------------------------- -- Authentication ----------------------------------------- @@ -986,6 +788,12 @@ update msg model = _ -> returnWithModel model (resetUrl model.navKey url Page.Index) + ----------------------------------------- + -- TARGET => Pieces + ----------------------------------------- + Audio a -> + Audio.update a model + updateTracksModel : (Tracks.Model -> Tracks.Model) -> Model -> Model updateTracksModel fn model = @@ -1034,7 +842,7 @@ translateReply reply model = returnWithModel model (Ports.seek percentage) TogglePlayPause -> - update PlayPause model + Audio.playPause model ToggleRememberProgress -> translateReply SaveSettings { model | rememberProgress = not model.rememberProgress } @@ -1936,19 +1744,19 @@ subscriptions model = -- Audio -------- , Ports.activeQueueItemEnded (QueueMsg << always Queue.Shift) - , Ports.noteProgress NoteProgress - , Ports.setAudioDuration SetAudioDuration - , Ports.setAudioHasStalled SetAudioHasStalled - , Ports.setAudioIsLoading SetAudioIsLoading - , Ports.setAudioIsPlaying SetAudioIsPlaying - , Ports.setAudioPosition SetAudioPosition + , Ports.noteProgress (Audio << Audio.NoteProgress) + , Ports.setAudioDuration (Audio << Audio.SetDuration) + , Ports.setAudioHasStalled (Audio << Audio.SetHasStalled) + , Ports.setAudioIsLoading (Audio << Audio.SetIsLoading) + , Ports.setAudioIsPlaying (Audio << Audio.SetIsPlaying) + , Ports.setAudioPosition (Audio << Audio.SetPosition) -- Remote --------- , Ports.requestNext <| always (QueueMsg Queue.Shift) - , Ports.requestPlayPause <| always PlayPause + , Ports.requestPlayPause <| always (Audio Audio.PlayPause) , Ports.requestPrevious <| always (QueueMsg Queue.Rewind) - , Ports.requestStop <| always Stop + , Ports.requestStop <| always (Audio Audio.Stop) -- Children ----------- @@ -2021,7 +1829,7 @@ translateAlienData event = SourcesMsg Sources.FinishedProcessing Just Alien.HideLoadingScreen -> - ToggleLoadingScreen Off + UI.Types.ToggleLoadingScreen Off Just Alien.ImportLegacyData -> ShowNotification (Notifications.success "Imported data successfully!") diff --git a/src/Applications/UI/Audio/State.elm b/src/Applications/UI/Audio/State.elm new file mode 100644 index 00000000..0b4a0b75 --- /dev/null +++ b/src/Applications/UI/Audio/State.elm @@ -0,0 +1,126 @@ +module UI.Audio.State exposing (..) + +import Dict +import LastFm +import Maybe.Extra as Maybe +import Return exposing (return) +import Return.Ext as Return exposing (communicate) +import UI.Audio.Types as Audio exposing (..) +import UI.Ports as Ports +import UI.Queue as Queue +import UI.Reply as Reply +import UI.Types as UI exposing (Manager) + + + +-- 📣 + + +update : Audio.Msg -> Manager +update msg = + case msg of + NoteProgress a -> + noteProgress a + + PlayPause -> + playPause + + SetDuration a -> + setDuration a + + SetHasStalled a -> + setHasStalled a + + SetIsLoading a -> + setIsLoading a + + SetIsPlaying a -> + setIsPlaying a + + SetPosition a -> + setPosition a + + Stop -> + stop + + + +-- 🔱 + + +noteProgress : { trackId : String, progress : Float } -> Manager +noteProgress { trackId, progress } model = + let + updatedProgressTable = + if not model.rememberProgress then + model.progress + + else if progress > 0.975 then + Dict.remove trackId model.progress + + else + Dict.insert trackId progress model.progress + in + if model.rememberProgress then + -- TODO! + Return.performance + (UI.Reply Reply.SaveProgress) + { model | progress = updatedProgressTable } + + else + Return.singleton model + + +playPause : Manager +playPause model = + if Maybe.isNothing model.queue.activeItem then + -- TODO! + Return.performance (UI.QueueMsg Queue.Shift) model + + else if model.audioIsPlaying then + communicate (Ports.pause ()) model + + else + communicate (Ports.play ()) model + + +setDuration : Float -> Manager +setDuration duration model = + (case model.tracks.nowPlaying of + Just ( _, track ) -> + { duration = round duration + , msg = UI.Bypass + , track = track + } + |> LastFm.nowPlaying model.lastFm + |> communicate + + Nothing -> + Return.singleton + ) + { model | audioDuration = duration } + + +setHasStalled : Bool -> Manager +setHasStalled hasStalled model = + Return.singleton { model | audioHasStalled = hasStalled } + + +setIsLoading : Bool -> Manager +setIsLoading isLoading model = + Return.singleton { model | audioIsLoading = isLoading } + + +setIsPlaying : Bool -> Manager +setIsPlaying isPlaying model = + Return.singleton { model | audioIsPlaying = isPlaying } + + +setPosition : Float -> Manager +setPosition position model = + Return.singleton { model | audioPosition = position } + + +stop : Manager +stop = + communicate (Ports.pause ()) diff --git a/src/Applications/UI/Audio/Types.elm b/src/Applications/UI/Audio/Types.elm new file mode 100644 index 00000000..257d0422 --- /dev/null +++ b/src/Applications/UI/Audio/Types.elm @@ -0,0 +1,14 @@ +module UI.Audio.Types exposing (..) + +-- 📣 + + +type Msg + = NoteProgress { trackId : String, progress : Float } + | PlayPause + | SetDuration Float + | SetHasStalled Bool + | SetIsLoading Bool + | SetIsPlaying Bool + | SetPosition Float + | Stop diff --git a/src/Applications/UI/Settings.elm b/src/Applications/UI/Settings.elm index 3ef4e35f..bf11a783 100644 --- a/src/Applications/UI/Settings.elm +++ b/src/Applications/UI/Settings.elm @@ -5,12 +5,11 @@ import Conditional exposing (ifThenElse) import Css.Classes as C import Html exposing (Html, text) import Html.Attributes exposing (..) -import Html.Events exposing (onClick, onInput) +import Html.Events exposing (onClick) import Html.Lazy import LastFm import Material.Icons as Icons import Material.Icons.Types exposing (Coloring(..)) -import Maybe.Extra as Maybe import Settings import UI.Backdrop as Backdrop exposing (backgroundPositioning) import UI.Kit @@ -243,13 +242,6 @@ label l = [ UI.Kit.label [] l ] -textField : List (Html.Attribute Reply) -> Html Reply -textField attributes = - chunk - [ C.max_w_xs ] - [ UI.Kit.textField attributes ] - - -- AUTHENTICATION diff --git a/src/Applications/UI/Types.elm b/src/Applications/UI/Types.elm new file mode 100644 index 00000000..86634117 --- /dev/null +++ b/src/Applications/UI/Types.elm @@ -0,0 +1,230 @@ +module UI.Types exposing (..) + +import Alfred exposing (Alfred) +import Alien +import Browser +import Browser.Dom +import Browser.Events +import Browser.Navigation as Nav +import Chunky exposing (..) +import Common exposing (Switch(..)) +import Conditional exposing (..) +import ContextMenu exposing (ContextMenu) +import Coordinates exposing (Viewport) +import Css exposing (url) +import Css.Classes as C +import Debouncer.Basic as Debouncer exposing (Debouncer) +import Dict exposing (Dict) +import Dict.Ext as Dict +import File exposing (File) +import File.Download +import File.Select +import Html exposing (Html, section) +import Html.Attributes exposing (class, id, style) +import Html.Events exposing (on, onClick) +import Html.Events.Extra.Pointer as Pointer +import Html.Lazy as Lazy +import Http +import Json.Decode +import Json.Encode +import Keyboard +import LastFm +import List.Ext as List +import List.Extra as List +import Maybe.Extra as Maybe +import Notifications exposing (Notification) +import Playlists.Encoding as Playlists +import Process +import Queue +import Return2 exposing (..) +import Return3 +import Settings +import Sources +import Sources.Encoding as Sources +import Sources.Services.Dropbox +import Sources.Services.Google +import String.Ext as String +import Task +import Time +import Tracks +import Tracks.Encoding as Tracks +import UI.Alfred as Alfred +import UI.Audio.Types as Audio +import UI.Authentication as Authentication +import UI.Authentication.ContextMenu as Authentication +import UI.Backdrop as Backdrop +import UI.Console +import UI.ContextMenu +import UI.Demo as Demo +import UI.DnD as DnD +import UI.Equalizer as Equalizer +import UI.Navigation as Navigation +import UI.Notifications +import UI.Page as Page exposing (Page) +import UI.Playlists as Playlists +import UI.Playlists.Alfred +import UI.Playlists.ContextMenu as Playlists +import UI.Playlists.Directory +import UI.Ports as Ports +import UI.Queue as Queue +import UI.Queue.ContextMenu as Queue +import UI.Reply as Reply exposing (Reply(..)) +import UI.Settings as Settings +import UI.Settings.Page +import UI.Sources as Sources +import UI.Sources.ContextMenu as Sources +import UI.Sources.Form +import UI.Sources.Page +import UI.Svg.Elements +import UI.Tracks as Tracks +import UI.Tracks.ContextMenu as Tracks +import UI.Tracks.Scene.List +import Url exposing (Protocol(..), Url) +import Url.Ext as Url +import User.Layer exposing (..) +import User.Layer.Methods.RemoteStorage as RemoteStorage + + + +-- ⛩ + + +type alias Flags = + { darkMode : Bool + , initialTime : Int + , isOnline : Bool + , upgrade : Bool + , viewport : Viewport + } + + + +-- 🌳 + + +type alias Model = + { contextMenu : Maybe (ContextMenu Reply) + , confirmation : Maybe String + , currentTime : Time.Posix + , darkMode : Bool + , debounce : Debouncer Msg Msg + , downloading : Maybe { notificationId : Int } + , focusedOnInput : Bool + , isDragging : Bool + , isLoading : Bool + , isOnline : Bool + , isTouchDevice : Bool + , isUpgrading : Bool + , lastFm : LastFm.Model + , navKey : Nav.Key + , notifications : UI.Notifications.Model + , page : Page + , pressedKeys : List Keyboard.Key + , processAutomatically : Bool + , url : Url + , viewport : Viewport + + ----------------------------------------- + -- Audio + ----------------------------------------- + , audioDuration : Float + , audioHasStalled : Bool + , audioIsLoading : Bool + , audioIsPlaying : Bool + , audioPosition : Float + + -- + , progress : Dict String Float + , rememberProgress : Bool + + ----------------------------------------- + -- Children + ----------------------------------------- + , alfred : Alfred.Model + , authentication : Authentication.Model + , backdrop : Backdrop.Model + , equalizer : Equalizer.Model + , queue : Queue.Model + , playlists : Playlists.Model + , sources : Sources.Model + , tracks : Tracks.Model + } + + + +-- 📣 + + +type Msg + = Bypass + | Reply Reply + -- + | Blur + | Debounce (Debouncer.Msg Msg) + | DownloadTracksFinished + | FocusedOnInput + | HideOverlay + | KeyboardMsg Keyboard.Msg + | LoadEnclosedUserData Json.Decode.Value + | LoadHypaethralUserData Json.Decode.Value + | PreferredColorSchemaChanged { dark : Bool } + | RemoveQueueSelection + | RemoveTrackSelection + | ResizedWindow ( Int, Int ) + | ShowNotification (Notification Reply) + | SetCurrentTime Time.Posix + | SetIsOnline Bool + | SetIsTouchDevice Bool + | StoppedDragging + | ToggleLoadingScreen Switch + ----------------------------------------- + -- Authentication + ----------------------------------------- + | AuthenticationBootFailure String + | MissingSecretKey Json.Decode.Value + | NotAuthenticated + | RemoteStorageWebfinger RemoteStorage.Attributes (Result Http.Error String) + ----------------------------------------- + -- Children + ----------------------------------------- + | AlfredMsg Alfred.Msg + | AuthenticationMsg Authentication.Msg + | BackdropMsg Backdrop.Msg + | EqualizerMsg Equalizer.Msg + | PlaylistsMsg Playlists.Msg + | QueueMsg Queue.Msg + | SourcesMsg Sources.Msg + | TracksMsg Tracks.Msg + ----------------------------------------- + -- Import / Export + ----------------------------------------- + | Import File + | ImportJson String + ----------------------------------------- + -- Last.fm + ----------------------------------------- + | GotLastFmSession (Result Http.Error String) + | Scrobble { duration : Int, timestamp : Int, trackId : String } + ----------------------------------------- + -- Page Transitions + ----------------------------------------- + | PageChanged Page + ----------------------------------------- + -- Tracks Cache + ----------------------------------------- + | FailedToStoreTracksInCache (List String) + | FinishedStoringTracksInCache (List String) + ----------------------------------------- + -- URL + ----------------------------------------- + | ChangeUrlUsingPage Page + | LinkClicked Browser.UrlRequest + | UrlChanged Url + ----------------------------------------- + -- TARGET => Pieces + ----------------------------------------- + | Audio Audio.Msg + + +type alias Manager = + Model -> ( Model, Cmd Msg ) diff --git a/src/Library/Return/Ext.elm b/src/Library/Return/Ext.elm new file mode 100644 index 00000000..4da40a90 --- /dev/null +++ b/src/Library/Return/Ext.elm @@ -0,0 +1,22 @@ +module Return.Ext exposing (..) + +import Task + + + +-- 🔱 + + +communicate : Cmd msg -> model -> ( model, Cmd msg ) +communicate c m = + ( m, c ) + + +{-| TODO: Remove when finished with refactor +-} +performance : msg -> model -> ( model, Cmd msg ) +performance msg model = + msg + |> Task.succeed + |> Task.perform identity + |> Tuple.pair model