1// ── FOLLOW/UNFOLLOW SYSTEM ──
2class UserService {
3 final _firestore = FirebaseFirestore.instance;
4 final _auth = FirebaseAuth.instance;
5
6 Future<void> toggleFollow(String targetUserId) async {
7 final currentUserId = _auth.currentUser!.uid;
8 final followRef = _firestore
9 .collection('users')
10 .doc(currentUserId)
11 .collection('following')
12 .doc(targetUserId);
13
14 final followDoc = await followRef.get();
15 final batch = _firestore.batch();
16
17 if (followDoc.exists) {
18 // Unfollow
19 batch.delete(followRef);
20 batch.delete(_firestore
21 .collection('users').doc(targetUserId)
22 .collection('followers').doc(currentUserId));
23 batch.update(_firestore.collection('users').doc(currentUserId), {
24 'followingCount': FieldValue.increment(-1),
25 });
26 batch.update(_firestore.collection('users').doc(targetUserId), {
27 'followersCount': FieldValue.increment(-1),
28 });
29 } else {
30 // Follow
31 batch.set(followRef, {'timestamp': FieldValue.serverTimestamp()});
32 batch.set(_firestore
33 .collection('users').doc(targetUserId)
34 .collection('followers').doc(currentUserId),
35 {'timestamp': FieldValue.serverTimestamp()});
36 batch.update(_firestore.collection('users').doc(currentUserId), {
37 'followingCount': FieldValue.increment(1),
38 });
39 batch.update(_firestore.collection('users').doc(targetUserId), {
40 'followersCount': FieldValue.increment(1),
41 });
42 }
43
44 await batch.commit();
45 }
46}
47
48// ── POST CARD WIDGET ──
49class PostCard extends StatelessWidget {
50 final Post post;
51 final VoidCallback onLike;
52 final VoidCallback onComment;
53 final VoidCallback onProfile;
54
55 const PostCard({
56 super.key,
57 required this.post,
58 required this.onLike,
59 required this.onComment,
60 required this.onProfile,
61 });
62
63 @override
64 Widget build(BuildContext context) {
65 return Card(
66 margin: const EdgeInsets.symmetric(vertical: 4),
67 child: Column(
68 crossAxisAlignment: CrossAxisAlignment.start,
69 children: [
70 // Author header
71 ListTile(
72 leading: GestureDetector(
73 onTap: onProfile,
74 child: CircleAvatar(
75 backgroundImage: post.authorAvatar != null
76 ? NetworkImage(post.authorAvatar!)
77 : null,
78 child: post.authorAvatar == null
79 ? Text(post.authorName[0])
80 : null,
81 ),
82 ),
83 title: Text(post.authorName,
84 style: const TextStyle(fontWeight: FontWeight.bold)),
85 subtitle: Text(_timeAgo(post.createdAt)),
86 trailing: const Icon(Icons.more_vert),
87 ),
88 // Image
89 if (post.imageUrl != null)
90 Image.network(post.imageUrl!, fit: BoxFit.cover,
91 width: double.infinity),
92 // Text
93 if (post.text != null)
94 Padding(
95 padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
96 child: Text(post.text!),
97 ),
98 // Actions
99 Padding(
100 padding: const EdgeInsets.symmetric(horizontal: 8),
101 child: Row(
102 children: [
103 IconButton(
104 icon: Icon(
105 post.isLikedByCurrentUser
106 ? Icons.favorite : Icons.favorite_border,
107 color: post.isLikedByCurrentUser ? Colors.red : null,
108 ),
109 onPressed: onLike,
110 ),
111 Text('${post.likesCount}'),
112 const SizedBox(width: 16),
113 IconButton(
114 icon: const Icon(Icons.chat_bubble_outline),
115 onPressed: onComment,
116 ),
117 Text('${post.commentsCount}'),
118 const Spacer(),
119 IconButton(
120 icon: const Icon(Icons.share_outlined),
121 onPressed: () {},
122 ),
123 ],
124 ),
125 ),
126 ],
127 ),
128 );
129 }
130}