mirror of
https://github.com/JideGuru/FlutterEbookApp.git
synced 2025-08-24 09:12:36 +08:00
used stream on favorites page and fix build error
This commit is contained in:
@ -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" />
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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(
|
||||
|
Reference in New Issue
Block a user