Skip to content

Commit

Permalink
Add back IntroductoryPrice.introPricePeriodUnit string (RevenueCat#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
beylmk authored Feb 15, 2022
1 parent 69440ec commit 03f4e18
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 60 deletions.
52 changes: 43 additions & 9 deletions lib/models/introductory_price.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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,
Expand All @@ -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<String, dynamic> json) =>
_$IntroductoryPriceFromJson(json);
}
}
88 changes: 49 additions & 39 deletions lib/models/introductory_price.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ 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(
price,
priceString,
period,
cycles,
periodUnit,
introPricePeriodUnit,
periodNumberOfUnits,
);
}
Expand Down Expand Up @@ -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')
Expand All @@ -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});
}
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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});
}
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -229,7 +233,7 @@ class __$IntroductoryPriceCopyWithImpl<$Res>

/// @nodoc
@JsonSerializable()
class _$_IntroductoryPrice implements _IntroductoryPrice {
class _$_IntroductoryPrice extends _IntroductoryPrice {
const _$_IntroductoryPrice(
@JsonKey(name: 'price')
this.price,
Expand All @@ -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<String, dynamic> json) =>
_$$_IntroductoryPriceFromJson(json);
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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));
}
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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<String, dynamic> json) =
_$_IntroductoryPrice.fromJson;
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 2 additions & 11 deletions lib/models/introductory_price.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions revenuecat_examples/purchase_tester/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ class _UpsellScreenState extends State<UpsellScreen> {
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')),
Expand All @@ -127,6 +132,41 @@ class _UpsellScreenState extends State<UpsellScreen> {
),
);
}

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 {
Expand Down
Loading

0 comments on commit 03f4e18

Please sign in to comment.