Skip to content
Snippets Groups Projects
Commit 22a181ef authored by Jean-Paul Calderone's avatar Jean-Paul Calderone
Browse files

Reduce duplication

and move platform-independent code out of Android module where it is expensive
to build/test.
parent 62029e6c
Branches
Tags
1 merge request!42Integrate with the Android "back" button and use it for navigation
-- | Implement platform-specific path-related operations for the Android
-- platform.
module FrontendPaths (getFilesDir, platformEventCallback, runWithRef) where
module FrontendPaths (getFilesDir, runWithRef) where
import Control.Concurrent (threadDelay)
import Control.Monad (forever)
import Control.Monad.IO.Class (liftIO)
import Data.Default.Class (def)
import Data.IORef (IORef, newIORef, readIORef)
import Data.String (fromString)
import JSDOM (currentWindow)
import JSDOM.History (back)
import JSDOM.Types (MonadDOM)
import JSDOM.Window (getHistory)
import Language.Javascript.JSaddle (JSM)
import MainWidget (startMainWidget)
import System.IO (hSetBuffering, stdout, stderr, BufferMode(LineBuffering))
import qualified Android.HaskellActivity as Android
import PlatformEvents
import PlatformEvents (PlatformEventCallbacks(..))
-- | Get the path to a directory that is private to the application where
-- internal application data may be written (and read back).
......@@ -28,22 +23,6 @@ import PlatformEvents
getFilesDir :: IO (Maybe FilePath)
getFilesDir = Android.getHaskellActivity >>= Android.getFilesDir
-- | Handle Android events related to our Activity.
platformEventCallback :: MonadDOM m => PlatformEvent -> m ()
platformEventCallback BackPressed = navigateBack
-- | Perform a standard "back" navigation through route history.
navigateBack :: MonadDOM m => m ()
navigateBack = do
windowM <- currentWindow
case windowM of
-- No window, no history navigation...
Nothing -> liftIO $ do
print "Back button but no window ..."
Just window -> do
history <- getHistory window
back history
-- | Instantiate the frontend hooked up to Android-specific platform event
-- sources. The frontend is responsible for populating an IORef with a
-- structure holding its handlers for those events.
......@@ -69,7 +48,6 @@ runWithRef makeJSM = do
let startPage = fromString "file:///android_asset/index.html"
startMainWidget a startPage (makeJSM (Just activityCallbackRef))
, Android._activityCallbacks_onBackPressed = do
print $ "Back pressed!"
f <- readIORef activityCallbackRef
onBackPressed f
}
......
......@@ -60,6 +60,7 @@ library
Controller
Frontend
FrontendPaths
Navigation
PlatformEvents
Static
Pages.FileBrowser
......
-- | Implement platform-specific path-related operations for the Linux
-- platform.
module FrontendPaths (getFilesDir, platformEventCallback, runWithRef) where
module FrontendPaths (getFilesDir, runWithRef) where
import Data.IORef (IORef, newIORef)
import System.Environment.XDG.BaseDir (getUserDataDir)
import System.Directory (createDirectoryIfMissing)
import JSDOM.Types (MonadDOM)
import Control.Monad.IO.Class (liftIO)
import Reflex.Dom (run)
import PlatformEvents
import Language.Javascript.JSaddle (JSM)
......@@ -23,13 +21,6 @@ getFilesDir = do
createDirectoryIfMissing True dataPath
pure $ Just dataPath
-- | Handle platform events on Linux (*not* for `ob run`).
--
-- Since we don't run in this configuration and we don't expect any such
-- events on Linux anyway, there is no real behavior here.
platformEventCallback :: (Show a, MonadDOM m) => a -> m ()
platformEventCallback = liftIO . print
-- | Run a frontend on Linux (not `ob run`).
runWithRef :: (Maybe (IORef PlatformEventCallbacks) -> JSM ()) -> IO ()
runWithRef makeJSM =
......
......@@ -17,7 +17,7 @@ import Controller (pageFromRoute)
import Static (mainCss, materialComponentsCss, materialComponentsJs, materialIconsCss, robotoCss)
import PlatformEvents (PlatformEventCallbacks(..), PlatformEvent(..))
import JSDOM.Types (liftJSM)
import FrontendPaths (platformEventCallback)
import Navigation (platformEventCallback)
import Data.Default.Class (def)
-- A frontend with no platform event integration. This is necessary for
......
{-# LANGUAGE GADTs #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Navigation where
import JSDOM (currentWindow)
import JSDOM.History (back)
import JSDOM.Window (getHistory)
import JSDOM.Types (MonadDOM)
import Control.Monad.IO.Class (liftIO)
import PlatformEvents (PlatformEvent(..))
-- | Handle platform events.
platformEventCallback :: MonadDOM m => PlatformEvent -> m ()
platformEventCallback BackPressed = navigateBack
-- | Perform a standard "back" navigation through route history.
navigateBack :: MonadDOM m => m ()
navigateBack = do
windowM <- currentWindow
case windowM of
-- No window, no history navigation...
Nothing -> liftIO $ do
print "Back button but no window ..."
Just window -> do
history <- getHistory window
back history
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment