Files
pluriwave/lib/servicios/servicio_anuncios.dart
T
FreeTLab aa0b242374 feat(iap): add freemium unlock via one-time in-app purchase
Adds a permanent, non-consumable premium unlock (EstadoEntitlement +
PuertoCompras/ServicioComprasPlayBilling) that removes ads and unlocks
alarm vacations, alarms past a 5-alarm free cap, recording start, and
full Android Auto browsing. The phone equalizer stays free for everyone.

- Entitlement is prefs-backed (compra_premium_v1), fail-open, and
  resolvable headlessly via esPremiumPersistido() for the Android Auto
  audio handler, which registers before runApp.
- Android Auto reduced mode keeps the real root folder labels for free
  users; browsing into any of them (and playFromMediaId/playFromSearch/
  skipToNext/skipToPrevious) is blocked at the getChildren/servicio_audio
  choke points, with a locked "Función Premium" item as the backstop.
  Current-station play/pause/stop stays untouched. A free -> premium
  transition actively invalidates the head unit's cached browse tree.
- Ads (top banner + capped interstitial before adding a station or an
  alarm) are gated behind entitlement via ServicioAnuncios, using
  official Google test ad unit IDs pending AdMob provisioning.
- Alarm cap UX shows an explanatory message with a secondary unlock
  action rather than a bare paywall jump; existing data is grandfathered.
- 4 new localization keys translated across all 13 supported locales.

Co-located tests use strict TDD (RED test before implementation) for
every new pure-logic unit; full existing suite passes unchanged.
2026-08-10 20:37:07 +02:00

118 lines
4.5 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart' show debugPrint;
import 'package:google_mobile_ads/google_mobile_ads.dart';
/// TODO(ads): official Google TEST ad unit ids — AdMob has not provisioned
/// real ones yet (design.md Open Questions). Swap these for the real banner
/// / interstitial unit ids once available; never ship the test ids to
/// production.
const bannerAdUnitIdPrueba = 'ca-app-pub-3940256099942544/6300978111';
const interstitialAdUnitIdPrueba = 'ca-app-pub-3940256099942544/1033173712';
/// Ads port + AdMob adapter (Design "Interfaces / Contracts", ADR-6): owns
/// the entitlement gate for both surfaces, the interstitial's session
/// frequency cap, and is the ONLY `google_mobile_ads` call site besides
/// `banner_anuncio_superior.dart`'s `BannerAd` widget wrapper. The frequency
/// cap and premium gating are pure/injectable (`ahora`,
/// `mostrarInterstitialImpl`) so they are unit-testable with a fake clock
/// and zero AdMob platform channels (Design Testing Strategy).
class ServicioAnuncios {
ServicioAnuncios({
bool Function()? esPremium,
DateTime Function()? ahora,
Future<bool> Function()? mostrarInterstitialImpl,
}) : _esPremium = esPremium ?? (() => false),
_ahora = ahora ?? DateTime.now,
_mostrarInterstitialImpl =
mostrarInterstitialImpl ?? _mostrarInterstitialAdMob;
/// Session-scoped cap (ad-display spec "Interstitial Frequency Cap"): at
/// most 2 interstitials per process lifetime.
static const maxInterstitialsPorSesion = 2;
/// Minimum spacing between two interstitials (ad-display spec, same
/// requirement).
static const separacionMinima = Duration(minutes: 3);
final bool Function() _esPremium;
final DateTime Function() _ahora;
final Future<bool> Function() _mostrarInterstitialImpl;
int _mostrados = 0;
DateTime? _ultimoMostrado;
/// Ad-display spec "Persistent Top Banner": absent entirely for premium.
bool get debeMostrarBanner => !_esPremium();
bool _dentroDelCap() {
if (_esPremium()) return false;
if (_mostrados >= maxInterstitialsPorSesion) return false;
final ultimo = _ultimoMostrado;
if (ultimo != null && _ahora().difference(ultimo) < separacionMinima) {
return false;
}
return true;
}
/// Attempts to show an interstitial for one of the two allowed CTAs (add
/// station manually, add alarm). Callers are responsible for the ADR-6
/// ordering invariant themselves (cap-check-before-interstitial for
/// add-alarm, so a refusal is never preceded by an ad) — this method only
/// owns entitlement + frequency-cap gating, never the caller's own
/// business-rule ordering.
///
/// Returns whether an interstitial actually rendered. A failed/aborted ad
/// load (network, no fill) does NOT consume the session cap — only a
/// genuinely SHOWN ad does (Spec intent: the cap limits driver-facing
/// interruptions, not load attempts).
Future<bool> intentarInterstitial() async {
if (!_dentroDelCap()) return false;
final mostrado = await _mostrarInterstitialImpl();
if (mostrado) {
_mostrados++;
_ultimoMostrado = _ahora();
}
return mostrado;
}
static Future<bool> _mostrarInterstitialAdMob() async {
try {
final cargaCompleter = Completer<InterstitialAd?>();
await InterstitialAd.load(
adUnitId: interstitialAdUnitIdPrueba,
request: const AdRequest(),
adLoadCallback: InterstitialAdLoadCallback(
onAdLoaded: (ad) {
if (!cargaCompleter.isCompleted) cargaCompleter.complete(ad);
},
onAdFailedToLoad: (error) {
debugPrint('[PluriWave][anuncios] interstitial load ERROR $error');
if (!cargaCompleter.isCompleted) cargaCompleter.complete(null);
},
),
);
final cargado = await cargaCompleter.future;
if (cargado == null) return false;
final cierreCompleter = Completer<void>();
cargado.fullScreenContentCallback = FullScreenContentCallback(
onAdDismissedFullScreenContent: (ad) {
ad.dispose();
if (!cierreCompleter.isCompleted) cierreCompleter.complete();
},
onAdFailedToShowFullScreenContent: (ad, error) {
ad.dispose();
if (!cierreCompleter.isCompleted) cierreCompleter.complete();
},
);
await cargado.show();
await cierreCompleter.future;
return true;
} catch (e) {
debugPrint('[PluriWave][anuncios] interstitial ERROR $e');
return false;
}
}
}