Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[In_app_purchase] Use json serializer for skproduct wrapper and related classes. #1147

Merged
merged 1 commit into from
Feb 4, 2019
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 @@ -3,11 +3,18 @@
// found in the LICENSE file.

import 'package:flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';

// WARNING: Changes to `@JsonSerializable` classes need to be reflected in the
// below generated file. Run `flutter packages pub run build_runner watch` to
// rebuild and watch for further changes.
part 'sk_product_wrapper.g.dart';

/// Dart wrapper around StoreKit's [SKProductsResponse](https://developer.apple.com/documentation/storekit/skproductsresponse?language=objc).
///
/// Represents the response object returned by [SKRequestMaker.startProductRequest].
/// Contains information about a list of products and a list of invalid product identifiers.
@JsonSerializable()
class SkProductResponseWrapper {
SkProductResponseWrapper(
{@required this.products, @required this.invalidProductIdentifiers});
Expand All @@ -16,11 +23,10 @@ class SkProductResponseWrapper {
///
/// This method should only be used with `map` values returned by [SKRequestMaker.startProductRequest].
/// The `map` parameter must not be null.
SkProductResponseWrapper.fromMap(Map<String, List<dynamic>> map)
: assert(map != null),
products = _getListFromMapList(_getProductMapListFromResponseMap(map)),
invalidProductIdentifiers =
List.castFrom<dynamic, String>(map['invalidProductIdentifiers']);
factory SkProductResponseWrapper.fromJson(Map map) {
assert(map != null);
return _$SkProductResponseWrapperFromJson(map);
}

/// Stores all matching successfully found products.
///
Expand All @@ -34,18 +40,6 @@ class SkProductResponseWrapper {
/// found here https://developer.apple.com/documentation/storekit/skproductsresponse/1505985-invalidproductidentifiers?language=objc.
/// Will be empty if all the product identifiers are valid.
final List<String> invalidProductIdentifiers;

static List<Map<dynamic, dynamic>> _getProductMapListFromResponseMap(
Map<String, List<dynamic>> map) {
return map['products'].cast<Map<dynamic, dynamic>>();
}

static List<SKProductWrapper> _getListFromMapList(
List<Map<dynamic, dynamic>> mapList) {
return mapList
.map((Map<dynamic, dynamic> map) => SKProductWrapper.fromMap(map))
.toList();
}
}

/// Dart wrapper around StoreKit's [SKProductPeriodUnit](https://developer.apple.com/documentation/storekit/skproductperiodunit?language=objc).
Expand All @@ -54,30 +48,34 @@ class SkProductResponseWrapper {
// The values of the enum options are matching the [SKProductPeriodUnit]'s values. Should there be an update or addition
// in the [SKProductPeriodUnit], this need to be updated to match.
enum SubscriptionPeriodUnit {
@JsonValue(0)
day,
@JsonValue(1)
week,
@JsonValue(2)
month,
@JsonValue(3)
year,
}

/// Dart wrapper around StoreKit's [SKProductSubscriptionPeriod](https://developer.apple.com/documentation/storekit/skproductsubscriptionperiod?language=objc).
///
/// A period is defined by a [numberOfUnits] and a [unit], e.g for a 3 months period [numberOfUnits] is 3 and [unit] is a month.
/// It is used as a property in [SKProductDiscountWrapper] and [SKProductWrapper].
@JsonSerializable(nullable: true)
class SKProductSubscriptionPeriodWrapper {
SKProductSubscriptionPeriodWrapper(
{@required this.numberOfUnits, @required this.unit});

/// Constructing an instance from a map from the Objective-C layer.
/// This method should only be used with `map` values returned by [SKProductDiscountWrapper.fromMap] or [SKProductWrapper.fromMap].
///
/// This method should only be used with `map` values returned by [SKProductDiscountWrapper.fromJson] or [SKProductWrapper.fromJson].
/// The `map` parameter must not be null.
SKProductSubscriptionPeriodWrapper.fromMap(Map<String, dynamic> map)
: assert(map != null &&
(map['numberOfUnits'] == null || map['numberOfUnits'] > 0)),
numberOfUnits = map['numberOfUnits'],
unit = (map['unit'] != null)
? SubscriptionPeriodUnit.values[map['unit']]
: null;
factory SKProductSubscriptionPeriodWrapper.fromJson(Map map) {
assert(map != null &&
(map['numberOfUnits'] == null || map['numberOfUnits'] > 0));
return _$SKProductSubscriptionPeriodWrapperFromJson(map);
}

/// The number of [unit] units in this period.
///
Expand All @@ -95,12 +93,15 @@ class SKProductSubscriptionPeriodWrapper {
// in the [SKProductDiscountPaymentMode], this need to be updated to match.
enum ProductDiscountPaymentMode {
/// Allows user to pay the discounted price at each payment period.
@JsonValue(0)
payAsYouGo,

/// Allows user to pay the discounted price upfront and receive the product for the rest of time that was paid for.
@JsonValue(1)
payUpFront,

/// User pays nothing during the discounted period.
@JsonValue(2)
freeTrail,
}

Expand All @@ -109,6 +110,7 @@ enum ProductDiscountPaymentMode {
/// 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,
Expand All @@ -119,20 +121,12 @@ class SKProductDiscountWrapper {

/// Constructing an instance from a map from the Objective-C layer.
///
/// This method should only be used with `map` values returned by [SKProductWrapper.fromMap].
/// This method should only be used with `map` values returned by [SKProductWrapper.fromJson].
/// The `map` parameter must not be null.
SKProductDiscountWrapper.fromMap(Map<String, dynamic> map)
: assert(map != null),
price = map['price'],
currencyCode = map['currencyCode'],
numberOfPeriods = map['numberOfPeriods'],
paymentMode = (map['paymentMode'] != null)
? ProductDiscountPaymentMode.values[map['paymentMode']]
: null,
subscriptionPeriod = map['subscriptionPeriod'] != null
? SKProductSubscriptionPeriodWrapper.fromMap(
map['subscriptionPeriod'].cast<String, dynamic>())
: null;
factory SKProductDiscountWrapper.fromJson(Map map) {
assert(map != null);
return _$SKProductDiscountWrapperFromJson(map);
}

/// The discounted price, in the currency that is defined in [currencyCode].
final double price;
Expand Down Expand Up @@ -164,6 +158,7 @@ class SKProductDiscountWrapper {
/// 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)
class SKProductWrapper {
SKProductWrapper({
@required this.productIdentifier,
Expand All @@ -181,28 +176,12 @@ class SKProductWrapper {

/// Constructing an instance from a map from the Objective-C layer.
///
/// This method should only be used with `map` values returned by [SkProductResponseWrapper.fromMap].
/// This method should only be used with `map` values returned by [SkProductResponseWrapper.fromJson].
/// The `map` parameter must not be null.
SKProductWrapper.fromMap(Map<dynamic, dynamic> map)
: assert(map != null),
productIdentifier = map['productIdentifier'],
localizedTitle = map['localizedTitle'],
localizedDescription = map['localizedDescription'],
currencyCode = map['currencyCode'],
downloadContentVersion = map['downloadContentVersion'],
subscriptionGroupIdentifier = map['subscriptionGroupIdentifier'],
price = map['price'],
downloadable = map['downloadable'],
downloadContentLengths =
List.castFrom<dynamic, int>(map['downloadContentLengths']),
subscriptionPeriod = map['subscriptionPeriod'] != null
? SKProductSubscriptionPeriodWrapper.fromMap(
map['subscriptionPeriod'].cast<String, dynamic>())
: null,
introductoryPrice = (map['introductoryPrice'] != null)
? SKProductDiscountWrapper.fromMap(
map['introductoryPrice'].cast<String, dynamic>())
: null;
factory SKProductWrapper.fromJson(Map map) {
assert(map != null);
return _$SKProductWrapperFromJson(map);
}

/// The unique identifier of the product.
final String productIdentifier;
Expand Down

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 @@ -24,7 +24,7 @@ class SKRequestMaker {
/// A [PlatformException] is thrown if the platform code making the request fails.
Future<SkProductResponseWrapper> startProductRequest(
List<String> productIdentifiers) async {
final Map<dynamic, dynamic> productResponseMap = await channel.invokeMethod(
final Map productResponseMap = await channel.invokeMethod(
'-[InAppPurchasePlugin startProductRequest:result:]',
productIdentifiers,
);
Expand All @@ -34,7 +34,6 @@ class SKRequestMaker {
message: 'StoreKit: Failed to get response from platform.',
);
}
return SkProductResponseWrapper.fromMap(
productResponseMap.cast<String, List<dynamic>>());
return SkProductResponseWrapper.fromJson(productResponseMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void main() {
'SKProductSubscriptionPeriodWrapper should have property values consistent with map',
() {
final SKProductSubscriptionPeriodWrapper wrapper =
SKProductSubscriptionPeriodWrapper.fromMap(subMap);
SKProductSubscriptionPeriodWrapper.fromJson(subMap);
expect(wrapper.numberOfUnits, subMap['numberOfUnits']);
expect(wrapper.unit, SubscriptionPeriodUnit.values[subMap['unit']]);
});
Expand All @@ -50,7 +50,7 @@ void main() {
'SKProductSubscriptionPeriodWrapper should have properties to be null if map is empty',
() {
final SKProductSubscriptionPeriodWrapper wrapper =
SKProductSubscriptionPeriodWrapper.fromMap(<String, dynamic>{});
SKProductSubscriptionPeriodWrapper.fromJson(<String, dynamic>{});
expect(wrapper.numberOfUnits, null);
expect(wrapper.unit, null);
});
Expand All @@ -59,7 +59,7 @@ void main() {
'SKProductDiscountWrapper should have property values consistent with map',
() {
final SKProductDiscountWrapper wrapper =
SKProductDiscountWrapper.fromMap(discountMap);
SKProductDiscountWrapper.fromJson(discountMap);
expect(wrapper.price, discountMap['price']);
expect(wrapper.currencyCode, discountMap['currencyCode']);
expect(wrapper.numberOfPeriods, discountMap['numberOfPeriods']);
Expand All @@ -77,7 +77,7 @@ void main() {
'SKProductDiscountWrapper should have properties to be null if map is empty',
() {
final SKProductDiscountWrapper wrapper =
SKProductDiscountWrapper.fromMap(<String, dynamic>{});
SKProductDiscountWrapper.fromJson(<String, dynamic>{});
expect(wrapper.price, null);
expect(wrapper.currencyCode, null);
expect(wrapper.numberOfPeriods, null);
Expand Down Expand Up @@ -125,15 +125,14 @@ void main() {

test('SKProductWrapper should have property values consistent with map',
() {
final SKProductWrapper wrapper = SKProductWrapper.fromMap(productMap);
final SKProductWrapper wrapper = SKProductWrapper.fromJson(productMap);
testMatchingProductMap(wrapper, productMap);
});

test(
'SKProductDiscountWrapper should have properties to be null if map is empty',
test('SKProductWrapper should have properties to be null if map is empty',
() {
final SKProductWrapper wrapper =
SKProductWrapper.fromMap(<String, dynamic>{});
SKProductWrapper.fromJson(<String, dynamic>{});
expect(wrapper.productIdentifier, null);
expect(wrapper.localizedTitle, null);
expect(wrapper.localizedDescription, null);
Expand All @@ -147,7 +146,7 @@ void main() {

test('SKProductResponse wrapper should match', () {
final SkProductResponseWrapper wrapper =
SkProductResponseWrapper.fromMap(productResponseMap);
SkProductResponseWrapper.fromJson(productResponseMap);
testMatchingProductMap(
wrapper.products[0], productResponseMap['products'][0]);
expect(wrapper.invalidProductIdentifiers,
Expand All @@ -160,7 +159,7 @@ void main() {
'invalidProductIdentifiers': <String>[],
};
final SkProductResponseWrapper wrapper =
SkProductResponseWrapper.fromMap(productResponseMapEmptyList);
SkProductResponseWrapper.fromJson(productResponseMapEmptyList);
expect(wrapper.products.length, 0);
expect(wrapper.invalidProductIdentifiers.length, 0);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void main() {
test('platform call should get result', () async {
stubPlatform.addResponse(
name: '-[InAppPurchasePlugin startProductRequest:result:]',
value: productResponseMap.cast<String, dynamic>());
value: productResponseMap);
final SKRequestMaker request = SKRequestMaker();
final SkProductResponseWrapper response =
await request.startProductRequest(<String>['123']);
Expand Down