community title will list from the api

This commit is contained in:
hannathkadher
2024-10-02 12:03:34 +04:00
parent 5d397691b6
commit 308a7adba7
4 changed files with 140 additions and 77 deletions

View File

@ -38,30 +38,3 @@ class SpacesResponse {
); );
} }
} }
class CommunitySpaceManagementApi {
Future<SpacesResponse> 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,
);
}
}
}

View File

@ -3,14 +3,14 @@ import 'package:syncrow_web/common/custom_expansion_tile.dart';
class CommunityTile extends StatelessWidget { class CommunityTile extends StatelessWidget {
final String title; final String title;
final String expandedTile; final bool isExpanded;
final Function(String, bool) onExpansionChanged; final Function(String, bool) onExpansionChanged;
final List<Widget>? children; final List<Widget>? children;
const CommunityTile({ const CommunityTile({
Key? key, Key? key,
required this.title, required this.title,
required this.expandedTile, required this.isExpanded,
required this.onExpansionChanged, required this.onExpansionChanged,
this.children, this.children,
}) : super(key: key); }) : super(key: key);
@ -19,11 +19,11 @@ class CommunityTile extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return CustomExpansionTile( return CustomExpansionTile(
title: title, title: title,
isExpanded: expandedTile == title, initiallyExpanded: isExpanded,
onExpansionChanged: (bool expanded) { onExpansionChanged: (bool expanded) {
onExpansionChanged(title, expanded); onExpansionChanged(title, expanded);
}, },
children: children, children: children ?? [],
); );
} }
} }

View File

@ -1,7 +1,8 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.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/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/pages/spaces_management/view/community_tile.dart';
import 'package:syncrow_web/utils/color_manager.dart'; import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart'; import 'package:syncrow_web/utils/constants/assets.dart';
@ -9,50 +10,77 @@ import 'package:syncrow_web/utils/style.dart';
class SidebarWidget extends StatefulWidget { class SidebarWidget extends StatefulWidget {
final Function(String)? onCommunitySelected; final Function(String)? onCommunitySelected;
final Map<String, List<SpaceModel>> communitySpaces;
SidebarWidget({this.onCommunitySelected}); SidebarWidget({this.onCommunitySelected, required this.communitySpaces});
@override @override
_SidebarWidgetState createState() => _SidebarWidgetState(); _SidebarWidgetState createState() => _SidebarWidgetState();
} }
class _SidebarWidgetState extends State<SidebarWidget> { class _SidebarWidgetState extends State<SidebarWidget> {
String? _expandedTile;
String _searchQuery = ''; // Track search query String _searchQuery = ''; // Track search query
Map<String, bool> _expandedTiles = {}; // Track expanded state for each UUID
// List of all communities @override
final List<Community> _communities = [ void initState() {
Community(name: 'Downtown Dubai'), super.initState();
Community(name: 'Dubai Creek Harbour'), _logCommunitySpaces(); // Log the structure on initialization
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);
} }
List<Community> _filterCommunities() { // Function to log the communitySpaces data structure for debugging
if (_searchQuery.isEmpty) { void _logCommunitySpaces() {
return _communities; 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<SpaceModel> 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())) // Function to filter communities based on the search query
.toList(); Map<String, List<SpaceModel>> _filterCommunities() {
if (_searchQuery.isEmpty) {
return widget.communitySpaces;
}
// Filter communities and their spaces based on the search query
final filteredCommunitySpaces = <String, List<SpaceModel>>{};
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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
List<Community> filteredCommunities = _filterCommunities(); final filteredCommunities = _filterCommunities();
return Container( return Container(
width: 300, width: 300,
@ -105,12 +133,10 @@ class _SidebarWidgetState extends State<SidebarWidget> {
const SizedBox(height: 16), const SizedBox(height: 16),
// Community list with one item expanded at a time // Community list with one item expanded at a time
Expanded( Expanded(
child: ListView.builder( child: ListView(
itemCount: filteredCommunities.length, children: filteredCommunities.keys.map((communityName) {
itemBuilder: (context, index) { return _buildCommunityTile(communityName, filteredCommunities[communityName]!);
Community community = filteredCommunities[index]; }).toList(),
return _buildCommunityTile(community);
},
), ),
), ),
], ],
@ -118,18 +144,43 @@ class _SidebarWidgetState extends State<SidebarWidget> {
); );
} }
Widget _buildCommunityTile(Community community) { Widget _buildCommunityTile(String communityName, List<SpaceModel> spaces) {
bool hasChildren = bool hasChildren = spaces.isNotEmpty;
community.children != null && community.children!.isNotEmpty;
debugPrint('Building CommunityTile for $communityName, hasChildren: $hasChildren');
return CommunityTile( return CommunityTile(
title: community.name, title: communityName,
expandedTile: _expandedTile ?? '', isExpanded: _expandedTiles[communityName] ?? false,
onExpansionChanged: _handleExpansionChange, onExpansionChanged: (String title, bool expanded) {
debugPrint('CommunityTile onExpansionChanged called for $title, expanded: $expanded');
_handleExpansionChange(title, expanded);
},
children: hasChildren children: hasChildren
? community.children! ? spaces.map((space) => _buildSpaceTile(space)).toList()
.map((child) => _buildCommunityTile(child)) : null, // Render spaces within the community
.toList()
: null, // Recursively render sub-communities
); );
} }
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);
}
} }

View File

@ -4,12 +4,15 @@ import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart'; import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_web/common/custom_expansion_tile.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/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/dialogs/create_space_dialog.dart';
import 'package:syncrow_web/pages/spaces_management/view/sidebar_widget.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/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart'; import 'package:syncrow_web/utils/constants/assets.dart';
class SpaceManagementPage extends StatefulWidget{ class SpaceManagementPage extends StatefulWidget {
@override @override
SpaceManagementPageState createState() => SpaceManagementPageState(); SpaceManagementPageState createState() => SpaceManagementPageState();
} }
@ -19,6 +22,41 @@ class SpaceManagementPageState extends State<SpaceManagementPage> {
List<SpaceData> spaces = []; List<SpaceData> spaces = [];
List<Connection> connections = []; List<Connection> connections = [];
// API instance
final CommunitySpaceManagementApi _api = CommunitySpaceManagementApi();
// Data structure to store community and associated spaces
Map<String, List<SpaceModel>> communitySpaces = {};
@override
void initState() {
super.initState();
_loadCommunityAndSpacesData();
}
// Function to load all communities and their respective spaces
void _loadCommunityAndSpacesData() async {
try {
// Fetch all communities
List<CommunityModel> communities = await _api.fetchCommunities();
for (CommunityModel community in communities) {
// Fetch spaces hierarchy for each community
List<SpaceModel> 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) { void _handleCommunitySelection(String community) {
// Handle community selection here // Handle community selection here
print("Selected Community: $community"); print("Selected Community: $community");
@ -40,6 +78,7 @@ class SpaceManagementPageState extends State<SpaceManagementPage> {
children: [ children: [
// Sidebar for navigation and community list // Sidebar for navigation and community list
SidebarWidget( SidebarWidget(
communitySpaces: communitySpaces, // Pass communitySpaces here
onCommunitySelected: _handleCommunitySelection, onCommunitySelected: _handleCommunitySelection,
), ),
// Right Side: Community Structure Area // Right Side: Community Structure Area