Skip to content

Commit

Permalink
add registerRestoreHandler (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
namidan authored Jul 20, 2023
1 parent f4f9af8 commit a5efae7
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class NamiBridgeModule(reactContext: ReactApplicationContext) :
} else {
Arguments.createArray()
}
val settingsList = mutableListOf("extendedClientInfo:react-native:3.0.22")
val settingsList = mutableListOf("extendedClientInfo:react-native:3.0.23")
namiCommandsReact?.toArrayList()?.filterIsInstance<String>()?.let { commandsFromReact ->
settingsList.addAll(commandsFromReact)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,19 @@ class NamiPaywallManagerBridgeModule(reactContext: ReactApplicationContext) :
}
}

@ReactMethod
fun registerRestoreHandler() {
NamiPaywallManager.registerRestoreHandler { activity ->
latestPaywallActivity = activity
val map = Arguments.createMap().apply {
putBoolean("paywallRestoreRequested", true)
}
reactApplicationContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
.emit("PaywallRestoreRequested", map)
}
}

@ReactMethod
fun show() {
// Do nothing on Android side
Expand Down
10 changes: 9 additions & 1 deletion examples/Basic/containers/CampaignScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,16 @@ const CampaignScreen: FC<CampaignScreenProps> = ({navigation}) => {
const subscriptionSignInRemover = NamiPaywallManager.registerSignInHandler(
() => {
console.log('sign in');
NamiPaywallManager.hide();
NamiPaywallManager.dismiss();
},
);

const subscriptionRestoreRemover =
NamiPaywallManager.registerRestoreHandler(() => {
console.log('restore');
NamiPaywallManager.dismiss();
});

const subscriptionRemover =
NamiCampaignManager.registerAvailableCampaignsHandler(
(availableCampaigns) => {
Expand All @@ -89,6 +96,7 @@ const CampaignScreen: FC<CampaignScreenProps> = ({navigation}) => {
return () => {
subscriptionRemover();
subscriptionSignInRemover();
subscriptionRestoreRemover();
};
//Note: not needed in depts
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
2 changes: 1 addition & 1 deletion ios/Nami.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ @implementation NamiBridge (RCTExternModule)
}

// Start commands with header iformation for Nami to let them know this is a React client.
NSMutableArray *namiCommandStrings = [NSMutableArray arrayWithArray:@[@"extendedClientInfo:react-native:3.0.22"]];
NSMutableArray *namiCommandStrings = [NSMutableArray arrayWithArray:@[@"extendedClientInfo:react-native:3.0.23"]];

// Add additional namiCommands app may have sent in.
NSObject *appCommandStrings = configDict[@"namiCommands"];
Expand Down
2 changes: 2 additions & 0 deletions ios/NamiPaywallManagerBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ @interface RCT_EXTERN_MODULE(RNNamiPaywallManager, NSObject)

RCT_EXTERN_METHOD(registerSignInHandler)

RCT_EXTERN_METHOD(registerRestoreHandler)

RCT_EXTERN_METHOD(dismiss:(BOOL)animated)

RCT_EXTERN_METHOD(show)
Expand Down
12 changes: 10 additions & 2 deletions ios/NamiPaywallManagerBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RNNamiPaywallManager: RCTEventEmitter {
}

override func supportedEvents() -> [String]! {
return ["RegisterBuySKU", "PaywallCloseRequested", "PaywallSignInRequested"]
return ["RegisterBuySKU", "PaywallCloseRequested", "PaywallSignInRequested", "PaywallRestoreRequested"]
}

@objc(buySkuComplete:)
Expand Down Expand Up @@ -103,6 +103,14 @@ class RNNamiPaywallManager: RCTEventEmitter {
}
}

@objc(registerRestoreHandler)
func registerRestoreHandler() {
NamiPaywallManager.registerRestoreHandler {
let dictionary = NSDictionary(dictionary: ["PaywallRestoreRequested": true].compactMapValues { $0 })
RNNamiPaywallManager.shared?.sendEvent(withName: "PaywallRestoreRequested", body: dictionary)
}
}

@objc(dismiss:)
func dismiss(animated: Bool) {
NamiPaywallManager.dismiss(animated: animated) {}
Expand All @@ -129,4 +137,4 @@ class RNNamiPaywallManager: RCTEventEmitter {
resolve(isHidden)
}
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-nami-sdk",
"version": "3.0.22",
"version": "3.0.23",
"description": "React Native Module for Nami - Easy subscriptions & in-app purchases, with powerful built-in paywalls and A/B testing.",
"main": "index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions src/NamiPaywallManager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const NamiPaywallManager: {
) => EmitterSubscription["remove"];
registerCloseHandler: (callback: () => void) => EmitterSubscription["remove"];
registerSignInHandler: (callback: () => void) => EmitterSubscription["remove"];
registerRestoreHandler: (callback: () => void) => EmitterSubscription["remove"];
show: () => void;
hide: () => void;
isHidden: () => Promise<boolean>;
Expand Down
11 changes: 11 additions & 0 deletions src/NamiPaywallManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ export const NamiPaywallManager = {
RNNamiPaywallManager.registerSignInHandler();
return subscription.remove;
},
registerRestoreHandler(callback) {
var subscription;
subscription = this.paywallEmitter.addListener(
"PaywallRestoreRequested",
(body) => {
callback(body);
}
);
RNNamiPaywallManager.registerRestoreHandler();
return subscription.remove;
},
dismiss(animated) {
RNNamiPaywallManager.dismiss(animated ?? true);
},
Expand Down

0 comments on commit a5efae7

Please sign in to comment.