Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ - (instancetype)initWithMap:(NSDictionary *)map {
self = [super init];
if (self) {
[self setValue:map[@"price"] ?: [NSNull null] forKey:@"price"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSLocale *locale = NSLocale.systemLocale;
[self setValue:locale ?: [NSNull null] forKey:@"priceLocale"];
[self setValue:map[@"numberOfPeriods"] ?: @(0) forKey:@"numberOfPeriods"];
SKProductSubscriptionPeriodStub *subscriptionPeriodSub =
Expand All @@ -46,7 +46,7 @@ - (instancetype)initWithMap:(NSDictionary *)map {
[self setValue:map[@"localizedTitle"] ?: [NSNull null] forKey:@"localizedTitle"];
[self setValue:map[@"downloadable"] ?: @NO forKey:@"downloadable"];
[self setValue:map[@"price"] ?: [NSNull null] forKey:@"price"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSLocale *locale = NSLocale.systemLocale;
[self setValue:locale ?: [NSNull null] forKey:@"priceLocale"];
[self setValue:map[@"downloadContentLengths"] ?: @(0) forKey:@"downloadContentLengths"];
SKProductSubscriptionPeriodStub *period =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ @interface TranslatorTest : XCTestCase
@property(strong, nonatomic) NSDictionary *discountMap;
@property(strong, nonatomic) NSDictionary *productMap;
@property(strong, nonatomic) NSDictionary *productResponseMap;
@property(strong, nonatomic) NSDictionary *localeMap;

@end

Expand All @@ -21,14 +22,14 @@ - (void)setUp {
self.periodMap = @{@"numberOfUnits" : @(0), @"unit" : @(0)};
self.discountMap = @{
@"price" : @1.0,
@"currencyCode" : @"USD",
@"priceLocale" : [NSLocale.systemLocale toMap],
@"numberOfPeriods" : @1,
@"subscriptionPeriod" : self.periodMap,
@"paymentMode" : @1
};
self.productMap = @{
@"price" : @1.0,
@"currencyCode" : @"USD",
@"priceLocale" : [NSLocale.systemLocale toMap],
@"productIdentifier" : @"123",
@"localizedTitle" : @"title",
@"localizedDescription" : @"des",
Expand Down Expand Up @@ -69,4 +70,10 @@ - (void)testProductResponseToMap {
XCTAssertEqualObjects(map, self.productResponseMap);
}

- (void)testLocaleToMap {
NSLocale *system = NSLocale.systemLocale;
NSDictionary *map = [system toMap];
XCTAssertEqualObjects(map[@"currencySymbol"], system.currencySymbol);
}

@end
6 changes: 6 additions & 0 deletions packages/in_app_purchase/ios/Classes/FIAObjectTranslator.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ NS_ASSUME_NONNULL_BEGIN

@end

@interface NSLocale (Coder)

- (nullable NSDictionary *)toMap;

@end

NS_ASSUME_NONNULL_END
31 changes: 19 additions & 12 deletions packages/in_app_purchase/ios/Classes/FIAObjectTranslator.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ - (NSDictionary *)toMap {
@"downloadContentVersion" : self.downloadContentVersion ?: [NSNull null]

}];
if (@available(iOS 10.0, *)) {
// TODO(cyanglaz): NSLocale is a complex object, want to see the actual need of getting this
// expanded to a map. Matching android to only get the currencyCode for now.
// https://github.com/flutter/flutter/issues/26610
[map setObject:self.priceLocale.currencyCode ?: [NSNull null] forKey:@"currencyCode"];
}
// TODO(cyanglaz): NSLocale is a complex object, want to see the actual need of getting this
// expanded to a map. Matching android to only get the currencySymbol for now.
// https://github.com/flutter/flutter/issues/26610
[map setObject:[self.priceLocale toMap] ?: [NSNull null] forKey:@"priceLocale"];
if (@available(iOS 11.2, *)) {
[map setObject:[self.subscriptionPeriod toMap] ?: [NSNull null] forKey:@"subscriptionPeriod"];
}
Expand Down Expand Up @@ -58,12 +56,10 @@ - (NSDictionary *)toMap {
@"paymentMode" : @(self.paymentMode)
}];

if (@available(iOS 10.0, *)) {
// TODO(cyanglaz): NSLocale is a complex object, want to see the actual need of getting this
// expanded to a map. Matching android to only get the currencyCode for now.
// https://github.com/flutter/flutter/issues/26610
[map setObject:self.priceLocale.currencyCode ?: [NSNull null] forKey:@"currencyCode"];
}
// TODO(cyanglaz): NSLocale is a complex object, want to see the actual need of getting this
// expanded to a map. Matching android to only get the currencySymbol for now.
// https://github.com/flutter/flutter/issues/26610
[map setObject:[self.priceLocale toMap] ?: [NSNull null] forKey:@"priceLocale"];
return map;
}

Expand All @@ -83,3 +79,14 @@ - (NSDictionary *)toMap {
}

@end

@implementation NSLocale (Coder)

- (nullable NSDictionary *)toMap {
NSMutableDictionary *map = [[NSMutableDictionary alloc] init];
[map setObject:[self objectForKey:NSLocaleCurrencySymbol] ?: [NSNull null]
forKey:@"currencySymbol"];
return map;
}

@end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,12 @@ enum ProductDiscountPaymentMode {

/// Dart wrapper around StoreKit's [SKProductDiscount](https://developer.apple.com/documentation/storekit/skproductdiscount?language=objc).
///
/// Most of the fields are identical to OBJC SKProduct.
/// The only difference is instead of the locale object, we only exposed currencyCode for simplicity.
/// It is used as a property in [SKProductWrapper].
@JsonSerializable(nullable: true)
class SKProductDiscountWrapper {
SKProductDiscountWrapper(
{@required this.price,
@required this.currencyCode,
@required this.priceLocale,
@required this.numberOfPeriods,
@required this.paymentMode,
@required this.subscriptionPeriod});
Expand All @@ -128,14 +126,11 @@ class SKProductDiscountWrapper {
return _$SKProductDiscountWrapperFromJson(map);
}

/// The discounted price, in the currency that is defined in [currencyCode].
/// The discounted price, in the currency that is defined in [priceLocale].
final double price;

// TODO(cyanglaz): NSLocale is a complex object, want to see the actual need of getting this expanded to
// a map. Matching android to only get the currencyCode for now.
// https://github.com/flutter/flutter/issues/26610
/// The currencyCode for the [price], e.g USD for U.S. dollars.
final String currencyCode;
/// Includes locale information about the price, e.g. `$` as the currency symbol for US locale.
final PriceLocaleWrapper priceLocale;

/// The object represent the discount period length.
///
Expand All @@ -154,8 +149,6 @@ class SKProductDiscountWrapper {

/// Dart wrapper around StoreKit's [SKProduct](https://developer.apple.com/documentation/storekit/skproduct?language=objc).
///
/// Most of the fields are identical to OBJC SKProduct.
/// The only difference is instead of the locale object, we only exposed currencyCode for simplicity.
/// A list of [SKProductWrapper] is returned in the [SKRequestMaker.startProductRequest] method, and
/// should be stored for use when making a payment.
@JsonSerializable(nullable: true)
Expand All @@ -164,7 +157,7 @@ class SKProductWrapper {
@required this.productIdentifier,
@required this.localizedTitle,
@required this.localizedDescription,
@required this.currencyCode,
@required this.priceLocale,
@required this.downloadContentVersion,
@required this.subscriptionGroupIdentifier,
@required this.price,
Expand Down Expand Up @@ -196,11 +189,8 @@ class SKProductWrapper {
/// It is localized based on the current locale.
final String localizedDescription;

// TODO(cyanglaz): NSLocale is a complex object, want to see the actual need of getting this expanded to
// a map. Matching android to only get the currencyCode for now.
// https://github.com/flutter/flutter/issues/26610
/// The currencyCode for the price, e.g USD for U.S. dollars.
final String currencyCode;
/// Includes locale information about the price, e.g. `$` as the currency symbol for US locale.
final PriceLocaleWrapper priceLocale;

/// The version of the downloadable content.
///
Expand All @@ -214,7 +204,7 @@ class SKProductWrapper {
/// Check [SubscriptionGroup](https://developer.apple.com/app-store/subscriptions/) for more details about subscription group.
final String subscriptionGroupIdentifier;

/// The price of the product, in the currency that is defined in [currencyCode].
/// The price of the product, in the currency that is defined in [priceLocale].
final double price;

/// Whether the AppStore has downloadable content for this product.
Expand Down Expand Up @@ -242,3 +232,26 @@ class SKProductWrapper {
/// and their units and duration do not have to be matched.
final SKProductDiscountWrapper introductoryPrice;
}

/// Object that indicates the locale of the price
///
/// It is a thin wrapper of [NSLocale](https://developer.apple.com/documentation/foundation/nslocale?language=objc).
// TODO(cyanglaz): NSLocale is a complex object, want to see the actual need of getting this expanded.
// Matching android to only get the currencySymbol for now.
// https://github.com/flutter/flutter/issues/26610
@JsonSerializable()
class PriceLocaleWrapper {
PriceLocaleWrapper({@required this.currencySymbol});

/// Constructing an instance from a map from the Objective-C layer.
///
/// This method should only be used with `map` values returned by [SKProductWrapper.fromJson] and [SKProductDiscountWrapper.fromJson].
/// The `map` parameter must not be null.
factory PriceLocaleWrapper.fromJson(Map map) {
assert(map != null);
return _$PriceLocaleWrapperFromJson(map);
}

///The currency symbol for the locale, e.g. $ for US locale.
final String currencySymbol;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import 'package:test/test.dart';
import 'package:in_app_purchase/src/store_kit_wrappers/sk_product_wrapper.dart';

void main() {
final Map<String, dynamic> localeMap = <String, dynamic>{
'currencySymbol': '\$'
};
final Map<String, dynamic> subMap = <String, dynamic>{
'numberOfUnits': 1,
'unit': 2
};
final Map<String, dynamic> discountMap = <String, dynamic>{
'price': 1.0,
'currencyCode': 'USD',
'priceLocale': localeMap,
'numberOfPeriods': 1,
'paymentMode': 2,
'subscriptionPeriod': subMap,
Expand All @@ -21,7 +24,7 @@ void main() {
'productIdentifier': 'id',
'localizedTitle': 'title',
'localizedDescription': 'description',
'currencyCode': 'USD',
'priceLocale': localeMap,
'downloadContentVersion': 'version',
'subscriptionGroupIdentifier': 'com.group',
'price': 1.0,
Expand All @@ -37,6 +40,11 @@ void main() {
};

group('product request wrapper test', () {
void testMatchLocale(
PriceLocaleWrapper wrapper, Map<String, dynamic> localeMap) {
expect(wrapper.currencySymbol, localeMap['currencySymbol']);
}

test(
'SKProductSubscriptionPeriodWrapper should have property values consistent with map',
() {
Expand All @@ -61,7 +69,7 @@ void main() {
final SKProductDiscountWrapper wrapper =
SKProductDiscountWrapper.fromJson(discountMap);
expect(wrapper.price, discountMap['price']);
expect(wrapper.currencyCode, discountMap['currencyCode']);
testMatchLocale(wrapper.priceLocale, discountMap['priceLocale']);
expect(wrapper.numberOfPeriods, discountMap['numberOfPeriods']);
expect(wrapper.paymentMode,
ProductDiscountPaymentMode.values[discountMap['paymentMode']]);
Expand All @@ -79,7 +87,7 @@ void main() {
final SKProductDiscountWrapper wrapper =
SKProductDiscountWrapper.fromJson(<String, dynamic>{});
expect(wrapper.price, null);
expect(wrapper.currencyCode, null);
expect(wrapper.priceLocale, null);
expect(wrapper.numberOfPeriods, null);
expect(wrapper.paymentMode, null);
expect(wrapper.subscriptionPeriod, null);
Expand All @@ -89,8 +97,8 @@ void main() {
SKProductWrapper wrapper, Map<String, dynamic> productMap) {
expect(wrapper.productIdentifier, productMap['productIdentifier']);
expect(wrapper.localizedTitle, productMap['localizedTitle']);
testMatchLocale(wrapper.priceLocale, productMap['priceLocale']);
expect(wrapper.localizedDescription, productMap['localizedDescription']);
expect(wrapper.currencyCode, productMap['currencyCode']);
expect(
wrapper.downloadContentVersion, productMap['downloadContentVersion']);
expect(wrapper.subscriptionGroupIdentifier,
Expand Down Expand Up @@ -121,6 +129,7 @@ void main() {
.values[productMap['subscriptionPeriod']['unit']]);
expect(wrapper.subscriptionPeriod.numberOfUnits,
productMap['subscriptionPeriod']['numberOfUnits']);
expect(wrapper.price, discountMap['price']);
}

test('SKProductWrapper should have property values consistent with map',
Expand All @@ -136,7 +145,7 @@ void main() {
expect(wrapper.productIdentifier, null);
expect(wrapper.localizedTitle, null);
expect(wrapper.localizedDescription, null);
expect(wrapper.currencyCode, null);
expect(wrapper.priceLocale, null);
expect(wrapper.downloadContentVersion, null);
expect(wrapper.subscriptionGroupIdentifier, null);
expect(wrapper.price, null);
Expand All @@ -163,5 +172,10 @@ void main() {
expect(wrapper.products.length, 0);
expect(wrapper.invalidProductIdentifiers.length, 0);
});

test('LocaleWrapper should have property values consistent with map', () {
final PriceLocaleWrapper wrapper = PriceLocaleWrapper.fromJson(localeMap);
testMatchLocale(wrapper, localeMap);
});
});
}
Loading