on master help sign in
Add AppDelegateConfig with APNs registration callback
eccf675Ali Abrar
jsaddle-wkwebview/cbits-uikit/AppDelegate.m+2−2
@@ -2,7 +2,7 @@
2#import "ViewController.h"
2#import "ViewController.h"
3#import <UserNotifications/UserNotifications.h>
3#import <UserNotifications/UserNotifications.h>
4
4
5−extern void didRegisterForRemoteNotificationsWithDeviceTokenCallback(const char * _Nonnull, HsStablePtr);
5+extern void handlerCString(const char * _Nonnull, HsStablePtr);
6
6
7@interface AppDelegate ()
7@interface AppDelegate ()
8
8
@@ -57,7 +57,7 @@ HsStablePtr global_didRegisterForRemoteNotificationsWithDeviceToken = 0;
57
57
58- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
58- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
59 NSString *deviceTokenString = [deviceToken base64EncodedStringWithOptions: 0];
59 NSString *deviceTokenString = [deviceToken base64EncodedStringWithOptions: 0];
60− didRegisterForRemoteNotificationsWithDeviceTokenCallback([deviceTokenString UTF8String], global_didRegisterForRemoteNotificationsWithDeviceToken);
60+ handlerCString([deviceTokenString UTF8String], global_didRegisterForRemoteNotificationsWithDeviceToken);
61}
61}
62
62
63- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
63- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
jsaddle-wkwebview/default.nix+2−2
@@ -1,4 +1,4 @@
1−{ mkDerivation, stdenv, aeson, base, bytestring, jsaddle
1+{ mkDerivation, stdenv, aeson, base, bytestring, jsaddle, data-default
2, buildPackages
2, buildPackages
3}:
3}:
4
4
@@ -6,7 +6,7 @@ mkDerivation {
6 pname = "jsaddle-wkwebview";
6 pname = "jsaddle-wkwebview";
7 version = "0.8.2.0";
7 version = "0.8.2.0";
8 src = ./.;
8 src = ./.;
9− libraryHaskellDepends = [ aeson base bytestring jsaddle ]
9+ libraryHaskellDepends = [ aeson base bytestring jsaddle data-default ]
10 # should use `librarySystemDepends` but it is not propagated
10 # should use `librarySystemDepends` but it is not propagated
11 ++ (with buildPackages; [ osx_sdk darwin.libobjc darwin.apple_sdk.libs.xpc ]);
11 ++ (with buildPackages; [ osx_sdk darwin.libobjc darwin.apple_sdk.libs.xpc ]);
12 description = "Interface for JavaScript that works with GHCJS and GHC";
12 description = "Interface for JavaScript that works with GHCJS and GHC";
jsaddle-wkwebview/jsaddle-wkwebview.cabal+2−1
@@ -30,7 +30,8 @@ library
30 aeson >=0.8.0.2 && <1.2,
30 aeson >=0.8.0.2 && <1.2,
31 base <5,
31 base <5,
32 bytestring >=0.10.6.0 && <0.11,
32 bytestring >=0.10.6.0 && <0.11,
33− jsaddle >= 0.8.0.0 && <0.9
33+ jsaddle >= 0.8.0.0 && <0.9,
34+ data-default
34 default-language: Haskell2010
35 default-language: Haskell2010
35 hs-source-dirs: src
36 hs-source-dirs: src
36 frameworks: Foundation, WebKit
37 frameworks: Foundation, WebKit
jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+4−4
@@ -8,7 +8,7 @@ module Language.Javascript.JSaddle.WKWebView
8 ) where
8 ) where
9
9
10import Data.ByteString (ByteString)
10import Data.ByteString (ByteString)
11−import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
11+import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath, AppDelegateConfig (..))
12import System.Environment (getProgName)
12import System.Environment (getProgName)
13import Foreign.C.String (CString, withCString, peekCString)
13import Foreign.C.String (CString, withCString, peekCString)
14import Foreign.StablePtr (StablePtr, newStablePtr)
14import Foreign.StablePtr (StablePtr, newStablePtr)
@@ -28,12 +28,12 @@ run didRegisterForRemoteNotificationsWithDeviceToken f = do
28-- from the mainBundle (relative to the resourcePath).
28-- from the mainBundle (relative to the resourcePath).
29runFile :: ByteString -- ^ The file to navigate to.
29runFile :: ByteString -- ^ The file to navigate to.
30 -> ByteString -- ^ The path to allow read access to.
30 -> ByteString -- ^ The path to allow read access to.
31− -> (CString -> IO ())
31+ -> AppDelegateConfig
32 -> JSM ()
32 -> JSM ()
33 -> IO ()
33 -> IO ()
34−runFile url allowing didRegisterForRemoteNotificationsWithDeviceToken f = do
34+runFile url allowing cfg f = do
35 handler <- newStablePtr (jsaddleMainFile url allowing f)
35 handler <- newStablePtr (jsaddleMainFile url allowing f)
36 progName <- getProgName
36 progName <- getProgName
37− deviceTokenHandler <- newStablePtr didRegisterForRemoteNotificationsWithDeviceToken
37+ deviceTokenHandler <- newStablePtr $ _appDelegateConfig_didRegisterForRemoteNotificationsWithDeviceToken cfg
38 withCString progName $ \pn -> runInWKWebView handler pn deviceTokenHandler
38 withCString progName $ \pn -> runInWKWebView handler pn deviceTokenHandler
39
39
jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+16−6
@@ -4,12 +4,14 @@ module Language.Javascript.JSaddle.WKWebView.Internal
4 , jsaddleMainFile
4 , jsaddleMainFile
5 , WKWebView(..)
5 , WKWebView(..)
6 , mainBundleResourcePath
6 , mainBundleResourcePath
7+ , AppDelegateConfig (..)
7 ) where
8 ) where
8
9
9import Control.Monad (void, join)
10import Control.Monad (void, join)
10import Control.Concurrent (forkIO, forkOS)
11import Control.Concurrent (forkIO, forkOS)
11import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar)
12import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar)
12
13
14+import Data.Default
13import Data.Monoid ((<>))
15import Data.Monoid ((<>))
14import Data.ByteString (useAsCString, packCString)
16import Data.ByteString (useAsCString, packCString)
15import qualified Data.ByteString as BS (ByteString)
17import qualified Data.ByteString as BS (ByteString)
@@ -29,7 +31,7 @@ newtype WKWebView = WKWebView (Ptr WKWebView)
29foreign export ccall jsaddleStart :: StablePtr (IO ()) -> IO ()
31foreign export ccall jsaddleStart :: StablePtr (IO ()) -> IO ()
30foreign export ccall jsaddleResult :: StablePtr (Results -> IO ()) -> CString -> IO ()
32foreign export ccall jsaddleResult :: StablePtr (Results -> IO ()) -> CString -> IO ()
31foreign export ccall withWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
33foreign export ccall withWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
32−foreign export ccall didRegisterForRemoteNotificationsWithDeviceTokenCallback :: CString -> StablePtr (CString -> IO ()) -> IO ()
34+foreign export ccall handlerCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
33foreign import ccall addJSaddleHandler :: WKWebView -> StablePtr (IO ()) -> StablePtr (Results -> IO ()) -> IO ()
35foreign import ccall addJSaddleHandler :: WKWebView -> StablePtr (IO ()) -> StablePtr (Results -> IO ()) -> IO ()
34foreign import ccall loadHTMLString :: WKWebView -> CString -> IO ()
36foreign import ccall loadHTMLString :: WKWebView -> CString -> IO ()
35foreign import ccall loadBundleFile :: WKWebView -> CString -> CString -> IO ()
37foreign import ccall loadBundleFile :: WKWebView -> CString -> CString -> IO ()
@@ -86,11 +88,19 @@ withWebView webView ptrF = do
86 f <- deRefStablePtr ptrF
88 f <- deRefStablePtr ptrF
87 f webView
89 f webView
88
90
89−-- | A base64 encoded device token
90−didRegisterForRemoteNotificationsWithDeviceTokenCallback :: CString -> StablePtr (CString -> IO ()) -> IO ()
91+handlerCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
91−didRegisterForRemoteNotificationsWithDeviceTokenCallback token f = do
92+handlerCString c fptr = do
92− f' <- deRefStablePtr f
93+ f <- deRefStablePtr fptr
93− f' token
94+ f c
95+
96+data AppDelegateConfig = AppDelegateConfig
97+ { _appDelegateConfig_didRegisterForRemoteNotificationsWithDeviceToken :: CString -> IO ()
98+ }
99+
100+instance Default AppDelegateConfig where
101+ def = AppDelegateConfig
102+ { _appDelegateConfig_didRegisterForRemoteNotificationsWithDeviceToken = \_ -> return ()
103+ }
94
104
95jsaddleJs :: ByteString
105jsaddleJs :: ByteString
96jsaddleJs = ghcjsHelpers <> "\
106jsaddleJs = ghcjsHelpers <> "\