Skip to content

Commit 46268fb

Browse files
fix timer keeps restarting and ads reload on every page change (#198)
* fix timer keeps restarting and ads reload on every page change * fix: reset count down function for ad added --------- Co-authored-by: Julius Caesar <juliuscaesarempo@proton.me>
1 parent cc696c3 commit 46268fb

2 files changed

Lines changed: 45 additions & 15 deletions

File tree

lib/modules/main/presentation/widgets/connection_button.dart

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,18 @@ class _ConnectionButtonState extends State<ConnectionButton>
224224
void _handleTimerState(vpn.ConnectionStatus status) {
225225
switch (status) {
226226
case vpn.ConnectionStatus.connected:
227-
if (_timer == null) _startTimer();
227+
if (_timer == null || !_timer!.isActive) {
228+
_startTimer();
229+
}
228230
_shieldLoadingController.stop();
229231
break;
230232
case vpn.ConnectionStatus.disconnected:
231-
if (_seconds > 0) _resetTimer();
233+
if (_timer != null && _timer!.isActive) {
234+
_stopTimer();
235+
}
236+
if (_seconds > 0) {
237+
_resetTimer();
238+
}
232239
_shieldLoadingController.stop();
233240
break;
234241
case vpn.ConnectionStatus.loading:
@@ -238,10 +245,15 @@ class _ConnectionButtonState extends State<ConnectionButton>
238245
_animationService.conditionalRepeat(_shieldLoadingController);
239246
break;
240247
case vpn.ConnectionStatus.disconnecting:
248+
if (_timer != null && _timer!.isActive) {
249+
_stopTimer();
250+
}
241251
_animationService.conditionalRepeat(_shieldLoadingController);
242252
break;
243253
default:
244-
if (_timer != null) _stopTimer();
254+
if (_timer != null && _timer!.isActive) {
255+
_stopTimer();
256+
}
245257
_shieldLoadingController.stop();
246258
}
247259
}

lib/modules/main/presentation/widgets/google_ads.dart

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:io';
22
import 'dart:async';
33
import 'package:defyx_vpn/app/advertise_director.dart';
4+
import 'package:defyx_vpn/shared/providers/connection_state_provider.dart';
45
import 'package:flutter/material.dart';
56
import 'package:flutter_dotenv/flutter_dotenv.dart';
67
import 'package:flutter_riverpod/flutter_riverpod.dart';
@@ -77,6 +78,10 @@ class GoogleAdsNotifier extends StateNotifier<GoogleAdsState> {
7778
});
7879
}
7980

81+
void resestCountDown() {
82+
state = state.copyWith(countdown: _countdownDuration);
83+
}
84+
8085
void setAdLoaded(bool isLoaded) {
8186
debugPrint('Ad loaded: $isLoaded');
8287
state = state.copyWith(
@@ -155,6 +160,7 @@ class _GoogleAdsState extends ConsumerState<GoogleAds> {
155160
bool _isLoading = false;
156161
bool _isDisposed = false;
157162
bool _hasInitialized = false;
163+
static bool _globalAdInitialized = false;
158164

159165
final _adUnitId = AdHelper.adUnitId;
160166

@@ -163,12 +169,18 @@ class _GoogleAdsState extends ConsumerState<GoogleAds> {
163169
super.initState();
164170
debugPrint('GoogleAds widget initState called');
165171

166-
// Reset state when widget is created
167172
WidgetsBinding.instance.addPostFrameCallback((_) {
168-
if (!_isDisposed) {
169-
debugPrint('Resetting Google ads state...');
173+
if (!_isDisposed && !_globalAdInitialized) {
174+
debugPrint('First time initializing ads...');
175+
_globalAdInitialized = true;
170176
ref.read(googleAdsProvider.notifier).resetState();
171177
_initializeAds();
178+
} else if (!_isDisposed && _globalAdInitialized) {
179+
debugPrint('Ads already initialized, skipping...');
180+
final adsState = ref.read(googleAdsProvider);
181+
if (!adsState.nativeAdIsLoaded && !adsState.adLoadFailed) {
182+
_initializeAds();
183+
}
172184
}
173185
});
174186
}
@@ -317,7 +329,6 @@ class _GoogleAdsState extends ConsumerState<GoogleAds> {
317329
@override
318330
void dispose() {
319331
_isDisposed = true;
320-
_nativeAd?.dispose();
321332
super.dispose();
322333
}
323334

@@ -327,15 +338,22 @@ class _GoogleAdsState extends ConsumerState<GoogleAds> {
327338
final shouldShowGoogle = ref.watch(shouldShowGoogleAdsProvider);
328339
final customAdData = ref.watch(customAdDataProvider);
329340

330-
// Listen for disposal requests
331-
ref.listen(googleAdsProvider, (previous, next) {
332-
if (next.shouldDisposeAd && !_isDisposed) {
333-
_nativeAd?.dispose();
334-
_nativeAd = null;
335-
setState(() {
336-
_isLoading = false;
337-
});
341+
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+
}
338352
ref.read(googleAdsProvider.notifier).acknowledgeDisposal();
353+
_globalAdInitialized = false;
354+
}
355+
if (next.status == ConnectionStatus.connected) {
356+
ref.read(googleAdsProvider.notifier).resestCountDown();
339357
}
340358
});
341359
return SizedBox(

0 commit comments

Comments
 (0)