Skip to content

Commit 5daef57

Browse files
Revert "Using json serializer for skproduct wrapper and related classes (flutter#1147)"
This reverts commit 9b4aaae.
1 parent b307e0f commit 5daef57

File tree

5 files changed

+73
-146
lines changed

5 files changed

+73
-146
lines changed

packages/in_app_purchase/lib/src/store_kit_wrappers/sk_product_wrapper.dart

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,11 @@
33
// found in the LICENSE file.
44

55
import 'package:flutter/foundation.dart';
6-
import 'package:json_annotation/json_annotation.dart';
7-
8-
// WARNING: Changes to `@JsonSerializable` classes need to be reflected in the
9-
// below generated file. Run `flutter packages pub run build_runner watch` to
10-
// rebuild and watch for further changes.
11-
part 'sk_product_wrapper.g.dart';
126

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

3125
/// Stores all matching successfully found products.
3226
///
@@ -40,6 +34,18 @@ class SkProductResponseWrapper {
4034
/// found here https://developer.apple.com/documentation/storekit/skproductsresponse/1505985-invalidproductidentifiers?language=objc.
4135
/// Will be empty if all the product identifiers are valid.
4236
final List<String> invalidProductIdentifiers;
37+
38+
static List<Map<dynamic, dynamic>> _getProductMapListFromResponseMap(
39+
Map<String, List<dynamic>> map) {
40+
return map['products'].cast<Map<dynamic, dynamic>>();
41+
}
42+
43+
static List<SKProductWrapper> _getListFromMapList(
44+
List<Map<dynamic, dynamic>> mapList) {
45+
return mapList
46+
.map((Map<dynamic, dynamic> map) => SKProductWrapper.fromMap(map))
47+
.toList();
48+
}
4349
}
4450

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

6163
/// Dart wrapper around StoreKit's [SKProductSubscriptionPeriod](https://developer.apple.com/documentation/storekit/skproductsubscriptionperiod?language=objc).
6264
///
6365
/// 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.
6466
/// It is used as a property in [SKProductDiscountWrapper] and [SKProductWrapper].
65-
@JsonSerializable(nullable: true)
6667
class SKProductSubscriptionPeriodWrapper {
6768
SKProductSubscriptionPeriodWrapper(
6869
{@required this.numberOfUnits, @required this.unit});
6970

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

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

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

103103
/// User pays nothing during the discounted period.
104-
@JsonValue(2)
105104
freeTrail,
106105
}
107106

@@ -110,7 +109,6 @@ enum ProductDiscountPaymentMode {
110109
/// Most of the fields are identical to OBJC SKProduct.
111110
/// The only difference is instead of the locale object, we only exposed currencyCode for simplicity.
112111
/// It is used as a property in [SKProductWrapper].
113-
@JsonSerializable(nullable: true)
114112
class SKProductDiscountWrapper {
115113
SKProductDiscountWrapper(
116114
{@required this.price,
@@ -121,12 +119,20 @@ class SKProductDiscountWrapper {
121119

122120
/// Constructing an instance from a map from the Objective-C layer.
123121
///
124-
/// This method should only be used with `map` values returned by [SKProductWrapper.fromJson].
122+
/// This method should only be used with `map` values returned by [SKProductWrapper.fromMap].
125123
/// The `map` parameter must not be null.
126-
factory SKProductDiscountWrapper.fromJson(Map map) {
127-
assert(map != null);
128-
return _$SKProductDiscountWrapperFromJson(map);
129-
}
124+
SKProductDiscountWrapper.fromMap(Map<String, dynamic> map)
125+
: assert(map != null),
126+
price = map['price'],
127+
currencyCode = map['currencyCode'],
128+
numberOfPeriods = map['numberOfPeriods'],
129+
paymentMode = (map['paymentMode'] != null)
130+
? ProductDiscountPaymentMode.values[map['paymentMode']]
131+
: null,
132+
subscriptionPeriod = map['subscriptionPeriod'] != null
133+
? SKProductSubscriptionPeriodWrapper.fromMap(
134+
map['subscriptionPeriod'].cast<String, dynamic>())
135+
: null;
130136

131137
/// The discounted price, in the currency that is defined in [currencyCode].
132138
final double price;
@@ -158,7 +164,6 @@ class SKProductDiscountWrapper {
158164
/// The only difference is instead of the locale object, we only exposed currencyCode for simplicity.
159165
/// A list of [SKProductWrapper] is returned in the [SKRequestMaker.startProductRequest] method, and
160166
/// should be stored for use when making a payment.
161-
@JsonSerializable(nullable: true)
162167
class SKProductWrapper {
163168
SKProductWrapper({
164169
@required this.productIdentifier,
@@ -176,12 +181,28 @@ class SKProductWrapper {
176181

177182
/// Constructing an instance from a map from the Objective-C layer.
178183
///
179-
/// This method should only be used with `map` values returned by [SkProductResponseWrapper.fromJson].
184+
/// This method should only be used with `map` values returned by [SkProductResponseWrapper.fromMap].
180185
/// The `map` parameter must not be null.
181-
factory SKProductWrapper.fromJson(Map map) {
182-
assert(map != null);
183-
return _$SKProductWrapperFromJson(map);
184-
}
186+
SKProductWrapper.fromMap(Map<dynamic, dynamic> map)
187+
: assert(map != null),
188+
productIdentifier = map['productIdentifier'],
189+
localizedTitle = map['localizedTitle'],
190+
localizedDescription = map['localizedDescription'],
191+
currencyCode = map['currencyCode'],
192+
downloadContentVersion = map['downloadContentVersion'],
193+
subscriptionGroupIdentifier = map['subscriptionGroupIdentifier'],
194+
price = map['price'],
195+
downloadable = map['downloadable'],
196+
downloadContentLengths =
197+
List.castFrom<dynamic, int>(map['downloadContentLengths']),
198+
subscriptionPeriod = map['subscriptionPeriod'] != null
199+
? SKProductSubscriptionPeriodWrapper.fromMap(
200+
map['subscriptionPeriod'].cast<String, dynamic>())
201+
: null,
202+
introductoryPrice = (map['introductoryPrice'] != null)
203+
? SKProductDiscountWrapper.fromMap(
204+
map['introductoryPrice'].cast<String, dynamic>())
205+
: null;
185206

186207
/// The unique identifier of the product.
187208
final String productIdentifier;

packages/in_app_purchase/lib/src/store_kit_wrappers/sk_product_wrapper.g.dart

Lines changed: 0 additions & 96 deletions
This file was deleted.

packages/in_app_purchase/lib/src/store_kit_wrappers/sk_request_maker.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class SKRequestMaker {
2424
/// A [PlatformException] is thrown if the platform code making the request fails.
2525
Future<SkProductResponseWrapper> startProductRequest(
2626
List<String> productIdentifiers) async {
27-
final Map productResponseMap = await channel.invokeMethod(
27+
final Map<dynamic, dynamic> productResponseMap = await channel.invokeMethod(
2828
'-[InAppPurchasePlugin startProductRequest:result:]',
2929
productIdentifiers,
3030
);
@@ -34,6 +34,7 @@ class SKRequestMaker {
3434
message: 'StoreKit: Failed to get response from platform.',
3535
);
3636
}
37-
return SkProductResponseWrapper.fromJson(productResponseMap);
37+
return SkProductResponseWrapper.fromMap(
38+
productResponseMap.cast<String, List<dynamic>>());
3839
}
3940
}

packages/in_app_purchase/test/store_kit_wrappers/sk_product_test.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void main() {
4141
'SKProductSubscriptionPeriodWrapper should have property values consistent with map',
4242
() {
4343
final SKProductSubscriptionPeriodWrapper wrapper =
44-
SKProductSubscriptionPeriodWrapper.fromJson(subMap);
44+
SKProductSubscriptionPeriodWrapper.fromMap(subMap);
4545
expect(wrapper.numberOfUnits, subMap['numberOfUnits']);
4646
expect(wrapper.unit, SubscriptionPeriodUnit.values[subMap['unit']]);
4747
});
@@ -50,7 +50,7 @@ void main() {
5050
'SKProductSubscriptionPeriodWrapper should have properties to be null if map is empty',
5151
() {
5252
final SKProductSubscriptionPeriodWrapper wrapper =
53-
SKProductSubscriptionPeriodWrapper.fromJson(<String, dynamic>{});
53+
SKProductSubscriptionPeriodWrapper.fromMap(<String, dynamic>{});
5454
expect(wrapper.numberOfUnits, null);
5555
expect(wrapper.unit, null);
5656
});
@@ -59,7 +59,7 @@ void main() {
5959
'SKProductDiscountWrapper should have property values consistent with map',
6060
() {
6161
final SKProductDiscountWrapper wrapper =
62-
SKProductDiscountWrapper.fromJson(discountMap);
62+
SKProductDiscountWrapper.fromMap(discountMap);
6363
expect(wrapper.price, discountMap['price']);
6464
expect(wrapper.currencyCode, discountMap['currencyCode']);
6565
expect(wrapper.numberOfPeriods, discountMap['numberOfPeriods']);
@@ -77,7 +77,7 @@ void main() {
7777
'SKProductDiscountWrapper should have properties to be null if map is empty',
7878
() {
7979
final SKProductDiscountWrapper wrapper =
80-
SKProductDiscountWrapper.fromJson(<String, dynamic>{});
80+
SKProductDiscountWrapper.fromMap(<String, dynamic>{});
8181
expect(wrapper.price, null);
8282
expect(wrapper.currencyCode, null);
8383
expect(wrapper.numberOfPeriods, null);
@@ -125,14 +125,15 @@ void main() {
125125

126126
test('SKProductWrapper should have property values consistent with map',
127127
() {
128-
final SKProductWrapper wrapper = SKProductWrapper.fromJson(productMap);
128+
final SKProductWrapper wrapper = SKProductWrapper.fromMap(productMap);
129129
testMatchingProductMap(wrapper, productMap);
130130
});
131131

132-
test('SKProductWrapper should have properties to be null if map is empty',
132+
test(
133+
'SKProductDiscountWrapper should have properties to be null if map is empty',
133134
() {
134135
final SKProductWrapper wrapper =
135-
SKProductWrapper.fromJson(<String, dynamic>{});
136+
SKProductWrapper.fromMap(<String, dynamic>{});
136137
expect(wrapper.productIdentifier, null);
137138
expect(wrapper.localizedTitle, null);
138139
expect(wrapper.localizedDescription, null);
@@ -146,7 +147,7 @@ void main() {
146147

147148
test('SKProductResponse wrapper should match', () {
148149
final SkProductResponseWrapper wrapper =
149-
SkProductResponseWrapper.fromJson(productResponseMap);
150+
SkProductResponseWrapper.fromMap(productResponseMap);
150151
testMatchingProductMap(
151152
wrapper.products[0], productResponseMap['products'][0]);
152153
expect(wrapper.invalidProductIdentifiers,
@@ -159,7 +160,7 @@ void main() {
159160
'invalidProductIdentifiers': <String>[],
160161
};
161162
final SkProductResponseWrapper wrapper =
162-
SkProductResponseWrapper.fromJson(productResponseMapEmptyList);
163+
SkProductResponseWrapper.fromMap(productResponseMapEmptyList);
163164
expect(wrapper.products.length, 0);
164165
expect(wrapper.invalidProductIdentifiers.length, 0);
165166
});

packages/in_app_purchase/test/store_kit_wrappers/sk_request_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void main() {
4848
test('platform call should get result', () async {
4949
stubPlatform.addResponse(
5050
name: '-[InAppPurchasePlugin startProductRequest:result:]',
51-
value: productResponseMap);
51+
value: productResponseMap.cast<String, dynamic>());
5252
final SKRequestMaker request = SKRequestMaker();
5353
final SkProductResponseWrapper response =
5454
await request.startProductRequest(<String>['123']);

0 commit comments

Comments
 (0)