@@ -19,32 +19,28 @@ Future<bool> _shouldShowGoogleAds(WidgetRef ref) async {
1919
2020class GoogleAdsState {
2121 final bool nativeAdIsLoaded;
22+ final bool adLoadFailed;
2223 final int countdown;
2324 final bool showCountdown;
24- final bool shouldDisposeAd;
25- final bool adLoadFailed;
2625
2726 const GoogleAdsState ({
2827 this .nativeAdIsLoaded = false ,
28+ this .adLoadFailed = false ,
2929 this .countdown = _countdownDuration,
3030 this .showCountdown = true ,
31- this .shouldDisposeAd = false ,
32- this .adLoadFailed = false ,
3331 });
3432
3533 GoogleAdsState copyWith ({
3634 bool ? nativeAdIsLoaded,
35+ bool ? adLoadFailed,
3736 int ? countdown,
3837 bool ? showCountdown,
39- bool ? shouldDisposeAd,
40- bool ? adLoadFailed,
4138 }) {
4239 return GoogleAdsState (
4340 nativeAdIsLoaded: nativeAdIsLoaded ?? this .nativeAdIsLoaded,
41+ adLoadFailed: adLoadFailed ?? this .adLoadFailed,
4442 countdown: countdown ?? this .countdown,
4543 showCountdown: showCountdown ?? this .showCountdown,
46- shouldDisposeAd: shouldDisposeAd ?? this .shouldDisposeAd,
47- adLoadFailed: adLoadFailed ?? this .adLoadFailed,
4844 );
4945 }
5046}
@@ -62,35 +58,27 @@ class GoogleAdsNotifier extends StateNotifier<GoogleAdsState> {
6258 state = state.copyWith (
6359 countdown: _countdownDuration,
6460 showCountdown: true ,
65- shouldDisposeAd: false ,
6661 );
6762 _countdownTimer = Timer .periodic (const Duration (seconds: 1 ), (timer) {
6863 if (state.countdown > 0 ) {
6964 state = state.copyWith (countdown: state.countdown - 1 );
7065 } else {
7166 state = state.copyWith (
7267 showCountdown: false ,
73- shouldDisposeAd: true ,
74- nativeAdIsLoaded: false ,
68+ // Keep nativeAdIsLoaded true - ad stays loaded, just hidden
7569 );
7670 timer.cancel ();
7771 }
7872 });
7973 }
8074
81- void resestCountDown () {
82- state = state.copyWith (countdown: _countdownDuration);
83- }
84-
8575 void setAdLoaded (bool isLoaded) {
8676 debugPrint ('Ad loaded: $isLoaded ' );
8777 state = state.copyWith (
8878 nativeAdIsLoaded: isLoaded,
8979 adLoadFailed: false ,
9080 );
91- if (isLoaded &&
92- state.showCountdown &&
93- state.countdown == _countdownDuration) {
81+ if (isLoaded && state.showCountdown && state.countdown == _countdownDuration) {
9482 startCountdownTimer ();
9583 }
9684 }
@@ -102,10 +90,6 @@ class GoogleAdsNotifier extends StateNotifier<GoogleAdsState> {
10290 );
10391 }
10492
105- void acknowledgeDisposal () {
106- state = state.copyWith (shouldDisposeAd: false );
107- }
108-
10993 void resetState () {
11094 state = const GoogleAdsState ();
11195 }
@@ -188,6 +172,8 @@ class _GoogleAdsState extends ConsumerState<GoogleAds> {
188172 void _initializeAds () async {
189173 if (_isDisposed || _hasInitialized) return ;
190174
175+ _hasInitialized = true ;
176+
191177 try {
192178 // Disable Google Ads on non-mobile platforms.
193179 if (! (Platform .isAndroid || Platform .isIOS)) {
@@ -329,6 +315,9 @@ class _GoogleAdsState extends ConsumerState<GoogleAds> {
329315 @override
330316 void dispose () {
331317 _isDisposed = true ;
318+ _nativeAd? .dispose ();
319+ _nativeAd = null ;
320+ _globalAdInitialized = false ;
332321 super .dispose ();
333322 }
334323
@@ -338,24 +327,17 @@ class _GoogleAdsState extends ConsumerState<GoogleAds> {
338327 final shouldShowGoogle = ref.watch (shouldShowGoogleAdsProvider);
339328 final customAdData = ref.watch (customAdDataProvider);
340329
330+ // Restart countdown on new connection for fresh 60-second impressions
331+ // Ad stays loaded through disconnect/reconnect - no disposal
341332 ref.listen (connectionStateProvider, (previous, next) {
342- if (next.status == ConnectionStatus .disconnected && ! _isDisposed) {
343- if (_nativeAd != null ) {
344- _nativeAd? .dispose ();
345- _nativeAd = null ;
346- }
347- if (mounted) {
348- setState (() {
349- _isLoading = false ;
350- });
351- }
352- ref.read (googleAdsProvider.notifier).acknowledgeDisposal ();
353- _globalAdInitialized = false ;
354- }
355- if (next.status == ConnectionStatus .connected) {
356- ref.read (googleAdsProvider.notifier).resestCountDown ();
333+ if (next.status == ConnectionStatus .connected &&
334+ previous? .status != ConnectionStatus .connected &&
335+ adsState.nativeAdIsLoaded &&
336+ ! _isDisposed) {
337+ ref.read (googleAdsProvider.notifier).startCountdownTimer ();
357338 }
358339 });
340+
359341 return SizedBox (
360342 height: 280. h,
361343 width: 336. w,
0 commit comments