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.i.configDelegate;
  String formatPrice(double value, String currencyCode) {
    if (delegate != null) {
      final formatted = delegate.formatPrice(
        InAppPurchaser.i.locale,
        currencyCode,
        value,
      );
      if (formatted != null) {
        return delegate.formatZeros(formatted);
      }
    }
    String str = value.toStringAsFixed(2);
    str = str.replaceAll(RegExp(r'([.]*0+)$'), '');
    return "$currencyCode $str";
  }

  double? formatNumber(double? b, double? r, double? c) {
    if (b == null || r == null || c == null) return null;
    final x = (r / b) * c;
    if (delegate != null) return delegate.prettyPrice(x);
    return (x.roundToDouble() + 0.99 - 1).abs();
  }

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