Skip to content

Commit

Permalink
update firestore data fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
LiquidatorCoder committed Oct 26, 2021
1 parent e91ef22 commit a8fe954
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 67 deletions.
16 changes: 16 additions & 0 deletions lib/notifiers/firestore_notifier.dart
Expand Up @@ -7,9 +7,25 @@ class FirestoreNotifier with ChangeNotifier {
final FirestoreService _firestoreService = locator<FirestoreService>();
Stream<List<Notice>>? get noticesStream => _firestoreService.noticesStream;
int get limit => _firestoreService.limit;
bool get priorityCheck => _firestoreService.priorityCheck;

set limit(int value) {
_firestoreService.limit = value;
notifyListeners();
}

void initNoticeStream() {
_firestoreService.initNoticeStream(false);
notifyListeners();
}

void loadMore() {
_firestoreService.loadMore();
notifyListeners();
}

void togglePriorityCheck() {
_firestoreService.togglePriorityCheck();
notifyListeners();
}
}
71 changes: 37 additions & 34 deletions lib/pages/home_page.dart
@@ -1,8 +1,12 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ip_notices/models/notice.dart';
import 'package:ip_notices/notifiers/firestore_notifier.dart';
import 'package:ip_notices/services/logger.dart';
import 'package:ip_notices/widgets/about_button.dart';
import 'package:ip_notices/widgets/notice_tile.dart';
import 'package:ip_notices/widgets/theme_switch.dart';
import 'package:provider/provider.dart';

class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
Expand All @@ -12,41 +16,37 @@ class HomePage extends StatefulWidget {
}

class _HomePageState extends State<HomePage> {
bool priorityCheck = false;
int limit = 8;
bool loading = false;
ScrollController controller = ScrollController();
@override
void initState() {
super.initState();
Future.delayed(const Duration())
.then((value) => context.read<FirestoreNotifier>().initNoticeStream());
controller.addListener(_scrollListener);
}

void _scrollListener() {
if (controller.position.pixels == controller.position.maxScrollExtent) {
print("at the end of list");
logger.d("at the end of list");
setState(() {
limit = limit + 8;
context.read<FirestoreNotifier>().loadMore();
});
print(limit);
logger.i(context.read<FirestoreNotifier>().limit);
}
}

@override
Widget build(BuildContext context) {
Query notices = FirebaseFirestore.instance
.collection('notices')
.orderBy('createdAt', descending: true)
.limit(limit);
return CupertinoPageScaffold(
backgroundColor: Colors.white,
child: CustomScrollView(
physics: const BouncingScrollPhysics(),
controller: controller,
slivers: [
CupertinoSliverNavigationBar(
const CupertinoSliverNavigationBar(
leading: AboutButton(),
// trailing: ThemeSwitchButton(),
trailing: ThemeSwitchButton(),
border: null,
automaticallyImplyLeading: false,
padding: EdgeInsetsDirectional.zero,
Expand All @@ -56,13 +56,11 @@ class _HomePageState extends State<HomePage> {
color: Colors.black,
),
),
backgroundColor: Colors.white,
backgroundColor: Colors.transparent,
),
CupertinoSliverRefreshControl(
onRefresh: () {
setState(() {
limit = 8;
});
context.read<FirestoreNotifier>().initNoticeStream();
return Future<void>.delayed(const Duration(seconds: 1))
..then<void>((_) {});
},
Expand All @@ -77,28 +75,32 @@ class _HomePageState extends State<HomePage> {
child: Row(
children: [
Text(
priorityCheck ? "Priority" : "Latest",
context.watch<FirestoreNotifier>().priorityCheck
? "Priority"
: "Latest",
style: TextStyle(
color: Colors.black.withOpacity(0.8),
fontWeight: FontWeight.w600,
fontSize: 20,
),
),
Spacer(),
const Spacer(),
ClipOval(
child: Material(
elevation: 0,
color: Colors.transparent,
borderRadius: BorderRadius.circular(500),
child: IconButton(
icon: priorityCheck
? Icon(CupertinoIcons.star)
: Icon(CupertinoIcons.time),
icon: context
.watch<FirestoreNotifier>()
.priorityCheck
? const Icon(CupertinoIcons.star)
: const Icon(CupertinoIcons.time),
color: Colors.black.withOpacity(0.8),
onPressed: () {
setState(() {
priorityCheck = !priorityCheck;
});
context
.read<FirestoreNotifier>()
.togglePriorityCheck();
},
),
),
Expand All @@ -111,16 +113,17 @@ class _HomePageState extends State<HomePage> {
childCount: 1,
),
),
StreamBuilder<QuerySnapshot>(
stream: notices.snapshots(),
StreamBuilder<List<Notice>>(
stream: context.watch<FirestoreNotifier>().noticesStream,
builder:
(BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
(BuildContext context, AsyncSnapshot<List<Notice>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
loading = true;
} else {
loading = false;
}
if (snapshot.hasError) {
logger.e(snapshot.error);
return SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
Expand All @@ -146,7 +149,7 @@ class _HomePageState extends State<HomePage> {
return SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
if (index == snapshot.data?.docs.length) {
if (index == snapshot.data?.length) {
return SizedBox(
height: 100,
width: 100,
Expand All @@ -159,27 +162,27 @@ class _HomePageState extends State<HomePage> {
: Container()),
);
}
final bool download = snapshot.data?.docs[index]['url']
final bool download = snapshot.data?[index].url
.toString()
.toLowerCase()
.contains(".pdf") ??
false;
if (priorityCheck) {
if (snapshot.data?.docs[index]['priority']) {
if (context.watch<FirestoreNotifier>().priorityCheck) {
if (snapshot.data?[index].priority ?? false) {
return NoticeTile(
download: download,
document: snapshot.data?.docs[index],
document: snapshot.data?[index],
);
}
} else {
return NoticeTile(
download: download,
document: snapshot.data?.docs[index],
document: snapshot.data?[index],
);
}
return Container();
},
childCount: snapshot.data?.docs.length ?? 0 + 1,
childCount: snapshot.data?.length ?? 0 + 1,
),
);
}
Expand Down
41 changes: 31 additions & 10 deletions lib/services/firestore_service.dart
@@ -1,22 +1,43 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:ip_notices/models/notice.dart';
import 'package:ip_notices/services/logger.dart';

class FirestoreService {
FirebaseFirestore firestore = FirebaseFirestore.instance;
Stream<List<Notice>>? noticesStream;
int limit = 10;
bool priorityCheck = false;

FirestoreService() {
initNoticeStream();
void initNoticeStream(bool more) {
logger.i("Init");
if (!more) {
limit = 10;
}
noticesStream = priorityCheck
? FirebaseFirestore.instance
.collection('notices')
.orderBy('createdAt', descending: true)
.where('priority', isEqualTo: true)
.limit(limit)
.snapshots()
.map((event) =>
event.docs.map((doc) => Notice.fromJson(doc.data())).toList())
: FirebaseFirestore.instance
.collection('notices')
.orderBy('createdAt', descending: true)
.limit(limit)
.snapshots()
.map((event) =>
event.docs.map((doc) => Notice.fromJson(doc.data())).toList());
}

void initNoticeStream() {
noticesStream = FirebaseFirestore.instance
.collection('notices')
.orderBy('createdAt', descending: true)
.limit(limit)
.snapshots()
.map((event) =>
event.docs.map((doc) => Notice.fromJson(doc.data())).toList());
void loadMore() {
limit = limit + 10;
initNoticeStream(true);
}

void togglePriorityCheck() {
priorityCheck = !priorityCheck;
initNoticeStream(false);
}
}
38 changes: 15 additions & 23 deletions lib/widgets/notice_tile.dart
@@ -1,11 +1,11 @@
import 'dart:io';
import 'dart:math';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:ip_notices/main.dart';
import 'package:ip_notices/models/notice.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:share_plus/share_plus.dart';
Expand All @@ -18,7 +18,7 @@ class NoticeTile extends StatelessWidget {
required this.download,
}) : super(key: key);

final DocumentSnapshot? document;
final Notice? document;
final bool download;

_launchURL(String url) async {
Expand Down Expand Up @@ -69,7 +69,7 @@ class NoticeTile extends StatelessWidget {
color: Colors.red[400], size: 30.0),
),
title: Text(
(document?.data() as Map)["title"],
(document?.title ?? ''),
style: TextStyle(
color: prefs.get('theme') == 0
? Color(0xFF333333)
Expand All @@ -90,7 +90,7 @@ class NoticeTile extends StatelessWidget {
Padding(
padding: const EdgeInsets.only(top: 2.0),
child: Text(
" ${(document?.data() as Map)["date"]}",
" ${document?.date}",
style: TextStyle(
color: prefs.get('theme') == 0
? Color(0xFF333333).withOpacity(0.8)
Expand All @@ -114,8 +114,7 @@ class NoticeTile extends StatelessWidget {
trailingIcon: CupertinoIcons.doc_text,
onPressed: () {
Navigator.pop(context);
String link =
"http://www.ipu.ac.in${(document?.data() as Map)["url"]}";
String link = "http://www.ipu.ac.in${document?.url}";
_launchURL(link);
},
),
Expand All @@ -128,8 +127,7 @@ class NoticeTile extends StatelessWidget {
if (!status.isGranted) {
await Permission.storage.request();
}
String link =
"http://www.ipu.ac.in${(document?.data() as Map)["url"]}";
String link = "http://www.ipu.ac.in${document?.url}";
String _localPath = (await _findLocalPath()) + '/Notices';
final savedDir = Directory(_localPath);
bool hasExisted = await savedDir.exists();
Expand All @@ -144,7 +142,7 @@ class NoticeTile extends StatelessWidget {
final taskId = await FlutterDownloader.enqueue(
url: link,
fileName:
'${(document?.data() as Map)["title"].toString().replaceAll("/", "")} $name.pdf',
'${document?.title.toString().replaceAll("/", "")} $name.pdf',
savedDir: _localPath,
showNotification: true,
openFileFromNotification: true,
Expand All @@ -157,10 +155,8 @@ class NoticeTile extends StatelessWidget {
trailingIcon: CupertinoIcons.share,
onPressed: () {
Navigator.pop(context);
String link =
"http://www.ipu.ac.in${(document?.data() as Map)["url"]}";
Share.share(
"$link\n${(document?.data() as Map)["title"]}");
String link = "http://www.ipu.ac.in${document?.url}";
Share.share("$link\n${document?.title}");
},
)
]
Expand All @@ -171,8 +167,7 @@ class NoticeTile extends StatelessWidget {
trailingIcon: CupertinoIcons.doc_text,
onPressed: () {
Navigator.pop(context);
String link =
"http://www.ipu.ac.in${(document?.data() as Map)["url"]}";
String link = "http://www.ipu.ac.in${document?.url}";
_launchURL(link);
},
),
Expand All @@ -181,10 +176,8 @@ class NoticeTile extends StatelessWidget {
trailingIcon: CupertinoIcons.share,
onPressed: () {
Navigator.pop(context);
String link =
"http://www.ipu.ac.in${(document?.data() as Map)["url"]}";
Share.share(
"$link\n${(document?.data() as Map)["title"]}");
String link = "http://www.ipu.ac.in${document?.url}";
Share.share("$link\n${document?.title}");
},
)
],
Expand Down Expand Up @@ -215,8 +208,7 @@ class NoticeTile extends StatelessWidget {
debugPrint("Long Press");
},
onTap: () {
String link =
"http://www.ipu.ac.in${(document?.data() as Map)["url"]}";
String link = "http://www.ipu.ac.in${document?.url}";
_launchURL(link);
},
contentPadding: EdgeInsets.fromLTRB(20, 10, 20, 10),
Expand All @@ -231,7 +223,7 @@ class NoticeTile extends StatelessWidget {
color: Colors.red[400], size: 30.0),
),
title: Text(
(document?.data() as Map)["title"],
document?.title ?? '',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w600,
Expand All @@ -248,7 +240,7 @@ class NoticeTile extends StatelessWidget {
Padding(
padding: const EdgeInsets.only(top: 2.0),
child: Text(
" ${(document?.data() as Map)["date"]}",
" ${document?.date}",
style: TextStyle(
color: Colors.black.withOpacity(0.8),
fontSize: 12,
Expand Down

0 comments on commit a8fe954

Please sign in to comment.