a5c13a4b3c
- Deleted `next_steps_flutter.md` and `teknisk_beskrivning_flutter.md` files as they were outdated. - Added new `next_steps_flutter.md` and `teknisk_beskrivning_flutter.md` files with updated migration plans and technical descriptions for the Flutter frontend. - Implemented `profile_repository.dart` to handle profile data retrieval and updates using the API. Co-authored-by: Copilot <copilot@github.com>
123 lines
3.7 KiB
Dart
123 lines
3.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import '../../../core/api/api_error_mapper.dart';
|
|
import '../../data/profile_repository.dart';
|
|
|
|
class ProfileScreen extends ConsumerStatefulWidget {
|
|
const ProfileScreen({super.key});
|
|
|
|
@override
|
|
ConsumerState<ProfileScreen> createState() => _ProfileScreenState();
|
|
}
|
|
|
|
class _ProfileScreenState extends ConsumerState<ProfileScreen> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
String _username = '';
|
|
String _email = '';
|
|
bool _isLoading = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadProfile();
|
|
}
|
|
|
|
Future<void> _loadProfile() async {
|
|
try {
|
|
final profile = await ref.read(profileRepositoryProvider).getProfile();
|
|
setState(() {
|
|
_username = profile['username'] ?? '';
|
|
_email = profile['email'] ?? '';
|
|
_isLoading = false;
|
|
});
|
|
} catch (e) {
|
|
_showErrorMessage(e);
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
Future<void> _updateProfile() async {
|
|
if (_formKey.currentState!.validate()) {
|
|
_formKey.currentState!.save();
|
|
setState(() {
|
|
_isLoading = true;
|
|
});
|
|
try {
|
|
await ref.read(profileRepositoryProvider).updateProfile({
|
|
'username': _username,
|
|
'email': _email,
|
|
});
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Profil uppdaterad!')),
|
|
);
|
|
} catch (e) {
|
|
_showErrorMessage(e);
|
|
} finally {
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
void _showErrorMessage(dynamic error) {
|
|
final message = mapErrorToUserMessage(error, context);
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(message)),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Användarprofil'),
|
|
),
|
|
body: _isLoading
|
|
? const Center(child: CircularProgressIndicator())
|
|
: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
children: <Widget>[
|
|
TextFormField(
|
|
decoration: const InputDecoration(labelText: 'Användarnamn'),
|
|
initialValue: _username,
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return 'Ange ett användarnamn';
|
|
}
|
|
return null;
|
|
},
|
|
onSaved: (value) => _username = value!,
|
|
),
|
|
TextFormField(
|
|
decoration: const InputDecoration(labelText: 'E-post'),
|
|
initialValue: _email,
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return 'Ange en e-postadress';
|
|
}
|
|
if (!RegExp(r'^[^@]+@[^@]+\.[^@]+').hasMatch(value)) {
|
|
return 'Ange en giltig e-postadress';
|
|
}
|
|
return null;
|
|
},
|
|
onSaved: (value) => _email = value!,
|
|
),
|
|
const SizedBox(height: 20),
|
|
ElevatedButton(
|
|
onPressed: _updateProfile,
|
|
child: const Text('Spara'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|