feat: integrate ErrorService for consistent error display and standardize bloc error messages.
This commit is contained in:
@@ -19,7 +19,9 @@
|
||||
/// The component automatically loads account data when initialized and
|
||||
/// provides a clean interface for managing group-based expenses.
|
||||
library;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../services/error_service.dart';
|
||||
import 'package:travel_mate/blocs/user/user_bloc.dart';
|
||||
import '../../models/account.dart';
|
||||
import '../../blocs/account/account_bloc.dart';
|
||||
@@ -45,10 +47,10 @@ class _AccountContentState extends State<AccountContent> {
|
||||
/// Repository for group data operations used for navigation
|
||||
final _groupRepository = GroupRepository(); // Ajouter cette ligne
|
||||
|
||||
@override
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
|
||||
// Load immediately without waiting for the next frame
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_loadInitialData();
|
||||
@@ -56,13 +58,13 @@ class _AccountContentState extends State<AccountContent> {
|
||||
}
|
||||
|
||||
/// Loads the initial account data for the current user.
|
||||
///
|
||||
///
|
||||
/// Retrieves the current user from UserBloc and loads their accounts
|
||||
/// using the AccountBloc. Handles errors gracefully with error display.
|
||||
void _loadInitialData() {
|
||||
try {
|
||||
final userState = context.read<UserBloc>().state;
|
||||
|
||||
|
||||
if (userState is user_state.UserLoaded) {
|
||||
final userId = userState.user.id;
|
||||
context.read<AccountBloc>().add(LoadAccountsByUserId(userId));
|
||||
@@ -70,65 +72,50 @@ class _AccountContentState extends State<AccountContent> {
|
||||
throw Exception('User not connected');
|
||||
}
|
||||
} catch (e) {
|
||||
ErrorContent(
|
||||
message: 'Error loading accounts: $e',
|
||||
onRetry: () {},
|
||||
);
|
||||
ErrorContent(message: 'Error loading accounts: $e', onRetry: () {});
|
||||
}
|
||||
}
|
||||
|
||||
/// Navigates to the group expenses page for a specific account.
|
||||
///
|
||||
///
|
||||
/// Retrieves the group associated with the account and navigates to
|
||||
/// the group expenses management page. Shows error messages if the
|
||||
/// group cannot be found or if navigation fails.
|
||||
///
|
||||
///
|
||||
/// Args:
|
||||
/// [account]: The account to navigate to for expense management
|
||||
Future<void> _navigateToGroupExpenses(Account account) async {
|
||||
try {
|
||||
// Retrieve the group associated with the account
|
||||
final group = await _groupRepository.getGroupByTripId(account.tripId);
|
||||
|
||||
|
||||
if (group != null && mounted) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => GroupExpensesPage(
|
||||
account: account,
|
||||
group: group,
|
||||
),
|
||||
builder: (context) =>
|
||||
GroupExpensesPage(account: account, group: group),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Group not found for this account'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
ErrorService().showError(message: 'Group not found for this account');
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Error loading group: $e'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
ErrorService().showError(message: 'Error loading group: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds the main widget for the account content page.
|
||||
///
|
||||
///
|
||||
/// Creates a responsive UI that handles different user and account states:
|
||||
/// - Shows loading indicator for user authentication
|
||||
/// - Displays error content for user errors
|
||||
/// - Builds account content based on account loading states
|
||||
///
|
||||
///
|
||||
/// Returns:
|
||||
/// Widget representing the complete account page UI
|
||||
@override
|
||||
@@ -139,9 +126,12 @@ class _AccountContentState extends State<AccountContent> {
|
||||
listener: (context, accountState) {
|
||||
if (accountState is AccountError) {
|
||||
ErrorContent(
|
||||
message: 'Erreur de chargement des comptes : ${accountState.message}',
|
||||
message:
|
||||
'Erreur de chargement des comptes : ${accountState.message}',
|
||||
onRetry: () {
|
||||
context.read<AccountBloc>().add(LoadAccountsByUserId(user.id));
|
||||
context.read<AccountBloc>().add(
|
||||
LoadAccountsByUserId(user.id),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -156,10 +146,7 @@ class _AccountContentState extends State<AccountContent> {
|
||||
loadingWidget: const Scaffold(
|
||||
body: Center(child: CircularProgressIndicator()),
|
||||
),
|
||||
errorWidget: ErrorContent(
|
||||
message: 'User error',
|
||||
onRetry: () {},
|
||||
),
|
||||
errorWidget: ErrorContent(message: 'User error', onRetry: () {}),
|
||||
noUserWidget: const Scaffold(
|
||||
body: Center(child: Text('Utilisateur non connecté')),
|
||||
),
|
||||
@@ -167,16 +154,16 @@ class _AccountContentState extends State<AccountContent> {
|
||||
}
|
||||
|
||||
/// Builds the main content based on the current account state.
|
||||
///
|
||||
///
|
||||
/// Handles different account loading states and renders appropriate UI:
|
||||
/// - Loading: Shows circular progress indicator with loading message
|
||||
/// - Error: Displays error content with retry functionality
|
||||
/// - Loaded: Renders the accounts list or empty state message
|
||||
///
|
||||
///
|
||||
/// Args:
|
||||
/// [accountState]: Current state of account loading
|
||||
/// [userId]: ID of the current user for reload operations
|
||||
///
|
||||
///
|
||||
/// Returns:
|
||||
/// Widget representing the account content UI
|
||||
Widget _buildContent(AccountState accountState, String userId) {
|
||||
@@ -188,11 +175,11 @@ class _AccountContentState extends State<AccountContent> {
|
||||
CircularProgressIndicator(),
|
||||
SizedBox(height: 16),
|
||||
Text('Chargement des comptes...'),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (accountState is AccountError) {
|
||||
return ErrorContent(
|
||||
message: 'Erreur de chargement des comptes...',
|
||||
@@ -223,15 +210,15 @@ class _AccountContentState extends State<AccountContent> {
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
/// Builds the empty state widget when no accounts are found.
|
||||
///
|
||||
///
|
||||
/// Displays a user-friendly message explaining that accounts are
|
||||
/// automatically created when trips are created. Shows an icon
|
||||
/// and informative text to guide the user.
|
||||
///
|
||||
///
|
||||
/// Returns:
|
||||
/// Widget representing the empty accounts state
|
||||
Widget _buildEmptyState() {
|
||||
@@ -241,7 +228,11 @@ class _AccountContentState extends State<AccountContent> {
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(Icons.account_balance_wallet, size: 80, color: Colors.grey),
|
||||
const Icon(
|
||||
Icons.account_balance_wallet,
|
||||
size: 80,
|
||||
color: Colors.grey,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
const Text(
|
||||
'No accounts found',
|
||||
@@ -260,15 +251,15 @@ class _AccountContentState extends State<AccountContent> {
|
||||
}
|
||||
|
||||
/// Builds a scrollable list of user accounts with pull-to-refresh functionality.
|
||||
///
|
||||
///
|
||||
/// Creates a RefreshIndicator-wrapped ListView that displays all user accounts
|
||||
/// in card format. Includes a header with title and description, and renders
|
||||
/// each account using the _buildSimpleAccountCard method.
|
||||
///
|
||||
///
|
||||
/// Args:
|
||||
/// [accounts]: List of accounts to display
|
||||
/// [userId]: Current user ID for refresh operations
|
||||
///
|
||||
///
|
||||
/// Returns:
|
||||
/// Widget containing the accounts list with pull-to-refresh capability
|
||||
Widget _buildAccountsList(List<Account> accounts, String userId) {
|
||||
@@ -280,28 +271,28 @@ class _AccountContentState extends State<AccountContent> {
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [
|
||||
...accounts.map((account) {
|
||||
...accounts.map((account) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 12),
|
||||
child: _buildSimpleAccountCard(account),
|
||||
);
|
||||
})
|
||||
}),
|
||||
],
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/// Builds an individual account card with account information.
|
||||
///
|
||||
///
|
||||
/// Creates a Material Design card displaying account details including:
|
||||
/// - Account name with color-coded avatar
|
||||
/// - Member count and member names (up to 2 displayed)
|
||||
/// - Navigation capability to group expenses
|
||||
/// - Error handling for card rendering issues
|
||||
///
|
||||
///
|
||||
/// Args:
|
||||
/// [account]: Account object containing account details
|
||||
///
|
||||
///
|
||||
/// Returns:
|
||||
/// Widget representing a single account card
|
||||
Widget _buildSimpleAccountCard(Account account) {
|
||||
@@ -309,9 +300,10 @@ class _AccountContentState extends State<AccountContent> {
|
||||
final colors = [Colors.blue, Colors.purple, Colors.green, Colors.orange];
|
||||
final color = colors[account.name.hashCode.abs() % colors.length];
|
||||
|
||||
String memberInfo = '${account.members.length} member${account.members.length > 1 ? 's' : ''}';
|
||||
String memberInfo =
|
||||
'${account.members.length} member${account.members.length > 1 ? 's' : ''}';
|
||||
|
||||
if(account.members.isNotEmpty){
|
||||
if (account.members.isNotEmpty) {
|
||||
final names = account.members
|
||||
.take(2)
|
||||
.map((m) => m.pseudo.isNotEmpty ? m.pseudo : m.firstName)
|
||||
@@ -324,15 +316,19 @@ class _AccountContentState extends State<AccountContent> {
|
||||
child: ListTile(
|
||||
leading: CircleAvatar(
|
||||
backgroundColor: color,
|
||||
child: const Icon(Icons.account_balance_wallet, color: Colors.white),
|
||||
child: const Icon(
|
||||
Icons.account_balance_wallet,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
title: Text(
|
||||
account.name,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
subtitle: Text(memberInfo),
|
||||
trailing: const Icon(Icons.chevron_right),
|
||||
onTap: () => _navigateToGroupExpenses(account), // Navigate to group expenses
|
||||
onTap: () =>
|
||||
_navigateToGroupExpenses(account), // Navigate to group expenses
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
@@ -341,8 +337,8 @@ class _AccountContentState extends State<AccountContent> {
|
||||
child: const ListTile(
|
||||
leading: Icon(Icons.error, color: Colors.red),
|
||||
title: Text('Display error'),
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import '../../services/activity_cache_service.dart';
|
||||
import '../loading/laoding_content.dart';
|
||||
import '../../blocs/user/user_bloc.dart';
|
||||
import '../../blocs/user/user_state.dart';
|
||||
import '../../services/error_service.dart';
|
||||
|
||||
class ActivitiesPage extends StatefulWidget {
|
||||
final Trip trip;
|
||||
@@ -120,22 +121,15 @@ class _ActivitiesPageState extends State<ActivitiesPage>
|
||||
return BlocListener<ActivityBloc, ActivityState>(
|
||||
listener: (context, state) {
|
||||
if (state is ActivityError) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(state.message),
|
||||
backgroundColor: Colors.red,
|
||||
action: SnackBarAction(
|
||||
label: 'Réessayer',
|
||||
textColor: Colors.white,
|
||||
onPressed: () {
|
||||
if (_tabController.index == 2) {
|
||||
_searchGoogleActivities();
|
||||
} else {
|
||||
_loadActivities();
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
ErrorService().showError(
|
||||
message: state.message,
|
||||
onRetry: () {
|
||||
if (_tabController.index == 2) {
|
||||
_searchGoogleActivities();
|
||||
} else {
|
||||
_loadActivities();
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -152,20 +146,14 @@ class _ActivitiesPageState extends State<ActivitiesPage>
|
||||
});
|
||||
|
||||
// Afficher un feedback de succès
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('${state.activity.name} ajoutée au voyage !'),
|
||||
duration: const Duration(seconds: 2),
|
||||
backgroundColor: Colors.green,
|
||||
action: SnackBarAction(
|
||||
label: 'Voir',
|
||||
textColor: Colors.white,
|
||||
onPressed: () {
|
||||
// Revenir à l'onglet des activités du voyage
|
||||
_tabController.animateTo(0);
|
||||
},
|
||||
),
|
||||
),
|
||||
// Afficher un feedback de succès
|
||||
ErrorService().showSnackbar(
|
||||
message: '${state.activity.name} ajoutée au voyage !',
|
||||
isError: false,
|
||||
onRetry: () {
|
||||
// Revenir à l'onglet des activités du voyage
|
||||
_tabController.animateTo(0);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -217,21 +205,13 @@ class _ActivitiesPageState extends State<ActivitiesPage>
|
||||
_tripActivities.add(state.newlyAddedActivity!);
|
||||
});
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
ErrorService().showSnackbar(
|
||||
message:
|
||||
'${state.newlyAddedActivity!.name} ajoutée au voyage !',
|
||||
),
|
||||
duration: const Duration(seconds: 2),
|
||||
backgroundColor: Colors.green,
|
||||
action: SnackBarAction(
|
||||
label: 'Voir',
|
||||
textColor: Colors.white,
|
||||
onPressed: () {
|
||||
_tabController.animateTo(0);
|
||||
},
|
||||
),
|
||||
),
|
||||
isError: false,
|
||||
onRetry: () {
|
||||
_tabController.animateTo(0);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -1026,12 +1006,9 @@ class _ActivitiesPageState extends State<ActivitiesPage>
|
||||
|
||||
// Si l'activité a été trouvée et que l'utilisateur a déjà voté
|
||||
if (currentActivity.id.isNotEmpty && currentActivity.hasUserVoted(userId)) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Vous avez déjà voté pour cette activité'),
|
||||
backgroundColor: Colors.orange,
|
||||
duration: Duration(seconds: 2),
|
||||
),
|
||||
ErrorService().showSnackbar(
|
||||
message: 'Vous avez déjà voté pour cette activité',
|
||||
isError: true,
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -1044,13 +1021,7 @@ class _ActivitiesPageState extends State<ActivitiesPage>
|
||||
final message = vote == 1
|
||||
? 'Vote positif ajouté !'
|
||||
: 'Vote négatif ajouté !';
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(message),
|
||||
duration: const Duration(seconds: 1),
|
||||
backgroundColor: vote == 1 ? Colors.green : Colors.orange,
|
||||
),
|
||||
);
|
||||
ErrorService().showSnackbar(message: message, isError: false);
|
||||
}
|
||||
|
||||
void _addGoogleActivityToTrip(Activity activity) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:travel_mate/components/error/error_content.dart';
|
||||
import '../../services/error_service.dart';
|
||||
import 'package:travel_mate/components/group/chat_group_content.dart';
|
||||
import 'package:travel_mate/components/widgets/user_state_widget.dart';
|
||||
import '../../blocs/user/user_bloc.dart';
|
||||
@@ -50,19 +50,12 @@ class _GroupContentState extends State<GroupContent> {
|
||||
return BlocConsumer<GroupBloc, GroupState>(
|
||||
listener: (context, groupState) {
|
||||
if (groupState is GroupOperationSuccess) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(groupState.message),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
ErrorService().showSnackbar(
|
||||
message: groupState.message,
|
||||
isError: false,
|
||||
);
|
||||
} else if (groupState is GroupError) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(groupState.message),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
ErrorService().showError(message: groupState.message);
|
||||
}
|
||||
},
|
||||
builder: (context, groupState) {
|
||||
@@ -209,8 +202,7 @@ class _GroupContentState extends State<GroupContent> {
|
||||
if (mounted) {
|
||||
if (retry) {
|
||||
if (userId == '') {
|
||||
showErrorDialog(
|
||||
context,
|
||||
ErrorService().showError(
|
||||
title: 'Erreur utilisateur',
|
||||
message: 'Utilisateur non connecté. Veuillez vous reconnecter.',
|
||||
icon: Icons.error,
|
||||
@@ -220,8 +212,7 @@ class _GroupContentState extends State<GroupContent> {
|
||||
},
|
||||
);
|
||||
} else {
|
||||
showErrorDialog(
|
||||
context,
|
||||
ErrorService().showError(
|
||||
title: 'Erreur de chargement',
|
||||
message: error,
|
||||
icon: Icons.cloud_off,
|
||||
@@ -232,8 +223,7 @@ class _GroupContentState extends State<GroupContent> {
|
||||
);
|
||||
}
|
||||
} else {
|
||||
showErrorDialog(
|
||||
context,
|
||||
ErrorService().showError(
|
||||
title: 'Erreur',
|
||||
message: error,
|
||||
icon: Icons.error,
|
||||
|
||||
Reference in New Issue
Block a user