@@ -5,10 +5,16 @@ module Language.Javascript.JSaddle.WKWebView
55 , run
66 , runFile
77 , mainBundleResourcePath
8+ , AppDelegateConfig (..)
9+ , AppDelegateNotificationConfig (..)
10+ , AuthorizationOption (..)
811 ) where
912
1013import Data.ByteString (ByteString)
11−import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath, AppDelegateConfig (..), boolToCChar)
14+import Data.Default (Default, def)
15+import Data.Set (Set)
16+import qualified Data.Set as Set
17+import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath, boolToCChar)
1218import System.Environment (getProgName)
1319import Foreign.C.String (CString, withCString, peekCString)
1420import Foreign.C.Types
@@ -17,18 +23,52 @@ import Language.Javascript.JSaddle (JSM)
1723
1824foreign import ccall runInWKWebView :: StablePtr (WKWebView -> IO ())
1925 -> 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
2131 -> CChar -- registerForRemoteNotifications
2232 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
2333 -> IO ()
2434
35+
36+data AppDelegateConfig = AppDelegateConfig
37+ { _appDelegateConfig_appDelegateNotificationConfig :: AppDelegateNotificationConfig
38+ }
39+
40+instance Default AppDelegateConfig where
41+ def = AppDelegateConfig
42+ { _appDelegateConfig_appDelegateNotificationConfig = def
43+ }
44+
45+data AuthorizationOption = AuthorizationOption_Badge
46+ | AuthorizationOption_Sound
47+ | AuthorizationOption_Alert
48+ | AuthorizationOption_CarPlay
49+ deriving (Show, Read, Eq, Ord)
50+
51+data AppDelegateNotificationConfig = AppDelegateNotificationConfig
52+ { _appDelegateNotificationConfig_requestAuthorizationWithOptions :: Maybe (Set AuthorizationOption)
53+ , _appDelegateNotificationConfig_registerForRemoteNotifications :: Bool
54+ , _appDelegateNotificationConfig_didRegisterForRemoteNotificationsWithDeviceToken :: CString -> IO ()
55+ }
56+
57+instance Default AppDelegateNotificationConfig where
58+ def = AppDelegateNotificationConfig
59+ { _appDelegateNotificationConfig_requestAuthorizationWithOptions = Nothing
60+ , _appDelegateNotificationConfig_registerForRemoteNotifications = False
61+ , _appDelegateNotificationConfig_didRegisterForRemoteNotificationsWithDeviceToken = \_ -> return ()
62+ }
63+
64+
2565-- | Run JSaddle in a WKWebView
2666run :: (CString -> IO ()) -> JSM () -> IO ()
2767run didRegisterForRemoteNotificationsWithDeviceToken f = do
2868 handler <- newStablePtr (jsaddleMain f)
2969 progName <- getProgName
3070 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
3272
3373-- | Run JSaddle in a WKWebView first loading the specified file
3474-- from the mainBundle (relative to the resourcePath).
@@ -40,13 +80,20 @@ runFile :: ByteString -- ^ The file to navigate to.
4080runFile url allowing cfg f = do
4181 handler <- newStablePtr (jsaddleMainFile url allowing f)
4282 progName <- getProgName
43− let requestAuthorizationWithOptions = boolToCChar $ _appDelegateConfig_requestAuthorizationWithOptions cfg
44− registerForRemoteNotifications = boolToCChar $ _appDelegateConfig_registerForRemoteNotifications cfg
45− didRegisterForRemoteNotificationsWithDeviceToken <- newStablePtr $ _appDelegateConfig_didRegisterForRemoteNotificationsWithDeviceToken cfg
83+ let ncfg = _appDelegateConfig_appDelegateNotificationConfig cfg
84+ (requestAuthorizationWithOptions, authorizationOptions) =
85+ case _appDelegateNotificationConfig_requestAuthorizationWithOptions ncfg of
86+ Nothing -> (False, Set.empty)
87+ Just opts -> (True, opts)
88+ registerForRemoteNotifications = _appDelegateNotificationConfig_registerForRemoteNotifications ncfg
89+ didRegisterForRemoteNotificationsWithDeviceToken <- newStablePtr $ _appDelegateNotificationConfig_didRegisterForRemoteNotificationsWithDeviceToken ncfg
4690 withCString progName $ \pn -> runInWKWebView
4791 handler
4892 pn
49− requestAuthorizationWithOptions
50− registerForRemoteNotifications
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)
98+ (boolToCChar registerForRemoteNotifications)
5199 didRegisterForRemoteNotificationsWithDeviceToken
52−