Merge pull request #54 from SyncrowIOT/bugfix/add-new-community

Bugfix/add new community
This commit is contained in:
Abdullah
2024-11-29 14:41:52 +03:00
committed by GitHub
11 changed files with 320 additions and 199 deletions

View File

@ -7,20 +7,24 @@ import 'package:syncrow_web/pages/spaces_management/bloc/space_management_state.
import 'package:syncrow_web/services/product_api.dart'; import 'package:syncrow_web/services/product_api.dart';
import 'package:syncrow_web/services/space_mana_api.dart'; import 'package:syncrow_web/services/space_mana_api.dart';
class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementState> { class SpaceManagementBloc
extends Bloc<SpaceManagementEvent, SpaceManagementState> {
final CommunitySpaceManagementApi _api; final CommunitySpaceManagementApi _api;
final ProductApi _productApi; final ProductApi _productApi;
List<ProductModel>? _cachedProducts; List<ProductModel>? _cachedProducts;
SpaceManagementBloc(this._api, this._productApi) : super(SpaceManagementInitial()) { SpaceManagementBloc(this._api, this._productApi)
: super(SpaceManagementInitial()) {
on<LoadCommunityAndSpacesEvent>(_onLoadCommunityAndSpaces); on<LoadCommunityAndSpacesEvent>(_onLoadCommunityAndSpaces);
on<UpdateSpacePositionEvent>(_onUpdateSpacePosition); on<UpdateSpacePositionEvent>(_onUpdateSpacePosition);
on<CreateCommunityEvent>(_onCreateCommunity); on<CreateCommunityEvent>(_onCreateCommunity);
on<SaveSpacesEvent>(_onSaveSpaces); on<SelectCommunityEvent>(_onSelectCommunity);
on<FetchProductsEvent>(_onFetchProducts);
on<DeleteCommunityEvent>(_onCommunityDelete); on<DeleteCommunityEvent>(_onCommunityDelete);
on<UpdateCommunityEvent>(_onUpdateCommunity); on<UpdateCommunityEvent>(_onUpdateCommunity);
on<SaveSpacesEvent>(_onSaveSpaces);
on<FetchProductsEvent>(_onFetchProducts);
on<SelectSpaceEvent>(_onSelectSpace);
} }
void _onUpdateCommunity( void _onUpdateCommunity(
@ -30,22 +34,23 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
final previousState = state; final previousState = state;
try { try {
emit(SpaceManagementLoading()); emit(SpaceManagementLoading());
final success = await _api.updateCommunity(event.communityUuid, event.name); final success =
await _api.updateCommunity(event.communityUuid, event.name);
if (success) { if (success) {
if (previousState is SpaceManagementLoaded) { if (previousState is SpaceManagementLoaded) {
final updatedCommunities = List<CommunityModel>.from(previousState.communities); final updatedCommunities =
for(var community in updatedCommunities){ List<CommunityModel>.from(previousState.communities);
if(community.uuid == event.communityUuid){ for (var community in updatedCommunities) {
if (community.uuid == event.communityUuid) {
community.name = event.name; community.name = event.name;
break; break;
} }
} }
emit(SpaceManagementLoaded( emit(SpaceManagementLoaded(
communities: updatedCommunities, communities: updatedCommunities,
products: previousState.products, products: previousState.products,
selectedCommunity: previousState.selectedCommunity, selectedCommunity: previousState.selectedCommunity,
)); ));
} }
} else { } else {
emit(const SpaceManagementError('Failed to update the community.')); emit(const SpaceManagementError('Failed to update the community.'));
@ -55,40 +60,42 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
} }
} }
void _onloadProducts() async {
if (_cachedProducts == null) {
final products = await _productApi.fetchProducts();
_cachedProducts = products;
}
}
void _onFetchProducts( void _onFetchProducts(
FetchProductsEvent event, FetchProductsEvent event,
Emitter<SpaceManagementState> emit, Emitter<SpaceManagementState> emit,
) async { ) async {
if (_cachedProducts != null) {
// Products are already cached, no need to fetch again
return;
}
try { try {
final products = await _productApi.fetchProducts(); _onloadProducts();
_cachedProducts = products; // Cache the products locally
} catch (e) { } catch (e) {
emit(SpaceManagementError('Error fetching products: $e')); emit(SpaceManagementError('Error fetching products: $e'));
} }
} }
Future<List<SpaceModel>> _fetchSpacesForCommunity(
String communityUuid) async {
return await _api.getSpaceHierarchy(communityUuid);
}
void _onLoadCommunityAndSpaces( void _onLoadCommunityAndSpaces(
LoadCommunityAndSpacesEvent event, LoadCommunityAndSpacesEvent event,
Emitter<SpaceManagementState> emit, Emitter<SpaceManagementState> emit,
) async { ) async {
emit(SpaceManagementLoading()); emit(SpaceManagementLoading());
try { try {
if (_cachedProducts == null) { _onloadProducts();
final products = await _productApi.fetchProducts();
_cachedProducts = products;
}
// Fetch all communities
List<CommunityModel> communities = await _api.fetchCommunities(); List<CommunityModel> communities = await _api.fetchCommunities();
List<CommunityModel> updatedCommunities = await Future.wait( List<CommunityModel> updatedCommunities = await Future.wait(
communities.map((community) async { communities.map((community) async {
List<SpaceModel> spaces = await _api.getSpaceHierarchy(community.uuid); List<SpaceModel> spaces =
await _fetchSpacesForCommunity(community.uuid);
return CommunityModel( return CommunityModel(
uuid: community.uuid, uuid: community.uuid,
createdAt: community.createdAt, createdAt: community.createdAt,
@ -101,7 +108,8 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
}).toList(), }).toList(),
); );
emit(SpaceManagementLoaded(communities: updatedCommunities, products: _cachedProducts ?? [])); emit(SpaceManagementLoaded(
communities: updatedCommunities, products: _cachedProducts ?? []));
} catch (e) { } catch (e) {
emit(SpaceManagementError('Error loading communities and spaces: $e')); emit(SpaceManagementError('Error loading communities and spaces: $e'));
} }
@ -139,16 +147,19 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
emit(SpaceManagementLoading()); emit(SpaceManagementLoading());
try { try {
CommunityModel? newCommunity = await _api.createCommunity(event.name, event.description); CommunityModel? newCommunity =
await _api.createCommunity(event.name, event.description);
if (newCommunity != null) { if (newCommunity != null) {
if (previousState is SpaceManagementLoaded) { if (previousState is SpaceManagementLoaded) {
final updatedCommunities = List<CommunityModel>.from(previousState.communities) final updatedCommunities =
..add(newCommunity); List<CommunityModel>.from(previousState.communities)
..add(newCommunity);
emit(SpaceManagementLoaded( emit(SpaceManagementLoaded(
communities: updatedCommunities, communities: updatedCommunities,
products: _cachedProducts ?? [], products: _cachedProducts ?? [],
selectedCommunity: newCommunity)); selectedCommunity: null,
selectedSpace: null));
} }
} else { } else {
emit(const SpaceManagementError('Error creating community')); emit(const SpaceManagementError('Error creating community'));
@ -158,6 +169,53 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
} }
} }
void _onSelectCommunity(
SelectCommunityEvent event,
Emitter<SpaceManagementState> emit,
) async {
_handleCommunitySpaceStateUpdate(
emit: emit,
selectedCommunity: event.selectedCommunity,
selectedSpace: null,
);
}
void _onSelectSpace(
SelectSpaceEvent event,
Emitter<SpaceManagementState> emit,
) {
_handleCommunitySpaceStateUpdate(
emit: emit,
selectedCommunity: event.selectedCommunity,
selectedSpace: event.selectedSpace,
);
}
void _handleCommunitySpaceStateUpdate({
required Emitter<SpaceManagementState> emit,
CommunityModel? selectedCommunity,
SpaceModel? selectedSpace,
}) {
final previousState = state;
emit(SpaceManagementLoading());
try {
if (previousState is SpaceManagementLoaded) {
final communities = List<CommunityModel>.from(
(previousState as dynamic).communities,
);
emit(SpaceManagementLoaded(
communities: communities,
products: _cachedProducts ?? [],
selectedCommunity: selectedCommunity,
selectedSpace: selectedSpace,
));
}
} catch (e) {
emit(SpaceManagementError('Error updating state: $e'));
}
}
void _onSaveSpaces( void _onSaveSpaces(
SaveSpacesEvent event, SaveSpacesEvent event,
Emitter<SpaceManagementState> emit, Emitter<SpaceManagementState> emit,
@ -166,17 +224,54 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
emit(SpaceManagementLoading()); emit(SpaceManagementLoading());
try { try {
final updatedSpaces = await saveSpacesHierarchically(event.spaces, event.communityUuid); final updatedSpaces =
await saveSpacesHierarchically(event.spaces, event.communityUuid);
final allSpaces = await _fetchSpacesForCommunity(event.communityUuid);
emit(SpaceCreationSuccess(spaces: updatedSpaces)); emit(SpaceCreationSuccess(spaces: updatedSpaces));
add(LoadCommunityAndSpacesEvent());
if (previousState is SpaceManagementLoaded) {
_updateLoadedState(
previousState,
allSpaces,
event.communityUuid,
emit,
);
} else {
add(LoadCommunityAndSpacesEvent());
}
} catch (e) { } catch (e) {
emit(SpaceManagementError('Error saving spaces: $e')); emit(SpaceManagementError('Error saving spaces: $e'));
if (previousState is SpaceManagementLoaded) { if (previousState is SpaceManagementLoaded) {
emit(previousState); emit(previousState);
} }
} }
} }
void _updateLoadedState(
SpaceManagementLoaded previousState,
List<SpaceModel> allSpaces,
String communityUuid,
Emitter<SpaceManagementState> emit,
) {
final communities = List<CommunityModel>.from(previousState.communities);
for (var community in communities) {
if (community.uuid == communityUuid) {
community.spaces = allSpaces;
emit(SpaceManagementLoaded(
communities: communities,
products: _cachedProducts ?? [],
selectedCommunity: community,
selectedSpace: null,
));
return;
}
}
}
Future<List<SpaceModel>> saveSpacesHierarchically( Future<List<SpaceModel>> saveSpacesHierarchically(
List<SpaceModel> spaces, String communityUuid) async { List<SpaceModel> spaces, String communityUuid) async {
final orderedSpaces = flattenHierarchy(spaces); final orderedSpaces = flattenHierarchy(spaces);
@ -187,7 +282,6 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
for (var parent in parentsToDelete) { for (var parent in parentsToDelete) {
try { try {
// Ensure parent.uuid is not null before calling the API
if (parent.uuid != null) { if (parent.uuid != null) {
await _api.deleteSpace(communityUuid, parent.uuid!); await _api.deleteSpace(communityUuid, parent.uuid!);
} }

View File

@ -1,5 +1,6 @@
import 'package:equatable/equatable.dart'; import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/spaces_management/model/community_model.dart';
import 'package:syncrow_web/pages/spaces_management/model/space_model.dart'; // Import for Offset import 'package:syncrow_web/pages/spaces_management/model/space_model.dart'; // Import for Offset
abstract class SpaceManagementEvent extends Equatable { abstract class SpaceManagementEvent extends Equatable {
@ -83,17 +84,6 @@ class CreateCommunityEvent extends SpaceManagementEvent {
List<Object> get props => [name, description]; List<Object> get props => [name, description];
} }
class FetchProductsEvent extends SpaceManagementEvent {}
class LoadSpaceHierarchyEvent extends SpaceManagementEvent {
final String communityId;
const LoadSpaceHierarchyEvent({required this.communityId});
@override
List<Object> get props => [communityId];
}
class UpdateCommunityEvent extends SpaceManagementEvent { class UpdateCommunityEvent extends SpaceManagementEvent {
final String communityUuid; final String communityUuid;
final String name; final String name;
@ -106,3 +96,38 @@ class UpdateCommunityEvent extends SpaceManagementEvent {
@override @override
List<Object> get props => [communityUuid, name]; List<Object> get props => [communityUuid, name];
} }
class SelectCommunityEvent extends SpaceManagementEvent {
final CommunityModel? selectedCommunity;
const SelectCommunityEvent({
required this.selectedCommunity,
});
@override
List<Object> get props => [];
}
class SelectSpaceEvent extends SpaceManagementEvent {
final CommunityModel? selectedCommunity;
final SpaceModel? selectedSpace;
const SelectSpaceEvent({
required this.selectedCommunity,
required this.selectedSpace,
});
@override
List<Object> get props => [];
}
class FetchProductsEvent extends SpaceManagementEvent {}
class LoadSpaceHierarchyEvent extends SpaceManagementEvent {
final String communityId;
const LoadSpaceHierarchyEvent({required this.communityId});
@override
List<Object> get props => [communityId];
}

View File

@ -17,10 +17,14 @@ class SpaceManagementLoading extends SpaceManagementState {}
class SpaceManagementLoaded extends SpaceManagementState { class SpaceManagementLoaded extends SpaceManagementState {
final List<CommunityModel> communities; final List<CommunityModel> communities;
final List<ProductModel> products; final List<ProductModel> products;
CommunityModel? selectedCommunity; // Include products in the state CommunityModel? selectedCommunity;
SpaceModel? selectedSpace;
SpaceManagementLoaded( SpaceManagementLoaded(
{required this.communities, required this.products, this.selectedCommunity}); {required this.communities,
required this.products,
this.selectedCommunity,
this.selectedSpace});
} }
class SpaceCreationSuccess extends SpaceManagementState { class SpaceCreationSuccess extends SpaceManagementState {

View File

@ -44,7 +44,8 @@ class SpaceModel {
this.selectedProducts = const [], this.selectedProducts = const [],
}) : internalId = internalId ?? const Uuid().v4(); }) : internalId = internalId ?? const Uuid().v4();
factory SpaceModel.fromJson(Map<String, dynamic> json, {String? parentInternalId}) { factory SpaceModel.fromJson(Map<String, dynamic> json,
{String? parentInternalId}) {
final String internalId = json['internalId'] ?? const Uuid().v4(); final String internalId = json['internalId'] ?? const Uuid().v4();
final List<SpaceModel> children = json['children'] != null final List<SpaceModel> children = json['children'] != null
@ -56,7 +57,7 @@ class SpaceModel {
}).toList() }).toList()
: []; : [];
return SpaceModel( final instance = SpaceModel(
internalId: internalId, internalId: internalId,
uuid: json['uuid'] ?? '', uuid: json['uuid'] ?? '',
spaceTuyaUuid: json['spaceTuyaUuid'], spaceTuyaUuid: json['spaceTuyaUuid'],
@ -72,11 +73,14 @@ class SpaceModel {
isPrivate: json['parent']?['isPrivate'] ?? false, isPrivate: json['parent']?['isPrivate'] ?? false,
invitationCode: json['parent']?['invitationCode'], invitationCode: json['parent']?['invitationCode'],
children: [], children: [],
position: Offset(json['parent']?['x'] ?? 0, json['parent']?['y'] ?? 0), position:
Offset(json['parent']?['x'] ?? 0, json['parent']?['y'] ?? 0),
icon: json['parent']?['icon'] ?? Assets.location, icon: json['parent']?['icon'] ?? Assets.location,
) )
: null, : null,
community: json['community'] != null ? CommunityModel.fromJson(json['community']) : null, community: json['community'] != null
? CommunityModel.fromJson(json['community'])
: null,
children: children, children: children,
icon: json['icon'] ?? Assets.location, icon: json['icon'] ?? Assets.location,
position: Offset(json['x'] ?? 0, json['y'] ?? 0), position: Offset(json['x'] ?? 0, json['y'] ?? 0),
@ -90,6 +94,20 @@ class SpaceModel {
}).toList() }).toList()
: [], : [],
); );
if (json['incomingConnections'] != null &&
json['incomingConnections'] is List &&
(json['incomingConnections'] as List).isNotEmpty &&
instance.parent != null) {
final conn = json['incomingConnections'][0];
instance.incomingConnection = Connection(
startSpace: instance.parent ?? instance, // Parent space
endSpace: instance, // This space instance
direction: conn['direction'],
);
}
return instance;
} }
Map<String, dynamic> toMap() { Map<String, dynamic> toMap() {

View File

@ -20,8 +20,6 @@ class SpaceManagementPage extends StatefulWidget {
} }
class SpaceManagementPageState extends State<SpaceManagementPage> { class SpaceManagementPageState extends State<SpaceManagementPage> {
CommunityModel? selectedCommunity;
SpaceModel? selectedSpace;
final CommunitySpaceManagementApi _api = CommunitySpaceManagementApi(); final CommunitySpaceManagementApi _api = CommunitySpaceManagementApi();
final ProductApi _productApi = ProductApi(); final ProductApi _productApi = ProductApi();
Map<String, List<SpaceModel>> communitySpaces = {}; Map<String, List<SpaceModel>> communitySpaces = {};
@ -36,43 +34,23 @@ class SpaceManagementPageState extends State<SpaceManagementPage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocProvider( return BlocProvider(
create: (context) => create: (context) => SpaceManagementBloc(_api, _productApi)
SpaceManagementBloc(_api, _productApi)..add(LoadCommunityAndSpacesEvent()), ..add(LoadCommunityAndSpacesEvent()),
child: WebScaffold( child: WebScaffold(
appBarTitle: Text('Space Management', style: Theme.of(context).textTheme.headlineLarge), appBarTitle: Text('Space Management',
style: Theme.of(context).textTheme.headlineLarge),
enableMenuSidebar: false, enableMenuSidebar: false,
rightBody: const NavigateHomeGridView(), rightBody: const NavigateHomeGridView(),
scaffoldBody: scaffoldBody: BlocBuilder<SpaceManagementBloc, SpaceManagementState>(
BlocBuilder<SpaceManagementBloc, SpaceManagementState>(builder: (context, state) { builder: (context, state) {
if (state is SpaceManagementLoading) { if (state is SpaceManagementLoading) {
return const Center(child: CircularProgressIndicator()); return const Center(child: CircularProgressIndicator());
} else if (state is SpaceManagementLoaded) { } else if (state is SpaceManagementLoaded) {
int selectedIndex = state.communities.indexWhere(
(community) => community.uuid == selectedCommunity?.uuid,
);
if (selectedIndex != -1) {
selectedCommunity = state.communities[selectedIndex];
} else if (state.selectedCommunity != null) {
selectedCommunity = state.selectedCommunity;
} else {
selectedCommunity = null;
selectedSpace = null;
}
return LoadedSpaceView( return LoadedSpaceView(
communities: state.communities, communities: state.communities,
selectedCommunity: selectedCommunity, selectedCommunity: state.selectedCommunity,
selectedSpace: selectedSpace, selectedSpace: state.selectedSpace,
products: state.products, products: state.products,
onCommunitySelected: (community) {
setState(() {
selectedCommunity = community;
});
},
onSpaceSelected: (space) {
setState(() {
selectedSpace = space;
});
},
); );
} else if (state is SpaceManagementError) { } else if (state is SpaceManagementError) {
return Center(child: Text('Error: ${state.errorMessage}')); return Center(child: Text('Error: ${state.errorMessage}'));

View File

@ -12,21 +12,24 @@ class BlankCommunityWidget extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Expanded( return Expanded(
child: Container( child: Container(
color: ColorsManager.whiteColors, // Parent container with white background color:
ColorsManager.whiteColors, // Parent container with white background
child: GridView.builder( child: GridView.builder(
padding: const EdgeInsets.only(left: 40.0, top: 20.0), padding: const EdgeInsets.only(left: 40.0, top: 20.0),
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent( gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 400, // Each item's width will be 400 or less maxCrossAxisExtent: 400, // Each item's width will be 400 or less
mainAxisSpacing: 10, // Spacing between items mainAxisSpacing: 10, // Spacing between items
crossAxisSpacing: 10, // Spacing between items crossAxisSpacing: 10, // Spacing between items
childAspectRatio: 2.0, // Aspect ratio for width:height (e.g., 300:150 = 2.0) childAspectRatio:
2.0, // Aspect ratio for width:height (e.g., 300:150 = 2.0)
), ),
itemCount: 1, // Only one item itemCount: 1, // Only one item
itemBuilder: (context, index) { itemBuilder: (context, index) {
return GestureDetector( return GestureDetector(
onTap: () => _showCreateCommunityDialog(context), onTap: () => _showCreateCommunityDialog(context),
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.center, // Center align the content crossAxisAlignment:
CrossAxisAlignment.center, // Center align the content
children: [ children: [
Expanded( Expanded(
child: AspectRatio( child: AspectRatio(
@ -34,7 +37,7 @@ class BlankCommunityWidget extends StatelessWidget {
child: Container( child: Container(
decoration: ShapeDecoration( decoration: ShapeDecoration(
shape: RoundedRectangleBorder( shape: RoundedRectangleBorder(
side: BorderSide( side: const BorderSide(
width: 4, width: 4,
strokeAlign: BorderSide.strokeAlignOutside, strokeAlign: BorderSide.strokeAlignOutside,
color: ColorsManager.borderColor, color: ColorsManager.borderColor,

View File

@ -86,7 +86,7 @@ class CommunityStructureHeader extends StatelessWidget {
), ),
if (isEditingName) if (isEditingName)
SizedBox( SizedBox(
width: screenWidth * 0.3, width: screenWidth * 0.1,
child: TextField( child: TextField(
controller: nameController, controller: nameController,
decoration: const InputDecoration( decoration: const InputDecoration(

View File

@ -52,7 +52,8 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
void initState() { void initState() {
super.initState(); super.initState();
spaces = widget.spaces.isNotEmpty ? flattenSpaces(widget.spaces) : []; spaces = widget.spaces.isNotEmpty ? flattenSpaces(widget.spaces) : [];
connections = widget.spaces.isNotEmpty ? createConnections(widget.spaces) : []; connections =
widget.spaces.isNotEmpty ? createConnections(widget.spaces) : [];
_adjustCanvasSizeForSpaces(); _adjustCanvasSizeForSpaces();
_nameController = TextEditingController( _nameController = TextEditingController(
text: widget.selectedCommunity?.name ?? '', text: widget.selectedCommunity?.name ?? '',
@ -79,12 +80,14 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
if (oldWidget.spaces != widget.spaces) { if (oldWidget.spaces != widget.spaces) {
setState(() { setState(() {
spaces = widget.spaces.isNotEmpty ? flattenSpaces(widget.spaces) : []; spaces = widget.spaces.isNotEmpty ? flattenSpaces(widget.spaces) : [];
connections = widget.spaces.isNotEmpty ? createConnections(widget.spaces) : []; connections =
widget.spaces.isNotEmpty ? createConnections(widget.spaces) : [];
_adjustCanvasSizeForSpaces(); _adjustCanvasSizeForSpaces();
}); });
} }
if (widget.selectedSpace != oldWidget.selectedSpace && widget.selectedSpace != null) { if (widget.selectedSpace != oldWidget.selectedSpace &&
widget.selectedSpace != null) {
WidgetsBinding.instance.addPostFrameCallback((_) { WidgetsBinding.instance.addPostFrameCallback((_) {
_moveToSpace(widget.selectedSpace!); _moveToSpace(widget.selectedSpace!);
}); });
@ -101,7 +104,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
return Expanded( return Expanded(
child: GestureDetector( child: GestureDetector(
onTap: () { onTap: () {
_deselectSpace(); _deselectSpace(context);
}, },
child: Container( child: Container(
decoration: const BoxDecoration( decoration: const BoxDecoration(
@ -156,9 +159,11 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
children: [ children: [
for (var connection in connections) for (var connection in connections)
Opacity( Opacity(
opacity: opacity: _isHighlightedConnection(connection)
_isHighlightedConnection(connection) ? 1.0 : 0.3, // Adjust opacity ? 1.0
child: CustomPaint(painter: CurvedLinePainter([connection])), : 0.3, // Adjust opacity
child: CustomPaint(
painter: CurvedLinePainter([connection])),
), ),
for (var entry in spaces.asMap().entries) for (var entry in spaces.asMap().entries)
if (entry.value.status != SpaceStatus.deleted) if (entry.value.status != SpaceStatus.deleted)
@ -167,10 +172,12 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
top: entry.value.position.dy, top: entry.value.position.dy,
child: SpaceCardWidget( child: SpaceCardWidget(
index: entry.key, index: entry.key,
onButtonTap: (int index, Offset newPosition, String direction) { onButtonTap: (int index, Offset newPosition,
String direction) {
_showCreateSpaceDialog( _showCreateSpaceDialog(
screenSize, screenSize,
position: spaces[index].position + newPosition, position:
spaces[index].position + newPosition,
parentIndex: index, parentIndex: index,
direction: direction, direction: direction,
); );
@ -183,7 +190,8 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
_updateNodePosition(entry.value, newPosition); _updateNodePosition(entry.value, newPosition);
}, },
buildSpaceContainer: (int index) { buildSpaceContainer: (int index) {
final bool isHighlighted = _isHighlightedSpace(spaces[index]); final bool isHighlighted =
_isHighlightedSpace(spaces[index]);
return Opacity( return Opacity(
opacity: isHighlighted ? 1.0 : 0.3, opacity: isHighlighted ? 1.0 : 0.3,
@ -193,7 +201,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
_showEditSpaceDialog(spaces[index]); _showEditSpaceDialog(spaces[index]);
}, },
onTap: () { onTap: () {
_selectSpace(spaces[index]); _selectSpace(context, spaces[index]);
}, },
icon: spaces[index].icon ?? '', icon: spaces[index].icon ?? '',
name: spaces[index].name, name: spaces[index].name,
@ -210,7 +218,8 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
child: AddSpaceButton( child: AddSpaceButton(
onTap: () { onTap: () {
_showCreateSpaceDialog(screenSize, _showCreateSpaceDialog(screenSize,
canvasHeight: canvasHeight, canvasWidth: canvasWidth); canvasHeight: canvasHeight,
canvasWidth: canvasWidth);
}, },
), ),
), ),
@ -270,12 +279,14 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
builder: (BuildContext context) { builder: (BuildContext context) {
return CreateSpaceDialog( return CreateSpaceDialog(
products: widget.products, products: widget.products,
parentSpace: parentIndex != null? spaces[parentIndex] : null, parentSpace: parentIndex != null ? spaces[parentIndex] : null,
onCreateSpace: (String name, String icon, List<SelectedProduct> selectedProducts) { onCreateSpace: (String name, String icon,
List<SelectedProduct> selectedProducts) {
setState(() { setState(() {
// Set the first space in the center or use passed position // Set the first space in the center or use passed position
Offset centerPosition = position ?? _getCenterPosition(screenSize); Offset centerPosition =
position ?? _getCenterPosition(screenSize);
SpaceModel newSpace = SpaceModel( SpaceModel newSpace = SpaceModel(
name: name, name: name,
icon: icon, icon: icon,
@ -319,7 +330,8 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
icon: space.icon, icon: space.icon,
isEdit: true, isEdit: true,
selectedProducts: space.selectedProducts, selectedProducts: space.selectedProducts,
onCreateSpace: (String name, String icon, List<SelectedProduct> selectedProducts) { onCreateSpace: (String name, String icon,
List<SelectedProduct> selectedProducts) {
setState(() { setState(() {
// Update the space's properties // Update the space's properties
space.name = name; space.name = name;
@ -331,8 +343,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
} }
}); });
}, },
// Pre-fill the dialog with current space data key: Key(space.name),
key: Key(space.name), // Add a unique key to ensure dialog refresh
); );
}, },
); );
@ -465,40 +476,30 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
..scale(1.2); ..scale(1.2);
} }
void _selectSpace(SpaceModel space) { void _selectSpace(BuildContext context, SpaceModel space) {
setState(() { context.read<SpaceManagementBloc>().add(
widget.selectedSpace = space; SelectSpaceEvent(
}); selectedCommunity: widget.selectedCommunity,
selectedSpace: space),
if (widget.onSpaceSelected != null) { );
widget.onSpaceSelected!(space);
}
} }
bool _isHighlightedSpace(SpaceModel space) { bool _isHighlightedSpace(SpaceModel space) {
if (widget.selectedSpace == null) return true; final selectedSpace = widget.selectedSpace;
if (space == widget.selectedSpace) return true; if (selectedSpace == null) return true;
if (widget.selectedSpace?.parent?.internalId == space.internalId) return true;
if (widget.selectedSpace?.children != null) { return space == selectedSpace ||
for (var child in widget.selectedSpace!.children) { selectedSpace.parent?.internalId == space.internalId ||
if (child.internalId == space.internalId) { selectedSpace.children
return true; ?.any((child) => child.internalId == space.internalId) ==
} true;
}
}
return false;
} }
void _deselectSpace() { void _deselectSpace(BuildContext context) {
if (widget.selectedSpace != null) { context.read<SpaceManagementBloc>().add(
setState(() { SelectSpaceEvent(
widget.selectedSpace = null; selectedCommunity: widget.selectedCommunity, selectedSpace: null),
}); );
if (widget.onSpaceSelected != null) {
widget.onSpaceSelected!(null); // Notify parent that no space is selected
}
}
} }
bool _isHighlightedConnection(Connection connection) { bool _isHighlightedConnection(Connection connection) {

View File

@ -10,8 +10,6 @@ class LoadedSpaceView extends StatefulWidget {
final List<CommunityModel> communities; final List<CommunityModel> communities;
final CommunityModel? selectedCommunity; final CommunityModel? selectedCommunity;
final SpaceModel? selectedSpace; final SpaceModel? selectedSpace;
final ValueChanged<CommunityModel>? onCommunitySelected;
final ValueChanged<SpaceModel?>? onSpaceSelected;
final List<ProductModel>? products; final List<ProductModel>? products;
const LoadedSpaceView({ const LoadedSpaceView({
@ -19,8 +17,6 @@ class LoadedSpaceView extends StatefulWidget {
required this.communities, required this.communities,
this.selectedCommunity, this.selectedCommunity,
this.selectedSpace, this.selectedSpace,
required this.onCommunitySelected,
required this.onSpaceSelected,
this.products, this.products,
}); });
@ -37,28 +33,14 @@ class _LoadedStateViewState extends State<LoadedSpaceView> {
Row( Row(
children: [ children: [
SidebarWidget( SidebarWidget(
communities: widget.communities, communities: widget.communities,
onCommunitySelected: widget.onCommunitySelected, selectedSpaceUuid: widget.selectedSpace?.uuid,
onSpaceSelected: widget.onSpaceSelected, ),
selectedSpaceUuid: widget.selectedSpace?.uuid,
onSelectedSpaceChanged: (String? spaceUuid) {
setState(() {
final selectedSpace = findSpaceByUuid(spaceUuid, widget.communities);
if (selectedSpace != null) {
widget.onSpaceSelected!(selectedSpace);
}
});
}),
CommunityStructureArea( CommunityStructureArea(
selectedCommunity: widget.selectedCommunity, selectedCommunity: widget.selectedCommunity,
selectedSpace: widget.selectedSpace, selectedSpace: widget.selectedSpace,
spaces: widget.selectedCommunity?.spaces ?? [], spaces: widget.selectedCommunity?.spaces ?? [],
products: widget.products, products: widget.products,
onSpaceSelected: (SpaceModel? space) {
setState(() {
widget.onSpaceSelected!(space);
});
},
), ),
], ],
), ),

View File

@ -42,7 +42,8 @@ class _SidebarWidgetState extends State<SidebarWidget> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_selectedId = widget.selectedSpaceUuid; // Initialize with the passed selected space UUID _selectedId = widget
.selectedSpaceUuid; // Initialize with the passed selected space UUID
} }
@override @override
@ -83,8 +84,8 @@ class _SidebarWidgetState extends State<SidebarWidget> {
return widget.communities.where((community) { return widget.communities.where((community) {
final containsQueryInCommunity = final containsQueryInCommunity =
community.name.toLowerCase().contains(_searchQuery.toLowerCase()); community.name.toLowerCase().contains(_searchQuery.toLowerCase());
final containsQueryInSpaces = final containsQueryInSpaces = community.spaces
community.spaces.any((space) => _containsQuery(space, _searchQuery.toLowerCase())); .any((space) => _containsQuery(space, _searchQuery.toLowerCase()));
return containsQueryInCommunity || containsQueryInSpaces; return containsQueryInCommunity || containsQueryInSpaces;
}).toList(); }).toList();
@ -93,8 +94,8 @@ class _SidebarWidgetState extends State<SidebarWidget> {
// Helper function to determine if any space or its children match the search query // Helper function to determine if any space or its children match the search query
bool _containsQuery(SpaceModel space, String query) { bool _containsQuery(SpaceModel space, String query) {
final matchesSpace = space.name.toLowerCase().contains(query); final matchesSpace = space.name.toLowerCase().contains(query);
final matchesChildren = final matchesChildren = space.children.any((child) =>
space.children.any((child) => _containsQuery(child, query)); // Recursive check for children _containsQuery(child, query)); // Recursive check for children
// If the space or any of its children match the query, expand this space // If the space or any of its children match the query, expand this space
if (matchesSpace || matchesChildren) { if (matchesSpace || matchesChildren) {
@ -128,7 +129,8 @@ class _SidebarWidgetState extends State<SidebarWidget> {
width: 300, width: 300,
decoration: subSectionContainerDecoration, decoration: subSectionContainerDecoration,
child: Column( child: Column(
mainAxisSize: MainAxisSize.min, // Ensures the Column only takes necessary height mainAxisSize:
MainAxisSize.min, // Ensures the Column only takes necessary height
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
// Communities title with the add button // Communities title with the add button
@ -176,7 +178,7 @@ class _SidebarWidgetState extends State<SidebarWidget> {
Expanded( Expanded(
child: ListView( child: ListView(
children: filteredCommunities.map((community) { children: filteredCommunities.map((community) {
return _buildCommunityTile(community); return _buildCommunityTile(context, community);
}).toList(), }).toList(),
), ),
), ),
@ -185,7 +187,7 @@ class _SidebarWidgetState extends State<SidebarWidget> {
); );
} }
Widget _buildCommunityTile(CommunityModel community) { Widget _buildCommunityTile(BuildContext context, CommunityModel community) {
bool hasChildren = community.spaces.isNotEmpty; bool hasChildren = community.spaces.isNotEmpty;
return CommunityTile( return CommunityTile(
@ -199,16 +201,17 @@ class _SidebarWidgetState extends State<SidebarWidget> {
_selectedSpaceUuid = null; // Update the selected community _selectedSpaceUuid = null; // Update the selected community
}); });
if (widget.onCommunitySelected != null) { context.read<SpaceManagementBloc>().add(
widget.onCommunitySelected!(community); SelectCommunityEvent(selectedCommunity: community),
widget.onSpaceSelected!(null); // Pass the entire community );
}
}, },
onExpansionChanged: (String title, bool expanded) { onExpansionChanged: (String title, bool expanded) {
_handleExpansionChange(community.uuid, expanded); _handleExpansionChange(community.uuid, expanded);
}, },
children: hasChildren children: hasChildren
? community.spaces.map((space) => _buildSpaceTile(space, community)).toList() ? community.spaces
.map((space) => _buildSpaceTile(space, community))
.toList()
: null, // Render spaces within the community : null, // Render spaces within the community
); );
} }
@ -230,17 +233,15 @@ class _SidebarWidgetState extends State<SidebarWidget> {
_selectedSpaceUuid = space.uuid; _selectedSpaceUuid = space.uuid;
}); });
if (widget.onSpaceSelected != null) { context.read<SpaceManagementBloc>().add(
widget.onCommunitySelected!(community); SelectSpaceEvent(
widget.onSpaceSelected!(space); selectedCommunity: community, selectedSpace: space),
} );
if (widget.onSelectedSpaceChanged != null) {
widget.onSelectedSpaceChanged!(space.uuid);
}
}, },
children: space.children.isNotEmpty children: space.children.isNotEmpty
? space.children.map((childSpace) => _buildSpaceTile(childSpace, community)).toList() ? space.children
.map((childSpace) => _buildSpaceTile(childSpace, community))
.toList()
: [], // Recursively render child spaces if available : [], // Recursively render child spaces if available
); );
} }

View File

@ -8,22 +8,31 @@ import 'package:syncrow_web/utils/constants/api_const.dart';
class CommunitySpaceManagementApi { class CommunitySpaceManagementApi {
// Community Management APIs // Community Management APIs
Future<List<CommunityModel>> fetchCommunities() async { Future<List<CommunityModel>> fetchCommunities({int page = 1}) async {
try { try {
final response = await HTTPService().get( List<CommunityModel> allCommunities = [];
path: ApiEndpoints.getCommunityList, bool hasNext = true;
expectedResponseModel: (json) {
// Access the 'data' key from the response
List<dynamic> jsonData = json['data'];
// Check if jsonData is actually a List while (hasNext) {
List<CommunityModel> communityList = jsonData.map((jsonItem) { await HTTPService().get(
return CommunityModel.fromJson(jsonItem); path: ApiEndpoints.getCommunityList,
}).toList(); queryParameters: {'page': page},
return communityList; expectedResponseModel: (json) {
}, List<dynamic> jsonData = json['data'];
); hasNext = json['hasNext'] ?? false;
return response; int currentPage = json['page'] ?? 1;
List<CommunityModel> communityList = jsonData.map((jsonItem) {
return CommunityModel.fromJson(jsonItem);
}).toList();
allCommunities.addAll(communityList);
page = currentPage + 1;
return communityList;
},
);
}
return allCommunities;
} catch (e) { } catch (e) {
debugPrint('Error fetching communities: $e'); debugPrint('Error fetching communities: $e');
return []; return [];
@ -33,7 +42,8 @@ class CommunitySpaceManagementApi {
Future<CommunityModel?> getCommunityById(String communityId) async { Future<CommunityModel?> getCommunityById(String communityId) async {
try { try {
final response = await HTTPService().get( final response = await HTTPService().get(
path: ApiEndpoints.getCommunityById.replaceAll('{communityId}', communityId), path: ApiEndpoints.getCommunityById
.replaceAll('{communityId}', communityId),
expectedResponseModel: (json) { expectedResponseModel: (json) {
return CommunityModel.fromJson(json['data']); return CommunityModel.fromJson(json['data']);
}, },
@ -45,7 +55,8 @@ class CommunitySpaceManagementApi {
} }
} }
Future<CommunityModel?> createCommunity(String name, String description) async { Future<CommunityModel?> createCommunity(
String name, String description) async {
try { try {
final response = await HTTPService().post( final response = await HTTPService().post(
path: ApiEndpoints.createCommunity, path: ApiEndpoints.createCommunity,
@ -67,7 +78,8 @@ class CommunitySpaceManagementApi {
Future<bool> updateCommunity(String communityId, String name) async { Future<bool> updateCommunity(String communityId, String name) async {
try { try {
final response = await HTTPService().put( final response = await HTTPService().put(
path: ApiEndpoints.updateCommunity.replaceAll('{communityId}', communityId), path: ApiEndpoints.updateCommunity
.replaceAll('{communityId}', communityId),
body: { body: {
'name': name, 'name': name,
}, },
@ -85,7 +97,8 @@ class CommunitySpaceManagementApi {
Future<bool> deleteCommunity(String communityId) async { Future<bool> deleteCommunity(String communityId) async {
try { try {
final response = await HTTPService().delete( final response = await HTTPService().delete(
path: ApiEndpoints.deleteCommunity.replaceAll('{communityId}', communityId), path: ApiEndpoints.deleteCommunity
.replaceAll('{communityId}', communityId),
expectedResponseModel: (json) { expectedResponseModel: (json) {
return json['success'] ?? false; return json['success'] ?? false;
}, },
@ -236,10 +249,12 @@ class CommunitySpaceManagementApi {
Future<List<SpaceModel>> getSpaceHierarchy(String communityId) async { Future<List<SpaceModel>> getSpaceHierarchy(String communityId) async {
try { try {
final response = await HTTPService().get( final response = await HTTPService().get(
path: ApiEndpoints.getSpaceHierarchy.replaceAll('{communityId}', communityId), path: ApiEndpoints.getSpaceHierarchy
.replaceAll('{communityId}', communityId),
expectedResponseModel: (json) { expectedResponseModel: (json) {
final spaceModels = final spaceModels = (json['data'] as List)
(json['data'] as List).map((spaceJson) => SpaceModel.fromJson(spaceJson)).toList(); .map((spaceJson) => SpaceModel.fromJson(spaceJson))
.toList();
return spaceModels; return spaceModels;
}, },