mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
community title will list from the api
This commit is contained in:
@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<Widget>? 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 ?? [],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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<String, List<SpaceModel>> communitySpaces;
|
||||
|
||||
SidebarWidget({this.onCommunitySelected});
|
||||
SidebarWidget({this.onCommunitySelected, required this.communitySpaces});
|
||||
|
||||
@override
|
||||
_SidebarWidgetState createState() => _SidebarWidgetState();
|
||||
}
|
||||
|
||||
class _SidebarWidgetState extends State<SidebarWidget> {
|
||||
String? _expandedTile;
|
||||
String _searchQuery = ''; // Track search query
|
||||
Map<String, bool> _expandedTiles = {}; // Track expanded state for each UUID
|
||||
|
||||
// List of all communities
|
||||
final List<Community> _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<Community> _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<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()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
// Function to filter communities based on the search query
|
||||
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
|
||||
Widget build(BuildContext context) {
|
||||
List<Community> filteredCommunities = _filterCommunities();
|
||||
final filteredCommunities = _filterCommunities();
|
||||
|
||||
return Container(
|
||||
width: 300,
|
||||
@ -105,12 +133,10 @@ class _SidebarWidgetState extends State<SidebarWidget> {
|
||||
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<SidebarWidget> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCommunityTile(Community community) {
|
||||
bool hasChildren =
|
||||
community.children != null && community.children!.isNotEmpty;
|
||||
Widget _buildCommunityTile(String communityName, List<SpaceModel> 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);
|
||||
}
|
||||
}
|
||||
|
@ -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<SpaceManagementPage> {
|
||||
List<SpaceData> spaces = [];
|
||||
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) {
|
||||
// Handle community selection here
|
||||
print("Selected Community: $community");
|
||||
@ -40,6 +78,7 @@ class SpaceManagementPageState extends State<SpaceManagementPage> {
|
||||
children: [
|
||||
// Sidebar for navigation and community list
|
||||
SidebarWidget(
|
||||
communitySpaces: communitySpaces, // Pass communitySpaces here
|
||||
onCommunitySelected: _handleCommunitySelection,
|
||||
),
|
||||
// Right Side: Community Structure Area
|
||||
|
Reference in New Issue
Block a user