@@ -1,15 +1,11 @@
1module Language.Javascript.JSaddle.WKWebView
1module Language.Javascript.JSaddle.WKWebView
2 ( jsaddleMain
2 ( jsaddleMain
3− , jsaddleMainHTMLWithBaseURL
4 , jsaddleMainFile
3 , jsaddleMainFile
5 , WKWebView(..)
4 , WKWebView(..)
8 , runWithAppConfig
6 , runWithAppConfig
11 , mainBundleResourcePath
8 , mainBundleResourcePath
12− , ApplicationState (..)
13 , AppDelegateConfig (..)
9 , AppDelegateConfig (..)
14 , AppDelegateNotificationConfig (..)
10 , AppDelegateNotificationConfig (..)
15 , AuthorizationOption (..)
11 , AuthorizationOption (..)
@@ -21,11 +17,9 @@ import Data.Set (Set)
21import qualified Data.Set as Set
17import qualified Data.Set as Set
22import Data.Word
18import Data.Word
23import Foreign.Marshal.Utils (fromBool)
19import Foreign.Marshal.Utils (fromBool)
24−import Language.Javascript.JSaddle.WKWebView.Internal
20+import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
25− (jsaddleMain, jsaddleMainHTMLWithBaseURL, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
26import System.Environment (getProgName)
21import System.Environment (getProgName)
27import Foreign.C.String (CString, withCString)
22import Foreign.C.String (CString, withCString)
28−import Foreign.C.Types (CBool, CInt)
29import Foreign.StablePtr (StablePtr, newStablePtr)
23import Foreign.StablePtr (StablePtr, newStablePtr)
30import Language.Javascript.JSaddle (JSM)
24import Language.Javascript.JSaddle (JSM)
@@ -41,7 +35,6 @@ foreign import ccall runInWKWebView
41 -> StablePtr (IO ()) -- applicationWillTerminate
35 -> StablePtr (IO ()) -- applicationWillTerminate
42 -> StablePtr (IO ()) -- applicationSignificantTimeChange
36 -> StablePtr (IO ()) -- applicationSignificantTimeChange
43 -> StablePtr (CString -> IO ()) -- applicationUniversalLink
37 -> StablePtr (CString -> IO ()) -- applicationUniversalLink
44− -> StablePtr (CInt -> CString -> IO ()) -- applicationDidReceiveRemoteNotification
45 -> Word64 -- whether to run requestAuthorizationWithOptions
38 -> Word64 -- whether to run requestAuthorizationWithOptions
46 -> Word64 -- Ask for Badge authorization
39 -> Word64 -- Ask for Badge authorization
47 -> Word64 -- Ask for Sound authorization
40 -> Word64 -- Ask for Sound authorization
@@ -50,8 +43,6 @@ foreign import ccall runInWKWebView
50 -> Word64 -- registerForRemoteNotifications
43 -> Word64 -- registerForRemoteNotifications
51 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
44 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
52 -> StablePtr (CString -> IO ()) -- didFailToRegisterForRemoteNotificationsWithError
45 -> StablePtr (CString -> IO ()) -- didFailToRegisterForRemoteNotificationsWithError
53− -> Word64 -- developerExtrasEnabled
54− -> StablePtr (CString -> IO CBool) -- applicationOpenFile
57data AppDelegateConfig = AppDelegateConfig
48data AppDelegateConfig = AppDelegateConfig
@@ -64,25 +55,7 @@ data AppDelegateConfig = AppDelegateConfig
64 , _appDelegateConfig_applicationWillTerminate :: IO ()
55 , _appDelegateConfig_applicationWillTerminate :: IO ()
65 , _appDelegateConfig_applicationSignificantTimeChange :: IO ()
56 , _appDelegateConfig_applicationSignificantTimeChange :: IO ()
66 , _appDelegateConfig_applicationUniversalLink :: CString -> IO ()
57 , _appDelegateConfig_applicationUniversalLink :: CString -> IO ()
67− -- ^ Called when the app is launched from a URL. 'CString' contains the URL.
69− -- The Core Foundation plist key CFBundleURLTypes controls which URL schemes
70− -- the app should respond to.
71− , _appDelegateConfig_applicationDidReceiveRemoteNotification :: ApplicationState -> CString -> IO ()
72− -- ^ Called when the application receives a remote notification.
73− -- 'ApplicationState' is the current state of the app.
74− -- The 'CString' JSON-encoded user info contained in the notification.
75− -- For more information see https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623117-application?language=objc
76 , _appDelegateConfig_appDelegateNotificationConfig :: AppDelegateNotificationConfig
58 , _appDelegateConfig_appDelegateNotificationConfig :: AppDelegateNotificationConfig
77− , _appDelegateConfig_developerExtrasEnabled :: Bool
78− -- ^ Allow devtools in the app. Defaults to 'True'
79− , _appDelegateConfig_applicationOpenFile :: CString -> IO Bool
80− -- ^ Called when the app is launched by opening a file. 'CString' contains
81− -- the file path. Return value is 'True' if the file was successfully
82− -- opened. Defaults to ignoring the file and returning 'False'.
84− -- The Core Foundation plist key CFBundleDocumentTypes controls which file
85− -- types should be associated with the app.
88instance Default AppDelegateConfig where
61instance Default AppDelegateConfig where
@@ -96,10 +69,7 @@ instance Default AppDelegateConfig where
96 , _appDelegateConfig_applicationWillTerminate = return ()
69 , _appDelegateConfig_applicationWillTerminate = return ()
97 , _appDelegateConfig_applicationSignificantTimeChange = return ()
70 , _appDelegateConfig_applicationSignificantTimeChange = return ()
98 , _appDelegateConfig_applicationUniversalLink = \_ -> return ()
71 , _appDelegateConfig_applicationUniversalLink = \_ -> return ()
99− , _appDelegateConfig_applicationDidReceiveRemoteNotification = \_ _ -> return ()
100 , _appDelegateConfig_appDelegateNotificationConfig = def
72 , _appDelegateConfig_appDelegateNotificationConfig = def
101− , _appDelegateConfig_developerExtrasEnabled = True
102− , _appDelegateConfig_applicationOpenFile = \_ -> return False
105data AuthorizationOption = AuthorizationOption_Badge
75data AuthorizationOption = AuthorizationOption_Badge
@@ -108,12 +78,6 @@ data AuthorizationOption = AuthorizationOption_Badge
108 | AuthorizationOption_CarPlay
78 | AuthorizationOption_CarPlay
109 deriving (Show, Read, Eq, Ord)
79 deriving (Show, Read, Eq, Ord)
111−data ApplicationState = ApplicationState_Active
112− | ApplicationState_Inactive
113− | ApplicationState_Background
114− | ApplicationState_Unknown CInt
115− deriving (Show, Read, Eq, Ord)
117data AppDelegateNotificationConfig = AppDelegateNotificationConfig
81data AppDelegateNotificationConfig = AppDelegateNotificationConfig
118 { _appDelegateNotificationConfig_requestAuthorizationWithOptions :: Maybe (Set AuthorizationOption)
82 { _appDelegateNotificationConfig_requestAuthorizationWithOptions :: Maybe (Set AuthorizationOption)
119 , _appDelegateNotificationConfig_registerForRemoteNotifications :: Bool
83 , _appDelegateNotificationConfig_registerForRemoteNotifications :: Bool
@@ -132,38 +96,29 @@ instance Default AppDelegateNotificationConfig where
133-- | Run JSaddle in a WKWebView
97-- | Run JSaddle in a WKWebView
134run :: JSM () -> IO ()
98run :: JSM () -> IO ()
135−run f = run' def (jsaddleMain f)
99+run = run' Nothing def
137-- | Run JSaddle in a WKWebView
101-- | Run JSaddle in a WKWebView
138runWithAppConfig :: AppDelegateConfig -> JSM () -> IO ()
102runWithAppConfig :: AppDelegateConfig -> JSM () -> IO ()
139−runWithAppConfig cfg = run' cfg . jsaddleMain
103+runWithAppConfig = run' Nothing
141-- | Run JSaddle in a WKWebView first loading the specified file
105-- | Run JSaddle in a WKWebView first loading the specified file
142-- from the mainBundle (relative to the resourcePath).
106-- from the mainBundle (relative to the resourcePath).
144− :: ByteString -- ^ The file to navigate to.
107+runFile :: ByteString -- ^ The file to navigate to.
145− -> ByteString -- ^ The path to allow read access to.
108+ -> ByteString -- ^ The path to allow read access to.
146− -> AppDelegateConfig
109+ -> AppDelegateConfig
147− -> JSM ()
110+ -> JSM ()
148− -> IO ()
111+ -> IO ()
149−runFile url allowing cfg = run' cfg . jsaddleMainFile url allowing
112+runFile url allowing = run' $ Just (url, allowing)
151−-- | Run JSaddle in a WKWebView first loading the specified html
152−-- as though it came from the specified URL
153−-- (calls WKWebKit function loadHTMLString).
155− :: ByteString -- ^ HTML to load.
114+run' :: Maybe (ByteString, ByteString)
156− -> ByteString -- ^ pretend it came from this URL.
157− -> AppDelegateConfig
160−runHTMLWithBaseURL url allowing cfg = run' cfg . jsaddleMainHTMLWithBaseURL url allowing
162−run' :: AppDelegateConfig
115+ -> AppDelegateConfig
163− -> (WKWebView -> IO ())
116+ -> JSM ()
165−run' cfg main = do
118+run' mUrl cfg f = do
166− handler <- newStablePtr main
119+ handler <- case mUrl of
120+ Just (url, allowing) -> newStablePtr (jsaddleMainFile url allowing f)
121+ Nothing -> newStablePtr (jsaddleMain f)
167 progName <- getProgName
122 progName <- getProgName
169 -- AppDelegate callbacks
124 -- AppDelegate callbacks
@@ -176,14 +131,6 @@ run' cfg main = do
176 applicationWillTerminate <- newStablePtr $ _appDelegateConfig_applicationWillTerminate cfg
131 applicationWillTerminate <- newStablePtr $ _appDelegateConfig_applicationWillTerminate cfg
177 applicationSignificantTimeChange <- newStablePtr $ _appDelegateConfig_applicationSignificantTimeChange cfg
132 applicationSignificantTimeChange <- newStablePtr $ _appDelegateConfig_applicationSignificantTimeChange cfg
178 applicationUniversalLink <- newStablePtr $ _appDelegateConfig_applicationUniversalLink cfg
133 applicationUniversalLink <- newStablePtr $ _appDelegateConfig_applicationUniversalLink cfg
179− applicationDidReceiveRemoteNotification <- newStablePtr $ \n s -> do
180− let appState = case n of
181− 0 -> ApplicationState_Active
182− 1 -> ApplicationState_Inactive
183− 2 -> ApplicationState_Background
184− _ -> ApplicationState_Unknown n
185− _appDelegateConfig_applicationDidReceiveRemoteNotification cfg appState s
186− applicationOpenFile <- newStablePtr $ fmap fromBool . _appDelegateConfig_applicationOpenFile cfg
188 -- AppDelegate notification configuration
135 -- AppDelegate notification configuration
189 let ncfg = _appDelegateConfig_appDelegateNotificationConfig cfg
136 let ncfg = _appDelegateConfig_appDelegateNotificationConfig cfg
@@ -206,7 +153,6 @@ run' cfg main = do
206 applicationWillTerminate
153 applicationWillTerminate
207 applicationSignificantTimeChange
154 applicationSignificantTimeChange
208 applicationUniversalLink
155 applicationUniversalLink
209− applicationDidReceiveRemoteNotification
210 (fromBool requestAuthorizationWithOptions)
156 (fromBool requestAuthorizationWithOptions)
211 (fromBool $ Set.member AuthorizationOption_Badge authorizationOptions)
157 (fromBool $ Set.member AuthorizationOption_Badge authorizationOptions)
212 (fromBool $ Set.member AuthorizationOption_Sound authorizationOptions)
158 (fromBool $ Set.member AuthorizationOption_Sound authorizationOptions)
@@ -215,5 +161,3 @@ run' cfg main = do
215 (fromBool registerForRemoteNotifications)
161 (fromBool registerForRemoteNotifications)
216 didRegisterForRemoteNotificationsWithDeviceToken
162 didRegisterForRemoteNotificationsWithDeviceToken
217 didFailToRegisterForRemoteNotificationsWithError
163 didFailToRegisterForRemoteNotificationsWithError
218− (fromBool $ _appDelegateConfig_developerExtrasEnabled cfg)