Skip to content

Commit

Permalink
Ads update (#953)
Browse files Browse the repository at this point in the history
* Added artificial delay while connecting VPN.

* Remove static delay.

* fix typo and do not show delay if user does not have permissions.
  • Loading branch information
jigar-f authored Nov 30, 2023
1 parent 943ed4e commit 28f870b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .run/main.dart.run.xml → .run/prod.run.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="main.dart" type="FlutterRunConfigurationType" factoryName="Flutter">
<configuration default="false" name="prod" type="FlutterRunConfigurationType" factoryName="Flutter">
<option name="additionalArgs" value="--dart-define=INTERSTITIAL_AD_UNIT_ID=ca-app-pub-2685698271254859/9922829329 --dart-define=DD_APPLICATION_ID=f8eabf3c-5db3-4f7e-8e6a-5a72433b46d2 --dart-define=DD_CLIENT_TOKEN=puba617ab01333a95a25a9d3709f04e1654" />
<option name="buildFlavor" value="prod" />
<option name="filePath" value="$PROJECT_DIR$/lib/main.dart" />
Expand Down
4 changes: 4 additions & 0 deletions android/app/src/main/kotlin/io/lantern/model/VpnModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class VpnModel(
saveVpnStatus(if (on) "connecting" else "disconnecting")
switchLantern(on)
}
"connectingDelay" -> {
val on = call.argument<Boolean>("on") ?: false
saveVpnStatus(if (on) "connecting" else "disconnecting")
}
else -> super.doMethodCall(call, notImplemented)
}
}
Expand Down
24 changes: 17 additions & 7 deletions lib/ad_helper.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:io';

import 'package:clever_ads_solutions/CAS.dart';
import 'package:clever_ads_solutions/public/AdCallback.dart';
import 'package:clever_ads_solutions/public/AdImpression.dart';
Expand All @@ -8,11 +6,10 @@ import 'package:clever_ads_solutions/public/Audience.dart';
import 'package:clever_ads_solutions/public/InitConfig.dart';
import 'package:clever_ads_solutions/public/InitializationListener.dart';
import 'package:clever_ads_solutions/public/MediationManager.dart';
import 'package:flutter/foundation.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:logger/logger.dart';
import 'package:lantern/common/common.dart';
import 'package:lantern/replica/common.dart';
import 'package:logger/logger.dart';

enum AdType { Google, CAS }

Expand Down Expand Up @@ -79,6 +76,16 @@ class AdHelper {
}
}

Future<bool> isAdsReadyToShow() async {
if (_currentAdType == AdType.Google) {
return _interstitialAd != null;
} else if (_currentAdType == AdType.CAS) {
return (casMediationManager != null &&
(await casMediationManager!.isInterstitialReady()));
}
return false;
}

Future<void> _decideAndShowAds() async {
if (_currentAdType == AdType.Google && _interstitialAd != null) {
await _showInterstitialAd();
Expand All @@ -95,7 +102,8 @@ class AdHelper {

Future<void> _loadInterstitialAd() async {
//To avoid calling multiple ads request repeatedly
assert(interstitialAdUnitId!="","interstitialAdUnitId should not be null or empty");
assert(interstitialAdUnitId != "",
"interstitialAdUnitId should not be null or empty");
if (_interstitialAd == null && _failedLoadAttempts < _maxFailAttempts) {
logger.i('[Ads Manager] Request: Making Google Ad request.');
await InterstitialAd.load(
Expand Down Expand Up @@ -126,12 +134,14 @@ class AdHelper {
);
_interstitialAd = ad;
logger.i('[Ads Manager] to loaded $ad');
PlausibleUtils.trackUserAction('Interstitial ad loaded', googleAttributes);
PlausibleUtils.trackUserAction(
'Interstitial ad loaded', googleAttributes);
},
onAdFailedToLoad: (err) {
_failedLoadAttempts++; // increment the count on failure
logger.i('[Ads Manager] failed to load $err');
PlausibleUtils.trackUserAction('Interstitial ad failed to load', googleAttributes);
PlausibleUtils.trackUserAction(
'Interstitial ad failed to load', googleAttributes);
_postShowingAds();
},
),
Expand Down
8 changes: 8 additions & 0 deletions lib/vpn/vpn_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ class VpnModel extends Model {
});
}

//This method will create artificial delay in connecting VPN
// So we can show ads to user
Future<void> connectingDelay<T>(bool on) async {
return methodChannel.invokeMethod('connectingDelay', <String, dynamic>{
'on': on,
});
}

Widget vpnStatus(ValueWidgetBuilder<String> builder) {
return subscribedSingleValueBuilder<String>(
'/vpn_status',
Expand Down
29 changes: 18 additions & 11 deletions lib/vpn/vpn_switch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@ class VPNSwitch extends StatefulWidget {
class _VPNSwitchState extends State<VPNSwitch> {
final adHelper = AdHelper();

@override
void initState() {
super.initState();
}

bool isIdle(String vpnStatus) =>
vpnStatus != 'connecting' && vpnStatus != 'disconnecting';

Future<void> onSwitchTap(bool newValue, String vpnStatus) async {
Future<void> onSwitchTap(
bool newValue, String vpnStatus, bool userHasPermission) async {
unawaited(HapticFeedback.lightImpact());
//Make sure user has permission all the permission
//if ads is not ready then wait for at least 5 seconds and then show ads
//if ads is ready then show ads immediately

if (vpnStatus != 'connected' && userHasPermission) {
if (!await adHelper.isAdsReadyToShow()) {
await vpnModel.connectingDelay(newValue);
await Future.delayed(const Duration(seconds: 5));
}
}
if (isIdle(vpnStatus)) {
await vpnModel.switchVPN(newValue);
}
Expand Down Expand Up @@ -49,11 +55,12 @@ class _VPNSwitchState extends State<VPNSwitch> {
child: vpnModel.vpnStatus(
(BuildContext context, String vpnStatus, Widget? child) {
return FlutterSwitch(
value: vpnStatus == 'connected' || vpnStatus == 'disconnecting',
activeColor: onSwitchColor,
inactiveColor: offSwitchColor,
onToggle: (bool newValue) => onSwitchTap(newValue, vpnStatus),
);
value:
vpnStatus == 'connected' || vpnStatus == 'disconnecting',
activeColor: onSwitchColor,
inactiveColor: offSwitchColor,
onToggle: (bool newValue) => onSwitchTap(newValue, vpnStatus,
(isGoogleAdsEnable || isCasAdsEnable)));
}));
});
});
Expand Down

0 comments on commit 28f870b

Please sign in to comment.