Skip to content

Commit

Permalink
Added: Ad loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
christiankyle-ching committed Apr 5, 2021
1 parent 9acf403 commit 935518b
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/ad_manager.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'dart:io';

String testID = "ca-app-pub-3940256099942544~4354546703";
String testInterID = "ca-app-pub-3940256099942544/7049598008";

String appID = "ca-app-pub-1880918445827744~7516812574";
String appInterID = "ca-app-pub-1880918445827744/1357215445";
String appBannerID = "ca-app-pub-1880918445827744/5753973067";

class AdManager {
static String get appId {
if (Platform.isAndroid) {
return appID;
} else {
throw new UnsupportedError("Unsupported platform");
}
}

static String get interstitialAdUnitId {
if (Platform.isAndroid) {
return appInterID;
} else {
throw new UnsupportedError("Unsupported platform");
}
}

static String get bannerAdUnitId {
if (Platform.isAndroid) {
return appBannerID;
} else {
throw new UnsupportedError("Unsupported platform");
}
}
}
7 changes: 7 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:nasa_apod/screens/media_screen.dart';
import 'package:nasa_apod/tasks/notifications.dart';
import 'package:nasa_apod/tasks/tasks.dart';
import 'package:nasa_apod/theme/theme.dart';
import 'package:nasa_apod/ad_manager.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

Expand All @@ -26,6 +27,8 @@ void main() {
initializeBackgroundTasks();
initializeNotifications();

_initAdMob();

runApp(ChangeNotifierProvider(
create: (context) => ApodModel(), child: MainApp()));
}
Expand All @@ -49,6 +52,10 @@ class MainApp extends StatelessWidget {
}
}

Future<void> _initAdMob() {
return FirebaseAdMob.instance.initialize(appId: AdManager.appId);
}

// DEBUG: Generate mock values
void generateMockData() {
DateTime _now = DateTime.now();
Expand Down
71 changes: 71 additions & 0 deletions lib/screens/home_screen.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:io';

import 'package:firebase_admob/firebase_admob.dart';
import 'package:flutter/material.dart';
import 'package:nasa_apod/models/api.dart';
import 'package:nasa_apod/models/apod_model.dart';
Expand All @@ -10,6 +11,7 @@ import 'package:nasa_apod/utils/utils.dart';
import 'package:provider/provider.dart';
import 'package:wallpaper_manager/wallpaper_manager.dart';

import '../ad_manager.dart';
import 'detail_screen.dart';
import 'favorites_screen.dart';

Expand Down Expand Up @@ -228,10 +230,69 @@ class _AppScaffoldState extends State<AppScaffold> {

Future<Apod> _futureHighlightApod;

// InterstitialAd
InterstitialAd _interstitialAd;
bool _isInterstitialAdReady;

void _initInterstitialAd() {
_isInterstitialAdReady = false;

_interstitialAd = InterstitialAd(
adUnitId: AdManager.interstitialAdUnitId,
listener: _onInterstitialAdEvent,
);
}

void _loadInterstitialAd() {
_interstitialAd.load();
}

void _onInterstitialAdEvent(MobileAdEvent event) {
switch (event) {
case MobileAdEvent.loaded:
_isInterstitialAdReady = true;
break;
case MobileAdEvent.failedToLoad:
_isInterstitialAdReady = false;
print('Failed to load an interstitial ad');
break;
case MobileAdEvent.closed:
// _loadInterstitialAd();
// _isInterstitialAdReady = false;
break;
default:
// do nothing
}
}

// BannerAd
BannerAd _bannerAd;

void _initBannerAd() {
_bannerAd = BannerAd(
adUnitId: AdManager.bannerAdUnitId,
size: AdSize.banner,
);

_loadBannerAd();
}

void _loadBannerAd() {
_bannerAd
..load()
..show(anchorType: AnchorType.bottom);
}

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

// TODO: Interstitial Ads - can be activated in the future
_initInterstitialAd();
// _loadInterstitialAd();

_initBannerAd();

// Init Pages
try {
_futureHighlightApod = ApodApi.fetchApodByDate(DateTime.now());
Expand All @@ -256,6 +317,8 @@ class _AppScaffoldState extends State<AppScaffold> {
}

void _onItemTapped(int index) {
if (_isInterstitialAdReady) _interstitialAd.show();

setState(() {
_currentTab = index;
});
Expand Down Expand Up @@ -359,6 +422,14 @@ class _AppScaffoldState extends State<AppScaffold> {
),
);
}

@override
void dispose() {
_interstitialAd.dispose();
_bannerAd.dispose();

super.dispose();
}
}

class DrawerSectionTitle extends StatelessWidget {
Expand Down

0 comments on commit 935518b

Please sign in to comment.