Skip to content

Commit

Permalink
Adds common classes
Browse files Browse the repository at this point in the history
  • Loading branch information
vegaro committed Jul 18, 2019
1 parent 8a85dd6 commit 3a0e9e9
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ios/RCEntitlement+HybridAdditions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Created by RevenueCat.
// Copyright © 2019 RevenueCat. All rights reserved.
//

#import <Purchases/Purchases.h>

@interface RCEntitlement (HybridAdditions)

- (NSDictionary *)dictionary;

@end
21 changes: 21 additions & 0 deletions ios/RCEntitlement+HybridAdditions.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Created by RevenueCat.
// Copyright © 2019 RevenueCat. All rights reserved.
//

#import "RCEntitlement+HybridAdditions.h"
#import "SKProduct+Purchases.h"

@implementation RCEntitlement (HybridAdditions)

- (NSDictionary *)dictionary
{
NSMutableDictionary *jsonDict = [NSMutableDictionary new];
for (NSString *key in self.offerings) {
RCOffering *offering = self.offerings[key];
jsonDict[key] = offering.activeProduct.dictionary;
}
return [NSDictionary dictionaryWithDictionary:jsonDict];
}

@end
12 changes: 12 additions & 0 deletions ios/RCPurchaserInfo+HybridAdditions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Created by RevenueCat.
// Copyright © 2019 RevenueCat. All rights reserved.
//

#import <Purchases/Purchases.h>

@interface RCPurchaserInfo (HybridAdditions)

- (NSDictionary *)dictionary;

@end
60 changes: 60 additions & 0 deletions ios/RCPurchaserInfo+HybridAdditions.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// Created by RevenueCat.
// Copyright © 2019 RevenueCat. All rights reserved.
//

#import "RCPurchaserInfo+HybridAdditions.h"

static NSDateFormatter *formatter;
static dispatch_once_t onceToken;

static NSString * stringFromDate(NSDate *date)
{
dispatch_once(&onceToken, ^{
// Here we're not using NSISO8601DateFormatter as we need to support iOS < 10
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss'Z'";
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
formatter = dateFormatter;
});

return [formatter stringFromDate:date];
}

@implementation RCPurchaserInfo (HybridAdditions)

- (NSDictionary *)dictionary
{
NSMutableDictionary *allExpirations = [NSMutableDictionary new];
for (NSString *productIdentifier in self.allPurchasedProductIdentifiers) {
NSDate *date = [self expirationDateForProductIdentifier:productIdentifier];
allExpirations[productIdentifier] = stringFromDate(date) ?: [NSNull null];
}

NSMutableDictionary *expirationsForActiveEntitlements = [NSMutableDictionary new];
for (NSString *entId in self.activeEntitlements) {
NSDate *date = [self expirationDateForEntitlement:entId];
expirationsForActiveEntitlements[entId] = stringFromDate(date) ?: [NSNull null];;
}

NSMutableDictionary *purchaseDatesForActiveEntitlements = [NSMutableDictionary new];
for (NSString *entId in self.activeEntitlements) {
NSDate *date = [self purchaseDateForEntitlement:entId];
purchaseDatesForActiveEntitlements[entId] = stringFromDate(date) ?: [NSNull null];;
}

id latestExpiration = stringFromDate(self.latestExpirationDate) ?: [NSNull null];

return @{
@"activeEntitlements": self.activeEntitlements.allObjects,
@"activeSubscriptions": self.activeSubscriptions.allObjects,
@"allPurchasedProductIdentifiers": self.allPurchasedProductIdentifiers.allObjects,
@"latestExpirationDate": latestExpiration,
@"allExpirationDates": allExpirations,
@"expirationsForActiveEntitlements": expirationsForActiveEntitlements,
@"purchaseDatesForActiveEntitlements": purchaseDatesForActiveEntitlements
};
}

@end
12 changes: 12 additions & 0 deletions ios/SKProduct+HybridAdditions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Created by RevenueCat.
// Copyright © 2019 RevenueCat. All rights reserved.
//

#import <StoreKit/StoreKit.h>

@interface SKProduct (HybridAdditions)

- (NSDictionary *)dictionary;

@end
84 changes: 84 additions & 0 deletions ios/SKProduct+HybridAdditions.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//
// Created by RevenueCat.
// Copyright © 2019 RevenueCat. All rights reserved.
//

#import "SKProduct+HybridAdditions.h"

@implementation SKProduct (RCPurchases)

- (nullable NSString *)rc_currencyCode {
if(@available(iOS 10.0, *)) {
return self.priceLocale.currencyCode;
} else {
return [self.priceLocale objectForKey:NSLocaleCurrencyCode];
}
}

- (NSDictionary *)dictionary
{
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
formatter.locale = self.priceLocale;
NSMutableDictionary *d = [NSMutableDictionary dictionaryWithDictionary:@{
@"identifier": self.productIdentifier ?: @"",
@"description": self.localizedDescription ?: @"",
@"title": self.localizedTitle ?: @"",
@"price": @(self.price.floatValue),
@"price_string": [formatter stringFromNumber:self.price],
@"currency_code": (self.rc_currencyCode) ? self.rc_currencyCode : [NSNull null]
}];


if (@available(iOS 11.2, *)) {
if (self.introductoryPrice) {
d[@"intro_price"] = @(self.introductoryPrice.price.floatValue) ?: @"";
if (self.introductoryPrice.price) {
d[@"intro_price_string"] = [formatter stringFromNumber:self.introductoryPrice.price];
} else {
d[@"intro_price_string"] = @"";
}
d[@"intro_price_period"] = [self normalizeSubscriptionPeriod:self.introductoryPrice.subscriptionPeriod] ?: @"";
d[@"intro_price_period_unit"] = [self normalizeSubscriptionPeriodUnit:self.introductoryPrice.subscriptionPeriod.unit] ?: @"";
d[@"intro_price_period_number_of_units"] = @(self.introductoryPrice.subscriptionPeriod.numberOfUnits) ?: @"";
d[@"intro_price_cycles"] = @(self.introductoryPrice.numberOfPeriods) ?: @"";
return d;
}
}
d[@"intro_price"] = @"";
d[@"intro_price_string"] = @"";
d[@"intro_price_period"] = @"";
d[@"intro_price_cycles"] = @"";

return d;
}

- (NSString *)normalizeSubscriptionPeriod:(SKProductSubscriptionPeriod *)subscriptionPeriod API_AVAILABLE(ios(11.2)){
NSString *unit;
switch (subscriptionPeriod.unit) {
case SKProductPeriodUnitDay:
unit = @"D";
case SKProductPeriodUnitWeek:
unit = @"W";
case SKProductPeriodUnitMonth:
unit = @"M";
case SKProductPeriodUnitYear:
unit = @"Y";
}
return [NSString stringWithFormat:@"%@%@%@", @"P", @(subscriptionPeriod.numberOfUnits), unit];
}

- (NSString *)normalizeSubscriptionPeriodUnit:(SKProductPeriodUnit)subscriptionPeriodUnit API_AVAILABLE(ios(11.2)){
switch (subscriptionPeriodUnit) {
case SKProductPeriodUnitDay:
return @"DAY";
case SKProductPeriodUnitWeek:
return @"WEEK";
case SKProductPeriodUnitMonth:
return @"MONTH";
case SKProductPeriodUnitYear:
return @"YEAR";
}
}

@end

0 comments on commit 3a0e9e9

Please sign in to comment.