|
| 1 | +----------------------------------------------------------------------------- |
| 2 | +{-# LANGUAGE GeneralizedNewtypeDeriving #-} |
| 3 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 4 | +{-# LANGUAGE TypeApplications #-} |
| 5 | +{-# LANGUAGE RecordWildCards #-} |
| 6 | +{-# LANGUAGE ViewPatterns #-} |
| 7 | +{-# LANGUAGE LambdaCase #-} |
| 8 | +{-# LANGUAGE CPP #-} |
| 9 | +----------------------------------------------------------------------------- |
| 10 | +-- | |
| 11 | +-- Module : Miso.Navigator |
| 12 | +-- Copyright : (C) 2016-2025 David M. Johnson |
| 13 | +-- License : BSD3-style (see the file LICENSE) |
| 14 | +-- Maintainer : David M. Johnson <[email protected]> |
| 15 | +-- Stability : experimental |
| 16 | +-- Portability : non-portable |
| 17 | +---------------------------------------------------------------------------- |
| 18 | +module Miso.Navigator |
| 19 | + ( -- ** User media |
| 20 | + getUserMedia |
| 21 | + , userMedia |
| 22 | + , UserMedia (..) |
| 23 | + , Stream |
| 24 | + -- ** Clipboard |
| 25 | + , copyClipboard |
| 26 | + -- ** OnLine |
| 27 | + , isOnLine |
| 28 | + -- ** Geolocation |
| 29 | + , geolocation |
| 30 | + , Geolocation (..) |
| 31 | + , GeolocationError (..) |
| 32 | + ) where |
| 33 | +----------------------------------------------------------------------------- |
| 34 | +import Control.Monad ((<=<)) |
| 35 | +import Language.Javascript.JSaddle |
| 36 | +import Prelude hiding ((!!)) |
| 37 | +----------------------------------------------------------------------------- |
| 38 | +import Miso.String |
| 39 | +import Miso.Effect |
| 40 | +import qualified Miso.FFI.Internal as FFI |
| 41 | +---------------------------------------------------------------------------- |
| 42 | +type Stream = JSVal |
| 43 | +---------------------------------------------------------------------------- |
| 44 | +-- | Get access to user's media devices. |
| 45 | +-- |
| 46 | +-- <https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia> |
| 47 | +-- |
| 48 | +getUserMedia |
| 49 | + :: UserMedia |
| 50 | + -- ^ Options |
| 51 | + -> (Stream -> action) |
| 52 | + -- ^ Successful callback |
| 53 | + -> (JSVal -> action) |
| 54 | + -- ^ Errorful callback |
| 55 | + -> Effect parent model action |
| 56 | +getUserMedia UserMedia {..} successful errorful = |
| 57 | + withSink $ \sink -> |
| 58 | + FFI.getUserMedia audio video |
| 59 | + (sink . successful) |
| 60 | + (sink . errorful) |
| 61 | +----------------------------------------------------------------------------- |
| 62 | +-- | Get access to the user's clipboard. |
| 63 | +-- |
| 64 | +-- <https://developer.mozilla.org/en-US/docs/Web/API/Navigator/clipboard> |
| 65 | +-- |
| 66 | +copyClipboard |
| 67 | + :: MisoString |
| 68 | + -- ^ Options |
| 69 | + -> action |
| 70 | + -- ^ Successful callback |
| 71 | + -> (JSVal -> action) |
| 72 | + -- ^ Errorful callback |
| 73 | + -> Effect parent model action |
| 74 | +copyClipboard txt successful errorful = |
| 75 | + withSink $ \sink -> |
| 76 | + FFI.copyClipboard txt |
| 77 | + (sink successful) |
| 78 | + (sink . errorful) |
| 79 | +----------------------------------------------------------------------------- |
| 80 | +-- | Get user's online status |
| 81 | +-- |
| 82 | +-- <https://developer.mozilla.org/en-US/docs/Web/API/Navigator/onLine> |
| 83 | +-- |
| 84 | +isOnLine |
| 85 | + :: (Bool -> action) |
| 86 | + -- ^ Successful callback |
| 87 | + -> Effect parent model action |
| 88 | +isOnLine action = io (action <$> FFI.isOnLine) |
| 89 | +----------------------------------------------------------------------------- |
| 90 | +-- | Type for dealing with 'navigator.mediaDevices.getUserMedia' |
| 91 | +-- |
| 92 | +-- <https://developer.mozilla.org/en-US/docs/Web/API/Navigator/mediaDevices> |
| 93 | +-- |
| 94 | +data UserMedia |
| 95 | + = UserMedia |
| 96 | + { audio, video :: Bool |
| 97 | + } deriving (Show, Eq) |
| 98 | +----------------------------------------------------------------------------- |
| 99 | +-- | Default 'UserMedia' |
| 100 | +userMedia :: UserMedia |
| 101 | +userMedia = UserMedia True True |
| 102 | +----------------------------------------------------------------------------- |
| 103 | +-- | Geolocation fetching |
| 104 | +-- |
| 105 | +-- <https://developer.mozilla.org/en-US/docs/Web/API/Navigator/geolocation> |
| 106 | +-- |
| 107 | +geolocation |
| 108 | + :: (Geolocation -> action) |
| 109 | + -> (GeolocationError -> action) |
| 110 | + -> Effect parent model action |
| 111 | +geolocation successful errorful = do |
| 112 | + withSink $ \sink -> |
| 113 | + FFI.geolocation |
| 114 | + (sink . successful <=< fromJSValUnchecked) |
| 115 | + (sink . errorful <=< fromJSValUnchecked) |
| 116 | +----------------------------------------------------------------------------- |
| 117 | +data GeolocationError = GeolocationError GeolocationErrorCode MisoString |
| 118 | + deriving (Show, Eq) |
| 119 | +----------------------------------------------------------------------------- |
| 120 | +instance FromJSVal GeolocationError where |
| 121 | + fromJSVal v = do |
| 122 | + code <- fromJSVal =<< (v ! "code") |
| 123 | + msg <- fromJSVal =<< (v ! "message") |
| 124 | + pure (GeolocationError <$> code <*> msg) |
| 125 | +----------------------------------------------------------------------------- |
| 126 | +data GeolocationErrorCode |
| 127 | + = PERMISSION_DENIED |
| 128 | + | POSITION_UNAVAILABLE |
| 129 | + | TIMEOUT |
| 130 | + deriving (Enum, Show, Eq) |
| 131 | +----------------------------------------------------------------------------- |
| 132 | +instance FromJSVal GeolocationErrorCode where |
| 133 | + fromJSVal code = |
| 134 | + fromJSValUnchecked code >>= \case |
| 135 | + (1 :: Int) -> pure (Just PERMISSION_DENIED) |
| 136 | + 2 -> pure (Just POSITION_UNAVAILABLE) |
| 137 | + 3 -> pure (Just TIMEOUT) |
| 138 | + _ -> pure Nothing |
| 139 | +----------------------------------------------------------------------------- |
| 140 | +data Geolocation |
| 141 | + = Geolocation |
| 142 | + { latitude, longitude, accuracy :: Double |
| 143 | + } deriving (Show, Eq) |
| 144 | +----------------------------------------------------------------------------- |
| 145 | +instance FromJSVal Geolocation where |
| 146 | + fromJSVal geo = do |
| 147 | + lat <- fromJSVal =<< geo ! "coords" ! "latitude" |
| 148 | + lon <- fromJSVal =<< geo ! "coords" ! "longitude" |
| 149 | + acc <- fromJSVal =<< geo ! "coords" ! "accuracy" |
| 150 | + pure (Geolocation <$> lat <*> lon <*> acc) |
| 151 | +----------------------------------------------------------------------------- |
0 commit comments