Files
syncrow-web/lib/pages/spaces_management/view/sidebar_widget.dart
2024-10-02 12:03:34 +04:00

187 lines
6.4 KiB
Dart

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/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';
import 'package:syncrow_web/utils/style.dart';
class SidebarWidget extends StatefulWidget {
final Function(String)? onCommunitySelected;
final Map<String, List<SpaceModel>> communitySpaces;
SidebarWidget({this.onCommunitySelected, required this.communitySpaces});
@override
_SidebarWidgetState createState() => _SidebarWidgetState();
}
class _SidebarWidgetState extends State<SidebarWidget> {
String _searchQuery = ''; // Track search query
Map<String, bool> _expandedTiles = {}; // Track expanded state for each UUID
@override
void initState() {
super.initState();
_logCommunitySpaces(); // Log the structure on initialization
}
// 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);
}
}
}
// 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) {
final filteredCommunities = _filterCommunities();
return Container(
width: 300,
decoration: subSectionContainerDecoration,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Communities title with the add button
Container(
decoration: subSectionContainerDecoration,
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Communities',
style: Theme.of(context).textTheme.titleMedium,
),
GestureDetector(
onTap: () {
// Handle add button action
},
child: Container(
width: 30,
height: 30,
decoration: const BoxDecoration(
color: ColorsManager.whiteColors,
shape: BoxShape.circle,
),
child: Center(
child: SvgPicture.asset(
Assets.roundedAddIcon,
width: 24,
height: 24,
),
),
),
),
],
),
),
// Search bar
CustomSearchBar(
onSearchChanged: (query) {
setState(() {
_searchQuery = query; // Update search query on text change
});
},
),
const SizedBox(height: 16),
// Community list with one item expanded at a time
Expanded(
child: ListView(
children: filteredCommunities.keys.map((communityName) {
return _buildCommunityTile(communityName, filteredCommunities[communityName]!);
}).toList(),
),
),
],
),
);
}
Widget _buildCommunityTile(String communityName, List<SpaceModel> spaces) {
bool hasChildren = spaces.isNotEmpty;
debugPrint('Building CommunityTile for $communityName, hasChildren: $hasChildren');
return CommunityTile(
title: communityName,
isExpanded: _expandedTiles[communityName] ?? false,
onExpansionChanged: (String title, bool expanded) {
debugPrint('CommunityTile onExpansionChanged called for $title, expanded: $expanded');
_handleExpansionChange(title, expanded);
},
children: hasChildren
? 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);
}
}