Skip to content

Commit

Permalink
refactor: Remove unused packages
Browse files Browse the repository at this point in the history
  • Loading branch information
christiankyle-ching committed Sep 1, 2021
1 parent 935518b commit d609307
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 141 deletions.
34 changes: 0 additions & 34 deletions lib/ad_manager.dart

This file was deleted.

8 changes: 0 additions & 8 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';

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

_initAdMob();

runApp(ChangeNotifierProvider(
create: (context) => ApodModel(), child: MainApp()));
}
Expand All @@ -52,10 +48,6 @@ 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
73 changes: 0 additions & 73 deletions lib/screens/home_screen.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';

Expand Down Expand Up @@ -230,69 +226,10 @@ 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 @@ -317,8 +254,6 @@ class _AppScaffoldState extends State<AppScaffold> {
}

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

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

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

super.dispose();
}
}

class DrawerSectionTitle extends StatelessWidget {
Expand Down
81 changes: 57 additions & 24 deletions lib/tasks/wallpaper_task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,33 +93,66 @@ Future<void> 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;
}
Expand Down
16 changes: 14 additions & 2 deletions lib/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d609307

Please sign in to comment.