summaryrefslogtreecommitdiff
path: root/lib/screens/favorites_screen.dart
diff options
context:
space:
mode:
Diffstat (limited to 'lib/screens/favorites_screen.dart')
-rw-r--r--lib/screens/favorites_screen.dart96
1 files changed, 37 insertions, 59 deletions
diff --git a/lib/screens/favorites_screen.dart b/lib/screens/favorites_screen.dart
index 74432a7..132b61f 100644
--- a/lib/screens/favorites_screen.dart
+++ b/lib/screens/favorites_screen.dart
@@ -1,5 +1,8 @@
import 'package:flutter/material.dart';
-import 'package:pmsna1/firebase/favorites.dart';
+import 'package:pmsna1/database/helper.dart';
+
+import '../widgets/loading_modal_widget.dart';
+import '../widgets/popular_list.dart';
class FavoritesScreen extends StatefulWidget {
const FavoritesScreen({super.key});
@@ -9,73 +12,48 @@ class FavoritesScreen extends StatefulWidget {
}
class _FavoritesScreenState extends State<FavoritesScreen> {
- final FavoritesFirebase _firebase = FavoritesFirebase();
+ late DatabaseHelper _db;
+
+ @override
+ void initState() {
+ super.initState();
+ _db = DatabaseHelper();
+ }
@override
Widget build(BuildContext context) {
return Scaffold(
- body: StreamBuilder(
- stream: _firebase.getAllFavorites(),
+ appBar: AppBar(title: const Text('Favoritos')),
+ body: FutureBuilder(
+ future: _db.getAllFavorites(),
builder: (context, snapshot) {
if (snapshot.hasData) {
- return ListView.builder(
- itemCount: snapshot.data!.size,
- itemBuilder: (context, index) => ListTile(
- title: Text(snapshot.data!.docs[index].get('title')),
- trailing: Row(
- mainAxisSize: MainAxisSize.min,
- children: [
- IconButton(
- onPressed: () {
- // _firebase.insertFavorite({
- // 'title': snapshot.data!.docs[index].get('title')
- // })
- },
- icon: Icon(
- Icons.favorite,
- color: Theme.of(context).colorScheme.onBackground,
- ),
- ),
- IconButton(
- onPressed: () {
- showDialog(
- context: context,
- builder: (context) => AlertDialog(
- title: const Text('Confirmar borrado'),
- content: const Text('¿Desea borrar el post?'),
- actions: [
- TextButton(
- child: const Text('Sí'),
- onPressed: () {
- // Delete post
- _firebase.deleteFavorite(
- snapshot.data!.docs[index].id);
- Navigator.of(context).pop();
- },
- ),
- TextButton(
- child: const Text('No'),
- onPressed: () {
- Navigator.of(context).pop();
- },
- )
- ],
- ),
- );
- },
- icon: Icon(
- Icons.delete,
- color: Theme.of(context).colorScheme.onBackground,
- ),
- ),
- ],
- ),
- ),
+ return PopularList(
+ snapshot.data!,
+ onFavorited: (popular, favorited) {
+ if (favorited) {
+ _db.favoritePopular(popular).whenComplete(() {
+ setState(() {});
+ });
+ } else {
+ _db.unfavoritePopular(popular).whenComplete(() {
+ setState(() {});
+ });
+ }
+ },
+ onPressed: (popular) {
+ Navigator.of(context)
+ .pushNamed('/popular_detail', arguments: popular);
+ },
);
} else if (snapshot.hasError) {
- return const Center(child: Text('Hubo un error'));
+ print(snapshot.error);
+ return const Center(
+ child: Text('Ocurrió un error'),
+ );
+ } else {
+ return const LoadingModal();
}
- return const Center(child: CircularProgressIndicator());
},
),
);