mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
fixed page load for space
This commit is contained in:
@ -1,5 +1,3 @@
|
|||||||
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:syncrow_web/pages/spaces_management/model/community_model.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/model/space_model.dart';
|
||||||
@ -7,15 +5,14 @@ import 'package:syncrow_web/pages/spaces_management/bloc/space_management_event.
|
|||||||
import 'package:syncrow_web/pages/spaces_management/bloc/space_management_state.dart';
|
import 'package:syncrow_web/pages/spaces_management/bloc/space_management_state.dart';
|
||||||
import 'package:syncrow_web/services/space_mana_api.dart';
|
import 'package:syncrow_web/services/space_mana_api.dart';
|
||||||
|
|
||||||
class SpaceManagementBloc
|
class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementState> {
|
||||||
extends Bloc<SpaceManagementEvent, SpaceManagementState> {
|
|
||||||
final CommunitySpaceManagementApi _api;
|
final CommunitySpaceManagementApi _api;
|
||||||
|
|
||||||
SpaceManagementBloc(this._api) : super(SpaceManagementInitial()) {
|
SpaceManagementBloc(this._api) : super(SpaceManagementInitial()) {
|
||||||
on<LoadCommunityAndSpacesEvent>(_onLoadCommunityAndSpaces);
|
on<LoadCommunityAndSpacesEvent>(_onLoadCommunityAndSpaces);
|
||||||
on<CreateSpaceEvent>(_onCreateSpace);
|
|
||||||
on<UpdateSpacePositionEvent>(_onUpdateSpacePosition);
|
on<UpdateSpacePositionEvent>(_onUpdateSpacePosition);
|
||||||
on<CreateCommunityEvent>(_onCreateCommunity);
|
on<CreateCommunityEvent>(_onCreateCommunity);
|
||||||
|
on<SaveSpacesEvent>(_onSaveSpaces);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onLoadCommunityAndSpaces(
|
void _onLoadCommunityAndSpaces(
|
||||||
@ -30,8 +27,7 @@ class SpaceManagementBloc
|
|||||||
// Use Future.wait to handle async calls within map
|
// Use Future.wait to handle async calls within map
|
||||||
List<CommunityModel> updatedCommunities = await Future.wait(
|
List<CommunityModel> updatedCommunities = await Future.wait(
|
||||||
communities.map((community) async {
|
communities.map((community) async {
|
||||||
List<SpaceModel> spaces =
|
List<SpaceModel> spaces = await _api.getSpaceHierarchy(community.uuid);
|
||||||
await _api.getSpaceHierarchy(community.uuid);
|
|
||||||
return CommunityModel(
|
return CommunityModel(
|
||||||
uuid: community.uuid,
|
uuid: community.uuid,
|
||||||
createdAt: community.createdAt,
|
createdAt: community.createdAt,
|
||||||
@ -50,15 +46,6 @@ class SpaceManagementBloc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onCreateSpace(
|
|
||||||
CreateSpaceEvent event,
|
|
||||||
Emitter<SpaceManagementState> emit,
|
|
||||||
) {
|
|
||||||
// Handle space creation logic
|
|
||||||
// You can emit a new state here based on your needs
|
|
||||||
emit(SpaceCreationSuccess());
|
|
||||||
}
|
|
||||||
|
|
||||||
void _onUpdateSpacePosition(
|
void _onUpdateSpacePosition(
|
||||||
UpdateSpacePositionEvent event,
|
UpdateSpacePositionEvent event,
|
||||||
Emitter<SpaceManagementState> emit,
|
Emitter<SpaceManagementState> emit,
|
||||||
@ -82,9 +69,8 @@ class SpaceManagementBloc
|
|||||||
|
|
||||||
if (newCommunity != null) {
|
if (newCommunity != null) {
|
||||||
if (previousState is SpaceManagementLoaded) {
|
if (previousState is SpaceManagementLoaded) {
|
||||||
final updatedCommunities =
|
final updatedCommunities = List<CommunityModel>.from(previousState.communities)
|
||||||
List<CommunityModel>.from(previousState.communities)
|
..add(newCommunity);
|
||||||
..add(newCommunity);
|
|
||||||
emit(SpaceManagementLoaded(communities: updatedCommunities));
|
emit(SpaceManagementLoaded(communities: updatedCommunities));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -95,34 +81,91 @@ class SpaceManagementBloc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onSaveSpaces(
|
void _onSaveSpaces(
|
||||||
SaveSpacesEvent event,
|
SaveSpacesEvent event,
|
||||||
Emitter<SpaceManagementState> emit,
|
Emitter<SpaceManagementState> emit,
|
||||||
) async {
|
) async {
|
||||||
final previousState = state;
|
final previousState = state;
|
||||||
emit(SpaceManagementLoading());
|
emit(SpaceManagementLoading());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Save spaces one by one
|
final updatedSpaces = await saveSpacesHierarchically(event.spaces, event.communityUuid);
|
||||||
for (var space in event.spaces) {
|
|
||||||
await _api.createSpace(
|
|
||||||
communityId: event.communityUuid,
|
|
||||||
name: space.name,
|
|
||||||
parentId: space.parent?.uuid,
|
|
||||||
isPrivate: space.isPrivate,
|
|
||||||
position: space.position,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
emit(SpaceCreationSuccess());
|
emit(SpaceCreationSuccess(spaces: updatedSpaces));
|
||||||
} catch (e) {
|
add(LoadCommunityAndSpacesEvent());
|
||||||
emit(SpaceManagementError('Error saving spaces: $e'));
|
} catch (e) {
|
||||||
|
emit(SpaceManagementError('Error saving spaces: $e'));
|
||||||
// Revert back to the previous state if an error occurs
|
if (previousState is SpaceManagementLoaded) {
|
||||||
if (previousState is SpaceManagementLoaded) {
|
emit(previousState);
|
||||||
emit(previousState);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
Future<List<SpaceModel>> saveSpacesHierarchically(
|
||||||
|
List<SpaceModel> spaces, String communityUuid) async {
|
||||||
|
final orderedSpaces = flattenHierarchy(spaces);
|
||||||
|
|
||||||
|
for (var space in orderedSpaces) {
|
||||||
|
try {
|
||||||
|
if (space.uuid != null && space.uuid!.isNotEmpty) {
|
||||||
|
// Call update if the space already has a UUID
|
||||||
|
final response = await _api.updateSpace(
|
||||||
|
communityId: communityUuid,
|
||||||
|
spaceId: space.uuid!,
|
||||||
|
name: space.name,
|
||||||
|
parentId: space.parent?.uuid,
|
||||||
|
isPrivate: space.isPrivate,
|
||||||
|
position: space.position,
|
||||||
|
icon: space.icon,
|
||||||
|
direction: space.incomingConnection?.direction,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// Call create if the space does not have a UUID
|
||||||
|
final response = await _api.createSpace(
|
||||||
|
communityId: communityUuid,
|
||||||
|
name: space.name,
|
||||||
|
parentId: space.parent?.uuid,
|
||||||
|
isPrivate: space.isPrivate,
|
||||||
|
position: space.position,
|
||||||
|
icon: space.icon,
|
||||||
|
direction: space.incomingConnection?.direction,
|
||||||
|
);
|
||||||
|
space.uuid = response?.uuid;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
print('Error creating space ${space.name}: $e');
|
||||||
|
rethrow; // Stop further execution on failure
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return spaces;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<SpaceModel> flattenHierarchy(List<SpaceModel> spaces) {
|
||||||
|
final result = <SpaceModel>{}; // Use a Set to avoid duplicates
|
||||||
|
|
||||||
|
// Collect all top-level spaces (those without a parent)
|
||||||
|
final topLevelSpaces = spaces.where((space) => space.parent == null);
|
||||||
|
|
||||||
|
void visit(SpaceModel space) {
|
||||||
|
if (!result.contains(space)) {
|
||||||
|
result.add(space); // Add the space
|
||||||
|
for (var child in spaces.where((s) => s.parent == space)) {
|
||||||
|
visit(child); // Recursively add children based on parent
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start with top-level spaces
|
||||||
|
for (var space in topLevelSpaces) {
|
||||||
|
visit(space);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add any spaces that were not part of the hierarchy
|
||||||
|
for (var space in spaces) {
|
||||||
|
if (!result.contains(space)) {
|
||||||
|
result.add(space); // Add standalone or orphan spaces
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.toList(); // Convert back to a list
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,3 +73,13 @@ class CreateCommunityEvent extends SpaceManagementEvent {
|
|||||||
@override
|
@override
|
||||||
List<Object> get props => [name, description, regionId];
|
List<Object> get props => [name, description, regionId];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class LoadSpaceHierarchyEvent extends SpaceManagementEvent {
|
||||||
|
final String communityId;
|
||||||
|
|
||||||
|
const LoadSpaceHierarchyEvent({required this.communityId});
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [communityId];
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
import 'package:equatable/equatable.dart';
|
import 'package:equatable/equatable.dart';
|
||||||
import 'package:syncrow_web/pages/spaces_management/model/community_model.dart';
|
import 'package:syncrow_web/pages/spaces_management/model/community_model.dart';
|
||||||
|
import 'package:syncrow_web/pages/spaces_management/model/space_model.dart';
|
||||||
|
|
||||||
abstract class SpaceManagementState extends Equatable {
|
abstract class SpaceManagementState extends Equatable {
|
||||||
const SpaceManagementState();
|
const SpaceManagementState();
|
||||||
@ -21,7 +22,14 @@ class SpaceManagementLoaded extends SpaceManagementState {
|
|||||||
List<Object> get props => [communities];
|
List<Object> get props => [communities];
|
||||||
}
|
}
|
||||||
|
|
||||||
class SpaceCreationSuccess extends SpaceManagementState {}
|
class SpaceCreationSuccess extends SpaceManagementState {
|
||||||
|
final List<SpaceModel> spaces;
|
||||||
|
|
||||||
|
const SpaceCreationSuccess({required this.spaces});
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [spaces];
|
||||||
|
}
|
||||||
|
|
||||||
class SpaceManagementError extends SpaceManagementState {
|
class SpaceManagementError extends SpaceManagementState {
|
||||||
final String errorMessage;
|
final String errorMessage;
|
||||||
|
@ -3,7 +3,7 @@ import 'package:syncrow_web/pages/spaces_management/model/community_model.dart';
|
|||||||
import 'package:syncrow_web/pages/spaces_management/model/connection_model.dart';
|
import 'package:syncrow_web/pages/spaces_management/model/connection_model.dart';
|
||||||
|
|
||||||
class SpaceModel {
|
class SpaceModel {
|
||||||
final String? uuid;
|
String? uuid;
|
||||||
final String? icon;
|
final String? icon;
|
||||||
final String? spaceTuyaUuid;
|
final String? spaceTuyaUuid;
|
||||||
final String name;
|
final String name;
|
||||||
@ -16,24 +16,25 @@ class SpaceModel {
|
|||||||
bool isHovered;
|
bool isHovered;
|
||||||
|
|
||||||
List<Connection> outgoingConnections = []; // Connections from this space
|
List<Connection> outgoingConnections = []; // Connections from this space
|
||||||
List<Connection> incomingConnections = []; // Connections to this space
|
Connection? incomingConnection; // Connections to this space
|
||||||
|
|
||||||
SpaceModel({
|
SpaceModel(
|
||||||
this.uuid,
|
{this.uuid,
|
||||||
this.spaceTuyaUuid,
|
this.spaceTuyaUuid,
|
||||||
required this.icon,
|
required this.icon,
|
||||||
required this.name,
|
required this.name,
|
||||||
required this.isPrivate,
|
required this.isPrivate,
|
||||||
this.invitationCode,
|
this.invitationCode,
|
||||||
this.parent,
|
this.parent,
|
||||||
this.community,
|
this.community,
|
||||||
required this.children,
|
required this.children,
|
||||||
required this.position,
|
required this.position,
|
||||||
this.isHovered = false,
|
this.isHovered = false,
|
||||||
});
|
this.incomingConnection});
|
||||||
|
|
||||||
factory SpaceModel.fromJson(Map<String, dynamic> json) {
|
factory SpaceModel.fromJson(Map<String, dynamic> json) {
|
||||||
return SpaceModel(
|
// Create SpaceModel instance first
|
||||||
|
final instance = SpaceModel(
|
||||||
uuid: json['uuid'] ?? '',
|
uuid: json['uuid'] ?? '',
|
||||||
spaceTuyaUuid: json['spaceTuyaUuid'],
|
spaceTuyaUuid: json['spaceTuyaUuid'],
|
||||||
name: json['spaceName'],
|
name: json['spaceName'],
|
||||||
@ -45,11 +46,38 @@ class SpaceModel {
|
|||||||
? (json['children'] as List).map((child) => SpaceModel.fromJson(child)).toList()
|
? (json['children'] as List).map((child) => SpaceModel.fromJson(child)).toList()
|
||||||
: [],
|
: [],
|
||||||
icon: json['icon'] as String?,
|
icon: json['icon'] as String?,
|
||||||
position: json['position'] != null
|
position: json['x'] != null && json['y'] != null
|
||||||
? Offset(json['position']['dx'], json['position']['dy'])
|
? Offset(json['x'], json['y'])
|
||||||
: const Offset(0, 0),
|
: const Offset(0, 0),
|
||||||
isHovered: false,
|
isHovered: false,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Add incomingConnection to the instance after creation
|
||||||
|
if (json['incomingConnections'] != null &&
|
||||||
|
json['incomingConnections'] is List &&
|
||||||
|
(json['incomingConnections'] as List).isNotEmpty && instance.parent != null ) {
|
||||||
|
final conn = json['incomingConnections'][0];
|
||||||
|
instance.incomingConnection = Connection(
|
||||||
|
startSpace: instance.parent ?? instance, // Parent space
|
||||||
|
endSpace: instance, // This space instance
|
||||||
|
direction: conn['direction'],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
@override
|
||||||
|
String toString() {
|
||||||
|
return '''
|
||||||
|
SpaceModel {
|
||||||
|
uuid: $uuid,
|
||||||
|
name: $name,
|
||||||
|
isPrivate: $isPrivate,
|
||||||
|
position: {dx: ${position.dx}, dy: ${position.dy}},
|
||||||
|
parentUuid: ${parent?.uuid},
|
||||||
|
children: ${children.map((child) => child.name).toList()},
|
||||||
|
isHovered: $isHovered
|
||||||
|
}''';
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, dynamic> toMap() {
|
Map<String, dynamic> toMap() {
|
||||||
@ -66,15 +94,11 @@ class SpaceModel {
|
|||||||
'position': {'dx': position.dx, 'dy': position.dy},
|
'position': {'dx': position.dx, 'dy': position.dy},
|
||||||
'isHovered': isHovered,
|
'isHovered': isHovered,
|
||||||
'outgoingConnections': outgoingConnections.map((c) => c.toMap()).toList(),
|
'outgoingConnections': outgoingConnections.map((c) => c.toMap()).toList(),
|
||||||
'incomingConnections': incomingConnections.map((c) => c.toMap()).toList(),
|
'incomingConnection': incomingConnection?.toMap(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void addOutgoingConnection(Connection connection) {
|
void addOutgoingConnection(Connection connection) {
|
||||||
outgoingConnections.add(connection);
|
outgoingConnections.add(connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
void addIncomingConnection(Connection connection) {
|
|
||||||
incomingConnections.add(connection);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -7,10 +7,7 @@ import 'package:syncrow_web/pages/spaces_management/model/community_model.dart';
|
|||||||
import 'package:syncrow_web/pages/spaces_management/model/connection_model.dart';
|
import 'package:syncrow_web/pages/spaces_management/model/connection_model.dart';
|
||||||
import 'package:syncrow_web/pages/spaces_management/model/space_data_model.dart';
|
import 'package:syncrow_web/pages/spaces_management/model/space_data_model.dart';
|
||||||
import 'package:syncrow_web/pages/spaces_management/model/space_model.dart';
|
import 'package:syncrow_web/pages/spaces_management/model/space_model.dart';
|
||||||
import 'package:syncrow_web/pages/spaces_management/widgets/community_structure_widget.dart';
|
|
||||||
import 'package:syncrow_web/pages/spaces_management/widgets/gradient_canvas_border_widget.dart';
|
|
||||||
import 'package:syncrow_web/pages/spaces_management/widgets/loaded_space_widget.dart';
|
import 'package:syncrow_web/pages/spaces_management/widgets/loaded_space_widget.dart';
|
||||||
import 'package:syncrow_web/pages/spaces_management/widgets/sidebar_widget.dart';
|
|
||||||
import 'package:syncrow_web/services/space_mana_api.dart';
|
import 'package:syncrow_web/services/space_mana_api.dart';
|
||||||
import 'package:syncrow_web/web_layout/web_scaffold.dart';
|
import 'package:syncrow_web/web_layout/web_scaffold.dart';
|
||||||
|
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:syncrow_web/pages/common/buttons/add_space_button.dart';
|
import 'package:syncrow_web/pages/common/buttons/add_space_button.dart';
|
||||||
|
import 'package:syncrow_web/pages/spaces_management/bloc/space_management_bloc.dart';
|
||||||
|
import 'package:syncrow_web/pages/spaces_management/bloc/space_management_event.dart';
|
||||||
import 'package:syncrow_web/pages/spaces_management/model/space_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/dialogs/create_space_dialog.dart';
|
||||||
import 'package:syncrow_web/pages/spaces_management/widgets/curved_line_painter.dart';
|
import 'package:syncrow_web/pages/spaces_management/widgets/curved_line_painter.dart';
|
||||||
@ -11,8 +14,14 @@ import 'package:syncrow_web/utils/color_manager.dart';
|
|||||||
|
|
||||||
class CommunityStructureArea extends StatefulWidget {
|
class CommunityStructureArea extends StatefulWidget {
|
||||||
final CommunityModel? selectedCommunity;
|
final CommunityModel? selectedCommunity;
|
||||||
|
final List<SpaceModel> spaces;
|
||||||
|
final List<Connection> connections;
|
||||||
|
|
||||||
CommunityStructureArea({this.selectedCommunity});
|
CommunityStructureArea({
|
||||||
|
this.selectedCommunity,
|
||||||
|
required this.spaces,
|
||||||
|
required this.connections,
|
||||||
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
_CommunityStructureAreaState createState() => _CommunityStructureAreaState();
|
_CommunityStructureAreaState createState() => _CommunityStructureAreaState();
|
||||||
@ -24,6 +33,28 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
List<SpaceModel> spaces = [];
|
List<SpaceModel> spaces = [];
|
||||||
List<Connection> connections = [];
|
List<Connection> connections = [];
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
spaces = widget.spaces.isNotEmpty ? flattenSpaces(widget.spaces) : [];
|
||||||
|
connections = widget.spaces.isNotEmpty ? createConnections(widget.spaces) : [];
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didUpdateWidget(covariant CommunityStructureArea oldWidget) {
|
||||||
|
super.didUpdateWidget(oldWidget);
|
||||||
|
|
||||||
|
if (oldWidget.spaces != widget.spaces || oldWidget.connections != widget.connections) {
|
||||||
|
setState(() {
|
||||||
|
spaces = widget.spaces.isNotEmpty ? flattenSpaces(widget.spaces) : [];
|
||||||
|
connections = widget.spaces.isNotEmpty ? createConnections(widget.spaces) : [];
|
||||||
|
});
|
||||||
|
for (var space in spaces) {
|
||||||
|
print('InitState - Space Position ${space.name}: ${space.incomingConnection?.direction}');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
Size screenSize = MediaQuery.of(context).size;
|
Size screenSize = MediaQuery.of(context).size;
|
||||||
@ -196,7 +227,6 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
screenSize.width / 2 - 75, // Center horizontally
|
screenSize.width / 2 - 75, // Center horizontally
|
||||||
screenSize.height / 2 - 50, // Slightly above the center vertically
|
screenSize.height / 2 - 50, // Slightly above the center vertically
|
||||||
);
|
);
|
||||||
|
|
||||||
SpaceModel newSpace = SpaceModel(
|
SpaceModel newSpace = SpaceModel(
|
||||||
name: name,
|
name: name,
|
||||||
icon: icon,
|
icon: icon,
|
||||||
@ -208,15 +238,15 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
if (parentIndex != null && direction != null) {
|
if (parentIndex != null && direction != null) {
|
||||||
SpaceModel parentSpace = spaces[parentIndex];
|
SpaceModel parentSpace = spaces[parentIndex];
|
||||||
newSpace.parent = parentSpace;
|
newSpace.parent = parentSpace;
|
||||||
parentSpace.children.add(newSpace);
|
|
||||||
final newConnection = Connection(
|
final newConnection = Connection(
|
||||||
startSpace: parentSpace,
|
startSpace: parentSpace,
|
||||||
endSpace: newSpace,
|
endSpace: newSpace,
|
||||||
direction: direction,
|
direction: direction,
|
||||||
);
|
);
|
||||||
connections.add(newConnection);
|
connections.add(newConnection);
|
||||||
newSpace.addIncomingConnection(newConnection);
|
newSpace.incomingConnection = newConnection;
|
||||||
parentSpace.addOutgoingConnection(newConnection);
|
parentSpace.addOutgoingConnection(newConnection);
|
||||||
|
parentSpace.children.add(newSpace);
|
||||||
}
|
}
|
||||||
|
|
||||||
spaces.add(newSpace);
|
spaces.add(newSpace);
|
||||||
@ -234,14 +264,77 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<SpaceModel> flattenSpaces(List<SpaceModel> spaces) {
|
||||||
|
List<SpaceModel> result = [];
|
||||||
|
|
||||||
|
void flatten(SpaceModel space) {
|
||||||
|
// Add the current space to the result
|
||||||
|
result.add(space);
|
||||||
|
|
||||||
|
// Recursively flatten child spaces
|
||||||
|
for (var child in space.children) {
|
||||||
|
flatten(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process each top-level space
|
||||||
|
for (var space in spaces) {
|
||||||
|
flatten(space);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Connection> createConnections(List<SpaceModel> spaces) {
|
||||||
|
List<Connection> connections = [];
|
||||||
|
|
||||||
|
void addConnections(SpaceModel parent, String direction) {
|
||||||
|
for (var child in parent.children) {
|
||||||
|
// Create a connection object
|
||||||
|
connections.add(
|
||||||
|
Connection(
|
||||||
|
startSpace: parent,
|
||||||
|
endSpace: child,
|
||||||
|
direction: child.incomingConnection?.direction ??
|
||||||
|
"down", // Assuming "down" for all direct children
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Recursively process the child's children
|
||||||
|
addConnections(child, direction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process each top-level space
|
||||||
|
for (var space in spaces) {
|
||||||
|
addConnections(space, "down");
|
||||||
|
}
|
||||||
|
|
||||||
|
return connections;
|
||||||
|
}
|
||||||
|
|
||||||
void _saveSpaces() {
|
void _saveSpaces() {
|
||||||
List<Map<String, dynamic>> spaceData = spaces.map((space) => space.toMap()).toList();
|
if (widget.selectedCommunity == null) {
|
||||||
List<Map<String, dynamic>> connectionData =
|
print("No community selected for saving spaces.");
|
||||||
connections.map((connection) => connection.toMap()).toList();
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Save to local storage, a file, or send to a backend
|
// Prepare spaces and connections data
|
||||||
print('Spaces: ${spaceData}');
|
List<SpaceModel> spaceList = spaces;
|
||||||
print('Connections: ${connectionData}');
|
String communityUuid = widget.selectedCommunity!.uuid;
|
||||||
|
|
||||||
|
for (var space in spaceList) {
|
||||||
|
print("Processing space: ${space.name}, UUID: ${space.uuid}");
|
||||||
|
// Example: Log connections
|
||||||
|
print("Parent UUID: ${space.parent?.uuid}");
|
||||||
|
print("Incoming connection: ${space.incomingConnection?.direction}");
|
||||||
|
print("Outgoing connections: ${space.outgoingConnections.map((c) => c.direction).toList()}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dispatch the save event
|
||||||
|
context.read<SpaceManagementBloc>().add(SaveSpacesEvent(
|
||||||
|
spaces: spaceList,
|
||||||
|
communityUuid: communityUuid,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,11 @@ class _LoadedStateViewState extends State<LoadedSpaceView> {
|
|||||||
communities: widget.communities,
|
communities: widget.communities,
|
||||||
onCommunitySelected: widget.onCommunitySelected,
|
onCommunitySelected: widget.onCommunitySelected,
|
||||||
),
|
),
|
||||||
CommunityStructureArea(selectedCommunity: widget.selectedCommunity),
|
CommunityStructureArea(
|
||||||
|
selectedCommunity: widget.selectedCommunity,
|
||||||
|
spaces: widget.selectedCommunity?.spaces ?? [],
|
||||||
|
connections: [],
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
const GradientCanvasBorderWidget(),
|
const GradientCanvasBorderWidget(),
|
||||||
|
@ -32,8 +32,7 @@ class CommunitySpaceManagementApi {
|
|||||||
Future<CommunityModel?> getCommunityById(String communityId) async {
|
Future<CommunityModel?> getCommunityById(String communityId) async {
|
||||||
try {
|
try {
|
||||||
final response = await HTTPService().get(
|
final response = await HTTPService().get(
|
||||||
path: ApiEndpoints.getCommunityById
|
path: ApiEndpoints.getCommunityById.replaceAll('{communityId}', communityId),
|
||||||
.replaceAll('{communityId}', communityId),
|
|
||||||
expectedResponseModel: (json) {
|
expectedResponseModel: (json) {
|
||||||
return CommunityModel.fromJson(json['data']);
|
return CommunityModel.fromJson(json['data']);
|
||||||
},
|
},
|
||||||
@ -45,8 +44,7 @@ class CommunitySpaceManagementApi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<CommunityModel?> createCommunity(
|
Future<CommunityModel?> createCommunity(String name, String description, String regionId) async {
|
||||||
String name, String description, String regionId) async {
|
|
||||||
try {
|
try {
|
||||||
final response = await HTTPService().post(
|
final response = await HTTPService().post(
|
||||||
path: ApiEndpoints.createCommunity,
|
path: ApiEndpoints.createCommunity,
|
||||||
@ -66,12 +64,10 @@ class CommunitySpaceManagementApi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> updateCommunity(
|
Future<bool> updateCommunity(String communityId, CommunityModel community) async {
|
||||||
String communityId, CommunityModel community) async {
|
|
||||||
try {
|
try {
|
||||||
final response = await HTTPService().put(
|
final response = await HTTPService().put(
|
||||||
path: ApiEndpoints.updateCommunity
|
path: ApiEndpoints.updateCommunity.replaceAll('{communityId}', communityId),
|
||||||
.replaceAll('{communityId}', communityId),
|
|
||||||
body: community.toMap(),
|
body: community.toMap(),
|
||||||
expectedResponseModel: (json) {
|
expectedResponseModel: (json) {
|
||||||
return json['success'] ?? false;
|
return json['success'] ?? false;
|
||||||
@ -87,8 +83,7 @@ class CommunitySpaceManagementApi {
|
|||||||
Future<bool> deleteCommunity(String communityId) async {
|
Future<bool> deleteCommunity(String communityId) async {
|
||||||
try {
|
try {
|
||||||
final response = await HTTPService().delete(
|
final response = await HTTPService().delete(
|
||||||
path: ApiEndpoints.deleteCommunity
|
path: ApiEndpoints.deleteCommunity.replaceAll('{communityId}', communityId),
|
||||||
.replaceAll('{communityId}', communityId),
|
|
||||||
expectedResponseModel: (json) {
|
expectedResponseModel: (json) {
|
||||||
return json['success'] ?? false;
|
return json['success'] ?? false;
|
||||||
},
|
},
|
||||||
@ -145,20 +140,24 @@ class CommunitySpaceManagementApi {
|
|||||||
required String communityId,
|
required String communityId,
|
||||||
required String name,
|
required String name,
|
||||||
String? parentId,
|
String? parentId,
|
||||||
|
String? direction,
|
||||||
bool isPrivate = false,
|
bool isPrivate = false,
|
||||||
required Offset position,
|
required Offset position,
|
||||||
|
String? icon,
|
||||||
}) async {
|
}) async {
|
||||||
try {
|
try {
|
||||||
final body = {
|
final body = {
|
||||||
'name': name,
|
'spaceName': name,
|
||||||
'isPrivate': isPrivate,
|
'isPrivate': isPrivate,
|
||||||
'x': position.dx,
|
'x': position.dx,
|
||||||
'y': position.dy,
|
'y': position.dy,
|
||||||
|
'direction': direction,
|
||||||
|
'icon': icon
|
||||||
};
|
};
|
||||||
if (parentId != null) {
|
if (parentId != null) {
|
||||||
body['parentId'] = parentId;
|
body['parentUuid'] = parentId;
|
||||||
}
|
}
|
||||||
|
print(ApiEndpoints.createSpace.replaceAll('{communityId}', communityId));
|
||||||
final response = await HTTPService().post(
|
final response = await HTTPService().post(
|
||||||
path: ApiEndpoints.createSpace.replaceAll('{communityId}', communityId),
|
path: ApiEndpoints.createSpace.replaceAll('{communityId}', communityId),
|
||||||
body: body,
|
body: body,
|
||||||
@ -173,22 +172,42 @@ class CommunitySpaceManagementApi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> updateSpace(
|
Future<SpaceModel?> updateSpace({
|
||||||
String communityId, String spaceId, SpaceModel space) async {
|
required String communityId,
|
||||||
|
required spaceId,
|
||||||
|
required String name,
|
||||||
|
String? parentId,
|
||||||
|
String? icon,
|
||||||
|
String? direction,
|
||||||
|
bool isPrivate = false,
|
||||||
|
required Offset position,
|
||||||
|
}) async {
|
||||||
try {
|
try {
|
||||||
|
final body = {
|
||||||
|
'spaceName': name,
|
||||||
|
'isPrivate': isPrivate,
|
||||||
|
'x': position.dx,
|
||||||
|
'y': position.dy,
|
||||||
|
'direction': direction,
|
||||||
|
'icon': icon
|
||||||
|
};
|
||||||
|
if (parentId != null) {
|
||||||
|
body['parentUuid'] = parentId;
|
||||||
|
}
|
||||||
|
|
||||||
final response = await HTTPService().put(
|
final response = await HTTPService().put(
|
||||||
path: ApiEndpoints.updateSpace
|
path: ApiEndpoints.updateSpace
|
||||||
.replaceAll('{communityId}', communityId)
|
.replaceAll('{communityId}', communityId)
|
||||||
.replaceAll('{spaceId}', spaceId),
|
.replaceAll('{spaceId}', spaceId),
|
||||||
body: space.toMap(),
|
body: body,
|
||||||
expectedResponseModel: (json) {
|
expectedResponseModel: (json) {
|
||||||
return json['success'] ?? false;
|
return SpaceModel.fromJson(json['data']);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
return response;
|
return response;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debugPrint('Error updating space: $e');
|
debugPrint('Error creating space: $e');
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -212,12 +231,9 @@ class CommunitySpaceManagementApi {
|
|||||||
Future<List<SpaceModel>> getSpaceHierarchy(String communityId) async {
|
Future<List<SpaceModel>> getSpaceHierarchy(String communityId) async {
|
||||||
try {
|
try {
|
||||||
final response = await HTTPService().get(
|
final response = await HTTPService().get(
|
||||||
path: ApiEndpoints.getSpaceHierarchy
|
path: ApiEndpoints.getSpaceHierarchy.replaceAll('{communityId}', communityId),
|
||||||
.replaceAll('{communityId}', communityId),
|
|
||||||
expectedResponseModel: (json) {
|
expectedResponseModel: (json) {
|
||||||
return (json['data'] as List)
|
return (json['data'] as List).map((spaceJson) => SpaceModel.fromJson(spaceJson)).toList();
|
||||||
.map((spaceJson) => SpaceModel.fromJson(spaceJson))
|
|
||||||
.toList();
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
return response;
|
return response;
|
||||||
|
Reference in New Issue
Block a user