mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-24 21:07:54 +00:00
Implemented home creation and add counter to the sign up screen
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:syncrow_app/features/auth/model/user_model.dart';
|
||||
import 'package:syncrow_app/features/menu/bloc/create_unit_bloc/create_unit_event.dart';
|
||||
import 'package:syncrow_app/features/menu/bloc/create_unit_bloc/create_unit_state.dart';
|
||||
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
|
||||
import 'package:syncrow_app/services/api/home_creation_api.dart';
|
||||
|
||||
class CreateUnitBloc extends Bloc<CreateUnitEvent, CreateUnitState> {
|
||||
List<String> rooms = [];
|
||||
@@ -11,6 +13,7 @@ class CreateUnitBloc extends Bloc<CreateUnitEvent, CreateUnitState> {
|
||||
CreateUnitBloc() : super(InitialState()) {
|
||||
on<CreateRoomEvent>(_createRoom);
|
||||
on<RemoveRoomEvent>(_removeRoom);
|
||||
on<SaveRooms>(_save);
|
||||
}
|
||||
|
||||
void _createRoom(CreateRoomEvent event, Emitter<CreateUnitState> emit) async {
|
||||
@@ -39,4 +42,189 @@ class CreateUnitBloc extends Bloc<CreateUnitEvent, CreateUnitState> {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void _save(SaveRooms event, Emitter<CreateUnitState> emit) async {
|
||||
try {
|
||||
emit(LoadingState());
|
||||
if (rooms.isEmpty) {
|
||||
const ErrorState(message: 'Please add at least one room!');
|
||||
return;
|
||||
}
|
||||
|
||||
var storage = const FlutterSecureStorage();
|
||||
var userId = await storage.read(key: UserModel.userUuidKey) ?? '';
|
||||
|
||||
Map<String, String> communityBody = {'communityName': event.communityName};
|
||||
final response = await HomeCreation.createCommunity(communityBody);
|
||||
if (response['data']['uuid'] != '') {
|
||||
// final result =
|
||||
// await _assignToCommunity(communityId: response['data']['uuid'], userId: userId);
|
||||
|
||||
// if (result) {
|
||||
String buildingId = await _createBuilding(
|
||||
buildingName: event.buildingName,
|
||||
communityId: response['data']['uuid'],
|
||||
userId: userId);
|
||||
|
||||
if (buildingId.isNotEmpty) {
|
||||
final floorId = await _createFloor(
|
||||
floorName: event.floorName, buildingId: buildingId, userId: userId);
|
||||
|
||||
if (floorId.isNotEmpty) {
|
||||
final unitId =
|
||||
await _createUnit(unitName: event.unitName, floorId: floorId, userId: userId);
|
||||
|
||||
if (unitId.isNotEmpty && rooms.isNotEmpty) {
|
||||
rooms.forEach((room) async {
|
||||
await _createNewRoom(roomName: room, unitId: unitId, userId: userId);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
// }
|
||||
}
|
||||
emit(RoomsSavedSuccessfully());
|
||||
} catch (_) {
|
||||
emit(const ErrorState(message: 'Something went wrong'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> _assignToCommunity({required String communityId, required String userId}) async {
|
||||
try {
|
||||
Map<String, String> body = {
|
||||
'communityUuid': communityId,
|
||||
'userUuid': userId,
|
||||
};
|
||||
final response = await HomeCreation.assignUserToCommunity(body);
|
||||
|
||||
return response['success'] ?? false;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> _createBuilding(
|
||||
{required String buildingName, required String communityId, required String userId}) async {
|
||||
try {
|
||||
Map<String, String> body = {'buildingName': buildingName, 'communityUuid': communityId};
|
||||
final response = await HomeCreation.createBuilding(body);
|
||||
// if (response['data']['uuid'] != '') {
|
||||
// final result = await _assignToBuilding(buildingId: response['data']['uuid'], userId: userId);
|
||||
|
||||
return response['data']['uuid'] ?? '';
|
||||
// } else {
|
||||
// return '';
|
||||
// }
|
||||
} catch (_) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> _assignToBuilding({required String buildingId, required String userId}) async {
|
||||
try {
|
||||
Map<String, String> body = {
|
||||
'buildingUuid': buildingId,
|
||||
'userUuid': userId,
|
||||
};
|
||||
final response = await HomeCreation.assignUserToBuilding(body);
|
||||
|
||||
return response['success'] ?? false;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> _createFloor(
|
||||
{required String floorName, required String buildingId, required String userId}) async {
|
||||
try {
|
||||
Map<String, String> body = {'floorName': floorName, 'buildingUuid': buildingId};
|
||||
final response = await HomeCreation.createFloor(body);
|
||||
// if (response['data']['uuid'] != '') {
|
||||
// final result = await _assignToFloor(buildingId: response['data']['uuid'], userId: userId);
|
||||
|
||||
return response['data']['uuid'] ?? '';
|
||||
// } else {
|
||||
// return '';
|
||||
// }
|
||||
} catch (_) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> _assignToFloor({required String buildingId, required String userId}) async {
|
||||
try {
|
||||
Map<String, String> body = {
|
||||
'floorUuid': buildingId,
|
||||
'userUuid': userId,
|
||||
};
|
||||
final response = await HomeCreation.assignUserToFloor(body);
|
||||
|
||||
return response['success'] ?? false;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> _createUnit(
|
||||
{required String unitName, required String floorId, required String userId}) async {
|
||||
try {
|
||||
Map<String, String> body = {'unitName': unitName, 'floorUuid': floorId};
|
||||
final response = await HomeCreation.createUnit(body);
|
||||
if (response['data']['uuid'] != '') {
|
||||
final result = await _assignToUnit(unitId: response['data']['uuid'], userId: userId);
|
||||
|
||||
return result ? response['data']['uuid'] : '';
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
} catch (_) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> _assignToUnit({required String unitId, required String userId}) async {
|
||||
try {
|
||||
Map<String, String> body = {
|
||||
'unitUuid': unitId,
|
||||
'userUuid': userId,
|
||||
};
|
||||
final response = await HomeCreation.assignUserToUnit(body);
|
||||
|
||||
return response['success'] ?? false;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> _createNewRoom(
|
||||
{required String roomName, required String unitId, required String userId}) async {
|
||||
try {
|
||||
Map<String, String> body = {'roomName': roomName, 'unitUuid': unitId};
|
||||
final response = await HomeCreation.createRoom(body);
|
||||
// if (response['data']['uuid'] != '') {
|
||||
// final result = await _assignToRoom(roomId: response['data']['uuid'], userId: userId);
|
||||
|
||||
return response['data']['uuid'] ?? '';
|
||||
// } else {
|
||||
// return '';
|
||||
// }
|
||||
} catch (e) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> _assignToRoom({required String roomId, required String userId}) async {
|
||||
try {
|
||||
Map<String, String> body = {
|
||||
'roomUuid': roomId,
|
||||
'userUuid': userId,
|
||||
};
|
||||
final response = await HomeCreation.assignUserToRoom(body);
|
||||
|
||||
return response['success'] ?? false;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,3 +28,20 @@ class RemoveRoomEvent extends CreateUnitEvent {
|
||||
@override
|
||||
List<Object> get props => [roomName];
|
||||
}
|
||||
|
||||
class SaveRooms extends CreateUnitEvent {
|
||||
final String communityName;
|
||||
final String buildingName;
|
||||
final String floorName;
|
||||
final String unitName;
|
||||
|
||||
const SaveRooms({
|
||||
required this.communityName,
|
||||
required this.buildingName,
|
||||
required this.floorName,
|
||||
required this.unitName,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object> get props => [communityName, buildingName, floorName, unitName];
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ class CreateRoomState extends CreateUnitState {
|
||||
List<Object> get props => [roomList];
|
||||
}
|
||||
|
||||
class RoomsSavedSuccessfully extends CreateUnitState {}
|
||||
|
||||
class ErrorState extends CreateUnitState {
|
||||
final String message;
|
||||
|
||||
|
||||
+21
-1
@@ -11,6 +11,8 @@ import 'package:syncrow_app/features/shared_widgets/text_widgets/body_large.dart
|
||||
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
|
||||
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.dart';
|
||||
import 'package:syncrow_app/generated/assets.dart';
|
||||
import 'package:syncrow_app/navigation/navigation_service.dart';
|
||||
import 'package:syncrow_app/navigation/routing_constants.dart';
|
||||
import 'package:syncrow_app/utils/context_extension.dart';
|
||||
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
|
||||
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
|
||||
@@ -26,6 +28,14 @@ class CreateUnitView extends StatelessWidget {
|
||||
return BlocProvider(
|
||||
create: (context) => CreateUnitBloc(),
|
||||
child: BlocConsumer<CreateUnitBloc, CreateUnitState>(listener: (context, state) {
|
||||
if (state is RoomsSavedSuccessfully) {
|
||||
CustomSnackBar.displaySnackBar('Saved successfully');
|
||||
NavigationService.navigatorKey.currentState!.pushNamedAndRemoveUntil(
|
||||
Routes.homeRoute,
|
||||
(Route route) => false,
|
||||
);
|
||||
}
|
||||
|
||||
if (state is ErrorState) {
|
||||
CustomSnackBar.displaySnackBar(state.message);
|
||||
}
|
||||
@@ -34,7 +44,17 @@ class CreateUnitView extends StatelessWidget {
|
||||
return DefaultScaffold(
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {},
|
||||
onPressed: () {
|
||||
if (unitName.isNotEmpty) {
|
||||
BlocProvider.of<CreateUnitBloc>(context).add(SaveRooms(
|
||||
communityName: 'Community Test',
|
||||
buildingName: 'Building Test',
|
||||
floorName: 'Floor Test',
|
||||
unitName: unitName));
|
||||
} else {
|
||||
CustomSnackBar.displaySnackBar('Please enter the unit name');
|
||||
}
|
||||
},
|
||||
child: const BodyLarge(
|
||||
text: 'Save',
|
||||
fontWeight: FontWeight.bold,
|
||||
Reference in New Issue
Block a user