Skip to content

Commit

Permalink
iOS bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloudis committed Oct 3, 2022
1 parent dee5b93 commit 480c670
Show file tree
Hide file tree
Showing 20 changed files with 665 additions and 1 deletion.
10 changes: 10 additions & 0 deletions ios/Callback.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Callback.h
// LncRn
//
#import <React/RCTBridgeModule.h>

@interface Callback : NSObject
-(void)setCallback:(RCTPromiseResolveBlock)callback;
-(void)sendResult:(NSString *)data;
@end
17 changes: 17 additions & 0 deletions ios/Callback.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Callback.m
#import "Callback.h"
#import <React/RCTEventEmitter.h>

@implementation Callback

RCTPromiseResolveBlock rnCallback;

-(void)setCallback:(RCTPromiseResolveBlock)callback {
rnCallback = callback;
}

-(void)sendResult:(NSString *)data {
rnCallback(data);
}

@end
8 changes: 8 additions & 0 deletions ios/LncRn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#import <React/RCTEventEmitter.h>
#import <React/RCTBridgeModule.h>

@interface LncRn : RCTEventEmitter <RCTBridgeModule>

-(void)sendEvent: (NSString *)event data:(NSString *)data;

@end
148 changes: 148 additions & 0 deletions ios/LncRn.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#import "LncRn.h"
#import "Callback.h"
#import "StreamingCallback.h"
#import "Lndmobile.xcframework/ios-arm64_x86_64-simulator/Lndmobile.framework/Headers/Lndmobile.h"


#ifdef RCT_NEW_ARCH_ENABLED
#import "RNLncRnSpec.h"
#endif

@implementation LncRn
RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(registerLocalPrivCreateCallback:(NSString *)title
resolver:(RCTPromiseResolveBlock)onLocalPrivCreate)
{
Callback *lpccb = [[Callback alloc] init];
// id<Callback> lpccb;
// lpccb.setCallback(onLocalPrivCreate);
[lpccb setCallback:onLocalPrivCreate];
MobileRegisterLocalPrivCreateCallback(lpccb);
}

RCT_EXPORT_METHOD(registerRemoteKeyReceiveCallback:(NSString *)title
resolver:(RCTPromiseResolveBlock)onRemoteKeyReceive)
{
Callback * rkrcb = [[Callback alloc] init];
[rkrcb setCallback:onRemoteKeyReceive];
MobileRegisterAuthDataCallback(rkrcb);
}

RCT_EXPORT_METHOD(registerAuthDataCallback:(NSString *)title
resolver:(RCTPromiseResolveBlock)onAuthData)
{
Callback * oacb = [[Callback alloc] init];
[oacb setCallback:onAuthData];
MobileRegisterAuthDataCallback(oacb);
}

RCT_EXPORT_METHOD(isConnected:(NSString *)title
resolver:(RCTPromiseResolveBlock)resolve)
{
BOOL isConnected = MobileIsConnected();
resolve(@(isConnected));
}

RCT_EXPORT_METHOD(status:(NSString *)title
resolver:(RCTPromiseResolveBlock)resolve)
{
NSString *status = MobileStatus();
resolve(status);
}

struct Data {
NSString* expiry;
NSError* error;
};

RCT_EXPORT_METHOD(expiry:(NSString *)title
resolver:(RCTPromiseResolveBlock)resolve)
{
NSString * response = MobileGetExpiry(nil);
resolve(response);
}

RCT_EXPORT_METHOD(isReadOnly:(NSString *)title
resolver:(RCTPromiseResolveBlock)resolve)
{
BOOL readOnly = MobileIsReadOnly();
resolve(@(readOnly));
}

RCT_EXPORT_METHOD(hasPerms:(NSString *)title
permission:(NSString *)permission
resolver:(RCTPromiseResolveBlock)resolve)
{
BOOL hasPermissions = MobileHasPermissions(permission);
resolve(@(hasPermissions));
}

RCT_EXPORT_METHOD(connectServer:(NSString *)title
mailboxServerAddr:(NSString *)mailboxServerAddr
isDevServer:(BOOL *)isDevServer
connectPhrase:(NSString *)connectPhrase
localStatic:(NSString *)localStatic
remoteStatic:(NSString *)remoteStatic)
{
MobileConnectServer(mailboxServerAddr, isDevServer, connectPhrase, localStatic, remoteStatic);
}

RCT_EXPORT_METHOD(disconnect:(NSString *)title)
{
MobileDisconnect();
}

RCT_EXPORT_METHOD(invokeRPC:(NSString *)title
route:(NSString *)route
requestData:(NSString *)requestData
resolver:(RCTPromiseResolveBlock)resolve)
{
Callback * gocb = [[Callback alloc] init];
[gocb setCallback:resolve];
MobileInvokeRPC(route, requestData, gocb, nil);
}

- (NSArray<NSString *> *)supportedEvents {
// TODO add all streaming events names to list
return @[@"Accelerometer"];
}

- (void)sendEvent: (NSString *)event data:(NSString *)data {
[self sendEventWithName:event body:@{@"result": data}];
}

- (void)startObserving: (NSString *)eventName request:(NSString *)request
{
StreamingCallback * gocb = [[StreamingCallback alloc] init];
[gocb setEventName:eventName];
// TODO set callback properly
// [gocb setCallback:[self sendEvent]];
MobileInvokeRPC(eventName, request, gocb, nil);
}

// TO DO
// ~registerLocalPrivCreateCallback
// ~registerRemoteKeyReceiveCallback
// ~registerAuthDataCallback
// +isConnected
// +status
// +expiry
// +isReadOnly
// +hasPerms
// +connectServer
// +disconnect
// ~invokeRPC
// ~sendEvent
// ~initListener

// Don't compile this code when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<facebook::react::NativeLncRnSpecJSI>(params);
}
#endif

@end
Loading

0 comments on commit 480c670

Please sign in to comment.