From 03f4e18742c986f31cc1f40b5727496b952cd82a Mon Sep 17 00:00:00 2001 From: beylmk Date: Tue, 15 Feb 2022 15:01:30 -0800 Subject: [PATCH] Add back IntroductoryPrice.introPricePeriodUnit string (#319) --- lib/models/introductory_price.dart | 52 +++++++++-- lib/models/introductory_price.freezed.dart | 88 +++++++++++-------- lib/models/introductory_price.g.dart | 13 +-- pubspec.yaml | 2 +- .../purchase_tester/lib/main.dart | 40 +++++++++ test/purchases_flutter_test.dart | 52 +++++++++++ 6 files changed, 187 insertions(+), 60 deletions(-) diff --git a/lib/models/introductory_price.dart b/lib/models/introductory_price.dart index 29414bdfb..a8ae658d6 100644 --- a/lib/models/introductory_price.dart +++ b/lib/models/introductory_price.dart @@ -4,13 +4,9 @@ part 'introductory_price.freezed.dart'; part 'introductory_price.g.dart'; enum PeriodUnit { - @JsonValue('DAY') day, - @JsonValue('WEEK') week, - @JsonValue('MONTH') month, - @JsonValue('YEAR') year, unknown } @@ -19,6 +15,8 @@ enum PeriodUnit { /// Contains all the introductory information associated with a [Product] class IntroductoryPrice with _$IntroductoryPrice { + const IntroductoryPrice._(); + const factory IntroductoryPrice( /// Introductory price of a subscription in the local currency. @JsonKey(name: 'price') double price, @@ -35,15 +33,51 @@ class IntroductoryPrice with _$IntroductoryPrice { /// user will be given the introductory price, such as 3. @JsonKey(name: 'cycles') int cycles, - /// Unit for the billing period of the introductory price, can be DAY, WEEK, - /// MONTH or YEAR. - @JsonKey(name: 'periodUnit', unknownEnumValue: PeriodUnit.unknown) - PeriodUnit periodUnit, + /// String representation of unit for the billing period of the introductory + /// price, can be DAY, WEEK, MONTH or YEAR. + @Deprecated('Use periodUnit property of type PeriodUnit instead.') + @JsonKey(name: 'periodUnit') String introPricePeriodUnit, /// Number of units for the billing period of the introductory price. @JsonKey(name: 'periodNumberOfUnits') int periodNumberOfUnits, ) = _IntroductoryPrice; + /// Unit for the billing period of the introductory price, can be DAY, WEEK, + /// MONTH or YEAR. + PeriodUnit get periodUnit { + // ignore: deprecated_member_use_from_same_package + switch (introPricePeriodUnit) { + case 'DAY': { return PeriodUnit.day; } + case 'WEEK': { return PeriodUnit.week; } + case 'MONTH': { return PeriodUnit.month; } + case 'YEAR': { return PeriodUnit.year; } + default: { return PeriodUnit.unknown; } + } + } + + /// Introductory price of a subscription in the local currency. + @Deprecated('Use price instead.') + double get introPrice => price; + + /// Formatted introductory price of a subscription, including + /// its currency sign, such as €3.99. + @Deprecated('Use priceString instead.') + String get introPriceString => priceString; + + /// Billing period of the introductory price, specified in + /// ISO 8601 format. + @Deprecated('Use period instead.') + String get introPricePeriod => period; + + /// Number of units for the billing period of the introductory price. + @Deprecated('Use periodNumberOfUnits instead.') + int get introPricePeriodNumberOfUnits => periodNumberOfUnits; + + /// Number of subscription billing periods for which the + /// user will be given the introductory price, such as 3. + @Deprecated('Use cycles instead.') + int get introPriceCycles => cycles; + factory IntroductoryPrice.fromJson(Map json) => _$IntroductoryPriceFromJson(json); -} +} \ No newline at end of file diff --git a/lib/models/introductory_price.freezed.dart b/lib/models/introductory_price.freezed.dart index 4ea99973f..0f8dba386 100644 --- a/lib/models/introductory_price.freezed.dart +++ b/lib/models/introductory_price.freezed.dart @@ -31,8 +31,9 @@ class _$IntroductoryPriceTearOff { String period, @JsonKey(name: 'cycles') int cycles, - @JsonKey(name: 'periodUnit', unknownEnumValue: PeriodUnit.unknown) - PeriodUnit periodUnit, + @Deprecated('Use periodUnit property of type PeriodUnit instead.') + @JsonKey(name: 'periodUnit') + String introPricePeriodUnit, @JsonKey(name: 'periodNumberOfUnits') int periodNumberOfUnits) { return _IntroductoryPrice( @@ -40,7 +41,7 @@ class _$IntroductoryPriceTearOff { priceString, period, cycles, - periodUnit, + introPricePeriodUnit, periodNumberOfUnits, ); } @@ -74,10 +75,11 @@ mixin _$IntroductoryPrice { @JsonKey(name: 'cycles') int get cycles => throw _privateConstructorUsedError; - /// Unit for the billing period of the introductory price, can be DAY, WEEK, - /// MONTH or YEAR. - @JsonKey(name: 'periodUnit', unknownEnumValue: PeriodUnit.unknown) - PeriodUnit get periodUnit => throw _privateConstructorUsedError; + /// String representation of unit for the billing period of the introductory + /// price, can be DAY, WEEK, MONTH or YEAR. + @Deprecated('Use periodUnit property of type PeriodUnit instead.') + @JsonKey(name: 'periodUnit') + String get introPricePeriodUnit => throw _privateConstructorUsedError; /// Number of units for the billing period of the introductory price. @JsonKey(name: 'periodNumberOfUnits') @@ -103,8 +105,9 @@ abstract class $IntroductoryPriceCopyWith<$Res> { String period, @JsonKey(name: 'cycles') int cycles, - @JsonKey(name: 'periodUnit', unknownEnumValue: PeriodUnit.unknown) - PeriodUnit periodUnit, + @Deprecated('Use periodUnit property of type PeriodUnit instead.') + @JsonKey(name: 'periodUnit') + String introPricePeriodUnit, @JsonKey(name: 'periodNumberOfUnits') int periodNumberOfUnits}); } @@ -124,7 +127,7 @@ class _$IntroductoryPriceCopyWithImpl<$Res> Object? priceString = freezed, Object? period = freezed, Object? cycles = freezed, - Object? periodUnit = freezed, + Object? introPricePeriodUnit = freezed, Object? periodNumberOfUnits = freezed, }) { return _then(_value.copyWith( @@ -144,10 +147,10 @@ class _$IntroductoryPriceCopyWithImpl<$Res> ? _value.cycles : cycles // ignore: cast_nullable_to_non_nullable as int, - periodUnit: periodUnit == freezed - ? _value.periodUnit - : periodUnit // ignore: cast_nullable_to_non_nullable - as PeriodUnit, + introPricePeriodUnit: introPricePeriodUnit == freezed + ? _value.introPricePeriodUnit + : introPricePeriodUnit // ignore: cast_nullable_to_non_nullable + as String, periodNumberOfUnits: periodNumberOfUnits == freezed ? _value.periodNumberOfUnits : periodNumberOfUnits // ignore: cast_nullable_to_non_nullable @@ -172,8 +175,9 @@ abstract class _$IntroductoryPriceCopyWith<$Res> String period, @JsonKey(name: 'cycles') int cycles, - @JsonKey(name: 'periodUnit', unknownEnumValue: PeriodUnit.unknown) - PeriodUnit periodUnit, + @Deprecated('Use periodUnit property of type PeriodUnit instead.') + @JsonKey(name: 'periodUnit') + String introPricePeriodUnit, @JsonKey(name: 'periodNumberOfUnits') int periodNumberOfUnits}); } @@ -195,7 +199,7 @@ class __$IntroductoryPriceCopyWithImpl<$Res> Object? priceString = freezed, Object? period = freezed, Object? cycles = freezed, - Object? periodUnit = freezed, + Object? introPricePeriodUnit = freezed, Object? periodNumberOfUnits = freezed, }) { return _then(_IntroductoryPrice( @@ -215,10 +219,10 @@ class __$IntroductoryPriceCopyWithImpl<$Res> ? _value.cycles : cycles // ignore: cast_nullable_to_non_nullable as int, - periodUnit == freezed - ? _value.periodUnit - : periodUnit // ignore: cast_nullable_to_non_nullable - as PeriodUnit, + introPricePeriodUnit == freezed + ? _value.introPricePeriodUnit + : introPricePeriodUnit // ignore: cast_nullable_to_non_nullable + as String, periodNumberOfUnits == freezed ? _value.periodNumberOfUnits : periodNumberOfUnits // ignore: cast_nullable_to_non_nullable @@ -229,7 +233,7 @@ class __$IntroductoryPriceCopyWithImpl<$Res> /// @nodoc @JsonSerializable() -class _$_IntroductoryPrice implements _IntroductoryPrice { +class _$_IntroductoryPrice extends _IntroductoryPrice { const _$_IntroductoryPrice( @JsonKey(name: 'price') this.price, @@ -239,10 +243,12 @@ class _$_IntroductoryPrice implements _IntroductoryPrice { this.period, @JsonKey(name: 'cycles') this.cycles, - @JsonKey(name: 'periodUnit', unknownEnumValue: PeriodUnit.unknown) - this.periodUnit, + @Deprecated('Use periodUnit property of type PeriodUnit instead.') + @JsonKey(name: 'periodUnit') + this.introPricePeriodUnit, @JsonKey(name: 'periodNumberOfUnits') - this.periodNumberOfUnits); + this.periodNumberOfUnits) + : super._(); factory _$_IntroductoryPrice.fromJson(Map json) => _$$_IntroductoryPriceFromJson(json); @@ -272,10 +278,11 @@ class _$_IntroductoryPrice implements _IntroductoryPrice { final int cycles; @override - /// Unit for the billing period of the introductory price, can be DAY, WEEK, - /// MONTH or YEAR. - @JsonKey(name: 'periodUnit', unknownEnumValue: PeriodUnit.unknown) - final PeriodUnit periodUnit; + /// String representation of unit for the billing period of the introductory + /// price, can be DAY, WEEK, MONTH or YEAR. + @Deprecated('Use periodUnit property of type PeriodUnit instead.') + @JsonKey(name: 'periodUnit') + final String introPricePeriodUnit; @override /// Number of units for the billing period of the introductory price. @@ -284,7 +291,7 @@ class _$_IntroductoryPrice implements _IntroductoryPrice { @override String toString() { - return 'IntroductoryPrice(price: $price, priceString: $priceString, period: $period, cycles: $cycles, periodUnit: $periodUnit, periodNumberOfUnits: $periodNumberOfUnits)'; + return 'IntroductoryPrice(price: $price, priceString: $priceString, period: $period, cycles: $cycles, introPricePeriodUnit: $introPricePeriodUnit, periodNumberOfUnits: $periodNumberOfUnits)'; } @override @@ -298,7 +305,7 @@ class _$_IntroductoryPrice implements _IntroductoryPrice { const DeepCollectionEquality().equals(other.period, period) && const DeepCollectionEquality().equals(other.cycles, cycles) && const DeepCollectionEquality() - .equals(other.periodUnit, periodUnit) && + .equals(other.introPricePeriodUnit, introPricePeriodUnit) && const DeepCollectionEquality() .equals(other.periodNumberOfUnits, periodNumberOfUnits)); } @@ -310,7 +317,7 @@ class _$_IntroductoryPrice implements _IntroductoryPrice { const DeepCollectionEquality().hash(priceString), const DeepCollectionEquality().hash(period), const DeepCollectionEquality().hash(cycles), - const DeepCollectionEquality().hash(periodUnit), + const DeepCollectionEquality().hash(introPricePeriodUnit), const DeepCollectionEquality().hash(periodNumberOfUnits)); @JsonKey(ignore: true) @@ -324,7 +331,7 @@ class _$_IntroductoryPrice implements _IntroductoryPrice { } } -abstract class _IntroductoryPrice implements IntroductoryPrice { +abstract class _IntroductoryPrice extends IntroductoryPrice { const factory _IntroductoryPrice( @JsonKey(name: 'price') double price, @@ -334,10 +341,12 @@ abstract class _IntroductoryPrice implements IntroductoryPrice { String period, @JsonKey(name: 'cycles') int cycles, - @JsonKey(name: 'periodUnit', unknownEnumValue: PeriodUnit.unknown) - PeriodUnit periodUnit, + @Deprecated('Use periodUnit property of type PeriodUnit instead.') + @JsonKey(name: 'periodUnit') + String introPricePeriodUnit, @JsonKey(name: 'periodNumberOfUnits') int periodNumberOfUnits) = _$_IntroductoryPrice; + const _IntroductoryPrice._() : super._(); factory _IntroductoryPrice.fromJson(Map json) = _$_IntroductoryPrice.fromJson; @@ -367,10 +376,11 @@ abstract class _IntroductoryPrice implements IntroductoryPrice { int get cycles; @override - /// Unit for the billing period of the introductory price, can be DAY, WEEK, - /// MONTH or YEAR. - @JsonKey(name: 'periodUnit', unknownEnumValue: PeriodUnit.unknown) - PeriodUnit get periodUnit; + /// String representation of unit for the billing period of the introductory + /// price, can be DAY, WEEK, MONTH or YEAR. + @Deprecated('Use periodUnit property of type PeriodUnit instead.') + @JsonKey(name: 'periodUnit') + String get introPricePeriodUnit; @override /// Number of units for the billing period of the introductory price. diff --git a/lib/models/introductory_price.g.dart b/lib/models/introductory_price.g.dart index decc658c3..310b4a595 100644 --- a/lib/models/introductory_price.g.dart +++ b/lib/models/introductory_price.g.dart @@ -12,8 +12,7 @@ _$_IntroductoryPrice _$$_IntroductoryPriceFromJson(Map json) => json['priceString'] as String, json['period'] as String, json['cycles'] as int, - $enumDecode(_$PeriodUnitEnumMap, json['periodUnit'], - unknownValue: PeriodUnit.unknown), + json['periodUnit'] as String, json['periodNumberOfUnits'] as int, ); @@ -24,14 +23,6 @@ Map _$$_IntroductoryPriceToJson( 'priceString': instance.priceString, 'period': instance.period, 'cycles': instance.cycles, - 'periodUnit': _$PeriodUnitEnumMap[instance.periodUnit], + 'periodUnit': instance.introPricePeriodUnit, 'periodNumberOfUnits': instance.periodNumberOfUnits, }; - -const _$PeriodUnitEnumMap = { - PeriodUnit.day: 'DAY', - PeriodUnit.week: 'WEEK', - PeriodUnit.month: 'MONTH', - PeriodUnit.year: 'YEAR', - PeriodUnit.unknown: 'unknown', -}; diff --git a/pubspec.yaml b/pubspec.yaml index 5030d947c..5fe6b4f77 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ dependencies: flutter: sdk: flutter freezed_annotation: ^1.1.0 - json_annotation: ^4.3.0 + json_annotation: ^4.4.0 dev_dependencies: build_runner: ^2.1.4 diff --git a/revenuecat_examples/purchase_tester/lib/main.dart b/revenuecat_examples/purchase_tester/lib/main.dart index 92d6a52c2..8c58aead4 100644 --- a/revenuecat_examples/purchase_tester/lib/main.dart +++ b/revenuecat_examples/purchase_tester/lib/main.dart @@ -104,6 +104,11 @@ class _UpsellScreenState extends State { if (offering != null) { final monthly = offering.monthly; final lifetime = offering.lifetime; + + if (monthly.product.introductoryPrice != null) { + apiTestIntroductoryPrice(monthly.product.introductoryPrice); + } + if (monthly != null && lifetime != null) { return Scaffold( appBar: AppBar(title: const Text('Upsell Screen')), @@ -127,6 +132,41 @@ class _UpsellScreenState extends State { ), ); } + + void apiTestIntroductoryPrice(IntroductoryPrice introPrice) { + final PeriodUnit periodUnit = introPrice.periodUnit; + final double price = introPrice.price; + final String priceString = introPrice.priceString; + final int cycles = introPrice.cycles; + final int periodNumberOfUnits = introPrice.periodNumberOfUnits; + + /// deprecated properties + // ignore: deprecated_member_use + final String introPricePeriodUnit = introPrice.introPricePeriodUnit; + // ignore: deprecated_member_use + final double introPricePrice = introPrice.introPrice; + // ignore: deprecated_member_use + final String introPriceString = introPrice.introPriceString; + // ignore: deprecated_member_use + final String introPricePeriod = introPrice.introPricePeriod; + final int introPricePeriodNumberOfUnits = + // ignore: deprecated_member_use + introPrice.introPricePeriodNumberOfUnits; + // ignore: deprecated_member_use + final int introPriceCycles = introPrice.introPriceCycles; + + + print( + 'introPricePeriodUnit: $introPricePeriodUnit, periodUnit: ' + '$periodUnit, price: $price.toString(), priceString: ' + '$priceString, cycles: $cycles.toString(), periodNumberOfUnits: ' + '$periodNumberOfUnits, introPrice: $introPrice.toString(), ' + 'introPriceString: $introPriceString, introPricePeriod: ' + '$introPricePeriod, introPricePeriodNumberOfUnits: ' + '$introPricePeriodNumberOfUnits, introPriceCycles: ' + '$introPriceCycles, introPricePrice: $introPricePrice.toString()'); + + } } class _PurchaseButton extends StatelessWidget { diff --git a/test/purchases_flutter_test.dart b/test/purchases_flutter_test.dart index d22f07193..16f3d6f30 100644 --- a/test/purchases_flutter_test.dart +++ b/test/purchases_flutter_test.dart @@ -4,6 +4,7 @@ import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:purchases_flutter/purchases_flutter.dart'; +// ignore_for_file: deprecated_member_use_from_same_package void main() { TestWidgetsFlutterBinding.ensureInitialized(); @@ -25,6 +26,15 @@ void main() { 'nonSubscriptionTransactions': [] }; + final mockIntroductoryPriceJson = { + 'price': 0.0, + 'priceString': '\$0.00', + 'period': 'P2W', + 'cycles': 1, + 'periodUnit': 'DAY', + 'periodNumberOfUnits': 14, + }; + setUp(() { channel.setMockMethodCallHandler((call) async { log.add(call); @@ -452,4 +462,46 @@ void main() { fail('there was an exception ' + e.toString()); } }); + + test('IntroductoryPrice has both PeriodUnit enum and periodUnit string', () async { + final mockIntroductoryPrice = IntroductoryPrice.fromJson( + mockIntroductoryPriceJson, + ); + expect(mockIntroductoryPrice.periodUnit, PeriodUnit.day); + expect(mockIntroductoryPrice.introPricePeriodUnit, 'DAY'); + }); + + test('IntroductoryPrice deprecated properties contain same values', () async { + final mockIntroPrice = IntroductoryPrice.fromJson( + mockIntroductoryPriceJson, + ); + expect(mockIntroPrice.price, mockIntroPrice.introPrice); + expect(mockIntroPrice.priceString, mockIntroPrice.introPriceString); + expect(mockIntroPrice.period, mockIntroPrice.introPricePeriod); + expect(mockIntroPrice.periodNumberOfUnits, + mockIntroPrice.introPricePeriodNumberOfUnits,); + expect(mockIntroPrice.cycles, mockIntroPrice.introPriceCycles); + }); + + test('IntroductoryPrice PeriodUnit maps correctly', () async { + /// test day + const introPricePeriodUnitDay = IntroductoryPrice(0.0, '\$0.00', 'P2W', 1, 'DAY', 14); + expect(introPricePeriodUnitDay.periodUnit, PeriodUnit.day); + + /// test week + const introPricePeriodUnitWeek = IntroductoryPrice(0.0, '\$0.00', 'P2W', 1, 'WEEK', 14); + expect(introPricePeriodUnitWeek.periodUnit, PeriodUnit.week); + + /// test month + const introPricePeriodUnitMonth = IntroductoryPrice(0.0, '\$0.00', 'P2W', 1, 'MONTH', 14); + expect(introPricePeriodUnitMonth.periodUnit, PeriodUnit.month); + + /// test year + const introPricePeriodUnitYear = IntroductoryPrice(0.0, '\$0.00', 'P2W', 1, 'YEAR', 14); + expect(introPricePeriodUnitYear.periodUnit, PeriodUnit.year); + + /// test unknown + const introPricePeriodUnitUnknown = IntroductoryPrice(0.0, '\$0.00', 'P2W', 1, 'sadf', 14); + expect(introPricePeriodUnitUnknown.periodUnit, PeriodUnit.unknown); +}); }