wesal/frontend/lib/screens/pages/profile_page.dart
2025-07-21 10:11:46 +03:00

606 lines
20 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../services/notification_service.dart';
import '../../services/user_service.dart';
import '../../services/auth_service.dart';
class ProfilePage extends StatefulWidget {
@override
_ProfilePageState createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
String? fcmToken;
final TextEditingController _tokenController = TextEditingController();
bool isLoading = false;
Map<String, dynamic>? userData;
bool isLoadingUser = true;
final List<Map<String, dynamic>> mockUserPosts = [
{
'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
void initState() {
super.initState();
_loadFCMToken();
_loadUserData();
}
@override
void dispose() {
_tokenController.dispose();
super.dispose();
}
Future<void> _loadFCMToken() async {
setState(() {
isLoading = true;
});
try {
final token = await NotificationService().getToken();
setState(() {
fcmToken = token;
_tokenController.text = token ?? 'Token not available';
isLoading = false;
});
} catch (e) {
setState(() {
fcmToken = null;
_tokenController.text = 'Error loading token: $e';
isLoading = false;
});
}
}
Future<void> _loadUserData() async {
setState(() {
isLoadingUser = true;
});
final result = await UserService.getCurrentUser();
setState(() {
isLoadingUser = false;
if (result['success'] == true) {
userData = result['data'];
} else {
userData = null;
_showErrorAlert(result['message'] ?? 'Failed to load user data');
}
});
}
void _showErrorAlert(String message) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Error'),
content: Text(message),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: Text(
'OK',
style: TextStyle(color: Color(0xFF6A4C93)),
),
),
],
),
);
}
Future<void> _copyToClipboard() async {
if (fcmToken != null && fcmToken!.isNotEmpty) {
await Clipboard.setData(ClipboardData(text: fcmToken!));
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('FCM Token copied to clipboard'),
backgroundColor: Color(0xFF6A4C93),
),
);
}
}
void _navigateToSettings() {
Navigator.of(
context,
).push(MaterialPageRoute(builder: (context) => SettingsPage()));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Profile', style: TextStyle(fontWeight: FontWeight.w600)),
backgroundColor: Colors.white,
foregroundColor: Color(0xFF6A4C93),
elevation: 0,
actions: [
IconButton(
onPressed: _navigateToSettings,
icon: Icon(Icons.settings),
),
],
bottom: PreferredSize(
preferredSize: Size.fromHeight(1),
child: Container(height: 1, color: Colors.grey[200]),
),
),
body: SingleChildScrollView(
child: Column(
children: [
Container(
padding: EdgeInsets.all(24),
child: Column(
children: [
if (isLoadingUser)
CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Color(0xFF6A4C93)),
)
else if (userData != null) ...[
CircleAvatar(
radius: 50,
backgroundColor: Color(0xFF6A4C93),
child: Text(
(userData!['displayName'] ?? 'U').substring(0, 1).toUpperCase(),
style: TextStyle(
color: Colors.white,
fontSize: 36,
fontWeight: FontWeight.bold,
),
),
),
SizedBox(height: 16),
Text(
userData!['displayName'] ?? 'Unknown User',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.black87,
),
),
SizedBox(height: 4),
Text(
'@${userData!['username'] ?? 'unknown'}',
style: TextStyle(fontSize: 16, color: Colors.grey[600]),
),
] else ...[
Icon(
Icons.error_outline,
size: 50,
color: Colors.grey[400],
),
SizedBox(height: 16),
Text(
'Failed to load user data',
style: TextStyle(
fontSize: 18,
color: Colors.grey[600],
),
),
SizedBox(height: 8),
ElevatedButton(
onPressed: _loadUserData,
style: ElevatedButton.styleFrom(
backgroundColor: Color(0xFF6A4C93),
foregroundColor: Colors.white,
),
child: Text('Retry'),
),
],
SizedBox(height: 20),
if (userData != null)
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Column(
children: [
Text(
'${mockUserPosts.length}',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Color(0xFF6A4C93),
),
),
Text(
'Posts',
style: TextStyle(
fontSize: 14,
color: Colors.grey[600],
),
),
],
),
Column(
children: [
Text(
'127',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Color(0xFF6A4C93),
),
),
Text(
'Followers',
style: TextStyle(
fontSize: 14,
color: Colors.grey[600],
),
),
],
),
],
),
],
),
),
if (userData != null) ...[
Container(
height: 1,
color: Colors.grey[200],
margin: EdgeInsets.symmetric(horizontal: 16),
),
SizedBox(height: 16),
ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
padding: EdgeInsets.symmetric(horizontal: 16),
itemCount: mockUserPosts.length,
itemBuilder: (context, index) {
final post = mockUserPosts[index];
return Container(
margin: EdgeInsets.only(bottom: 16),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.05),
blurRadius: 10,
offset: Offset(0, 2),
),
],
),
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
CircleAvatar(
radius: 16,
backgroundColor: Color(0xFF6A4C93),
child: Text(
(userData!['displayName'] ?? 'U').substring(0, 1).toUpperCase(),
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 14,
),
),
),
SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
userData!['displayName'] ?? 'Unknown User',
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 14,
color: Colors.black87,
),
),
Text(
post['timestamp'],
style: TextStyle(
color: Colors.grey[600],
fontSize: 12,
),
),
],
),
),
],
),
SizedBox(height: 12),
Text(
post['content'],
style: TextStyle(
fontSize: 15,
height: 1.4,
color: Colors.black87,
),
),
SizedBox(height: 12),
Row(
children: [
Icon(
post['isLiked']
? Icons.favorite
: Icons.favorite_border,
color: post['isLiked']
? Colors.red
: Colors.grey[600],
size: 20,
),
SizedBox(width: 4),
Text(
'${post['likes']}',
style: TextStyle(
color: Colors.grey[700],
fontSize: 14,
),
),
SizedBox(width: 16),
Icon(
Icons.chat_bubble_outline,
color: Colors.grey[600],
size: 20,
),
SizedBox(width: 4),
Text(
'${post['comments']}',
style: TextStyle(
color: Colors.grey[700],
fontSize: 14,
),
),
],
),
],
),
),
);
},
),
],
SizedBox(height: 24),
],
),
),
);
}
}
class SettingsPage extends StatefulWidget {
@override
_SettingsPageState createState() => _SettingsPageState();
}
class _SettingsPageState extends State<SettingsPage> {
String? fcmToken;
final TextEditingController _tokenController = TextEditingController();
bool isLoading = false;
@override
void initState() {
super.initState();
_loadFCMToken();
}
@override
void dispose() {
_tokenController.dispose();
super.dispose();
}
Future<void> _loadFCMToken() async {
setState(() {
isLoading = true;
});
try {
final token = await NotificationService().getToken();
setState(() {
fcmToken = token;
_tokenController.text = token ?? 'Token not available';
isLoading = false;
});
} catch (e) {
setState(() {
fcmToken = null;
_tokenController.text = 'Error loading token: $e';
isLoading = false;
});
}
}
Future<void> _copyToClipboard() async {
if (fcmToken != null && fcmToken!.isNotEmpty) {
await Clipboard.setData(ClipboardData(text: fcmToken!));
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('FCM Token copied to clipboard'),
backgroundColor: Color(0xFF6A4C93),
),
);
}
}
void _signOut() async {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Sign Out'),
content: Text('Are you sure you want to sign out?'),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: Text('Cancel'),
),
TextButton(
onPressed: () async {
Navigator.of(context).pop();
await AuthService.logout();
Navigator.of(context).pushNamedAndRemoveUntil(
'/',
(route) => false,
);
},
child: Text('Sign Out', style: TextStyle(color: Colors.red)),
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Settings', style: TextStyle(fontWeight: FontWeight.w600)),
backgroundColor: Colors.white,
foregroundColor: Color(0xFF6A4C93),
elevation: 0,
bottom: PreferredSize(
preferredSize: Size.fromHeight(1),
child: Container(height: 1, color: Colors.grey[200]),
),
),
body: Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Development Tools',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w600,
color: Color(0xFF6A4C93),
),
),
SizedBox(height: 16),
Text(
'FCM Token:',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
color: Colors.black87,
),
),
SizedBox(height: 8),
if (isLoading)
Center(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Color(0xFF6A4C93)),
),
)
else
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[300]!),
borderRadius: BorderRadius.circular(8),
),
child: TextField(
controller: _tokenController,
readOnly: true,
maxLines: 4,
style: TextStyle(fontSize: 12, fontFamily: 'monospace'),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.all(12),
hintText: 'FCM Token will appear here...',
),
),
),
SizedBox(height: 8),
Row(
children: [
ElevatedButton.icon(
onPressed: fcmToken != null ? _copyToClipboard : null,
icon: Icon(Icons.copy, size: 18),
label: Text('Copy Token'),
style: ElevatedButton.styleFrom(
backgroundColor: Color(0xFF6A4C93),
foregroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
SizedBox(width: 12),
OutlinedButton.icon(
onPressed: _loadFCMToken,
icon: Icon(Icons.refresh, size: 18),
label: Text('Refresh'),
style: OutlinedButton.styleFrom(
foregroundColor: Color(0xFF6A4C93),
side: BorderSide(color: Color(0xFF6A4C93)),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
],
),
],
),
SizedBox(height: 32),
Container(
width: double.infinity,
child: ElevatedButton.icon(
onPressed: _signOut,
icon: Icon(Icons.logout, color: Colors.red),
label: Text('Sign Out', style: TextStyle(color: Colors.red)),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.white,
side: BorderSide(color: Colors.red),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
padding: EdgeInsets.symmetric(vertical: 16),
),
),
),
],
),
),
);
}
}