|
| 1 | +import 'package:firebase_database_repository/firebase_database_repository.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 4 | +import 'package:magic_yeti/app/bloc/app_bloc.dart'; |
| 5 | +import 'package:magic_yeti/friends_list/friends_list/bloc/friend_list_bloc.dart'; |
| 6 | + |
| 7 | +/// This file implements the UI for displaying and managing the user's friends list. |
| 8 | +/// It allows users to view their friends and remove them if desired. |
| 9 | +/// |
| 10 | +/// Key features: |
| 11 | +/// - Display a list of current friends |
| 12 | +/// - Remove friends with confirmation |
| 13 | +/// |
| 14 | +/// @dependencies |
| 15 | +/// - Flutter Bloc: For state management |
| 16 | +/// - Firebase Database Repository: To interact with Firestore |
| 17 | +/// |
| 18 | +/// @notes |
| 19 | +/// - Handles network errors gracefully |
| 20 | +/// - Ensures real-time updates using Firestore sync |
| 21 | +
|
| 22 | +class FriendsList extends StatelessWidget { |
| 23 | + const FriendsList({super.key}); |
| 24 | + |
| 25 | + @override |
| 26 | + Widget build(BuildContext context) { |
| 27 | + final userId = context.read<AppBloc>().state.user.id; |
| 28 | + return Scaffold( |
| 29 | + body: BlocProvider( |
| 30 | + create: (context) => FriendBloc( |
| 31 | + repository: context.read<FirebaseDatabaseRepository>(), |
| 32 | + )..add(LoadFriends(userId)), |
| 33 | + child: const FriendsListView(), |
| 34 | + ), |
| 35 | + ); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +class FriendsListView extends StatelessWidget { |
| 40 | + const FriendsListView({super.key}); |
| 41 | + |
| 42 | + @override |
| 43 | + Widget build(BuildContext context) { |
| 44 | + final userId = context.read<AppBloc>().state.user.id; |
| 45 | + return BlocBuilder<FriendBloc, FriendState>( |
| 46 | + builder: (context, state) { |
| 47 | + if (state is FriendsLoading) { |
| 48 | + return const Center(child: CircularProgressIndicator()); |
| 49 | + } else if (state is FriendsLoaded) { |
| 50 | + return state.friends.isEmpty |
| 51 | + ? const Center(child: Text('No friends found')) |
| 52 | + : ListView.builder( |
| 53 | + itemCount: state.friends.length, |
| 54 | + itemBuilder: (context, index) { |
| 55 | + final friend = state.friends[index]; |
| 56 | + return ListTile( |
| 57 | + title: Text(friend.username), |
| 58 | + trailing: IconButton( |
| 59 | + icon: const Icon(Icons.delete), |
| 60 | + onPressed: () => |
| 61 | + _confirmRemoveFriend(context, friend, userId), |
| 62 | + ), |
| 63 | + ); |
| 64 | + }, |
| 65 | + ); |
| 66 | + } else if (state is FriendsError) { |
| 67 | + return const Center(child: Text('Failed to load friends')); |
| 68 | + } |
| 69 | + return const Center(child: Text('No friends found')); |
| 70 | + }, |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + void _confirmRemoveFriend( |
| 75 | + BuildContext context, |
| 76 | + FriendModel friend, |
| 77 | + String userId, |
| 78 | + ) { |
| 79 | + showDialog<void>( |
| 80 | + context: context, |
| 81 | + builder: (BuildContext context) { |
| 82 | + return AlertDialog( |
| 83 | + title: const Text('Remove Friend'), |
| 84 | + content: Text('Are you sure you want to remove ${friend.username}?'), |
| 85 | + actions: <Widget>[ |
| 86 | + TextButton( |
| 87 | + child: const Text('Cancel'), |
| 88 | + onPressed: () => Navigator.of(context).pop(), |
| 89 | + ), |
| 90 | + TextButton( |
| 91 | + child: const Text('Remove'), |
| 92 | + onPressed: () { |
| 93 | + context |
| 94 | + .read<FriendBloc>() |
| 95 | + .add(RemoveFriend(userId, friend.userId)); |
| 96 | + Navigator.of(context).pop(); |
| 97 | + }, |
| 98 | + ), |
| 99 | + ], |
| 100 | + ); |
| 101 | + }, |
| 102 | + ); |
| 103 | + } |
| 104 | +} |
0 commit comments