diff --git a/lib/pages/spaces_management/model/space_response_model.dart b/lib/pages/spaces_management/model/space_response_model.dart index ab6a8f79..11511481 100644 --- a/lib/pages/spaces_management/model/space_response_model.dart +++ b/lib/pages/spaces_management/model/space_response_model.dart @@ -38,30 +38,3 @@ class SpacesResponse { ); } } - -class CommunitySpaceManagementApi { - Future fetchSpaces(String communityId) async { - try { - final response = await HTTPService().get( - path: ApiEndpoints.listSpaces.replaceAll('{communityId}', communityId), - showServerMessage: true, - expectedResponseModel: (json) { - return SpacesResponse.fromJson(json); - }, - ); - return response; - } catch (e) { - debugPrint('Error fetching spaces: $e'); - return SpacesResponse( - data: [], - message: 'Error fetching spaces', - page: 1, - size: 10, - totalItem: 0, - totalPage: 0, - hasNext: false, - hasPrevious: false, - ); - } - } -} diff --git a/lib/pages/spaces_management/view/community_tile.dart b/lib/pages/spaces_management/view/community_tile.dart index 5530ea13..7511d4f3 100644 --- a/lib/pages/spaces_management/view/community_tile.dart +++ b/lib/pages/spaces_management/view/community_tile.dart @@ -3,14 +3,14 @@ import 'package:syncrow_web/common/custom_expansion_tile.dart'; class CommunityTile extends StatelessWidget { final String title; - final String expandedTile; + final bool isExpanded; final Function(String, bool) onExpansionChanged; final List? children; const CommunityTile({ Key? key, required this.title, - required this.expandedTile, + required this.isExpanded, required this.onExpansionChanged, this.children, }) : super(key: key); @@ -19,11 +19,11 @@ class CommunityTile extends StatelessWidget { Widget build(BuildContext context) { return CustomExpansionTile( title: title, - isExpanded: expandedTile == title, + initiallyExpanded: isExpanded, onExpansionChanged: (bool expanded) { onExpansionChanged(title, expanded); }, - children: children, + children: children ?? [], ); } } diff --git a/lib/pages/spaces_management/view/sidebar_widget.dart b/lib/pages/spaces_management/view/sidebar_widget.dart index 8d775672..7bdcdb7a 100644 --- a/lib/pages/spaces_management/view/sidebar_widget.dart +++ b/lib/pages/spaces_management/view/sidebar_widget.dart @@ -1,7 +1,8 @@ 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/view/community_tile.dart'; import 'package:syncrow_web/utils/color_manager.dart'; import 'package:syncrow_web/utils/constants/assets.dart'; @@ -9,50 +10,77 @@ import 'package:syncrow_web/utils/style.dart'; class SidebarWidget extends StatefulWidget { final Function(String)? onCommunitySelected; + final Map> communitySpaces; - SidebarWidget({this.onCommunitySelected}); + SidebarWidget({this.onCommunitySelected, required this.communitySpaces}); @override _SidebarWidgetState createState() => _SidebarWidgetState(); } class _SidebarWidgetState extends State { - String? _expandedTile; String _searchQuery = ''; // Track search query + Map _expandedTiles = {}; // Track expanded state for each UUID - // List of all communities - final List _communities = [ - Community(name: 'Downtown Dubai'), - Community(name: 'Dubai Creek Harbour'), - Community( - name: 'Dubai Hills Estate', - children: [ - Community(name: 'South Side'), // Nested community - ], - ), - ]; - - // A helper method to handle the expansion logic for CustomExpansionTile - void _handleExpansionChange(String title, bool expanded) { - setState(() { - _expandedTile = expanded ? title : null; - }); - widget.onCommunitySelected?.call(title); + @override + void initState() { + super.initState(); + _logCommunitySpaces(); // Log the structure on initialization } - List _filterCommunities() { - if (_searchQuery.isEmpty) { - return _communities; + // Function to log the communitySpaces data structure for debugging + void _logCommunitySpaces() { + debugPrint('Logging communitySpaces structure:'); + widget.communitySpaces.forEach((communityName, spaces) { + debugPrint('Community: $communityName'); + _logSpaces(spaces, 1); + }); + } + + // Helper function to log the spaces and their children + void _logSpaces(List spaces, int level) { + for (SpaceModel space in spaces) { + debugPrint('${' ' * level}Space: ${space.name}, UUID: ${space.uuid}, Has children: ${space.children.isNotEmpty}'); + if (space.children.isNotEmpty) { + _logSpaces(space.children, level + 1); + } } - return _communities - .where((community) => - community.name.toLowerCase().contains(_searchQuery.toLowerCase())) - .toList(); + } + + // Function to filter communities based on the search query + Map> _filterCommunities() { + if (_searchQuery.isEmpty) { + return widget.communitySpaces; + } + + // 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; + } + + // Helper function to determine if any space or its children match the search query + bool _containsQuery(SpaceModel space, String query) { + if (space.name.toLowerCase().contains(query)) { + return true; + } + for (var child in space.children) { + if (_containsQuery(child, query)) { + return true; + } + } + return false; } @override Widget build(BuildContext context) { - List filteredCommunities = _filterCommunities(); + final filteredCommunities = _filterCommunities(); return Container( width: 300, @@ -105,12 +133,10 @@ class _SidebarWidgetState extends State { const SizedBox(height: 16), // Community list with one item expanded at a time Expanded( - child: ListView.builder( - itemCount: filteredCommunities.length, - itemBuilder: (context, index) { - Community community = filteredCommunities[index]; - return _buildCommunityTile(community); - }, + child: ListView( + children: filteredCommunities.keys.map((communityName) { + return _buildCommunityTile(communityName, filteredCommunities[communityName]!); + }).toList(), ), ), ], @@ -118,18 +144,43 @@ class _SidebarWidgetState extends State { ); } - Widget _buildCommunityTile(Community community) { - bool hasChildren = - community.children != null && community.children!.isNotEmpty; + Widget _buildCommunityTile(String communityName, List spaces) { + bool hasChildren = spaces.isNotEmpty; + + debugPrint('Building CommunityTile for $communityName, hasChildren: $hasChildren'); return CommunityTile( - title: community.name, - expandedTile: _expandedTile ?? '', - onExpansionChanged: _handleExpansionChange, + title: communityName, + isExpanded: _expandedTiles[communityName] ?? false, + onExpansionChanged: (String title, bool expanded) { + debugPrint('CommunityTile onExpansionChanged called for $title, expanded: $expanded'); + _handleExpansionChange(title, expanded); + }, children: hasChildren - ? community.children! - .map((child) => _buildCommunityTile(child)) - .toList() - : null, // Recursively render sub-communities + ? spaces.map((space) => _buildSpaceTile(space)).toList() + : null, // Render spaces within the community ); } + + Widget _buildSpaceTile(SpaceModel space) { + debugPrint('Building SpaceTile for ${space.name}, hasChildren: ${space.children.isNotEmpty}'); + return CustomExpansionTile( + title: space.name, + isExpanded: _expandedTiles[space.uuid] ?? false, + onExpansionChanged: (bool expanded) { + debugPrint('SpaceTile onExpansionChanged called for ${space.name}, expanded: $expanded'); + _handleExpansionChange(space.uuid, expanded); + }, + children: space.children.isNotEmpty + ? space.children.map((childSpace) => _buildSpaceTile(childSpace)).toList() + : [], // Recursively render child spaces if available + ); + } + + void _handleExpansionChange(String uuid, bool expanded) { + setState(() { + _expandedTiles[uuid] = expanded; + debugPrint('_expandedTiles updated: $_expandedTiles'); + }); + widget.onCommunitySelected?.call(uuid); + } } diff --git a/lib/pages/spaces_management/view/spaces_management_page.dart b/lib/pages/spaces_management/view/spaces_management_page.dart index 3f646d05..420eab36 100644 --- a/lib/pages/spaces_management/view/spaces_management_page.dart +++ b/lib/pages/spaces_management/view/spaces_management_page.dart @@ -4,12 +4,15 @@ import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:syncrow_web/common/custom_expansion_tile.dart'; import 'package:syncrow_web/pages/common/buttons/add_space_button.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/dialogs/create_space_dialog.dart'; import 'package:syncrow_web/pages/spaces_management/view/sidebar_widget.dart'; +import 'package:syncrow_web/services/space_mana_api.dart'; import 'package:syncrow_web/utils/color_manager.dart'; import 'package:syncrow_web/utils/constants/assets.dart'; -class SpaceManagementPage extends StatefulWidget{ +class SpaceManagementPage extends StatefulWidget { @override SpaceManagementPageState createState() => SpaceManagementPageState(); } @@ -19,6 +22,41 @@ class SpaceManagementPageState extends State { List spaces = []; List connections = []; + // API instance + final CommunitySpaceManagementApi _api = CommunitySpaceManagementApi(); + + // Data structure to store community and associated spaces + Map> communitySpaces = {}; + + @override + void initState() { + super.initState(); + _loadCommunityAndSpacesData(); + } + + // Function to load all communities and their respective spaces + void _loadCommunityAndSpacesData() async { + try { + // Fetch all communities + List communities = await _api.fetchCommunities(); + + for (CommunityModel community in communities) { + // Fetch spaces hierarchy for each community + List spaces = await _api.getSpaceHierarchy(community.uuid); + + // Store the result in the communitySpaces map + communitySpaces[community.name] = spaces; + } + + // Update the state to reflect loaded data + setState(() { + // Optionally, you can initialize something here based on the loaded data + }); + } catch (e) { + debugPrint('Error loading communities and spaces: $e'); + } + } + void _handleCommunitySelection(String community) { // Handle community selection here print("Selected Community: $community"); @@ -40,6 +78,7 @@ class SpaceManagementPageState extends State { children: [ // Sidebar for navigation and community list SidebarWidget( + communitySpaces: communitySpaces, // Pass communitySpaces here onCommunitySelected: _handleCommunitySelection, ), // Right Side: Community Structure Area