selecting space selects community

This commit is contained in:
hannathkadher
2024-11-19 15:32:33 +04:00
parent 3a333188f8
commit a464788e88
2 changed files with 27 additions and 21 deletions

View File

@ -17,8 +17,7 @@ class SidebarWidget extends StatefulWidget {
final Function(CommunityModel)? onCommunitySelected; final Function(CommunityModel)? onCommunitySelected;
final List<CommunityModel> communities; final List<CommunityModel> communities;
const SidebarWidget( const SidebarWidget({super.key, this.onCommunitySelected, required this.communities});
{super.key, this.onCommunitySelected, required this.communities});
@override @override
_SidebarWidgetState createState() => _SidebarWidgetState(); _SidebarWidgetState createState() => _SidebarWidgetState();
@ -38,8 +37,7 @@ class _SidebarWidgetState extends State<SidebarWidget> {
showDialog( showDialog(
context: parentContext, context: parentContext,
builder: (context) => CreateCommunityDialog( builder: (context) => CreateCommunityDialog(
onCreateCommunity: onCreateCommunity: (String communityName, String description, String regionId) {
(String communityName, String description, String regionId) {
parentContext.read<SpaceManagementBloc>().add( parentContext.read<SpaceManagementBloc>().add(
CreateCommunityEvent( CreateCommunityEvent(
name: communityName, name: communityName,
@ -65,12 +63,11 @@ class _SidebarWidgetState extends State<SidebarWidget> {
return widget.communities.where((community) { return widget.communities.where((community) {
final containsQueryInCommunity = final containsQueryInCommunity =
community.name.toLowerCase().contains(_searchQuery.toLowerCase()); community.name.toLowerCase().contains(_searchQuery.toLowerCase());
final containsQueryInSpaces = community.spaces final containsQueryInSpaces =
.any((space) => _containsQuery(space, _searchQuery.toLowerCase())); community.spaces.any((space) => _containsQuery(space, _searchQuery.toLowerCase()));
if (containsQueryInCommunity || containsQueryInSpaces) { if (containsQueryInCommunity || containsQueryInSpaces) {
_selectedCommunityUuid = _selectedCommunityUuid = community.uuid; // Set the community to expanded
community.uuid; // Set the community to expanded
} }
return containsQueryInCommunity || containsQueryInSpaces; return containsQueryInCommunity || containsQueryInSpaces;
@ -80,8 +77,8 @@ class _SidebarWidgetState extends State<SidebarWidget> {
// Helper function to determine if any space or its children match the search query // Helper function to determine if any space or its children match the search query
bool _containsQuery(SpaceModel space, String query) { bool _containsQuery(SpaceModel space, String query) {
final matchesSpace = space.name.toLowerCase().contains(query); final matchesSpace = space.name.toLowerCase().contains(query);
final matchesChildren = space.children.any((child) => final matchesChildren =
_containsQuery(child, query)); // Recursive check for children space.children.any((child) => _containsQuery(child, query)); // Recursive check for children
// If the space or any of its children match the query, expand this space // If the space or any of its children match the query, expand this space
if (matchesSpace || matchesChildren) { if (matchesSpace || matchesChildren) {
@ -115,8 +112,7 @@ class _SidebarWidgetState extends State<SidebarWidget> {
width: 300, width: 300,
decoration: subSectionContainerDecoration, decoration: subSectionContainerDecoration,
child: Column( child: Column(
mainAxisSize: mainAxisSize: MainAxisSize.min, // Ensures the Column only takes necessary height
MainAxisSize.min, // Ensures the Column only takes necessary height
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
// Communities title with the add button // Communities title with the add button
@ -175,8 +171,8 @@ class _SidebarWidgetState extends State<SidebarWidget> {
Widget _buildCommunityTile(CommunityModel community) { Widget _buildCommunityTile(CommunityModel community) {
bool hasChildren = community.spaces.isNotEmpty; bool hasChildren = community.spaces.isNotEmpty;
bool isSelectedCommunity = _selectedCommunityUuid == bool isSelectedCommunity =
community.uuid; // Check if this community is selected _selectedCommunityUuid == community.uuid; // Check if this community is selected
return CommunityTile( return CommunityTile(
title: community.name, title: community.name,
isSelected: isSelectedCommunity, isSelected: isSelectedCommunity,
@ -194,24 +190,28 @@ class _SidebarWidgetState extends State<SidebarWidget> {
_handleExpansionChange(community.uuid, expanded); _handleExpansionChange(community.uuid, expanded);
}, },
children: hasChildren children: hasChildren
? community.spaces.map((space) => _buildSpaceTile(space)).toList() ? community.spaces.map((space) => _buildSpaceTile(space, community)).toList()
: null, // Render spaces within the community : null, // Render spaces within the community
); );
} }
Widget _buildSpaceTile(SpaceModel space) { Widget _buildSpaceTile(SpaceModel space, CommunityModel community) {
bool isSelectedSpace = bool isSelectedSpace = _isSpaceOrChildSelected(space); // Check if space should be expanded
_isSpaceOrChildSelected(space); // Check if space should be expanded
return SpaceTile( return SpaceTile(
title: space.name, title: space.name,
isSelected: isSelectedSpace,
initiallyExpanded: isSelectedSpace, initiallyExpanded: isSelectedSpace,
onExpansionChanged: (bool expanded) { onExpansionChanged: (bool expanded) {
_handleExpansionChange(space.uuid ?? '', expanded); _handleExpansionChange(space.uuid ?? '', expanded);
}, },
onItemSelected: () => {
if (widget.onCommunitySelected != null)
{
widget.onCommunitySelected!(community) // Pass the entire community
}
},
children: space.children.isNotEmpty children: space.children.isNotEmpty
? space.children ? space.children.map((childSpace) => _buildSpaceTile(childSpace, community)).toList()
.map((childSpace) => _buildSpaceTile(childSpace))
.toList()
: [], // Recursively render child spaces if available : [], // Recursively render child spaces if available
); );
} }

View File

@ -3,15 +3,20 @@ import 'package:syncrow_web/common/custom_expansion_tile.dart';
class SpaceTile extends StatefulWidget { class SpaceTile extends StatefulWidget {
final String title; final String title;
final bool isSelected;
final bool initiallyExpanded; final bool initiallyExpanded;
final ValueChanged<bool> onExpansionChanged; final ValueChanged<bool> onExpansionChanged;
final List<Widget>? children; final List<Widget>? children;
final Function() onItemSelected;
const SpaceTile({ const SpaceTile({
super.key, super.key,
required this.title, required this.title,
required this.initiallyExpanded, required this.initiallyExpanded,
required this.onExpansionChanged, required this.onExpansionChanged,
required this.onItemSelected,
required this.isSelected,
this.children, this.children,
}); });
@ -34,6 +39,7 @@ class _SpaceTileState extends State<SpaceTile> {
isSelected: false, isSelected: false,
title: widget.title, title: widget.title,
initiallyExpanded: _isExpanded, initiallyExpanded: _isExpanded,
onItemSelected: widget.onItemSelected,
onExpansionChanged: (bool expanded) { onExpansionChanged: (bool expanded) {
setState(() { setState(() {
_isExpanded = expanded; _isExpanded = expanded;