fixed community structure

This commit is contained in:
hannathkadher
2024-12-31 10:40:32 +04:00
parent edf8bdfdcd
commit 7935864208
5 changed files with 185 additions and 57 deletions

View File

@ -26,6 +26,7 @@ class SpaceManagementBloc
on<FetchProductsEvent>(_onFetchProducts);
on<SelectSpaceEvent>(_onSelectSpace);
on<NewCommunityEvent>(_onNewCommunity);
on<BlankStateEvent>(_onBlankState);
}
void _onUpdateCommunity(
@ -103,6 +104,45 @@ class SpaceManagementBloc
}
}
Future<void> _onBlankState(
BlankStateEvent event, Emitter<SpaceManagementState> emit) async {
try {
final previousState = state;
if (previousState is SpaceManagementLoaded ||
previousState is BlankState) {
final prevCommunities = (previousState as dynamic).communities ?? [];
emit(BlankState(
communities: List<CommunityModel>.from(prevCommunities),
products: _cachedProducts ?? [],
));
return;
}
final communities = await _api.fetchCommunities();
final updatedCommunities =
await Future.wait(communities.map((community) async {
final spaces = await _fetchSpacesForCommunity(community.uuid);
return CommunityModel(
uuid: community.uuid,
createdAt: community.createdAt,
updatedAt: community.updatedAt,
name: community.name,
description: community.description,
spaces: spaces,
region: community.region,
);
}));
emit(BlankState(
communities: updatedCommunities,
products: _cachedProducts ?? [],
));
} catch (error) {
emit(SpaceManagementError('Error loading communities: $error'));
}
}
void _onLoadCommunityAndSpaces(
LoadCommunityAndSpacesEvent event,
Emitter<SpaceManagementState> emit,

View File

@ -140,3 +140,6 @@ class LoadSpaceHierarchyEvent extends SpaceManagementEvent {
@override
List<Object> get props => [communityId];
}
class BlankStateEvent extends SpaceManagementEvent {}

View File

@ -1,16 +1,17 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/shared/navigate_home_grid_view.dart';
import 'package:syncrow_web/pages/spaces_management/structure_selector/bloc/center_body_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_event.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_state.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/product_model.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/space_model.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/widgets/loaded_space_widget.dart';
import 'package:syncrow_web/pages/spaces_management/structure_selector/view/center_body_widget.dart';
import 'package:syncrow_web/services/product_api.dart';
import 'package:syncrow_web/services/space_mana_api.dart';
import 'package:syncrow_web/web_layout/web_scaffold.dart';
class SpaceManagementPage extends StatefulWidget {
const SpaceManagementPage({super.key});
@ -21,70 +22,49 @@ class SpaceManagementPage extends StatefulWidget {
class SpaceManagementPageState extends State<SpaceManagementPage> {
final CommunitySpaceManagementApi _api = CommunitySpaceManagementApi();
final ProductApi _productApi = ProductApi();
Map<String, List<SpaceModel>> communitySpaces = {};
List<ProductModel> products = [];
bool isProductDataLoaded = false;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => SpaceManagementBloc(_api, _productApi)
..add(LoadCommunityAndSpacesEvent()),
return MultiBlocProvider(
providers: [
BlocProvider(
create: (_) => SpaceManagementBloc(_api, _productApi)
..add(LoadCommunityAndSpacesEvent()),
),
BlocProvider(
create: (_) => CenterBodyBloc(),
),
],
child: WebScaffold(
appBarTitle: Text('Space Management',
style: Theme.of(context).textTheme.headlineLarge),
enableMenuSidebar: false,
centerBody: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
GestureDetector(
onTap: () {},
child: Text(
'Community Structure',
style: Theme.of(context).textTheme.bodyLarge,
),
),
const SizedBox(width: 20),
GestureDetector(
onTap: () {},
child: Text(
'Space Model',
style: Theme.of(context).textTheme.bodyLarge,
),
),
],
),
),
centerBody: CenterBodyWidget(),
rightBody: const NavigateHomeGridView(),
scaffoldBody: BlocBuilder<SpaceManagementBloc, SpaceManagementState>(
builder: (context, state) {
if (state is SpaceManagementLoading) {
return const Center(child: CircularProgressIndicator());
} else if (state is BlankState) {
return LoadedSpaceView(
communities: state.communities,
selectedCommunity: null,
selectedSpace: null,
products: state.products,
);
} else if (state is SpaceManagementLoaded) {
return LoadedSpaceView(
communities: state.communities,
selectedCommunity: state.selectedCommunity,
selectedSpace: state.selectedSpace,
products: state.products,
);
} else if (state is SpaceManagementError) {
return Center(child: Text('Error: ${state.errorMessage}'));
}
return Container();
}),
builder: (context, state) {
if (state is SpaceManagementLoading) {
return const Center(child: CircularProgressIndicator());
} else if (state is BlankState) {
return LoadedSpaceView(
communities: state.communities,
selectedCommunity: null,
selectedSpace: null,
products: state.products,
);
} else if (state is SpaceManagementLoaded) {
return LoadedSpaceView(
communities: state.communities,
selectedCommunity: state.selectedCommunity,
selectedSpace: state.selectedSpace,
products: state.products,
);
} else if (state is SpaceManagementError) {
return Center(child: Text('Error: ${state.errorMessage}'));
}
return Container();
},
),
),
);
}

View File

@ -0,0 +1,30 @@
import 'package:flutter_bloc/flutter_bloc.dart';
// Define Events
abstract class CenterBodyEvent {}
class Button1PressedEvent extends CenterBodyEvent {}
class Button2PressedEvent extends CenterBodyEvent {}
// Define States
abstract class CenterBodyState {}
class InitialState extends CenterBodyState {}
class Button1State extends CenterBodyState {}
class Button2State extends CenterBodyState {}
// Bloc Implementation
class CenterBodyBloc extends Bloc<CenterBodyEvent, CenterBodyState> {
CenterBodyBloc() : super(InitialState()) {
on<Button1PressedEvent>((event, emit) {
emit(Button1State());
});
on<Button2PressedEvent>((event, emit) {
emit(Button2State());
});
}
}

View File

@ -0,0 +1,75 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_event.dart';
import '../bloc/center_body_bloc.dart';
class CenterBodyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<CenterBodyBloc, CenterBodyState>(
builder: (context, state) {
if (state is InitialState) {
context.read<CenterBodyBloc>().add(Button1PressedEvent());
}
if (state is Button1State) {
context.read<SpaceManagementBloc>().add(BlankStateEvent());
}
return Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GestureDetector(
onTap: () {
context.read<CenterBodyBloc>().add(Button1PressedEvent());
},
child: Text(
'Community Structure',
style: Theme.of(context).textTheme.bodyLarge!.copyWith(
fontWeight: state is Button1State
? FontWeight.bold
: FontWeight.normal,
color: state is Button1State
? Theme.of(context).textTheme.bodyLarge!.color
: Theme.of(context)
.textTheme
.bodyLarge!
.color!
.withOpacity(0.5),
),
),
),
SizedBox(width: 20),
GestureDetector(
onTap: () {
context.read<CenterBodyBloc>().add(Button2PressedEvent());
},
child: Text(
'Space Model',
style: Theme.of(context).textTheme.bodyLarge!.copyWith(
fontWeight: state is Button2State
? FontWeight.bold
: FontWeight.normal,
color: state is Button2State
? Theme.of(context).textTheme.bodyLarge!.color
: Theme.of(context)
.textTheme
.bodyLarge!
.color!
.withOpacity(0.5),
),
),
),
],
),
],
),
);
},
);
}
}