diff options
Diffstat (limited to 'lib/screens')
-rw-r--r-- | lib/screens/chat_screen.dart | 42 | ||||
-rw-r--r-- | lib/screens/dashboard_screen.dart | 8 | ||||
-rw-r--r-- | lib/screens/favorites_screen.dart | 86 | ||||
-rw-r--r-- | lib/screens/new_chat_screen.dart | 94 |
4 files changed, 179 insertions, 51 deletions
diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart index bd0d5d7..61f3f9e 100644 --- a/lib/screens/chat_screen.dart +++ b/lib/screens/chat_screen.dart @@ -1,8 +1,10 @@ +import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; import 'package:linkchat/firebase/auth.dart'; import 'package:linkchat/firebase/database.dart'; import 'package:linkchat/models/group.dart'; +import '../models/favorite.dart'; import '../models/message.dart'; import '../models/user.dart'; import '../widgets/chat_bottom_sheet.dart'; @@ -75,16 +77,44 @@ class _ChatScreenState extends State<ChatScreen> { }); List<Message> msgs = snapshot.data!; + User? user = _auth.currentUser; return ListView.builder( controller: _scroll, padding: const EdgeInsets.only(bottom: 80.0), itemCount: snapshot.data!.length, - itemBuilder: (context, index) => ChatBubble( - msgs[index], - alignment: msgs[index].sentBy == _auth.currentUser?.uid - ? ChatBubbleAlignment.end - : ChatBubbleAlignment.start, - ), + itemBuilder: (context, index) => StreamBuilder( + stream: _db.hasFavoriteForMessage( + _auth.currentUser!.uid, msgs[index].id), + builder: (context, snapshot) { + return ChatBubble( + msgs[index], + alignment: msgs[index].sentBy == user?.uid + ? ChatBubbleAlignment.end + : ChatBubbleAlignment.start, + favorited: snapshot.hasData + ? snapshot.data == true + : (snapshot.hasError ? false : false), + onFavorite: (String id, bool value) { + if (value) { + _db.saveFavorite( + Favorite( + groupId: group!.id!, + messageId: msgs[index].id!, + savedAt: DateTime.now(), + messageText: msgs[index].messageText, + linkTitle: msgs[index].linkTitle, + linkDescription: msgs[index].linkDescription, + linkPhotoURL: msgs[index].linkPhotoURL, + sentAt: msgs[index].sentAt, + sentBy: msgs[index].sentBy, + ), + user!.uid); + } else { + _db.removeFavorite(user!.uid, msgs[index].id); + } + }, + ); + }), ); } else if (snapshot.hasError) { print('Error: ${snapshot.error}'); diff --git a/lib/screens/dashboard_screen.dart b/lib/screens/dashboard_screen.dart index 88bd8e7..97a9de6 100644 --- a/lib/screens/dashboard_screen.dart +++ b/lib/screens/dashboard_screen.dart @@ -30,6 +30,14 @@ class _DashboardScreenState extends State<DashboardScreen> { slivers: [ SliverAppBar.large( title: const Text('Inicio'), + actions: [ + IconButton( + icon: const Icon(Icons.favorite_outline), + onPressed: () { + Navigator.of(context).pushNamed('/favorites'); + }, + ) + ], ), SliverFillRemaining( hasScrollBody: true, diff --git a/lib/screens/favorites_screen.dart b/lib/screens/favorites_screen.dart new file mode 100644 index 0000000..4d6f949 --- /dev/null +++ b/lib/screens/favorites_screen.dart @@ -0,0 +1,86 @@ +import 'package:flutter/material.dart'; +import 'package:linkchat/firebase/database.dart'; +import 'package:linkchat/widgets/chat_bubble.dart'; + +import '../firebase/auth.dart'; +import '../models/favorite.dart'; + +class FavoritesScreen extends StatefulWidget { + const FavoritesScreen({super.key}); + + @override + State<FavoritesScreen> createState() => _FavoritesScreenState(); +} + +class _FavoritesScreenState extends State<FavoritesScreen> { + final Auth _auth = Auth(); + final Database _db = Database(); + final TextEditingController _controller = TextEditingController(); + List<Favorite> favorites = []; + List<Favorite> filteredFavorites = []; + + @override + void initState() { + super.initState(); + _db.getFavoritesByUserID(_auth.currentUser!.uid).first.then((f) { + setState(() { + favorites = f; + filteredFavorites = f; + }); + }).onError((e, st) { + print(e); + }); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('Favoritos'), + leading: IconButton( + icon: const Icon(Icons.arrow_back), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + ), + body: Column( + children: [ + Padding( + padding: const EdgeInsets.all(8.0), + child: TextField( + controller: _controller, + decoration: const InputDecoration( + border: OutlineInputBorder(), + labelText: 'Buscar', + ), + onChanged: (value) { + setState(() { + if (value.isNotEmpty) { + filteredFavorites = favorites + .where((fav) => + fav.messageText.contains(value) == true || + fav.linkTitle?.contains(value) == true || + fav.linkDescription?.contains(value) == true) + .toList(); + } else { + filteredFavorites = favorites; + } + }); + }, + ), + ), + Expanded( + child: ListView.builder( + itemCount: filteredFavorites.length, + itemBuilder: (context, index) { + Favorite fav = filteredFavorites[index]; + return LinkPreview(fav.getMessage()); + }, + ), + ), + ], + ), + ); + } +} diff --git a/lib/screens/new_chat_screen.dart b/lib/screens/new_chat_screen.dart index 7b7721f..bf7486f 100644 --- a/lib/screens/new_chat_screen.dart +++ b/lib/screens/new_chat_screen.dart @@ -44,53 +44,57 @@ class _NewChatScreenState extends State<NewChatScreen> { ), body: Column( children: [ - TextField( - controller: _controller, - decoration: const InputDecoration( - border: OutlineInputBorder(), - labelText: 'Nombre del contacto', + Padding( + padding: const EdgeInsets.all(8.0), + child: TextField( + controller: _controller, + decoration: const InputDecoration( + border: OutlineInputBorder(), + labelText: 'Nombre del contacto', + ), + onChanged: (value) { + setState(() { + if (value.isNotEmpty) { + filteredUsers = users + .where((user) => user.displayName.contains(value)) + .toList(); + } else { + filteredUsers = users; + } + }); + }, ), - onChanged: (value) { - setState(() { - if (value.isNotEmpty) { - filteredUsers = users - .where((user) => user.displayName.contains(value)) - .toList(); - } else { - filteredUsers = users; - } - }); - }, ), - ListView.builder( - shrinkWrap: true, - itemCount: filteredUsers.length, - itemBuilder: (context, index) { - FsUser user = filteredUsers[index]; - return ListTile( - leading: CircleAvatar( - backgroundImage: NetworkImage(user.photoUrl), - ), - title: Text(user.displayName), - trailing: IconButton( - icon: const Icon(Icons.send), - onPressed: () { - _db - .saveGroup(Group( - createdBy: _auth.currentUser!.uid, - createdAt: DateTime.now(), - members: [ - _auth.currentUser!.uid, - user.uid, - ], - )) - .whenComplete(() { - Navigator.of(context).pop(); - }); - }, - ), - ); - }, + Expanded( + child: ListView.builder( + itemCount: filteredUsers.length, + itemBuilder: (context, index) { + FsUser user = filteredUsers[index]; + return ListTile( + leading: CircleAvatar( + backgroundImage: NetworkImage(user.photoUrl), + ), + title: Text(user.displayName), + trailing: IconButton( + icon: const Icon(Icons.send), + onPressed: () { + _db + .saveGroup(Group( + createdBy: _auth.currentUser!.uid, + createdAt: DateTime.now(), + members: [ + _auth.currentUser!.uid, + user.uid, + ], + )) + .whenComplete(() { + Navigator.of(context).pop(); + }); + }, + ), + ); + }, + ), ), ], ), |