used stream on favorites page and fix build error

This commit is contained in:
jideguru
2022-08-25 14:22:15 +01:00
parent 30e1a11f96
commit 1f421f7c2d
4 changed files with 39 additions and 30 deletions

View File

@ -1,5 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.jideguru.flutter_ebook_app" xmlns:tools="schemas.android.com/tools">
package="dev.jideguru.flutter_ebook_app" xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />

View File

@ -33,6 +33,11 @@ class FavoriteDB {
return val;
}
Future<Stream<List>> listAllStream() async {
final db = ObjectDB(FileSystemStorage(await getPath()));
return db.find({}).asStream();
}
Future<List> check(Map item) async {
final db = ObjectDB(FileSystemStorage(await getPath()));
List val = await db.find(item);

View File

@ -1,30 +1,40 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter_ebook_app/database/favorite_helper.dart';
class FavoritesProvider extends ChangeNotifier {
List posts = [];
bool loading = true;
List favorites = [];
var db = FavoriteDB();
getFavorites() async {
setLoading(true);
posts.clear();
List all = await db.listAll();
posts.addAll(all);
setLoading(false);
StreamSubscription<List>? _streamSubscription;
Future<void> listen() async {
if (_streamSubscription != null) {
_streamSubscription!.cancel();
_streamSubscription = null;
}
_streamSubscription = (await db.listAllStream()).listen(
(books) => favorites = books,
);
}
void setLoading(value) {
loading = value;
@override
void dispose() {
if (_streamSubscription != null) {
_streamSubscription!.cancel();
_streamSubscription = null;
}
super.dispose();
}
Future<Stream<List>> getFavoritesStream() async {
Stream<List<dynamic>> all = await db.listAllStream();
return all;
}
void setFavorites(value) {
favorites = value;
notifyListeners();
}
void setPosts(value) {
posts = value;
notifyListeners();
}
List getPosts() {
return posts;
}
}

View File

@ -17,17 +17,11 @@ class _FavoritesState extends State<Favorites> {
getFavorites();
}
@override
void deactivate() {
super.deactivate();
getFavorites();
}
getFavorites() {
SchedulerBinding.instance.addPostFrameCallback(
(_) {
if (mounted) {
Provider.of<FavoritesProvider>(context, listen: false).getFavorites();
Provider.of<FavoritesProvider>(context, listen: false).listen();
}
},
);
@ -45,7 +39,7 @@ class _FavoritesState extends State<Favorites> {
'Favorites',
),
),
body: favoritesProvider.posts.isEmpty
body: favoritesProvider.favorites.isEmpty
? _buildEmptyListView()
: _buildGridView(favoritesProvider),
);
@ -79,13 +73,13 @@ class _FavoritesState extends State<Favorites> {
return GridView.builder(
padding: EdgeInsets.fromLTRB(10.0, 20.0, 10.0, 0.0),
shrinkWrap: true,
itemCount: favoritesProvider.posts.length,
itemCount: favoritesProvider.favorites.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
childAspectRatio: 200 / 340,
),
itemBuilder: (BuildContext context, int index) {
Entry entry = Entry.fromJson(favoritesProvider.posts[index]['item']);
Entry entry = Entry.fromJson(favoritesProvider.favorites[index]['item']);
return Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0),
child: BookItem(