3
3
// found in the LICENSE file.
4
4
5
5
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' ;
12
6
13
7
/// Dart wrapper around StoreKit's [SKProductsResponse] (https://developer.apple.com/documentation/storekit/skproductsresponse?language=objc).
14
8
///
15
9
/// Represents the response object returned by [SKRequestMaker.startProductRequest] .
16
10
/// Contains information about a list of products and a list of invalid product identifiers.
17
- @JsonSerializable ()
18
11
class SkProductResponseWrapper {
19
12
SkProductResponseWrapper (
20
13
{@required this .products, @required this .invalidProductIdentifiers});
@@ -23,10 +16,11 @@ class SkProductResponseWrapper {
23
16
///
24
17
/// This method should only be used with `map` values returned by [SKRequestMaker.startProductRequest] .
25
18
/// 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' ]);
30
24
31
25
/// Stores all matching successfully found products.
32
26
///
@@ -40,6 +34,18 @@ class SkProductResponseWrapper {
40
34
/// found here https://developer.apple.com/documentation/storekit/skproductsresponse/1505985-invalidproductidentifiers?language=objc.
41
35
/// Will be empty if all the product identifiers are valid.
42
36
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
+ }
43
49
}
44
50
45
51
/// Dart wrapper around StoreKit's [SKProductPeriodUnit] (https://developer.apple.com/documentation/storekit/skproductperiodunit?language=objc).
@@ -48,34 +54,30 @@ class SkProductResponseWrapper {
48
54
// The values of the enum options are matching the [SKProductPeriodUnit]'s values. Should there be an update or addition
49
55
// in the [SKProductPeriodUnit], this need to be updated to match.
50
56
enum SubscriptionPeriodUnit {
51
- @JsonValue (0 )
52
57
day,
53
- @JsonValue (1 )
54
58
week,
55
- @JsonValue (2 )
56
59
month,
57
- @JsonValue (3 )
58
60
year,
59
61
}
60
62
61
63
/// Dart wrapper around StoreKit's [SKProductSubscriptionPeriod] (https://developer.apple.com/documentation/storekit/skproductsubscriptionperiod?language=objc).
62
64
///
63
65
/// 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.
64
66
/// It is used as a property in [SKProductDiscountWrapper] and [SKProductWrapper] .
65
- @JsonSerializable (nullable: true )
66
67
class SKProductSubscriptionPeriodWrapper {
67
68
SKProductSubscriptionPeriodWrapper (
68
69
{@required this .numberOfUnits, @required this .unit});
69
70
70
71
/// 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] .
73
73
/// 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 ;
79
81
80
82
/// The number of [unit] units in this period.
81
83
///
@@ -93,15 +95,12 @@ class SKProductSubscriptionPeriodWrapper {
93
95
// in the [SKProductDiscountPaymentMode], this need to be updated to match.
94
96
enum ProductDiscountPaymentMode {
95
97
/// Allows user to pay the discounted price at each payment period.
96
- @JsonValue (0 )
97
98
payAsYouGo,
98
99
99
100
/// Allows user to pay the discounted price upfront and receive the product for the rest of time that was paid for.
100
- @JsonValue (1 )
101
101
payUpFront,
102
102
103
103
/// User pays nothing during the discounted period.
104
- @JsonValue (2 )
105
104
freeTrail,
106
105
}
107
106
@@ -110,7 +109,6 @@ enum ProductDiscountPaymentMode {
110
109
/// Most of the fields are identical to OBJC SKProduct.
111
110
/// The only difference is instead of the locale object, we only exposed currencyCode for simplicity.
112
111
/// It is used as a property in [SKProductWrapper] .
113
- @JsonSerializable (nullable: true )
114
112
class SKProductDiscountWrapper {
115
113
SKProductDiscountWrapper (
116
114
{@required this .price,
@@ -121,12 +119,20 @@ class SKProductDiscountWrapper {
121
119
122
120
/// Constructing an instance from a map from the Objective-C layer.
123
121
///
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 ] .
125
123
/// 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 ;
130
136
131
137
/// The discounted price, in the currency that is defined in [currencyCode] .
132
138
final double price;
@@ -158,7 +164,6 @@ class SKProductDiscountWrapper {
158
164
/// The only difference is instead of the locale object, we only exposed currencyCode for simplicity.
159
165
/// A list of [SKProductWrapper] is returned in the [SKRequestMaker.startProductRequest] method, and
160
166
/// should be stored for use when making a payment.
161
- @JsonSerializable (nullable: true )
162
167
class SKProductWrapper {
163
168
SKProductWrapper ({
164
169
@required this .productIdentifier,
@@ -176,12 +181,28 @@ class SKProductWrapper {
176
181
177
182
/// Constructing an instance from a map from the Objective-C layer.
178
183
///
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 ] .
180
185
/// 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 ;
185
206
186
207
/// The unique identifier of the product.
187
208
final String productIdentifier;
0 commit comments