From d60930752329992d649c8ebf598301926720ae52 Mon Sep 17 00:00:00 2001 From: Christian Kyle <57316283+christiankyle-ching@users.noreply.github.com> Date: Wed, 1 Sep 2021 08:44:36 +0800 Subject: [PATCH] refactor: Remove unused packages --- lib/ad_manager.dart | 34 --------------- lib/main.dart | 8 ---- lib/screens/home_screen.dart | 73 ------------------------------- lib/tasks/wallpaper_task.dart | 81 ++++++++++++++++++++++++----------- lib/utils/utils.dart | 16 ++++++- 5 files changed, 71 insertions(+), 141 deletions(-) delete mode 100644 lib/ad_manager.dart diff --git a/lib/ad_manager.dart b/lib/ad_manager.dart deleted file mode 100644 index 2a46ab5..0000000 --- a/lib/ad_manager.dart +++ /dev/null @@ -1,34 +0,0 @@ -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 9955e04..e330444 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,3 @@ -import 'package:firebase_admob/firebase_admob.dart'; import 'package:firebase_analytics/firebase_analytics.dart'; import 'package:firebase_analytics/observer.dart'; import 'package:flutter/material.dart'; @@ -8,7 +7,6 @@ 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'; @@ -27,8 +25,6 @@ void main() { initializeBackgroundTasks(); initializeNotifications(); - _initAdMob(); - runApp(ChangeNotifierProvider( create: (context) => ApodModel(), child: MainApp())); } @@ -52,10 +48,6 @@ 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 b07ad4e..29cd70b 100644 --- a/lib/screens/home_screen.dart +++ b/lib/screens/home_screen.dart @@ -1,6 +1,4 @@ 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,8 +8,6 @@ import 'package:nasa_apod/theme/theme.dart'; 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'; @@ -230,69 +226,10 @@ 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()); @@ -317,8 +254,6 @@ class _AppScaffoldState extends State { } void _onItemTapped(int index) { - if (_isInterstitialAdReady) _interstitialAd.show(); - setState(() { _currentTab = index; }); @@ -422,14 +357,6 @@ class _AppScaffoldState extends State { ), ); } - - @override - void dispose() { - _interstitialAd.dispose(); - _bannerAd.dispose(); - - super.dispose(); - } } class DrawerSectionTitle extends StatelessWidget { diff --git a/lib/tasks/wallpaper_task.dart b/lib/tasks/wallpaper_task.dart index ea3ab0b..42b17a0 100644 --- a/lib/tasks/wallpaper_task.dart +++ b/lib/tasks/wallpaper_task.dart @@ -93,33 +93,66 @@ Future changeWallpaper( (await AppStorage.getHdSetting()) ? apod.hdurl : apod.url; String fileDir = await cacheImage(downloadUrl, WALLPAPER_CACHE_FILENAME); - // Crop image + /** + * Crop Image + * + * Notes: Current bug on wallpaper_manager when device is in landscape. + */ + + // Get File File imageFile = File(fileDir); var decodedImage = await decodeImageFromList(imageFile.readAsBytesSync()); - int widthMid = (decodedImage.width / 2).floor(); - int widthOffset = ((decodedImage.height * screenRatio) / 2).floor(); - - int top = 0; - int bottom = decodedImage.height; - int left = widthMid - widthOffset; - int right = widthMid + widthOffset; - - // DEBUG - print('Screen Ratio: $screenRatio'); - print('Image Height: ${decodedImage.height}'); - print('Image Width: ${decodedImage.width}'); - print('Image Middle: $widthMid'); - print('Top: $top'); - print('Bottom: $bottom'); - print('Left: $left'); - print('Right: $right'); - - print(imageFile); - - // Set Wallpaper - await WallpaperManager.setWallpaperFromFileWithCrop( - fileDir, wallpaperLocation, left, top, right, bottom); + bool isImageLandscape = decodedImage.width >= + decodedImage.height; // Note: Same logic when image is square, hence >= + + // Different logic based on downloaded image orientation to prevent exceeding max values of crop + int top, bottom, left, right; + + if (isImageLandscape) { + // Get Width Midpoint of image + int widthMid = (decodedImage.width / 2).floor(); + // Offset from center of image in x (width) + int widthOffset = ((decodedImage.height * screenRatio) / 2).floor(); + + // Use max of height + top = 0; + bottom = decodedImage.height; + // Then adjust width to adapt screen ratio + left = widthMid - widthOffset; + right = widthMid + widthOffset; + } else { + // Get Height Midpoint of image + int heightMid = (decodedImage.height / 2).floor(); + // Offset from center of image in x (width) + int heightOffset = ((decodedImage.width * screenRatio) / 2).floor(); + + // Use max of width + left = 0; + right = decodedImage.width; + // Then adjust height to adapt screen ratio + top = heightMid - heightOffset; + bottom = heightMid + heightOffset; + } + + /* DEBUG */ + // print('Screen Ratio: $screenRatio'); + // print('Image in Landscape: $isImageLandscape'); + // print('Image Width: ${decodedImage.width}'); + // print('Image Height: ${decodedImage.height}'); + // print('Top: $top'); + // print('Bottom: $bottom'); + // print('Left: $left'); + // print('Right: $right'); + // print(imageFile); + + // Set Wallpaper, crop if supplied with values + if (left != null && top != null && right != null && bottom != null) { + await WallpaperManager.setWallpaperFromFileWithCrop( + fileDir, wallpaperLocation, left, top, right, bottom); + } else { + await WallpaperManager.setWallpaperFromFile(fileDir, wallpaperLocation); + } } catch (_) { rethrow; } diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart index 7024ef3..497a595 100644 --- a/lib/utils/utils.dart +++ b/lib/utils/utils.dart @@ -60,8 +60,20 @@ bool isDateWithinRange(DateTimeRange range, DateTime date) { } double getScreenRatio(BuildContext context) { - return MediaQuery.of(context).size.width.floor() / - MediaQuery.of(context).size.height.floor(); + int screenWidth = MediaQuery.of(context).size.width.floor(); + int screenHeight = MediaQuery.of(context).size.height.floor(); + + /** + * FIXME: Better logic + * + * Note: Screen Ratio = Width / Height + * + * If screen is in landscape, flip calculation + * Screen ratio is always calculated by portrait to ensure phone compatability + */ + return (screenWidth > screenHeight) + ? screenHeight / screenWidth + : screenWidth / screenHeight; } // Widgets