Remove outdated Flutter migration documents and add new technical descriptions and profile repository implementation
- 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>
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../../core/api/api_client.dart';
|
||||
import '../../../core/api/guarded_api_call.dart';
|
||||
import '../../../core/api/api_exception.dart';
|
||||
|
||||
final profileRepositoryProvider = Provider<ProfileRepository>((ref) {
|
||||
final apiClient = ref.read(apiClientProvider);
|
||||
return ProfileRepository(apiClient);
|
||||
});
|
||||
|
||||
class ProfileRepository {
|
||||
final ApiClient _apiClient;
|
||||
|
||||
ProfileRepository(this._apiClient);
|
||||
|
||||
Future<Map<String, dynamic>> getProfile() async {
|
||||
return guardedApiCall(
|
||||
() => _apiClient.getJson('/api/profile'),
|
||||
);
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> updateProfile(Map<String, dynamic> profileData) async {
|
||||
return guardedApiCall(
|
||||
() => _apiClient.patchJson('/api/profile', profileData),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,122 @@
|
||||
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 StatelessWidget {
|
||||
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 const Center(
|
||||
child: Text('Profilsida (grundversion)'),
|
||||
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'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user