diff --git a/lib/ad_manager.dart b/lib/ad_manager.dart new file mode 100644 index 0000000..2a46ab5 --- /dev/null +++ b/lib/ad_manager.dart @@ -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"); + } + } +} diff --git a/lib/main.dart b/lib/main.dart index 8975f7c..9955e04 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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'; @@ -26,6 +27,8 @@ void main() { initializeBackgroundTasks(); initializeNotifications(); + _initAdMob(); + runApp(ChangeNotifierProvider( create: (context) => ApodModel(), child: MainApp())); } @@ -49,6 +52,10 @@ class MainApp extends StatelessWidget { } } +Future _initAdMob() { + return FirebaseAdMob.instance.initialize(appId: AdManager.appId); +} + // DEBUG: Generate mock values void generateMockData() { DateTime _now = DateTime.now(); diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart index be13411..b07ad4e 100644 --- a/lib/screens/home_screen.dart +++ b/lib/screens/home_screen.dart @@ -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'; @@ -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'; @@ -228,10 +230,69 @@ class _AppScaffoldState extends State { Future _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()); @@ -256,6 +317,8 @@ class _AppScaffoldState extends State { } void _onItemTapped(int index) { + if (_isInterstitialAdReady) _interstitialAd.show(); + setState(() { _currentTab = index; }); @@ -359,6 +422,14 @@ class _AppScaffoldState extends State { ), ); } + + @override + void dispose() { + _interstitialAd.dispose(); + _bannerAd.dispose(); + + super.dispose(); + } } class DrawerSectionTitle extends StatelessWidget {