@@ -5,10 +5,16 @@ module Language.Javascript.JSaddle.WKWebView
7 , mainBundleResourcePath
7 , mainBundleResourcePath
8+ , AppDelegateConfig (..)
9+ , AppDelegateNotificationConfig (..)
10+ , AuthorizationOption (..)
10import Data.ByteString (ByteString)
13import Data.ByteString (ByteString)
14+import Data.Default (Default, def)
16+import qualified Data.Set as Set
11−import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath, AppDelegateConfig (..), boolToCChar)
17+import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath, boolToCChar)
12import System.Environment (getProgName)
18import System.Environment (getProgName)
13import Foreign.C.String (CString, withCString, peekCString)
19import Foreign.C.String (CString, withCString, peekCString)
14import Foreign.C.Types
20import Foreign.C.Types
@@ -17,18 +23,52 @@ import Language.Javascript.JSaddle (JSM)
18foreign import ccall runInWKWebView :: StablePtr (WKWebView -> IO ())
24foreign import ccall runInWKWebView :: StablePtr (WKWebView -> IO ())
19 -> CString
25 -> CString
20− -> CChar -- requestAuthorizationWithOptions
26+ -> CChar -- whether to run requestAuthorizationWithOptions
27+ -> CChar -- Ask for Badge authorization
28+ -> CChar -- Ask for Sound authorization
29+ -> CChar -- Ask for Alert authorization
30+ -> CChar -- Ask for CarPlay authorization
21 -> CChar -- registerForRemoteNotifications
31 -> CChar -- registerForRemoteNotifications
22 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
32 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
36+data AppDelegateConfig = AppDelegateConfig
37+ { _appDelegateConfig_appDelegateNotificationConfig :: AppDelegateNotificationConfig
40+instance Default AppDelegateConfig where
41+ def = AppDelegateConfig
42+ { _appDelegateConfig_appDelegateNotificationConfig = def
45+data AuthorizationOption = AuthorizationOption_Badge
46+ | AuthorizationOption_Sound
47+ | AuthorizationOption_Alert
48+ | AuthorizationOption_CarPlay
49+ deriving (Show, Read, Eq, Ord)
51+data AppDelegateNotificationConfig = AppDelegateNotificationConfig
52+ { _appDelegateNotificationConfig_requestAuthorizationWithOptions :: Maybe (Set AuthorizationOption)
53+ , _appDelegateNotificationConfig_registerForRemoteNotifications :: Bool
54+ , _appDelegateNotificationConfig_didRegisterForRemoteNotificationsWithDeviceToken :: CString -> IO ()
57+instance Default AppDelegateNotificationConfig where
58+ def = AppDelegateNotificationConfig
59+ { _appDelegateNotificationConfig_requestAuthorizationWithOptions = Nothing
60+ , _appDelegateNotificationConfig_registerForRemoteNotifications = False
61+ , _appDelegateNotificationConfig_didRegisterForRemoteNotificationsWithDeviceToken = \_ -> return ()
25-- | Run JSaddle in a WKWebView
65-- | Run JSaddle in a WKWebView
26run :: (CString -> IO ()) -> JSM () -> IO ()
66run :: (CString -> IO ()) -> JSM () -> IO ()
27run didRegisterForRemoteNotificationsWithDeviceToken f = do
67run didRegisterForRemoteNotificationsWithDeviceToken f = do
28 handler <- newStablePtr (jsaddleMain f)
68 handler <- newStablePtr (jsaddleMain f)
29 progName <- getProgName
69 progName <- getProgName
30 deviceTokenHandler <- newStablePtr didRegisterForRemoteNotificationsWithDeviceToken
70 deviceTokenHandler <- newStablePtr didRegisterForRemoteNotificationsWithDeviceToken
31− withCString progName $ \pn -> runInWKWebView handler pn 1 1 deviceTokenHandler
71+ withCString progName $ \pn -> runInWKWebView handler pn 1 1 1 1 1 1 deviceTokenHandler
33-- | Run JSaddle in a WKWebView first loading the specified file
73-- | Run JSaddle in a WKWebView first loading the specified file
34-- from the mainBundle (relative to the resourcePath).
74-- from the mainBundle (relative to the resourcePath).
@@ -40,13 +80,20 @@ runFile :: ByteString -- ^ The file to navigate to.
40runFile url allowing cfg f = do
80runFile url allowing cfg f = do
41 handler <- newStablePtr (jsaddleMainFile url allowing f)
81 handler <- newStablePtr (jsaddleMainFile url allowing f)
42 progName <- getProgName
82 progName <- getProgName
83+ let ncfg = _appDelegateConfig_appDelegateNotificationConfig cfg
43− let requestAuthorizationWithOptions = boolToCChar $ _appDelegateConfig_requestAuthorizationWithOptions cfg
84+ (requestAuthorizationWithOptions, authorizationOptions) =
44− registerForRemoteNotifications = boolToCChar $ _appDelegateConfig_registerForRemoteNotifications cfg
85+ case _appDelegateNotificationConfig_requestAuthorizationWithOptions ncfg of
86+ Nothing -> (False, Set.empty)
87+ Just opts -> (True, opts)
88+ registerForRemoteNotifications = _appDelegateNotificationConfig_registerForRemoteNotifications ncfg
45− didRegisterForRemoteNotificationsWithDeviceToken <- newStablePtr $ _appDelegateConfig_didRegisterForRemoteNotificationsWithDeviceToken cfg
89+ didRegisterForRemoteNotificationsWithDeviceToken <- newStablePtr $ _appDelegateNotificationConfig_didRegisterForRemoteNotificationsWithDeviceToken ncfg
46 withCString progName $ \pn -> runInWKWebView
90 withCString progName $ \pn -> runInWKWebView
49− requestAuthorizationWithOptions
93+ (boolToCChar requestAuthorizationWithOptions)
94+ (boolToCChar $ Set.member AuthorizationOption_Badge authorizationOptions)
95+ (boolToCChar $ Set.member AuthorizationOption_Sound authorizationOptions)
96+ (boolToCChar $ Set.member AuthorizationOption_Alert authorizationOptions)
97+ (boolToCChar $ Set.member AuthorizationOption_CarPlay authorizationOptions)
50− registerForRemoteNotifications
98+ (boolToCChar registerForRemoteNotifications)
51 didRegisterForRemoteNotificationsWithDeviceToken
99 didRegisterForRemoteNotificationsWithDeviceToken