Merge pull request #80 from SyncrowIOT/side_tree

Side tree
This commit is contained in:
Abdullah
2025-02-04 01:56:52 +03:00
committed by GitHub
11 changed files with 63 additions and 26 deletions

View File

@ -1,6 +1,8 @@
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_model.dart';
import 'package:syncrow_web/pages/space_tree/bloc/space_tree_bloc.dart';
import 'package:syncrow_web/services/devices_mang_api.dart';
part 'device_managment_event.dart';
@ -32,7 +34,20 @@ class DeviceManagementBloc extends Bloc<DeviceManagementEvent, DeviceManagementS
Future<void> _onFetchDevices(FetchDevices event, Emitter<DeviceManagementState> emit) async {
emit(DeviceManagementLoading());
try {
final devices = await DevicesManagementApi().fetchDevices(event.communityId, event.spaceId);
List<AllDevicesModel> devices = [];
_devices.clear();
var spaceBloc = event.context.read<SpaceTreeBloc>();
if (spaceBloc.state.selectedCommunities.isEmpty) {
devices = await DevicesManagementApi().fetchDevices('', '');
} else {
for (var community in spaceBloc.state.selectedCommunities) {
List<String> spacesList = spaceBloc.state.selectedCommunityAndSpaces[community] ?? [];
for (var space in spacesList) {
devices.addAll(await DevicesManagementApi().fetchDevices(community, space));
}
}
}
_selectedDevices.clear();
_devices = devices;
_filteredDevices = devices;

View File

@ -8,12 +8,13 @@ abstract class DeviceManagementEvent extends Equatable {
}
class FetchDevices extends DeviceManagementEvent {
final String communityId;
final String spaceId;
// final Map<String, List<String>> selectedCommunitiesSpaces;
// final String spaceId;
final BuildContext context;
const FetchDevices(this.communityId, this.spaceId);
const FetchDevices(this.context);
@override
List<Object?> get props => [communityId, spaceId];
List<Object?> get props => [context];
}
class FilterDevices extends DeviceManagementEvent {

View File

@ -19,7 +19,7 @@ class DeviceManagementPage extends StatelessWidget with HelperResponsiveLayout {
return MultiBlocProvider(
providers: [
BlocProvider(
create: (context) => DeviceManagementBloc()..add(const FetchDevices('', '')),
create: (context) => DeviceManagementBloc()..add(FetchDevices(context)),
),
],
child: WebScaffold(

View File

@ -8,6 +8,7 @@ import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_mo
import 'package:syncrow_web/pages/device_managment/all_devices/widgets/device_search_filters.dart';
import 'package:syncrow_web/pages/device_managment/shared/device_batch_control_dialog.dart';
import 'package:syncrow_web/pages/device_managment/shared/device_control_dialog.dart';
import 'package:syncrow_web/pages/space_tree/bloc/space_tree_bloc.dart';
import 'package:syncrow_web/pages/space_tree/view/space_tree_view.dart';
import 'package:syncrow_web/utils/format_date_time.dart';
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';
@ -62,12 +63,11 @@ class DeviceManagementBody extends StatelessWidget with HelperResponsiveLayout {
return Row(
children: [
const Expanded(
child: SpaceTreeView(
// onSelectAction: (String communityId, String spaceId) {
// context.read<DeviceManagementBloc>().add(FetchDevices(communityId, spaceId));
// },
)),
Expanded(child: SpaceTreeView(
onSelect: () {
context.read<DeviceManagementBloc>().add(FetchDevices(context));
},
)),
Expanded(
flex: 3,
child: state is DeviceManagementLoading

View File

@ -85,7 +85,7 @@ class _DeviceSearchFiltersState extends State<DeviceSearchFilters> with HelperRe
productNameController.clear();
context.read<DeviceManagementBloc>()
..add(ResetFilters())
..add(const FetchDevices('', ''));
..add(FetchDevices(context));
},
);
}

View File

@ -816,7 +816,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
FutureOr<void> _fetchDevices(FetchDevicesInRoutine event, Emitter<RoutineState> emit) async {
emit(state.copyWith(isLoading: true));
try {
final devices = await DevicesManagementApi().fetchDevices(communityId, spaceId);
final devices = await DevicesManagementApi().fetchDevices('', '');
emit(state.copyWith(isLoading: false, devices: devices));
} catch (e) {

View File

@ -32,7 +32,7 @@ class _RoutinesViewState extends State<RoutinesView> {
}
return Row(
children: [
const Expanded(
Expanded(
child:
// SideSpacesView(
// onSelectAction: (String communityId, String spaceId) {
@ -41,7 +41,9 @@ class _RoutinesViewState extends State<RoutinesView> {
// // ..add(LoadAutomation(spaceId));
// },
// )
SpaceTreeView()),
SpaceTreeView(
onSelect: () {},
)),
Expanded(
flex: 3,
child: Padding(

View File

@ -6,8 +6,8 @@ import 'package:syncrow_web/pages/spaces_management/all_spaces/model/space_model
import 'package:syncrow_web/services/space_mana_api.dart';
class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
String selectedCommunityId = '';
String selectedSpaceId = '';
String selectedCommunityId = 'aff21a57-2f91-4e5c-b99b-0182c3ab65a9';
String selectedSpaceId = '25c96044-fadf-44bb-93c7-3c079e527ce6';
SpaceTreeBloc() : super(const SpaceTreeState()) {
on<InitialEvent>(_fetchSpaces);
@ -87,6 +87,7 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
List.from(state.selectedCommunities.toSet().toList());
List<String> updatedSelectedSpaces = List.from(state.selectedSpaces.toSet().toList());
List<String> updatedSoldChecks = List.from(state.soldCheck.toSet().toList());
Map<String, List<String>> communityAndSpaces = Map.from(state.selectedCommunityAndSpaces);
List<String> childrenIds = _getAllChildIds(event.children);
@ -101,10 +102,13 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
updatedSoldChecks.removeWhere(childrenIds.contains);
}
communityAndSpaces[event.communityId] = updatedSelectedSpaces;
emit(state.copyWith(
selectedCommunities: updatedSelectedCommunities,
selectedSpaces: updatedSelectedSpaces,
soldCheck: updatedSoldChecks));
soldCheck: updatedSoldChecks,
selectedCommunityAndSpaces: communityAndSpaces));
} catch (e) {
emit(const SpaceTreeErrorState('Something went wrong'));
}
@ -116,6 +120,7 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
List.from(state.selectedCommunities.toSet().toList());
List<String> updatedSelectedSpaces = List.from(state.selectedSpaces.toSet().toList());
List<String> updatedSoldChecks = List.from(state.soldCheck.toSet().toList());
Map<String, List<String>> communityAndSpaces = Map.from(state.selectedCommunityAndSpaces);
List<String> childrenIds = _getAllChildIds(event.children);
bool isChildSelected = false;
@ -166,10 +171,13 @@ class SpaceTreeBloc extends Bloc<SpaceTreeEvent, SpaceTreeState> {
}
}
communityAndSpaces[event.communityId] = updatedSelectedSpaces;
emit(state.copyWith(
selectedCommunities: updatedSelectedCommunities,
selectedSpaces: updatedSelectedSpaces,
soldCheck: updatedSoldChecks));
soldCheck: updatedSoldChecks,
selectedCommunityAndSpaces: communityAndSpaces));
emit(state.copyWith(selectedSpaces: updatedSelectedSpaces));
} catch (e) {
emit(const SpaceTreeErrorState('Something went wrong'));

View File

@ -2,6 +2,7 @@ import 'package:equatable/equatable.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/community_model.dart';
class SpaceTreeState extends Equatable {
final Map<String, List<String>> selectedCommunityAndSpaces;
final List<CommunityModel> communityList;
final List<CommunityModel> filteredCommunity;
final List<String> expandedCommunities;
@ -19,7 +20,8 @@ class SpaceTreeState extends Equatable {
this.selectedCommunities = const [],
this.selectedSpaces = const [],
this.soldCheck = const [],
this.isSearching = false});
this.isSearching = false,
this.selectedCommunityAndSpaces = const {}});
SpaceTreeState copyWith(
{List<CommunityModel>? communitiesList,
@ -29,7 +31,8 @@ class SpaceTreeState extends Equatable {
List<String>? selectedCommunities,
List<String>? selectedSpaces,
List<String>? soldCheck,
bool? isSearching}) {
bool? isSearching,
Map<String, List<String>>? selectedCommunityAndSpaces}) {
return SpaceTreeState(
communityList: communitiesList ?? this.communityList,
filteredCommunity: filteredCommunity ?? this.filteredCommunity,
@ -38,7 +41,8 @@ class SpaceTreeState extends Equatable {
selectedCommunities: selectedCommunities ?? this.selectedCommunities,
selectedSpaces: selectedSpaces ?? this.selectedSpaces,
soldCheck: soldCheck ?? this.soldCheck,
isSearching: isSearching ?? this.isSearching);
isSearching: isSearching ?? this.isSearching,
selectedCommunityAndSpaces: selectedCommunityAndSpaces ?? this.selectedCommunityAndSpaces);
}
@override
@ -50,7 +54,8 @@ class SpaceTreeState extends Equatable {
selectedCommunities,
selectedSpaces,
soldCheck,
isSearching
isSearching,
selectedCommunityAndSpaces
];
}

View File

@ -11,7 +11,8 @@ import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/style.dart';
class SpaceTreeView extends StatelessWidget {
const SpaceTreeView({super.key});
final Function onSelect;
const SpaceTreeView({required this.onSelect, super.key});
@override
Widget build(BuildContext context) {
@ -64,6 +65,8 @@ class SpaceTreeView extends StatelessWidget {
onItemSelected: () {
context.read<SpaceTreeBloc>().add(
OnCommunitySelected(community.uuid, community.spaces));
onSelect();
},
children: community.spaces.map((space) {
return CustomExpansionTileSpaceTree(
@ -72,6 +75,7 @@ class SpaceTreeView extends StatelessWidget {
onItemSelected: () {
context.read<SpaceTreeBloc>().add(OnSpaceSelected(
community.uuid, space.uuid ?? '', space.children));
onSelect();
},
onExpansionChanged: () {
context.read<SpaceTreeBloc>().add(
@ -109,6 +113,7 @@ class SpaceTreeView extends StatelessWidget {
context
.read<SpaceTreeBloc>()
.add(OnSpaceSelected(communityId, child.uuid ?? '', child.children));
onSelect();
},
onExpansionChanged: () {
context.read<SpaceTreeBloc>().add(OnSpaceExpanded(communityId, child.uuid ?? ''));