diff --git a/lib/pages/spaces_management/bloc/space_management_bloc.dart b/lib/pages/spaces_management/bloc/space_management_bloc.dart index dc2829c5..f8093208 100644 --- a/lib/pages/spaces_management/bloc/space_management_bloc.dart +++ b/lib/pages/spaces_management/bloc/space_management_bloc.dart @@ -1,3 +1,4 @@ +import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_web/pages/spaces_management/model/community_model.dart'; import 'package:syncrow_web/pages/spaces_management/model/space_model.dart'; @@ -24,16 +25,28 @@ class SpaceManagementBloc // Fetch all communities List communities = await _api.fetchCommunities(); - Map> communitySpaces = {}; + // Use Future.wait to handle async calls within map + List updatedCommunities = await Future.wait( + communities.map((community) async { + List spaces = + await _api.getSpaceHierarchy(community.uuid); - for (CommunityModel community in communities) { - // Fetch spaces hierarchy for each community - List spaces = await _api.getSpaceHierarchy(community.uuid); - community.spaces = spaces; - communitySpaces[community.uuid] = spaces; - } + debugPrint( + 'Fetched spaces for community ${community.name}: ${spaces.length}'); - emit(SpaceManagementLoaded(communitySpaces: communitySpaces)); + return CommunityModel( + uuid: community.uuid, + createdAt: community.createdAt, + updatedAt: community.updatedAt, + name: community.name, + description: community.description, + spaces: spaces, // New spaces list + region: community.region, + ); + }).toList(), + ); + + emit(SpaceManagementLoaded(communities: updatedCommunities)); } catch (e) { emit(SpaceManagementError('Error loading communities and spaces: $e')); } diff --git a/lib/pages/spaces_management/bloc/space_management_state.dart b/lib/pages/spaces_management/bloc/space_management_state.dart index e1088cfc..3f8c4af8 100644 --- a/lib/pages/spaces_management/bloc/space_management_state.dart +++ b/lib/pages/spaces_management/bloc/space_management_state.dart @@ -1,4 +1,5 @@ import 'package:equatable/equatable.dart'; +import 'package:syncrow_web/pages/spaces_management/model/community_model.dart'; import 'package:syncrow_web/pages/spaces_management/model/space_model.dart'; abstract class SpaceManagementState extends Equatable { @@ -13,12 +14,13 @@ class SpaceManagementInitial extends SpaceManagementState {} class SpaceManagementLoading extends SpaceManagementState {} class SpaceManagementLoaded extends SpaceManagementState { - final Map> communitySpaces; + final List communities; - const SpaceManagementLoaded({required this.communitySpaces}); + + const SpaceManagementLoaded({ required this.communities}); @override - List get props => [communitySpaces]; + List get props => [communities]; } class SpaceCreationSuccess extends SpaceManagementState {} diff --git a/lib/pages/spaces_management/model/space_model.dart b/lib/pages/spaces_management/model/space_model.dart index 4ba54545..273c09cd 100644 --- a/lib/pages/spaces_management/model/space_model.dart +++ b/lib/pages/spaces_management/model/space_model.dart @@ -13,7 +13,7 @@ class SpaceModel { final SpaceModel? parent; final CommunityModel? community; final List children; - final String icon; + final String? icon; Offset position; bool isHovered; @@ -54,11 +54,11 @@ class SpaceModel { .map((child) => SpaceModel.fromJson(child)) .toList() : [], - icon: json['icon'], // New field from JSON + icon: json['icon'], position: json['position'] != null ? Offset(json['position']['dx'], json['position']['dy']) - : Offset(0, 0), // Default position if not provided in JSON - isHovered: json['isHovered'] ?? false, // Default isHovered if not in JSON + : const Offset(0, 0), + isHovered: false, ); } diff --git a/lib/pages/spaces_management/view/spaces_management_page.dart b/lib/pages/spaces_management/view/spaces_management_page.dart index e41af548..86bfe880 100644 --- a/lib/pages/spaces_management/view/spaces_management_page.dart +++ b/lib/pages/spaces_management/view/spaces_management_page.dart @@ -4,6 +4,7 @@ import 'package:syncrow_web/pages/common/buttons/add_space_button.dart'; import 'package:syncrow_web/pages/spaces_management/bloc/space_management_bloc.dart'; import 'package:syncrow_web/pages/spaces_management/bloc/space_management_event.dart'; import 'package:syncrow_web/pages/spaces_management/bloc/space_management_state.dart'; +import 'package:syncrow_web/pages/spaces_management/model/community_model.dart'; import 'package:syncrow_web/pages/spaces_management/model/space_model.dart'; import 'package:syncrow_web/pages/spaces_management/view/curved_line_painter.dart'; import 'package:syncrow_web/pages/spaces_management/view/dialogs/create_space_dialog.dart'; @@ -53,8 +54,7 @@ class SpaceManagementPageState extends State { if (state is SpaceManagementLoading) { return const Center(child: CircularProgressIndicator()); } else if (state is SpaceManagementLoaded) { - return _buildLoadedState( - context, screenSize, state.communitySpaces); + return _buildLoadedState(context, screenSize, state.communities); } else if (state is SpaceManagementError) { return Center(child: Text('Error: ${state.errorMessage}')); } @@ -65,24 +65,22 @@ class SpaceManagementPageState extends State { ); } - Widget _buildLoadedState(BuildContext context, Size screenSize, - Map> communitySpaces) { + Widget _buildLoadedState( + BuildContext context, Size screenSize, List communities) { return Stack( clipBehavior: Clip.none, children: [ Row( children: [ SidebarWidget( - communitySpaces: communitySpaces, + communities: communities, onCommunitySelected: (community) { context.read().add( LoadCommunityAndSpacesEvent(), // Re-fetch or perform community-specific actions ); }, ), - Expanded( - child: _buildCommunityStructureArea(context, screenSize), - ), + _buildCommunityStructureArea(context, screenSize), ], ), _buildGradientBorder(), diff --git a/lib/pages/spaces_management/widgets/sidebar_widget.dart b/lib/pages/spaces_management/widgets/sidebar_widget.dart index a91d9977..b5596315 100644 --- a/lib/pages/spaces_management/widgets/sidebar_widget.dart +++ b/lib/pages/spaces_management/widgets/sidebar_widget.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_svg/svg.dart'; import 'package:syncrow_web/common/custom_expansion_tile.dart'; import 'package:syncrow_web/common/search_bar.dart'; +import 'package:syncrow_web/pages/spaces_management/model/community_model.dart'; import 'package:syncrow_web/pages/spaces_management/model/space_model.dart'; import 'package:syncrow_web/pages/spaces_management/widgets/community_tile.dart'; import 'package:syncrow_web/pages/spaces_management/view/dialogs/create_community_dialog.dart'; @@ -11,9 +12,9 @@ import 'package:syncrow_web/utils/style.dart'; class SidebarWidget extends StatefulWidget { final Function(String)? onCommunitySelected; - final Map> communitySpaces; + final List communities; - SidebarWidget({this.onCommunitySelected, required this.communitySpaces}); + SidebarWidget({this.onCommunitySelected, required this.communities}); @override _SidebarWidgetState createState() => _SidebarWidgetState(); @@ -34,9 +35,7 @@ class _SidebarWidgetState extends State { builder: (context) => CreateCommunityDialog( onCreateCommunity: (String communityName) { setState(() { - debugPrint("hello"); - - // You can update the communitySpaces map here + // You can update the community list here when a new community is added }); }, ), @@ -44,21 +43,19 @@ class _SidebarWidgetState extends State { } // Function to filter communities based on the search query - Map> _filterCommunities() { + List _filterCommunities() { if (_searchQuery.isEmpty) { - return widget.communitySpaces; + return widget.communities; } // Filter communities and their spaces based on the search query - final filteredCommunitySpaces = >{}; - widget.communitySpaces.forEach((communityName, spaces) { - if (communityName.toLowerCase().contains(_searchQuery.toLowerCase()) || - spaces.any( - (space) => _containsQuery(space, _searchQuery.toLowerCase()))) { - filteredCommunitySpaces[communityName] = spaces; - } - }); - return filteredCommunitySpaces; + return widget.communities.where((community) { + final containsQueryInCommunity = + community.name.toLowerCase().contains(_searchQuery.toLowerCase()); + final containsQueryInSpaces = community.spaces + .any((space) => _containsQuery(space, _searchQuery.toLowerCase())); + return containsQueryInCommunity || containsQueryInSpaces; + }).toList(); } // Helper function to determine if any space or its children match the search query @@ -128,13 +125,10 @@ class _SidebarWidgetState extends State { ), const SizedBox(height: 16), // Community list - Flexible( - fit: FlexFit - .loose, // Ensure the ListView can flex but doesn't expand indefinitely + Expanded( child: ListView( - children: filteredCommunities.keys.map((communityName) { - return _buildCommunityTile( - communityName, filteredCommunities[communityName]!); + children: filteredCommunities.map((community) { + return _buildCommunityTile(community); }).toList(), ), ), @@ -143,21 +137,21 @@ class _SidebarWidgetState extends State { ); } - Widget _buildCommunityTile(String communityName, List spaces) { - bool hasChildren = spaces.isNotEmpty; + Widget _buildCommunityTile(CommunityModel community) { + bool hasChildren = community.spaces.isNotEmpty; debugPrint( - 'Building CommunityTile for $communityName, hasChildren: $hasChildren'); + 'Building CommunityTile for ${community.name}, hasChildren: $hasChildren'); return CommunityTile( - title: communityName, - isExpanded: _expandedTiles[communityName] ?? false, + title: community.name, + isExpanded: _expandedTiles[community.uuid] ?? false, onExpansionChanged: (String title, bool expanded) { debugPrint( 'CommunityTile onExpansionChanged called for $title, expanded: $expanded'); - _handleExpansionChange(title, expanded); + _handleExpansionChange(community.uuid, expanded); }, children: hasChildren - ? spaces.map((space) => _buildSpaceTile(space)).toList() + ? community.spaces.map((space) => _buildSpaceTile(space)).toList() : null, // Render spaces within the community ); } diff --git a/lib/services/space_mana_api.dart b/lib/services/space_mana_api.dart index bdb369d7..4a60c8c3 100644 --- a/lib/services/space_mana_api.dart +++ b/lib/services/space_mana_api.dart @@ -223,4 +223,5 @@ class CommunitySpaceManagementApi { return []; } } + }