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