Skip to content

Commit

Permalink
added books
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamsinha2009 committed Jun 5, 2022
1 parent 13a34b1 commit 7e55e37
Show file tree
Hide file tree
Showing 23 changed files with 1,356 additions and 34 deletions.
5 changes: 4 additions & 1 deletion .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ libraryBanner=ca-app-pub-3940256099942544/6300978111
encryptedBanner=ca-app-pub-3940256099942544/6300978111
bottomBanner=ca-app-pub-3940256099942544/6300978111
settingsBanner=ca-app-pub-3940256099942544/6300978111
viewPdfBanner=ca-app-pub-3940256099942544/2934735716
viewPdfBanner=ca-app-pub-3940256099942544/2934735716
bookBanner=ca-app-pub-3940256099942544/6300978111
bookBottom=ca-app-pub-3940256099942544/6300978111
viewBookPdf=ca-app-pub-3940256099942544/1033173712
12 changes: 12 additions & 0 deletions lib/app/core/helpers/ad_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,16 @@ class AdHelper {
static String get viewPdfBanner {
return dotenv.get('viewPdfBanner');
}

static String get bookBanner {
return dotenv.get('bookBanner');
}

static String get viewBookPdf {
return dotenv.get('viewBookPdf');
}

static String get bookBottom {
return dotenv.get('bookBottom');
}
}
23 changes: 23 additions & 0 deletions lib/app/data/model/book_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Book {
final String bookName;
final int classNumber;
// final String thumbnailLink;
// final List<String> telegramLink;
// final List<String> gdriveLink;
// final int views;
final List<String> chapterNames;
final List<String> ncertDirectLinks;
// final String amazonBuyLink;

Book({
required this.bookName,
required this.classNumber,
//required this.thumbnailLink,
// required this.telegramLink,
// required this.gdriveLink,
// required this.views,
required this.chapterNames,
required this.ncertDirectLinks,
// required this.amazonBuyLink,
});
}
7 changes: 7 additions & 0 deletions lib/app/data/model/dashboard_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class DashboardModel {
final List<String> dashboardList;

DashboardModel({
required this.dashboardList,
});
}
66 changes: 34 additions & 32 deletions lib/app/data/provider/firestore_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:filegram/app/data/model/views_model.dart';
import '../enums/docpermission.dart';
import 'package:flutter/services.dart';
import '../model/book_model.dart';
import '../model/dashboard_model.dart';
import '../model/documents_model.dart';
import '../model/gullak_model.dart';
import '../model/user_model.dart';
Expand Down Expand Up @@ -169,24 +171,24 @@ class FirestoreData {
}
}

// static Future<DashboardModel> getDashboard({
// required String collection,
// required String uid,
// required String listName,
// }) async {
// try {
// DocumentSnapshot _doc =
// await _firestore.collection(collection).doc(uid).get();
static Future<DashboardModel> getDashboard({
required String collection,
required String uid,
required String listName,
}) async {
try {
DocumentSnapshot _doc =
await _firestore.collection(collection).doc(uid).get();

// Map<String, dynamic> _data = _doc.data() as Map<String, dynamic>;
Map<String, dynamic> _data = _doc.data() as Map<String, dynamic>;

// return DashboardModel(
// dashboardList: List<String>.from(_data[listName]),
// );
// } catch (e) {
// rethrow;
// }
// }
return DashboardModel(
dashboardList: List<String>.from(_data[listName]),
);
} catch (e) {
rethrow;
}
}

static Future<void> updatePhoneUser(
{required String id, required String phoneNumber}) async {
Expand Down Expand Up @@ -340,24 +342,24 @@ class FirestoreData {
}
}

// static Future<Book> getBook(String uid) async {
// try {
// DocumentSnapshot _doc =
// await _firestore.collection("books").doc(uid).get();
static Future<Book> getBook(String uid) async {
try {
DocumentSnapshot _doc =
await _firestore.collection("books").doc(uid).get();

// Map<String, dynamic> _data = _doc.data() as Map<String, dynamic>;
Map<String, dynamic> _data = _doc.data() as Map<String, dynamic>;

// return Book(
// bookName: _data["bookName"] as String,
// chapterNames: List<String>.from(_data["chapterNames"]),
// ncertDirectLinks: List<String>.from(_data["ncertLinks"]),
// classNumber: _data["classNumber"] as int,
// // gdriveLink:
// );
// } catch (e) {
// rethrow;
// }
// }
return Book(
bookName: _data["bookName"] as String,
chapterNames: List<String>.from(_data["chapterNames"]),
ncertDirectLinks: List<String>.from(_data["ncertLinks"]),
classNumber: _data["classNumber"] as int,
// gdriveLink:
);
} catch (e) {
rethrow;
}
}
// Future<UserModel> getUser(String uid) async {
// try {
// DocumentSnapshot _doc = await _firestore
Expand Down
12 changes: 12 additions & 0 deletions lib/app/modules/book/bindings/book_binding.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:get/get.dart';

import '../controllers/book_controller.dart';

class BookBinding extends Bindings {
@override
void dependencies() {
Get.lazyPut<BookController>(
() => BookController(),
);
}
}
229 changes: 229 additions & 0 deletions lib/app/modules/book/controllers/book_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
import 'dart:io';

import 'package:filegram/app/core/extensions.dart';
import 'package:get/get.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';
import '../../../core/helpers/ad_helper.dart';
import '../../../data/model/book_model.dart';
import '../../../data/provider/firestore_data.dart';
import '../../home/controllers/home_controller.dart';

class BookController extends GetxController {
final homeController = Get.find<HomeController>();
final isLoding = true.obs;
Book book = Book(
bookName: '', classNumber: 0, chapterNames: [], ncertDirectLinks: []);
final total = 1.obs;
final received = 0.obs;
late http.StreamedResponse response;
final List<int> _bytes = [];
InterstitialAd? interstitialAd;
final int maxFailedLoadAttempts = 3;
int interstitialLoadAttempts = 0;
final adDismissed = false.obs;
final inlineAdIndex = 0;
late BannerAd inlineBannerAd;
final isInlineBannerAdLoaded = false.obs;
final isBottomBannerAdLoaded = false.obs;
late BannerAd bottomBannerAd;
String get uid =>
((Get.arguments[0] as String) + (Get.arguments[1] as String)).sort;

Future<void> getdetails(String url) async {
try {
response = await http.Client().send(http.Request('GET', Uri.parse(url)));
total.value = response.contentLength ?? 0;
received.value = 0;
} catch (e) {
Get.showSnackbar(GetSnackBar(
backgroundColor: Get.theme.snackBarTheme.backgroundColor!,
title: 'Error',
message: e.toString(),
duration: const Duration(seconds: 5),
));
}
}

Future<void> downloadFile(String filePath) async {
final _file = File(filePath);
try {
response.stream.listen((value) {
_bytes.addAll(value);
received.value += value.length;
}).onDone(() async {
await _file.writeAsBytes(_bytes);
});
} catch (e) {
Get.showSnackbar(GetSnackBar(
backgroundColor: Get.theme.snackBarTheme.backgroundColor!,
title: 'Error',
message: e.toString(),
duration: const Duration(seconds: 5),
));
}
}

void _createBottomBannerAd() {
bottomBannerAd = BannerAd(
adUnitId: AdHelper.bookBottom,
size: AdSize.banner,
request: const AdRequest(),
listener: BannerAdListener(
onAdLoaded: (_) {
isBottomBannerAdLoaded.value = true;
},
onAdFailedToLoad: (ad, error) {
ad.dispose();
},
),
);
if (isBottomBannerAdLoaded.isFalse) {
bottomBannerAd.load();
}
}

Future<void> createInterstitialAd() async {
try {
await InterstitialAd.load(
adUnitId: AdHelper.viewBookPdf,
request: const AdRequest(),
adLoadCallback: InterstitialAdLoadCallback(
onAdLoaded: (InterstitialAd ad) {
interstitialAd = ad;
interstitialLoadAttempts = 0;
},
onAdFailedToLoad: (LoadAdError error) {
interstitialLoadAttempts += 1;
interstitialAd = null;
if (interstitialLoadAttempts <= maxFailedLoadAttempts) {
createInterstitialAd();
}
},
),
);
} on Exception catch (e) {
Get.showSnackbar(GetSnackBar(
backgroundColor: Get.theme.snackBarTheme.backgroundColor!,
title: 'Error',
message: e.toString(),
duration: const Duration(seconds: 5),
));
}
}

AdWidget adWidget({required AdWithView ad}) {
return AdWidget(ad: ad);
}

Future<void> showInterstitialAd({String? uid}) async {
try {
if (interstitialAd != null) {
interstitialAd!.fullScreenContentCallback = FullScreenContentCallback(
onAdDismissedFullScreenContent: (InterstitialAd ad) {
ad.dispose();
adDismissed.value = true;
createInterstitialAd();
},
onAdFailedToShowFullScreenContent:
(InterstitialAd ad, AdError error) {
ad.dispose();
createInterstitialAd();
},
onAdShowedFullScreenContent: (InterstitialAd ad) {});
if (interstitialAd != null) {
interstitialAd!.show();
}
}
} on Exception catch (e) {
Get.showSnackbar(GetSnackBar(
backgroundColor: Get.theme.snackBarTheme.backgroundColor!,
title: 'Error',
message: e.toString(),
duration: const Duration(seconds: 5),
));
}
}

int getListViewItemIndex(int index) {
if (index >= inlineAdIndex &&
isInlineBannerAdLoaded.isTrue &&
(book.chapterNames.length >= inlineAdIndex)) {
return index - 1;
}
return index;
}

void _createInlineBannerAd() {
inlineBannerAd = BannerAd(
size: AdSize.mediumRectangle,
adUnitId: AdHelper.bookBanner,
request: const AdRequest(),
listener: BannerAdListener(
onAdLoaded: (_) {
isInlineBannerAdLoaded.value = true;
},
onAdFailedToLoad: (ad, error) {
ad.dispose();
},
),
);

inlineBannerAd.load();
}

Future<String> filesDocDir() async {
//Get this App Document Directory
//App Document Directory + folder name

final Directory? _appDocDir = await getExternalStorageDirectory();
//App Document Directory + folder name
final Directory _appDocDirFolder =
Directory('${_appDocDir?.path}/Downloads');

if (await _appDocDirFolder.exists()) {
//if folder already exists return path
return _appDocDirFolder.path;
} else {
//if folder not exists create folder and then return its path
final Directory _appDocDirNewFolder =
await _appDocDirFolder.create(recursive: true);
return _appDocDirNewFolder.path;
}
}

@override
void onInit() {
FirestoreData.getBook(uid)
.then((value) {
book = value;
})
.whenComplete(() => isLoding.value = false)
.catchError((e) {
Get.showSnackbar(GetSnackBar(
backgroundColor: Get.theme.snackBarTheme.backgroundColor!,
title: 'Error',
message: e.toString(),
duration: const Duration(seconds: 5),
));
});
if (isInlineBannerAdLoaded.isFalse) {
_createInlineBannerAd();
}
createInterstitialAd();

_createBottomBannerAd();

super.onInit();
}

@override
void onClose() {
interstitialAd?.dispose();
inlineBannerAd.dispose();
bottomBannerAd.dispose();

super.onClose();
}
}
Loading

0 comments on commit 7e55e37

Please sign in to comment.