Skip to content

Commit

Permalink
Merge pull request #11 from apphud/develop
Browse files Browse the repository at this point in the history
feat: implemented delegate methods
  • Loading branch information
alexandr-makarov-am authored Apr 19, 2021
2 parents 62c160e + 0951b06 commit bb318f2
Show file tree
Hide file tree
Showing 12 changed files with 338 additions and 207 deletions.
6 changes: 3 additions & 3 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ PODS:
- React-cxxreact (= 0.63.4)
- React-jsi (= 0.63.4)
- React-jsinspector (0.63.4)
- react-native-apphud-sdk (1.0.4):
- ApphudSDK
- react-native-apphud-sdk (1.0.5):
- ApphudSDK (= 1.2)
- React-Core
- react-native-safe-area-context (3.1.9):
- React-Core
Expand Down Expand Up @@ -492,7 +492,7 @@ SPEC CHECKSUMS:
React-jsi: a0418934cf48f25b485631deb27c64dc40fb4c31
React-jsiexecutor: 93bd528844ad21dc07aab1c67cb10abae6df6949
React-jsinspector: 58aef7155bc9a9683f5b60b35eccea8722a4f53a
react-native-apphud-sdk: 4139f22ed5f50daadb40f9f102291249a809893a
react-native-apphud-sdk: 89b0dcd5264ed11aea17d0ccec4c36af348322cc
react-native-safe-area-context: b6e0e284002381d2ff29fa4fff42b4d8282e3c94
React-RCTActionSheet: 89a0ca9f4a06c1f93c26067af074ccdce0f40336
React-RCTAnimation: 1bde3ecc0c104c55df246eda516e0deb03c4e49b
Expand Down
65 changes: 6 additions & 59 deletions ios/ApphudSdk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ class ApphudSdk: NSObject {
let products:[SKProduct]? = Apphud.products();
resolve(
products?.map{ (product) -> NSDictionary in
return [
"id": product.productIdentifier,
"price": product.price,
"regionCode": product.priceLocale.regionCode as Any,
"currencyCode": product.priceLocale.currencyCode as Any,
];
return DataTransformer.skProduct(product: product);
}
);
}
Expand All @@ -61,26 +56,8 @@ class ApphudSdk: NSObject {
Apphud.purchase(productIdentifier) { (result:ApphudPurchaseResult) in
let transaction:SKPaymentTransaction? = result.transaction;
var response = [
"subscription": [
"productId": result.subscription?.productId as Any,
"expiresDate": result.subscription?.expiresDate.timeIntervalSince1970 as Any,
"startedAt": result.subscription?.startedAt.timeIntervalSince1970 as Any,
"canceledAt": result.subscription?.canceledAt?.timeIntervalSince1970 as Any,
"isInRetryBilling": result.subscription?.isInRetryBilling as Any,
"isAutorenewEnabled": result.subscription?.isAutorenewEnabled as Any,
"isIntroductoryActivated": result.subscription?.isIntroductoryActivated as Any,
"isActive": result.subscription?.isActive() as Any,
"status": result.subscription?.status.rawValue as Any,
"isLocal": result.subscription?.isLocal as Any,
"isSandbox": result.subscription?.isSandbox as Any
],
"nonRenewingPurchase": [
"productId": result.nonRenewingPurchase?.productId as Any,
"purchasedAt": result.nonRenewingPurchase?.purchasedAt.timeIntervalSince1970 as Any,
"canceledAt": result.nonRenewingPurchase?.canceledAt?.timeIntervalSince1970 as Any,
"isLocal": result.nonRenewingPurchase?.isLocal as Any,
"isSandbox": result.nonRenewingPurchase?.isSandbox as Any
],
"subscription": DataTransformer.apphudSubscription(subscription: result.subscription),
"nonRenewingPurchase": DataTransformer.nonRenewingPurchase(nonRenewingPurchase: result.nonRenewingPurchase),
"error": result.error.debugDescription
] as [String : Any];
if (transaction != nil) {
Expand All @@ -102,19 +79,7 @@ class ApphudSdk: NSObject {
@objc(subscription:withRejecter:)
func subscription(resolve: RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
let subscription = Apphud.subscription();
resolve([
"productId": subscription?.productId as Any,
"expiresDate": subscription?.expiresDate.timeIntervalSince1970 as Any,
"startedAt": subscription?.startedAt.timeIntervalSince1970 as Any,
"canceledAt": subscription?.canceledAt?.timeIntervalSince1970 as Any,
"isInRetryBilling": subscription?.isInRetryBilling as Any,
"isAutorenewEnabled": subscription?.isAutorenewEnabled as Any,
"isIntroductoryActivated": subscription?.isIntroductoryActivated as Any,
"isActive": subscription?.isActive() as Any,
"status": subscription?.status.rawValue as Any,
"isLocal": subscription?.isLocal as Any,
"isSandbox": subscription?.isSandbox as Any
]);
resolve(DataTransformer.apphudSubscription(subscription: subscription));
}

@objc(isNonRenewingPurchaseActive:withResolver:withRejecter:)
Expand All @@ -129,13 +94,7 @@ class ApphudSdk: NSObject {
let purchases = Apphud.nonRenewingPurchases();
resolve(
purchases?.map({ (purchase) -> NSDictionary in
return [
"productId": purchase.productId,
"canceledAt": purchase.canceledAt?.timeIntervalSince1970 as Any,
"purchasedAt": purchase.purchasedAt.timeIntervalSince1970 as Any,
"isLocal": purchase.isLocal as Any,
"isSandbox": purchase.isSandbox as Any
]
return DataTransformer.nonRenewingPurchase(nonRenewingPurchase: purchase);
})
);
}
Expand All @@ -145,19 +104,7 @@ class ApphudSdk: NSObject {
Apphud.restorePurchases { (subscriptions, purchases, error) in
resolve([
"subscriptions": subscriptions?.map{ (subscription) -> NSDictionary in
return [
"productId": subscription.productId as Any,
"expiresDate": subscription.expiresDate.timeIntervalSince1970 as Any,
"startedAt": subscription.startedAt.timeIntervalSince1970 as Any,
"canceledAt": subscription.canceledAt?.timeIntervalSince1970 as Any,
"isInRetryBilling": subscription.isInRetryBilling as Any,
"isAutorenewEnabled": subscription.isAutorenewEnabled as Any,
"isIntroductoryActivated": subscription.isIntroductoryActivated as Any,
"isActive": subscription.isActive() as Any,
"status": subscription.status.rawValue as Any,
"isLocal": subscription.isLocal as Any,
"isSandbox": subscription.isSandbox as Any
]
return DataTransformer.apphudSubscription(subscription: subscription);
} as Any,
"purchases": purchases?.map{ (purchase) -> NSDictionary in
return [
Expand Down
30 changes: 15 additions & 15 deletions ios/ApphudSdk.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
objects = {

/* Begin PBXBuildFile section */

5E555C0D2413F4C50049A1A2 /* ApphudSdk.m in Sources */ = {isa = PBXBuildFile; fileRef = B3E7B5891CC2AC0600A0062D /* ApphudSdk.m */; };
F4FF95D7245B92E800C19C63 /* ApphudSdk.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4FF95D6245B92E800C19C63 /* ApphudSdk.swift */; };

A878EB9B2625BFBA00A20068 /* ApphudSdkEvents.swift in Sources */ = {isa = PBXBuildFile; fileRef = A878EB972625BFBA00A20068 /* ApphudSdkEvents.swift */; };
A878EB9C2625BFBA00A20068 /* DataTransformer.swift in Sources */ = {isa = PBXBuildFile; fileRef = A878EB992625BFBA00A20068 /* DataTransformer.swift */; };
A878EB9D2625BFBA00A20068 /* ApphudSdkEvents.m in Sources */ = {isa = PBXBuildFile; fileRef = A878EB9A2625BFBA00A20068 /* ApphudSdkEvents.m */; };
F4FF95D7245B92E800C19C63 /* ApphudSdk.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4FF95D6245B92E800C19C63 /* ApphudSdk.swift */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand All @@ -27,11 +27,13 @@

/* Begin PBXFileReference section */
134814201AA4EA6300B7C361 /* libApphudSdk.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libApphudSdk.a; sourceTree = BUILT_PRODUCTS_DIR; };

A878EB972625BFBA00A20068 /* ApphudSdkEvents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ApphudSdkEvents.swift; sourceTree = "<group>"; };
A878EB982625BFBA00A20068 /* ApphudSdkEvents-Bridging-Header.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ApphudSdkEvents-Bridging-Header.h"; sourceTree = "<group>"; };
A878EB992625BFBA00A20068 /* DataTransformer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataTransformer.swift; sourceTree = "<group>"; };
A878EB9A2625BFBA00A20068 /* ApphudSdkEvents.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ApphudSdkEvents.m; sourceTree = "<group>"; };
B3E7B5891CC2AC0600A0062D /* ApphudSdk.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ApphudSdk.m; sourceTree = "<group>"; };
F4FF95D5245B92E700C19C63 /* ApphudSdk-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ApphudSdk-Bridging-Header.h"; sourceTree = "<group>"; };
F4FF95D6245B92E800C19C63 /* ApphudSdk.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApphudSdk.swift; sourceTree = "<group>"; };

/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -56,11 +58,13 @@
58B511D21A9E6C8500147676 = {
isa = PBXGroup;
children = (

F4FF95D6245B92E800C19C63 /* ApphudSdk.swift */,
B3E7B5891CC2AC0600A0062D /* ApphudSdk.m */,
A878EB982625BFBA00A20068 /* ApphudSdkEvents-Bridging-Header.h */,
A878EB9A2625BFBA00A20068 /* ApphudSdkEvents.m */,
A878EB972625BFBA00A20068 /* ApphudSdkEvents.swift */,
A878EB992625BFBA00A20068 /* DataTransformer.swift */,
F4FF95D5245B92E700C19C63 /* ApphudSdk-Bridging-Header.h */,

134814211AA4EA7D00B7C361 /* Products */,
);
sourceTree = "<group>";
Expand Down Expand Up @@ -122,10 +126,10 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (

F4FF95D7245B92E800C19C63 /* ApphudSdk.swift in Sources */,
B3E7B58A1CC2AC0600A0062D /* ApphudSdk.m in Sources */,

A878EB9D2625BFBA00A20068 /* ApphudSdkEvents.m in Sources */,
A878EB9B2625BFBA00A20068 /* ApphudSdkEvents.swift in Sources */,
A878EB9C2625BFBA00A20068 /* DataTransformer.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -238,11 +242,9 @@
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = ApphudSdk;
SKIP_INSTALL = YES;

SWIFT_OBJC_BRIDGING_HEADER = "ApphudSdk-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;

};
name = Debug;
};
Expand All @@ -259,10 +261,8 @@
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = ApphudSdk;
SKIP_INSTALL = YES;

SWIFT_OBJC_BRIDGING_HEADER = "ApphudSdk-Bridging-Header.h";
SWIFT_VERSION = 5.0;

};
name = Release;
};
Expand Down
3 changes: 3 additions & 0 deletions ios/ApphudSdkEvents-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
#import <React/RCTUtils.h>
12 changes: 12 additions & 0 deletions ios/ApphudSdkEvents.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>

@interface RCT_EXTERN_MODULE(ApphudSdkEvents, RCTEventEmitter)

RCT_EXTERN_METHOD(
setApphudProductIdentifiers:(NSArray)ids
withResolve:(RCTPromiseResolveBlock)resolve
withReject:(RCTPromiseRejectBlock)reject
)

@end
104 changes: 104 additions & 0 deletions ios/ApphudSdkEvents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import Foundation
import React
import ApphudSDK
import StoreKit

@objc(ApphudSdkEvents)
class ApphudSdkEvents: RCTEventEmitter {

var productIdentifiers:[String] = [];

override init() {
super.init();
Apphud.setDelegate(self);
Apphud.setUIDelegate(self);
}

@objc(setApphudProductIdentifiers:withResolve:withReject:)
public func setApphudProductIdentifiers(ids: NSArray, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
self.productIdentifiers = ids as! [String];
resolve(self.productIdentifiers);
}

override func supportedEvents() -> [String]! {
return [
"apphudDidFetchStoreKitProducts",
"apphudDidChangeUserID",
"apphudSubscriptionsUpdated",
"apphudNonRenewingPurchasesUpdated",
"apphudProductIdentifiers",
"apphudDidPurchase",
"apphudWillPurchase",
"apphudDidFailPurchase",
"apphudDidSelectSurveyAnswer"
]
}
}

extension ApphudSdkEvents: ApphudDelegate {

func apphudDidFetchStoreKitProducts(_ products: [SKProduct]) {
let result:[NSDictionary] = products.map{ (product) -> NSDictionary in
return DataTransformer.skProduct(product: product);
}
self.sendEvent(withName: "apphudDidFetchStoreKitProducts", body: result);
}

func apphudDidChangeUserID(_ userID: String) {
self.sendEvent(withName: "apphudDidChangeUserID", body: userID);
}

func apphudSubscriptionsUpdated(_ subscriptions: [ApphudSubscription]) {
let result:[NSDictionary] = subscriptions.map{ (subscription) -> NSDictionary in
return DataTransformer.apphudSubscription(subscription: subscription);
}
self.sendEvent(withName: "apphudSubscriptionsUpdated", body: result);
}

func apphudNonRenewingPurchasesUpdated(_ purchases: [ApphudNonRenewingPurchase]) {
let result:[NSDictionary] = purchases.map{ (purchase) -> NSDictionary in
return DataTransformer.nonRenewingPurchase(nonRenewingPurchase: purchase);
}
self.sendEvent(withName: "apphudNonRenewingPurchasesUpdated", body: result);
}

func apphudProductIdentifiers() -> [String] {
return self.productIdentifiers;
}
}

extension ApphudSdkEvents: ApphudUIDelegate {

func apphudDidPurchase(product: SKProduct, offerID: String?, screenName: String) {
self.sendEvent(withName: "apphudDidPurchase", body: [
"product": DataTransformer.skProduct(product: product),
"offerId": offerID as Any,
"screenName": screenName
]);
}

func apphudWillPurchase(product: SKProduct, offerID: String?, screenName: String) {
self.sendEvent(withName: "apphudWillPurchase", body: [
"product": DataTransformer.skProduct(product: product),
"offerId": offerID as Any,
"screenName": screenName
]);
}

func apphudDidFailPurchase(product: SKProduct, offerID: String?, errorCode: SKError.Code, screenName: String) {
self.sendEvent(withName: "apphudWillPurchase", body: [
"product": DataTransformer.skProduct(product: product),
"offerId": offerID as Any,
"screenName": screenName,
"errorCode": errorCode.rawValue
]);
}

func apphudDidSelectSurveyAnswer(question: String, answer: String, screenName: String) {
self.sendEvent(withName: "apphudDidSelectSurveyAnswer", body: [
"question": question,
"answer": answer,
"screenName": screenName
])
}
}
56 changes: 56 additions & 0 deletions ios/DataTransformer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// SKProductDictionary.swift
// ApphudSDK
//
// Created by Alexandr Makarov on 4/8/21.
//

import Foundation
import StoreKit
import ApphudSDK

public class DataTransformer {
public static func skProduct(product: SKProduct) -> NSDictionary {
return [
"id": product.productIdentifier,
"price": product.price,
"regionCode": product.priceLocale.regionCode as Any,
"currencyCode": product.priceLocale.currencyCode as Any,
];
}

public static func apphudSubscription(subscription: ApphudSubscription?) -> NSDictionary {
var result:NSDictionary = NSDictionary();
if (subscription != nil) {
result = [
"productId": subscription!.productId as Any,
"expiresDate": subscription!.expiresDate.timeIntervalSince1970 as Any,
"startedAt": subscription!.startedAt.timeIntervalSince1970 as Any,
"canceledAt": subscription?.canceledAt?.timeIntervalSince1970 as Any,
"isInRetryBilling": subscription!.isInRetryBilling as Any,
"isAutorenewEnabled": subscription!.isAutorenewEnabled as Any,
"isIntroductoryActivated": subscription!.isIntroductoryActivated as Any,
"isActive": subscription!.isActive() as Any,
"status": subscription!.status.rawValue as Any,
"isLocal": subscription!.isLocal as Any,
"isSandbox": subscription!.isSandbox as Any
]
}
return result;
}

public static func nonRenewingPurchase(nonRenewingPurchase: ApphudNonRenewingPurchase?) -> NSDictionary {
var result:NSDictionary = NSDictionary();
if (nonRenewingPurchase != nil) {
result = [
"productId": nonRenewingPurchase!.productId as Any,
"purchasedAt": nonRenewingPurchase!.purchasedAt.timeIntervalSince1970 as Any,
"canceledAt": nonRenewingPurchase?.canceledAt?.timeIntervalSince1970 as Any,
"isLocal": nonRenewingPurchase!.isLocal as Any,
"isSandbox": nonRenewingPurchase!.isSandbox as Any
]
}
return result;
}
}

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@apphud/react-native-apphud-sdk",
"version": "1.0.5",
"version": "1.0.6",
"description": "Apphud SDK for react native",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
2 changes: 1 addition & 1 deletion react-native-apphud-sdk.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ Pod::Spec.new do |s|


s.dependency "React-Core"
s.dependency "ApphudSDK"
s.dependency "ApphudSDK", "1.2"
end
Loading

0 comments on commit bb318f2

Please sign in to comment.