Files
b0esche_cloud/b0esche_cloud/lib/widgets/account_settings_dialog.dart
Leon Bösche 262ce18902 idle
2026-01-27 09:20:16 +01:00

543 lines
17 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_blurhash/flutter_blurhash.dart' as flutter_blurhash;
import 'package:file_picker/file_picker.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:blurhash_dart/blurhash_dart.dart' as blurhash_dart;
import 'package:image/image.dart' as img;
import 'package:get_it/get_it.dart';
import 'dart:io';
import '../blocs/auth/auth_bloc.dart';
import '../blocs/auth/auth_state.dart';
import '../blocs/auth/auth_event.dart';
import '../models/user.dart';
import '../services/api_client.dart';
import '../theme/app_theme.dart';
import '../theme/modern_glass_button.dart';
class AccountSettingsDialog extends StatefulWidget {
const AccountSettingsDialog({super.key});
@override
State<AccountSettingsDialog> createState() => _AccountSettingsDialogState();
}
class _AccountSettingsDialogState extends State<AccountSettingsDialog> {
int _selectedTabIndex = 0;
bool _isLoading = false;
String? _error;
// Profile fields
late TextEditingController _displayNameController;
String? _avatarUrl;
String? _blurHash;
// Security fields
late TextEditingController _currentPasswordController;
late TextEditingController _newPasswordController;
late TextEditingController _confirmPasswordController;
User? _currentUser;
@override
void initState() {
super.initState();
_displayNameController = TextEditingController();
_currentPasswordController = TextEditingController();
_newPasswordController = TextEditingController();
_confirmPasswordController = TextEditingController();
_loadUserData();
}
@override
void dispose() {
_displayNameController.dispose();
_currentPasswordController.dispose();
_newPasswordController.dispose();
_confirmPasswordController.dispose();
super.dispose();
}
void _loadUserData() {
final authState = context.read<AuthBloc>().state;
if (authState is AuthAuthenticated) {
_currentUser = authState.user;
_displayNameController.text = _currentUser?.displayName ?? '';
_avatarUrl = _currentUser?.avatarUrl;
_blurHash = _currentUser?.blurHash;
}
}
Future<void> _pickAndUploadAvatar() async {
try {
final result = await FilePicker.platform.pickFiles(
type: FileType.image,
allowMultiple: false,
);
if (result != null && result.files.single.path != null) {
final file = File(result.files.single.path!);
final bytes = await file.readAsBytes();
final filename = result.files.single.name;
final image = img.decodeImage(bytes);
if (image == null) throw Exception('Invalid image');
// Generate blur hash
final blurHash = blurhash_dart.BlurHash.encode(image).hash;
setState(() {
_blurHash = blurHash;
_isLoading = true;
});
try {
final apiClient = GetIt.I<ApiClient>();
final response = await apiClient.uploadAvatar(bytes, filename);
setState(() {
_avatarUrl = response['avatarUrl'] as String?;
_isLoading = false;
});
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Avatar uploaded successfully')),
);
}
} catch (e) {
setState(() => _isLoading = false);
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Failed to upload avatar: $e')),
);
}
}
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Failed to process image: $e')));
}
}
}
Future<void> _updateProfile() async {
if (_currentUser == null) return;
setState(() => _isLoading = true);
try {
final apiClient = GetIt.I<ApiClient>();
await apiClient.updateUserProfile(
displayName: _displayNameController.text.isEmpty
? null
: _displayNameController.text,
avatarUrl: _avatarUrl,
blurHash: _blurHash,
);
final updatedUser = _currentUser!.copyWith(
displayName: _displayNameController.text.isEmpty
? null
: _displayNameController.text,
avatarUrl: _avatarUrl,
blurHash: _blurHash,
);
if (mounted) {
// Update auth state
context.read<AuthBloc>().add(UpdateUserProfile(updatedUser));
ScaffoldMessenger.of(
context,
).showSnackBar(const SnackBar(content: Text('Profile updated')));
}
} catch (e) {
setState(() => _error = e.toString());
} finally {
setState(() => _isLoading = false);
}
}
Future<void> _changePassword() async {
if (_newPasswordController.text != _confirmPasswordController.text) {
setState(() => _error = 'Passwords do not match');
return;
}
setState(() => _isLoading = true);
try {
final apiClient = GetIt.I<ApiClient>();
await apiClient.changePassword(
currentPassword: _currentPasswordController.text,
newPassword: _newPasswordController.text,
);
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(const SnackBar(content: Text('Password changed')));
}
// Clear fields
_currentPasswordController.clear();
_newPasswordController.clear();
_confirmPasswordController.clear();
} catch (e) {
setState(() => _error = e.toString());
} finally {
setState(() => _isLoading = false);
}
}
Future<void> _logout() async {
context.read<AuthBloc>().add(const LogoutRequested());
Navigator.of(context).pop();
}
@override
Widget build(BuildContext context) {
return Dialog(
backgroundColor: AppTheme.primaryBackground,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
child: Container(
width: 500,
height: 600,
padding: const EdgeInsets.all(24),
child: Column(
children: [
// Header
Row(
children: [
Text(
'Account Settings',
style: TextStyle(
color: AppTheme.primaryText,
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
const Spacer(),
IconButton(
onPressed: () => Navigator.of(context).pop(),
icon: Icon(Icons.close, color: AppTheme.secondaryText),
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
),
],
),
const SizedBox(height: 16),
// Tabs
Row(
children: [
_buildTabButton('Profile', 0),
_buildTabButton('Security', 1),
],
),
const SizedBox(height: 16),
// Content
Expanded(
child: _isLoading
? Center(
child: CircularProgressIndicator(
color: AppTheme.accentColor,
),
)
: _error != null
? Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
_error!,
style: TextStyle(color: AppTheme.errorColor),
),
const SizedBox(height: 16),
ModernGlassButton(
onPressed: () {
setState(() => _error = null);
},
child: const Text('Retry'),
),
],
),
)
: _buildTabContent(),
),
],
),
),
);
}
Widget _buildTabButton(String text, int index) {
final isSelected = _selectedTabIndex == index;
return Expanded(
child: GestureDetector(
onTap: () => setState(() => _selectedTabIndex = index),
child: AnimatedContainer(
duration: const Duration(milliseconds: 200),
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 8),
margin: const EdgeInsets.symmetric(horizontal: 2),
decoration: BoxDecoration(
color: isSelected
? AppTheme.accentColor.withValues(alpha: 0.15)
: Colors.transparent,
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: isSelected
? AppTheme.accentColor
: AppTheme.secondaryText.withValues(alpha: 0.3),
width: 1.5,
),
boxShadow: isSelected
? [
BoxShadow(
color: AppTheme.accentColor.withValues(alpha: 0.3),
blurRadius: 8,
spreadRadius: 1,
),
]
: null,
),
child: AnimatedDefaultTextStyle(
duration: const Duration(milliseconds: 200),
style: TextStyle(
color: isSelected ? AppTheme.accentColor : AppTheme.secondaryText,
fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
fontSize: 14,
),
child: Text(text, textAlign: TextAlign.center),
),
),
),
);
}
Widget _buildTabContent() {
switch (_selectedTabIndex) {
case 0:
return _buildProfileTab();
case 1:
return _buildSecurityTab();
default:
return const SizedBox.shrink();
}
}
Widget _buildProfileTab() {
return SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Avatar
Center(
child: Column(
children: [
SizedBox(height: 16),
GestureDetector(
onTap: _pickAndUploadAvatar,
child: CircleAvatar(
radius: 50,
backgroundColor: AppTheme.secondaryText.withValues(
alpha: 0.2,
),
child: _avatarUrl != null && _blurHash != null
? ClipOval(
child: flutter_blurhash.BlurHash(
hash: _blurHash!,
image: _avatarUrl,
imageFit: BoxFit.cover,
),
)
: Icon(
Icons.person,
size: 50,
color: AppTheme.secondaryText,
),
),
),
const SizedBox(height: 8),
Text(
'Tap to change avatar',
style: TextStyle(color: AppTheme.secondaryText, fontSize: 12),
),
],
),
),
const SizedBox(height: 24),
// Display Name
Text(
'Display Name',
style: TextStyle(
color: AppTheme.primaryText,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Container(
decoration: BoxDecoration(
color: AppTheme.primaryBackground.withValues(alpha: 0.5),
borderRadius: BorderRadius.circular(16),
border: Border.all(
color: AppTheme.accentColor.withValues(alpha: 0.3),
),
),
child: TextFormField(
controller: _displayNameController,
cursorColor: AppTheme.accentColor,
style: TextStyle(color: AppTheme.primaryText),
decoration: InputDecoration(
hintText: 'Enter display name',
hintStyle: TextStyle(color: AppTheme.secondaryText),
border: InputBorder.none,
contentPadding: const EdgeInsets.all(12),
),
),
),
const SizedBox(height: 24),
// Save Button
Center(
child: SizedBox(
width: 104,
child: ModernGlassButton(
onPressed: _updateProfile,
child: const Text('Save Changes'),
),
),
),
const SizedBox(height: 24),
// Logout Button
Center(
child: IconButton(
onPressed: _logout,
icon: Icon(Icons.logout, color: AppTheme.errorColor),
tooltip: 'Logout',
iconSize: 28,
),
),
],
),
);
}
Widget _buildSecurityTab() {
return SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Current Password
Text(
'Current Password',
style: TextStyle(
color: AppTheme.primaryText,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Container(
decoration: BoxDecoration(
color: AppTheme.primaryBackground.withValues(alpha: 0.5),
borderRadius: BorderRadius.circular(16),
border: Border.all(
color: AppTheme.accentColor.withValues(alpha: 0.3),
),
),
child: TextFormField(
controller: _currentPasswordController,
obscureText: true,
cursorColor: AppTheme.accentColor,
style: TextStyle(color: AppTheme.primaryText),
decoration: InputDecoration(
hintText: 'Enter current password',
hintStyle: TextStyle(color: AppTheme.secondaryText),
border: InputBorder.none,
contentPadding: const EdgeInsets.all(12),
),
),
),
const SizedBox(height: 16),
// New Password
Text(
'New Password',
style: TextStyle(
color: AppTheme.primaryText,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Container(
decoration: BoxDecoration(
color: AppTheme.primaryBackground.withValues(alpha: 0.5),
borderRadius: BorderRadius.circular(16),
border: Border.all(
color: AppTheme.accentColor.withValues(alpha: 0.3),
),
),
child: TextFormField(
controller: _newPasswordController,
obscureText: true,
cursorColor: AppTheme.accentColor,
style: TextStyle(color: AppTheme.primaryText),
decoration: InputDecoration(
hintText: 'Enter new password',
hintStyle: TextStyle(color: AppTheme.secondaryText),
border: InputBorder.none,
contentPadding: const EdgeInsets.all(12),
),
),
),
const SizedBox(height: 16),
// Confirm Password
Text(
'Confirm New Password',
style: TextStyle(
color: AppTheme.primaryText,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Container(
decoration: BoxDecoration(
color: AppTheme.primaryBackground.withValues(alpha: 0.5),
borderRadius: BorderRadius.circular(16),
border: Border.all(
color: AppTheme.accentColor.withValues(alpha: 0.3),
),
),
child: TextFormField(
controller: _confirmPasswordController,
obscureText: true,
cursorColor: AppTheme.accentColor,
style: TextStyle(color: AppTheme.primaryText),
decoration: InputDecoration(
hintText: 'Confirm new password',
hintStyle: TextStyle(color: AppTheme.secondaryText),
border: InputBorder.none,
contentPadding: const EdgeInsets.all(12),
),
),
),
const SizedBox(height: 24),
// Change Password Button
Center(
child: SizedBox(
width: 180,
child: ModernGlassButton(
onPressed: _changePassword,
child: const Text('Change Password'),
),
),
),
],
),
);
}
}