stringify method

PaywallLocalizedContent<String?> stringify({
  1. bool? all,
  2. required double? usdPrice,
  3. required double? price,
  4. required int unit,
  5. required double discountPrice,
  6. required String localizedPrice,
  7. required String currencyCode,
})

Implementation

PaywallLocalizedContent<String?> stringify({
  bool? all,
  required double? usdPrice,
  required double? price,
  required int unit,
  required double discountPrice,
  required String localizedPrice,
  required String currencyCode,
}) {
  if (isEmpty) return this;
  final delegate = InAppPurchaser.iOrNull?.configDelegate;

  String formatPrice(double value, String code) {
    if (delegate != null) {
      final formatted = delegate.formatPrice(
        InAppPurchaser.i.locale,
        code,
        value,
      );
      if (formatted != null) return delegate.formatZeros(formatted);
    }
    final str = value.toStringAsFixed(2).replaceAll(RegExp(r'([.]*0+)$'), '');
    return '$code $str';
  }

  double prettyPrice(double x) {
    if (delegate != null) return delegate.prettyPrice(x);
    return (x.roundToDouble() + 0.99 - 1).abs();
  }

  double? computePrice(double? base, double? ratio, double? target) {
    if (base == null || ratio == null || target == null) return null;
    return prettyPrice((ratio / base) * target);
  }

  return replaceAll(kDiscountPrice, formatPrice(discountPrice, currencyCode),
          all: all)
      .replaceAll(
        kFormatedPrice,
        formatPrice(discountPrice / unit, currencyCode),
        all: all,
      )
      .replaceAll(
        kPrice,
        formatPrice(
          computePrice(usdPrice, discountPrice, price) ?? discountPrice,
          currencyCode,
        ),
        all: all,
      )
      .replaceAll(kLocalizedPrice, localizedPrice, all: all);
}