Add loading and success snackbar helpers: Introduced showLoadingDialog and showSuccessSnackBar methods in SpaceManagementCommunityDialogHelper for consistent loading indicators and success messages across community dialogs. Updated CreateCommunityDialog and EditCommunityDialog to utilize these new helpers, enhancing user experience and maintainability.

This commit is contained in:
Faris Armoush
2025-07-06 11:17:28 +03:00
parent dd735032ea
commit 823d86fd80
6 changed files with 61 additions and 30 deletions

View File

@ -18,4 +18,19 @@ abstract final class SpaceManagementCommunityDialogHelper {
builder: (_) => EditCommunityDialog(community: community), builder: (_) => EditCommunityDialog(community: community),
); );
} }
static void showLoadingDialog(BuildContext context) => showDialog<void>(
context: context,
barrierDismissible: false,
builder: (context) => const Center(
child: CircularProgressIndicator(),
),
);
static void showSuccessSnackBar(BuildContext context, String message) =>
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
),
);
} }

View File

@ -16,6 +16,7 @@ class CommunitiesBloc extends Bloc<CommunitiesEvent, CommunitiesState> {
on<LoadCommunities>(_onLoadCommunities); on<LoadCommunities>(_onLoadCommunities);
on<LoadMoreCommunities>(_onLoadMoreCommunities); on<LoadMoreCommunities>(_onLoadMoreCommunities);
on<InsertCommunity>(_onInsertCommunity); on<InsertCommunity>(_onInsertCommunity);
on<CommunitiesUpdateCommunity>(_onCommunitiesUpdateCommunity);
} }
final CommunitiesService _communitiesService; final CommunitiesService _communitiesService;
@ -114,4 +115,17 @@ class CommunitiesBloc extends Bloc<CommunitiesEvent, CommunitiesState> {
) { ) {
emit(state.copyWith(communities: [event.community, ...state.communities])); emit(state.copyWith(communities: [event.community, ...state.communities]));
} }
void _onCommunitiesUpdateCommunity(
CommunitiesUpdateCommunity event,
Emitter<CommunitiesState> emit,
) {
emit(
state.copyWith(
communities: state.communities
.map((e) => e.uuid == event.community.uuid ? event.community : e)
.toList(),
),
);
}
} }

View File

@ -31,3 +31,12 @@ final class InsertCommunity extends CommunitiesEvent {
@override @override
List<Object?> get props => [community]; List<Object?> get props => [community];
} }
final class CommunitiesUpdateCommunity extends CommunitiesEvent {
const CommunitiesUpdateCommunity(this.community);
final CommunityModel community;
@override
List<Object?> get props => [community];
}

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/space_management_v2/main_module/shared/helpers/space_management_community_dialog_helper.dart';
import 'package:syncrow_web/pages/space_management_v2/main_module/shared/widgets/community_dialog.dart'; import 'package:syncrow_web/pages/space_management_v2/main_module/shared/widgets/community_dialog.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/presentation/bloc/communities_bloc.dart'; import 'package:syncrow_web/pages/space_management_v2/modules/communities/presentation/bloc/communities_bloc.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/presentation/communities_tree_selection_bloc/communities_tree_selection_bloc.dart'; import 'package:syncrow_web/pages/space_management_v2/modules/communities/presentation/communities_tree_selection_bloc/communities_tree_selection_bloc.dart';
@ -20,19 +21,15 @@ class CreateCommunityDialog extends StatelessWidget {
child: BlocConsumer<CreateCommunityBloc, CreateCommunityState>( child: BlocConsumer<CreateCommunityBloc, CreateCommunityState>(
listener: (context, state) { listener: (context, state) {
switch (state) { switch (state) {
case CreateCommunityLoading(): case CreateCommunityLoading() || CreateCommunityInitial():
showDialog<void>( SpaceManagementCommunityDialogHelper.showLoadingDialog(context);
context: context,
builder: (context) => const Center(
child: CircularProgressIndicator(),
),
);
break; break;
case CreateCommunitySuccess(:final community): case CreateCommunitySuccess(:final community):
Navigator.of(context).pop(); Navigator.of(context).pop();
Navigator.of(context).pop(); Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar( SpaceManagementCommunityDialogHelper.showSuccessSnackBar(
const SnackBar(content: Text('Community created successfully')), context,
'${community.name} community created successfully',
); );
context.read<CommunitiesBloc>().add( context.read<CommunitiesBloc>().add(
InsertCommunity(community), InsertCommunity(community),
@ -44,8 +41,6 @@ class CreateCommunityDialog extends StatelessWidget {
case CreateCommunityFailure(): case CreateCommunityFailure():
Navigator.of(context).pop(); Navigator.of(context).pop();
break; break;
default:
break;
} }
}, },
builder: (BuildContext context, CreateCommunityState state) { builder: (BuildContext context, CreateCommunityState state) {

View File

@ -21,10 +21,10 @@ final class UpdateCommunitySuccess extends UpdateCommunityState {
} }
final class UpdateCommunityFailure extends UpdateCommunityState { final class UpdateCommunityFailure extends UpdateCommunityState {
final String message; final String errorMessage;
const UpdateCommunityFailure(this.message); const UpdateCommunityFailure(this.errorMessage);
@override @override
List<Object> get props => [message]; List<Object> get props => [errorMessage];
} }

View File

@ -1,7 +1,9 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/space_management_v2/main_module/shared/helpers/space_management_community_dialog_helper.dart';
import 'package:syncrow_web/pages/space_management_v2/main_module/shared/widgets/community_dialog.dart'; import 'package:syncrow_web/pages/space_management_v2/main_module/shared/widgets/community_dialog.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart'; import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/presentation/bloc/communities_bloc.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/update_community/data/services/remote_update_community_service.dart'; import 'package:syncrow_web/pages/space_management_v2/modules/update_community/data/services/remote_update_community_service.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/update_community/presentation/bloc/update_community_bloc.dart'; import 'package:syncrow_web/pages/space_management_v2/modules/update_community/presentation/bloc/update_community_bloc.dart';
import 'package:syncrow_web/services/api/http_service.dart'; import 'package:syncrow_web/services/api/http_service.dart';
@ -14,39 +16,35 @@ class EditCommunityDialog extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocProvider( return BlocProvider(
create: (_) => create: (_) => UpdateCommunityBloc(
UpdateCommunityBloc(RemoteUpdateCommunityService(HTTPService())), RemoteUpdateCommunityService(HTTPService()),
),
child: BlocConsumer<UpdateCommunityBloc, UpdateCommunityState>( child: BlocConsumer<UpdateCommunityBloc, UpdateCommunityState>(
listener: (context, state) { listener: (context, state) {
switch (state) { switch (state) {
case UpdateCommunityLoading(): case UpdateCommunityInitial() || UpdateCommunityLoading():
showDialog<void>( SpaceManagementCommunityDialogHelper.showLoadingDialog(context);
context: context,
builder: (context) => const Center(
child: CircularProgressIndicator(),
),
);
break; break;
case UpdateCommunitySuccess(:final community): case UpdateCommunitySuccess(:final community):
Navigator.of(context).pop(); Navigator.of(context).pop();
Navigator.of(context).pop(); Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar( SpaceManagementCommunityDialogHelper.showSuccessSnackBar(
SnackBar( context,
content: Text('${community.name} updated successfully'), '${community.name} community updated successfully',
),
); );
context.read<CommunitiesBloc>().add(
CommunitiesUpdateCommunity(community),
);
break; break;
case UpdateCommunityFailure(): case UpdateCommunityFailure():
Navigator.of(context).pop(); Navigator.of(context).pop();
break; break;
default:
break;
} }
}, },
builder: (context, state) => CommunityDialog( builder: (context, state) => CommunityDialog(
title: const Text('Edit Community'), title: const Text('Edit Community'),
initialName: community.name, initialName: community.name,
errorMessage: state is UpdateCommunityFailure ? state.message : null, errorMessage: state is UpdateCommunityFailure ? state.errorMessage : null,
onSubmit: (name) => context.read<UpdateCommunityBloc>().add( onSubmit: (name) => context.read<UpdateCommunityBloc>().add(
UpdateCommunity(community.copyWith(name: name)), UpdateCommunity(community.copyWith(name: name)),
), ),