fixed page load for space

This commit is contained in:
hannathkadher
2024-11-19 01:21:32 +04:00
parent 7241f78566
commit 20f94e290d
8 changed files with 303 additions and 108 deletions

View File

@ -1,5 +1,3 @@
import 'package:flutter/material.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/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/services/space_mana_api.dart';
class SpaceManagementBloc
extends Bloc<SpaceManagementEvent, SpaceManagementState> {
class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementState> {
final CommunitySpaceManagementApi _api;
SpaceManagementBloc(this._api) : super(SpaceManagementInitial()) {
on<LoadCommunityAndSpacesEvent>(_onLoadCommunityAndSpaces);
on<CreateSpaceEvent>(_onCreateSpace);
on<UpdateSpacePositionEvent>(_onUpdateSpacePosition);
on<CreateCommunityEvent>(_onCreateCommunity);
on<SaveSpacesEvent>(_onSaveSpaces);
}
void _onLoadCommunityAndSpaces(
@ -30,8 +27,7 @@ class SpaceManagementBloc
// Use Future.wait to handle async calls within map
List<CommunityModel> updatedCommunities = await Future.wait(
communities.map((community) async {
List<SpaceModel> spaces =
await _api.getSpaceHierarchy(community.uuid);
List<SpaceModel> spaces = await _api.getSpaceHierarchy(community.uuid);
return CommunityModel(
uuid: community.uuid,
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(
UpdateSpacePositionEvent event,
Emitter<SpaceManagementState> emit,
@ -82,8 +69,7 @@ class SpaceManagementBloc
if (newCommunity != null) {
if (previousState is SpaceManagementLoaded) {
final updatedCommunities =
List<CommunityModel>.from(previousState.communities)
final updatedCommunities = List<CommunityModel>.from(previousState.communities)
..add(newCommunity);
emit(SpaceManagementLoaded(communities: updatedCommunities));
}
@ -95,34 +81,91 @@ class SpaceManagementBloc
}
}
void _onSaveSpaces(
void _onSaveSpaces(
SaveSpacesEvent event,
Emitter<SpaceManagementState> emit,
) async {
) async {
final previousState = state;
emit(SpaceManagementLoading());
try {
// Save spaces one by one
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,
);
}
final updatedSpaces = await saveSpacesHierarchically(event.spaces, event.communityUuid);
emit(SpaceCreationSuccess());
emit(SpaceCreationSuccess(spaces: updatedSpaces));
add(LoadCommunityAndSpacesEvent());
} catch (e) {
emit(SpaceManagementError('Error saving spaces: $e'));
// Revert back to the previous state if an error occurs
if (previousState is SpaceManagementLoaded) {
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
}
}

View File

@ -73,3 +73,13 @@ class CreateCommunityEvent extends SpaceManagementEvent {
@override
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];
}

View File

@ -1,5 +1,6 @@
import 'package:equatable/equatable.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 {
const SpaceManagementState();
@ -21,7 +22,14 @@ class SpaceManagementLoaded extends SpaceManagementState {
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 {
final String errorMessage;

View File

@ -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';
class SpaceModel {
final String? uuid;
String? uuid;
final String? icon;
final String? spaceTuyaUuid;
final String name;
@ -16,10 +16,10 @@ class SpaceModel {
bool isHovered;
List<Connection> outgoingConnections = []; // Connections from this space
List<Connection> incomingConnections = []; // Connections to this space
Connection? incomingConnection; // Connections to this space
SpaceModel({
this.uuid,
SpaceModel(
{this.uuid,
this.spaceTuyaUuid,
required this.icon,
required this.name,
@ -30,10 +30,11 @@ class SpaceModel {
required this.children,
required this.position,
this.isHovered = false,
});
this.incomingConnection});
factory SpaceModel.fromJson(Map<String, dynamic> json) {
return SpaceModel(
// Create SpaceModel instance first
final instance = SpaceModel(
uuid: json['uuid'] ?? '',
spaceTuyaUuid: json['spaceTuyaUuid'],
name: json['spaceName'],
@ -45,11 +46,38 @@ class SpaceModel {
? (json['children'] as List).map((child) => SpaceModel.fromJson(child)).toList()
: [],
icon: json['icon'] as String?,
position: json['position'] != null
? Offset(json['position']['dx'], json['position']['dy'])
position: json['x'] != null && json['y'] != null
? Offset(json['x'], json['y'])
: const Offset(0, 0),
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() {
@ -66,15 +94,11 @@ class SpaceModel {
'position': {'dx': position.dx, 'dy': position.dy},
'isHovered': isHovered,
'outgoingConnections': outgoingConnections.map((c) => c.toMap()).toList(),
'incomingConnections': incomingConnections.map((c) => c.toMap()).toList(),
'incomingConnection': incomingConnection?.toMap(),
};
}
void addOutgoingConnection(Connection connection) {
outgoingConnections.add(connection);
}
void addIncomingConnection(Connection connection) {
incomingConnections.add(connection);
}
}

View File

@ -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/space_data_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/sidebar_widget.dart';
import 'package:syncrow_web/services/space_mana_api.dart';
import 'package:syncrow_web/web_layout/web_scaffold.dart';

View File

@ -1,5 +1,8 @@
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/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/view/dialogs/create_space_dialog.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 {
final CommunityModel? selectedCommunity;
final List<SpaceModel> spaces;
final List<Connection> connections;
CommunityStructureArea({this.selectedCommunity});
CommunityStructureArea({
this.selectedCommunity,
required this.spaces,
required this.connections,
});
@override
_CommunityStructureAreaState createState() => _CommunityStructureAreaState();
@ -24,6 +33,28 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
List<SpaceModel> spaces = [];
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
Widget build(BuildContext context) {
Size screenSize = MediaQuery.of(context).size;
@ -196,7 +227,6 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
screenSize.width / 2 - 75, // Center horizontally
screenSize.height / 2 - 50, // Slightly above the center vertically
);
SpaceModel newSpace = SpaceModel(
name: name,
icon: icon,
@ -208,15 +238,15 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
if (parentIndex != null && direction != null) {
SpaceModel parentSpace = spaces[parentIndex];
newSpace.parent = parentSpace;
parentSpace.children.add(newSpace);
final newConnection = Connection(
startSpace: parentSpace,
endSpace: newSpace,
direction: direction,
);
connections.add(newConnection);
newSpace.addIncomingConnection(newConnection);
newSpace.incomingConnection = newConnection;
parentSpace.addOutgoingConnection(newConnection);
parentSpace.children.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() {
List<Map<String, dynamic>> spaceData = spaces.map((space) => space.toMap()).toList();
List<Map<String, dynamic>> connectionData =
connections.map((connection) => connection.toMap()).toList();
if (widget.selectedCommunity == null) {
print("No community selected for saving spaces.");
return;
}
// Save to local storage, a file, or send to a backend
print('Spaces: ${spaceData}');
print('Connections: ${connectionData}');
// Prepare spaces and connections data
List<SpaceModel> spaceList = spaces;
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,
));
}
}

View File

@ -32,7 +32,11 @@ class _LoadedStateViewState extends State<LoadedSpaceView> {
communities: widget.communities,
onCommunitySelected: widget.onCommunitySelected,
),
CommunityStructureArea(selectedCommunity: widget.selectedCommunity),
CommunityStructureArea(
selectedCommunity: widget.selectedCommunity,
spaces: widget.selectedCommunity?.spaces ?? [],
connections: [],
),
],
),
const GradientCanvasBorderWidget(),

View File

@ -32,8 +32,7 @@ class CommunitySpaceManagementApi {
Future<CommunityModel?> getCommunityById(String communityId) async {
try {
final response = await HTTPService().get(
path: ApiEndpoints.getCommunityById
.replaceAll('{communityId}', communityId),
path: ApiEndpoints.getCommunityById.replaceAll('{communityId}', communityId),
expectedResponseModel: (json) {
return CommunityModel.fromJson(json['data']);
},
@ -45,8 +44,7 @@ class CommunitySpaceManagementApi {
}
}
Future<CommunityModel?> createCommunity(
String name, String description, String regionId) async {
Future<CommunityModel?> createCommunity(String name, String description, String regionId) async {
try {
final response = await HTTPService().post(
path: ApiEndpoints.createCommunity,
@ -66,12 +64,10 @@ class CommunitySpaceManagementApi {
}
}
Future<bool> updateCommunity(
String communityId, CommunityModel community) async {
Future<bool> updateCommunity(String communityId, CommunityModel community) async {
try {
final response = await HTTPService().put(
path: ApiEndpoints.updateCommunity
.replaceAll('{communityId}', communityId),
path: ApiEndpoints.updateCommunity.replaceAll('{communityId}', communityId),
body: community.toMap(),
expectedResponseModel: (json) {
return json['success'] ?? false;
@ -87,8 +83,7 @@ class CommunitySpaceManagementApi {
Future<bool> deleteCommunity(String communityId) async {
try {
final response = await HTTPService().delete(
path: ApiEndpoints.deleteCommunity
.replaceAll('{communityId}', communityId),
path: ApiEndpoints.deleteCommunity.replaceAll('{communityId}', communityId),
expectedResponseModel: (json) {
return json['success'] ?? false;
},
@ -145,20 +140,24 @@ class CommunitySpaceManagementApi {
required String communityId,
required String name,
String? parentId,
String? direction,
bool isPrivate = false,
required Offset position,
String? icon,
}) async {
try {
final body = {
'name': name,
'spaceName': name,
'isPrivate': isPrivate,
'x': position.dx,
'y': position.dy,
'direction': direction,
'icon': icon
};
if (parentId != null) {
body['parentId'] = parentId;
body['parentUuid'] = parentId;
}
print(ApiEndpoints.createSpace.replaceAll('{communityId}', communityId));
final response = await HTTPService().post(
path: ApiEndpoints.createSpace.replaceAll('{communityId}', communityId),
body: body,
@ -173,22 +172,42 @@ class CommunitySpaceManagementApi {
}
}
Future<bool> updateSpace(
String communityId, String spaceId, SpaceModel space) async {
Future<SpaceModel?> updateSpace({
required String communityId,
required spaceId,
required String name,
String? parentId,
String? icon,
String? direction,
bool isPrivate = false,
required Offset position,
}) async {
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(
path: ApiEndpoints.updateSpace
.replaceAll('{communityId}', communityId)
.replaceAll('{spaceId}', spaceId),
body: space.toMap(),
body: body,
expectedResponseModel: (json) {
return json['success'] ?? false;
return SpaceModel.fromJson(json['data']);
},
);
return response;
} catch (e) {
debugPrint('Error updating space: $e');
return false;
debugPrint('Error creating space: $e');
return null;
}
}
@ -212,12 +231,9 @@ class CommunitySpaceManagementApi {
Future<List<SpaceModel>> getSpaceHierarchy(String communityId) async {
try {
final response = await HTTPService().get(
path: ApiEndpoints.getSpaceHierarchy
.replaceAll('{communityId}', communityId),
path: ApiEndpoints.getSpaceHierarchy.replaceAll('{communityId}', communityId),
expectedResponseModel: (json) {
return (json['data'] as List)
.map((spaceJson) => SpaceModel.fromJson(spaceJson))
.toList();
return (json['data'] as List).map((spaceJson) => SpaceModel.fromJson(spaceJson)).toList();
},
);
return response;