feat: implement profile page user feed

This commit is contained in:
sBubshait 2025-07-23 14:07:34 +03:00
parent c548c7e2a3
commit 86a4535f9a
2 changed files with 228 additions and 145 deletions

View File

@ -3,6 +3,8 @@ import 'package:flutter/services.dart';
import '../../services/notification_service.dart'; import '../../services/notification_service.dart';
import '../../services/user_service.dart'; import '../../services/user_service.dart';
import '../../services/auth_service.dart'; import '../../services/auth_service.dart';
import '../../services/post_service.dart';
import '../../models/post_models.dart';
class ProfilePage extends StatefulWidget { class ProfilePage extends StatefulWidget {
@override @override
@ -15,42 +17,16 @@ class _ProfilePageState extends State<ProfilePage> {
bool isLoading = false; bool isLoading = false;
Map<String, dynamic>? userData; Map<String, dynamic>? userData;
bool isLoadingUser = true; bool isLoadingUser = true;
List<Post> userPosts = [];
final List<Map<String, dynamic>> mockUserPosts = [ bool isLoadingPosts = true;
{ int totalLikes = 0;
'id': '1',
'content':
'Just finished working on a new Flutter project! The development experience keeps getting better.',
'timestamp': '2 hours ago',
'likes': 15,
'comments': 4,
'isLiked': true,
},
{
'id': '2',
'content':
'Beautiful sunset from my office window today. Sometimes you need to take a moment to appreciate the simple things.',
'timestamp': '1 day ago',
'likes': 23,
'comments': 8,
'isLiked': false,
},
{
'id': '3',
'content':
'Excited to share that I completed my certification today! Hard work pays off.',
'timestamp': '3 days ago',
'likes': 42,
'comments': 12,
'isLiked': true,
},
];
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_loadFCMToken(); _loadFCMToken();
_loadUserData(); _loadUserData();
_loadUserPosts();
} }
@override @override
@ -98,6 +74,26 @@ class _ProfilePageState extends State<ProfilePage> {
}); });
} }
Future<void> _loadUserPosts() async {
setState(() {
isLoadingPosts = true;
});
final result = await PostService.getUserPosts();
setState(() {
isLoadingPosts = false;
if (result['success'] == true) {
userPosts = result['posts'] as List<Post>;
totalLikes = userPosts.fold(0, (sum, post) => sum + post.likes);
} else {
userPosts = [];
totalLikes = 0;
_showErrorAlert(result['message'] ?? 'Failed to load posts');
}
});
}
void _showErrorAlert(String message) { void _showErrorAlert(String message) {
showDialog( showDialog(
context: context, context: context,
@ -132,6 +128,21 @@ class _ProfilePageState extends State<ProfilePage> {
).push(MaterialPageRoute(builder: (context) => SettingsPage())); ).push(MaterialPageRoute(builder: (context) => SettingsPage()));
} }
String _formatTimestamp(DateTime dateTime) {
final now = DateTime.now();
final difference = now.difference(dateTime);
if (difference.inDays > 0) {
return '${difference.inDays}d ago';
} else if (difference.inHours > 0) {
return '${difference.inHours}h ago';
} else if (difference.inMinutes > 0) {
return '${difference.inMinutes}m ago';
} else {
return 'Just now';
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@ -223,7 +234,7 @@ class _ProfilePageState extends State<ProfilePage> {
Column( Column(
children: [ children: [
Text( Text(
'${mockUserPosts.length}', '${userPosts.length}',
style: TextStyle( style: TextStyle(
fontSize: 20, fontSize: 20,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
@ -242,7 +253,7 @@ class _ProfilePageState extends State<ProfilePage> {
Column( Column(
children: [ children: [
Text( Text(
'127', '$totalLikes',
style: TextStyle( style: TextStyle(
fontSize: 20, fontSize: 20,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
@ -250,7 +261,7 @@ class _ProfilePageState extends State<ProfilePage> {
), ),
), ),
Text( Text(
'Followers', 'Likes Received',
style: TextStyle( style: TextStyle(
fontSize: 14, fontSize: 14,
color: Colors.grey[600], color: Colors.grey[600],
@ -270,13 +281,57 @@ class _ProfilePageState extends State<ProfilePage> {
margin: EdgeInsets.symmetric(horizontal: 16), margin: EdgeInsets.symmetric(horizontal: 16),
), ),
SizedBox(height: 16), SizedBox(height: 16),
if (isLoadingPosts)
Container(
padding: EdgeInsets.all(32),
child: Center(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(
Color(0xFF6A4C93),
),
),
),
)
else if (userPosts.isEmpty)
Container(
padding: EdgeInsets.all(32),
child: Center(
child: Column(
children: [
Icon(
Icons.post_add,
size: 64,
color: Colors.grey[400],
),
SizedBox(height: 16),
Text(
'No posts yet',
style: TextStyle(
fontSize: 18,
color: Colors.grey[600],
fontWeight: FontWeight.w500,
),
),
SizedBox(height: 8),
Text(
'Start sharing your thoughts!',
style: TextStyle(
fontSize: 14,
color: Colors.grey[500],
),
),
],
),
),
)
else
ListView.builder( ListView.builder(
shrinkWrap: true, shrinkWrap: true,
physics: NeverScrollableScrollPhysics(), physics: NeverScrollableScrollPhysics(),
padding: EdgeInsets.symmetric(horizontal: 16), padding: EdgeInsets.symmetric(horizontal: 16),
itemCount: mockUserPosts.length, itemCount: userPosts.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
final post = mockUserPosts[index]; final post = userPosts[index];
return Container( return Container(
margin: EdgeInsets.only(bottom: 16), margin: EdgeInsets.only(bottom: 16),
decoration: BoxDecoration( decoration: BoxDecoration(
@ -301,9 +356,11 @@ class _ProfilePageState extends State<ProfilePage> {
radius: 16, radius: 16,
backgroundColor: Color(0xFF6A4C93), backgroundColor: Color(0xFF6A4C93),
child: Text( child: Text(
(userData!['displayName'] ?? 'U') post.creator.displayName.isNotEmpty
? post.creator.displayName
.substring(0, 1) .substring(0, 1)
.toUpperCase(), .toUpperCase()
: 'U',
style: TextStyle( style: TextStyle(
color: Colors.white, color: Colors.white,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
@ -317,8 +374,9 @@ class _ProfilePageState extends State<ProfilePage> {
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Text( Text(
userData!['displayName'] ?? post.creator.displayName.isNotEmpty
'Unknown User', ? post.creator.displayName
: 'Unknown User',
style: TextStyle( style: TextStyle(
fontWeight: FontWeight.w600, fontWeight: FontWeight.w600,
fontSize: 14, fontSize: 14,
@ -326,7 +384,7 @@ class _ProfilePageState extends State<ProfilePage> {
), ),
), ),
Text( Text(
post['timestamp'], _formatTimestamp(post.creationDate),
style: TextStyle( style: TextStyle(
color: Colors.grey[600], color: Colors.grey[600],
fontSize: 12, fontSize: 12,
@ -339,7 +397,7 @@ class _ProfilePageState extends State<ProfilePage> {
), ),
SizedBox(height: 12), SizedBox(height: 12),
Text( Text(
post['content'], post.body,
style: TextStyle( style: TextStyle(
fontSize: 15, fontSize: 15,
height: 1.4, height: 1.4,
@ -350,17 +408,13 @@ class _ProfilePageState extends State<ProfilePage> {
Row( Row(
children: [ children: [
Icon( Icon(
post['isLiked'] Icons.favorite_border,
? Icons.favorite color: Colors.grey[600],
: Icons.favorite_border,
color: post['isLiked']
? Colors.red
: Colors.grey[600],
size: 20, size: 20,
), ),
SizedBox(width: 4), SizedBox(width: 4),
Text( Text(
'${post['likes']}', '${post.likes}',
style: TextStyle( style: TextStyle(
color: Colors.grey[700], color: Colors.grey[700],
fontSize: 14, fontSize: 14,
@ -374,7 +428,7 @@ class _ProfilePageState extends State<ProfilePage> {
), ),
SizedBox(width: 4), SizedBox(width: 4),
Text( Text(
'${post['comments']}', '${post.comments}',
style: TextStyle( style: TextStyle(
color: Colors.grey[700], color: Colors.grey[700],
fontSize: 14, fontSize: 14,

View File

@ -36,6 +36,35 @@ class PostService {
} }
} }
static Future<Map<String, dynamic>> getUserPosts() async {
try {
final response = await HttpService.get('/posts/user');
final responseData = jsonDecode(response.body);
if (response.statusCode == 200) {
return {
'success': responseData['status'] ?? false,
'message': responseData['message'] ?? '',
'posts': (responseData['data'] as List?)?.map((post) => Post.fromJson(post)).toList() ?? [],
};
} else {
return {
'success': false,
'message': responseData['message'] ?? 'Failed to fetch user posts',
'posts': <Post>[],
};
}
} catch (e) {
print('Error fetching user posts: $e');
return {
'success': false,
'message': 'Network error: $e',
'posts': <Post>[],
};
}
}
static Future<Map<String, dynamic>> createPost(String body) async { static Future<Map<String, dynamic>> createPost(String body) async {
try { try {
final createPostRequest = CreatePostRequest(body: body); final createPostRequest = CreatePostRequest(body: body);