From ea9a3a00ef05cdf29fa84fe63f06342ae7825f5e Mon Sep 17 00:00:00 2001 From: Steven Vandevelde Date: Fri, 25 May 2018 16:23:06 +0200 Subject: [PATCH] Closes #86 --- src/App/Ports.elm | 3 +++ src/App/Sources/Types.elm | 4 +++- src/App/Sources/Utils.elm | 6 ++++-- src/App/Sources/View.elm | 2 +- src/App/State.elm | 22 ++++++++++++++++++++++ src/App/Tracks/View.elm | 34 ++++++++++++++++++++++++++++------ src/App/Types.elm | 3 +++ src/Js/elm-loader.js | 13 +++++++++++++ 8 files changed, 77 insertions(+), 10 deletions(-) diff --git a/src/App/Ports.elm b/src/App/Ports.elm index 557886f5..b30f7717 100644 --- a/src/App/Ports.elm +++ b/src/App/Ports.elm @@ -27,6 +27,9 @@ port slaveEvent : AlienEvent -> Cmd msg port setIsTouchDevice : (Bool -> msg) -> Sub msg +port setIsOnline : (Bool -> msg) -> Sub msg + + port shortcutNext : (() -> msg) -> Sub msg diff --git a/src/App/Sources/Types.elm b/src/App/Sources/Types.elm index a21e9738..cd1c0429 100644 --- a/src/App/Sources/Types.elm +++ b/src/App/Sources/Types.elm @@ -91,4 +91,6 @@ type alias IsProcessing = type alias ViabilityDependencies = - { isElectron : Bool } + { isElectron : Bool + , isOnline : Bool + } diff --git a/src/App/Sources/Utils.elm b/src/App/Sources/Utils.elm index c931c75f..9e20ac23 100644 --- a/src/App/Sources/Utils.elm +++ b/src/App/Sources/Utils.elm @@ -48,7 +48,7 @@ isViable deps source = deps.isElectron _ -> - True + deps.isOnline {-| Some types of sources are only usable on certain platforms. @@ -56,6 +56,8 @@ Therefor in some situations we need to filter out the unusable ones. -} viableSourcesOnly : TopLevel.Model -> List Source -> List Source viableSourcesOnly model = - { isElectron = model.isElectron } + { isElectron = model.isElectron + , isOnline = model.isOnline + } |> isViable |> List.filter diff --git a/src/App/Sources/View.elm b/src/App/Sources/View.elm index 93d60a79..98f9c80a 100644 --- a/src/App/Sources/View.elm +++ b/src/App/Sources/View.elm @@ -67,7 +67,7 @@ entry page model = pageIndex model.sources.collection ( model.sources.isProcessing, model.sources.processingErrors ) - { isElectron = model.isElectron } + { isElectron = model.isElectron, isOnline = model.isOnline } New -> lazySpread3 diff --git a/src/App/State.elm b/src/App/State.elm index d1629d75..1a51d186 100644 --- a/src/App/State.elm +++ b/src/App/State.elm @@ -71,6 +71,7 @@ initialModel flags initialPage origin = , isDevelopmentEnvironment = flags.isDevelopmentEnvironment , isElectron = flags.isElectron , isHTTPS = flags.isHTTPS + , isOnline = flags.isOnline , isTouchDevice = False , origin = origin , screenHeight = flags.screenHeight @@ -159,6 +160,7 @@ update msg model = { isDevelopmentEnvironment = model.isDevelopmentEnvironment , isElectron = model.isElectron , isHTTPS = model.isHTTPS + , isOnline = model.isOnline , screenHeight = model.screenHeight } @@ -175,6 +177,25 @@ update msg model = newModel [ initialCommand flags model.routing.currentPage ] + -- Are we online or offline? + SetIsOnline bool -> + (!) + { model | isOnline = bool } + [ -- Show a notification when the status changes + -- + (if bool == False then + "😯 Your internet connection went missing" + else + "🚀 Aaand we're back" + ) + |> Notifications.Types.Message + |> ShowNotification + |> do + + -- Reset enabled-source ids + , do (SetEnabledSourceIds model.sources.collection) + ] + -- We have a message for this because this value comes -- in through a port. The reason for this is that the -- detection happens on the first "touchstart" event. @@ -832,6 +853,7 @@ subscriptions model = -- Ports , Ports.setIsTouchDevice SetIsTouchDevice + , Ports.setIsOnline SetIsOnline , Ports.slaveEventResult handleSlaveResult , Ports.syncCompleted SyncCompleted , Ports.syncStarted (\_ -> SyncStarted) diff --git a/src/App/Tracks/View.elm b/src/App/Tracks/View.elm index 0fb8d0a0..ca2ad9f8 100644 --- a/src/App/Tracks/View.elm +++ b/src/App/Tracks/View.elm @@ -66,7 +66,19 @@ entry : TopLevel.Model -> Node entry model = column (Tracks Container) - [ height fill ] + [ height fill + + -- Hide the tracks view when a different page is shown. + -- This is to avoid a visual glitch. + , inlineStyle + (case model.routing.currentPage of + Routing.Types.Index -> + [] + + _ -> + [ ( "opacity", "0" ) ] + ) + ] [ lazy3 navigation ( model.tracks.searchTerm @@ -226,9 +238,11 @@ content model = (if List.isEmpty model.tracks.collection.harvested then lazySpread3 noTracksFound - model.sources.collection - model.sources.isProcessing + ( model.sources.collection + , model.sources.isProcessing + ) (List.length model.tracks.favourites) + model.isOnline else case Maybe.map .autoGenerated model.tracks.selectedPlaylist of Just False -> @@ -256,6 +270,11 @@ content model = -- Content messages +msgOffline : Node +msgOffline = + el (Tracks Placeholder) [] (text "No sources available") + + msgProcessing : Node msgProcessing = el (Tracks Placeholder) [] (text "Processing Tracks") @@ -316,8 +335,8 @@ msgNoTracks = -- Content views, Part 1 -noTracksFound : List Source -> IsProcessing -> Int -> Node -noTracksFound sources isProcessing amountOfFavourites = +noTracksFound : ( List Source, IsProcessing ) -> Int -> Bool -> Node +noTracksFound ( sources, isProcessing ) amountOfFavourites isOnline = within [ logoBackdrop , el @@ -333,7 +352,10 @@ noTracksFound sources isProcessing amountOfFavourites = msgNoSources amountOfFavourites _ -> - msgNoTracks + if isOnline then + msgNoTracks + else + msgOffline ) ] (el diff --git a/src/App/Types.elm b/src/App/Types.elm index 792456c2..3fce68aa 100644 --- a/src/App/Types.elm +++ b/src/App/Types.elm @@ -38,6 +38,7 @@ type Msg | NoOp | Reset | SetIsTouchDevice Bool + | SetIsOnline Bool -- Keyboard | KeydownMsg Keyboard.Key | KeyupMsg Keyboard.Key @@ -102,6 +103,7 @@ type alias Model = , isDevelopmentEnvironment : Bool , isElectron : Bool , isHTTPS : Bool + , isOnline : Bool , isTouchDevice : Bool , origin : String , screenHeight : Int @@ -149,5 +151,6 @@ type alias ProgramFlags = { isDevelopmentEnvironment : Bool , isElectron : Bool , isHTTPS : Bool + , isOnline : Bool , screenHeight : Int } diff --git a/src/Js/elm-loader.js b/src/Js/elm-loader.js index d0d831c1..bc444a71 100644 --- a/src/Js/elm-loader.js +++ b/src/Js/elm-loader.js @@ -15,6 +15,7 @@ app = Elm.App.embed( { isDevelopmentEnvironment: location.hostname === "localhost" , isElectron: !!self.electron , isHTTPS: location.protocol === "https:" + , isOnline: navigator.onLine , screenHeight: window.screen.height || window.outerHeight } ); @@ -194,6 +195,18 @@ app.ports.fadeInNotifications.subscribe(_ => { +// +// > Offline + +window.addEventListener("online", onlineStatusChanged); +window.addEventListener("offline", onlineStatusChanged); + +function onlineStatusChanged() { + app.ports.setIsOnline.send(navigator.onLine); +} + + + // // > Queue -- 2.51.2