mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-25 21:27:56 +00:00
four_scene
This commit is contained in:
@@ -18,6 +18,7 @@ import 'package:syncrow_app/services/api/devices_api.dart';
|
||||
import 'package:syncrow_app/services/api/home_management_api.dart';
|
||||
import 'package:syncrow_app/services/api/scene_api.dart';
|
||||
import 'package:syncrow_app/services/api/spaces_api.dart';
|
||||
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
|
||||
|
||||
class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
final String fourSceneId;
|
||||
@@ -27,6 +28,7 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
on<FourSceneInitial>(_fetchStatus);
|
||||
on<FourSceneInitialInfo>(fetchDeviceData);
|
||||
on<ReportLogsInitial>(fetchLogsForLastMonth);
|
||||
on<SaveNameEvent>(saveName);
|
||||
on<ToggleNotificationEvent>(_toggleNotification);
|
||||
on<ChangeNameEvent>(_changeName);
|
||||
on<SearchFaqEvent>(_onSearchFaq);
|
||||
@@ -40,6 +42,7 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
on<AddDeviceToGroup>(_addDeviceToGroup);
|
||||
on<RemoveDeviceFromGroup>(_removeDeviceFromGroup);
|
||||
on<FourSceneInitialQuestion>(_onFourSceneInitial);
|
||||
on<FetchDeviceScene>(_fetchDeviceScene);
|
||||
}
|
||||
|
||||
final TextEditingController nameController =
|
||||
@@ -93,6 +96,47 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
),
|
||||
);
|
||||
|
||||
// void changeDeviceName(
|
||||
// SaveNameEvent event, Emitter<FourSceneState> emit) async {
|
||||
// emit(FourSceneLoadingState());
|
||||
// try {
|
||||
// var response = await DevicesAPI.putDeviceName(
|
||||
// deviceId: fourSceneId, deviceName: event.deviceName!);
|
||||
// List<StatusModel> statusModelList = [];
|
||||
// for (var status in response['status']) {
|
||||
// statusModelList.add(StatusModel.fromJson(status));
|
||||
// }
|
||||
// deviceStatus = FourSceneModel.fromJson(
|
||||
// statusModelList,
|
||||
// );
|
||||
// emit(UpdateState(sensor: deviceStatus));
|
||||
// Future.delayed(const Duration(milliseconds: 500));
|
||||
// // _listenToChanges();
|
||||
// } catch (e) {
|
||||
// emit(FourSceneFailedState(errorMessage: e.toString()));
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
|
||||
Future<void> saveName(
|
||||
SaveNameEvent event, Emitter<FourSceneState> emit) async {
|
||||
if (_validateInputs()) return;
|
||||
try {
|
||||
add(const ChangeNameEvent(value: false));
|
||||
isSaving = true;
|
||||
emit(FourSceneLoadingState());
|
||||
var response = await DevicesAPI.putDeviceName(
|
||||
deviceId: fourSceneId, deviceName: nameController.text);
|
||||
add(FourSceneInitialInfo());
|
||||
CustomSnackBar.displaySnackBar('Save Successfully');
|
||||
emit(SaveState());
|
||||
} catch (e) {
|
||||
emit(FourSceneFailedState(errorMessage: e.toString()));
|
||||
} finally {
|
||||
isSaving = false;
|
||||
}
|
||||
}
|
||||
|
||||
void _fetchStatus(
|
||||
FourSceneInitial event, Emitter<FourSceneState> emit) async {
|
||||
emit(FourSceneLoadingState());
|
||||
@@ -114,6 +158,27 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _fetchDeviceScene(
|
||||
FetchDeviceScene event, Emitter<FourSceneState> emit) async {
|
||||
emit(FourSceneLoadingState());
|
||||
try {
|
||||
var response = await DevicesAPI.getDeviceStatus(fourSceneId);
|
||||
List<StatusModel> statusModelList = [];
|
||||
for (var status in response['status']) {
|
||||
statusModelList.add(StatusModel.fromJson(status));
|
||||
}
|
||||
deviceStatus = FourSceneModel.fromJson(
|
||||
statusModelList,
|
||||
);
|
||||
emit(UpdateState(sensor: deviceStatus));
|
||||
Future.delayed(const Duration(milliseconds: 500));
|
||||
// _listenToChanges();
|
||||
} catch (e) {
|
||||
emit(FourSceneFailedState(errorMessage: e.toString()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Future fetchDeviceData(
|
||||
FourSceneInitialInfo event, Emitter<FourSceneState> emit) async {
|
||||
emit(FourSceneLoadingState());
|
||||
@@ -369,6 +434,8 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
allDevices.forEach((element) {
|
||||
allDevicesIds.add(element.uuid!);
|
||||
});
|
||||
CustomSnackBar.displaySnackBar('Save Successfully');
|
||||
|
||||
emit(SaveSelectionSuccessState());
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -376,5 +443,27 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool _validateInputs() {
|
||||
final nameError = fullNameValidator(nameController.text);
|
||||
if (nameError != null) {
|
||||
CustomSnackBar.displaySnackBar(nameError);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
String? fullNameValidator(String? value) {
|
||||
if (value == null) return 'name is required';
|
||||
final withoutExtraSpaces = value.replaceAll(RegExp(r"\s+"), ' ').trim();
|
||||
if (withoutExtraSpaces.length < 2 || withoutExtraSpaces.length > 30) {
|
||||
return 'Full name must be between 2 and 30 characters long';
|
||||
}
|
||||
// Test if it contains anything but alphanumeric spaces and single quote
|
||||
if (RegExp(r"/[^ a-zA-Z0-9-\']/").hasMatch(withoutExtraSpaces)) {
|
||||
return 'Only alphanumeric characters, space, dash and single quote are allowed';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/group_devices_model.dart';
|
||||
|
||||
@@ -23,12 +24,19 @@ class FourSceneSwitch extends FourSceneEvent {
|
||||
}
|
||||
|
||||
class FourSceneUpdated extends FourSceneEvent {}
|
||||
|
||||
class FourSceneInitialInfo extends FourSceneEvent {}
|
||||
|
||||
class FourSceneInitial extends FourSceneEvent {
|
||||
const FourSceneInitial();
|
||||
}
|
||||
|
||||
class SaveNameEvent extends FourSceneEvent {
|
||||
final String? deviceName;
|
||||
|
||||
const SaveNameEvent({this.deviceName});
|
||||
}
|
||||
|
||||
class ReportLogsInitial extends FourSceneEvent {
|
||||
const ReportLogsInitial();
|
||||
}
|
||||
@@ -173,19 +181,25 @@ class RemoveDeviceFromGroup extends FourSceneEvent {
|
||||
RemoveDeviceFromGroup(this.device, this.icon);
|
||||
}
|
||||
|
||||
|
||||
class AssignRoomEvent extends FourSceneEvent {
|
||||
final String roomId;
|
||||
final SpaceModel unit;
|
||||
final BuildContext context;
|
||||
|
||||
const AssignRoomEvent({
|
||||
required this.roomId,
|
||||
required this.unit,
|
||||
required this.context,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object> get props => [
|
||||
roomId,
|
||||
unit,
|
||||
context,
|
||||
];
|
||||
}
|
||||
|
||||
class FetchDeviceScene extends FourSceneEvent {
|
||||
const FetchDeviceScene();
|
||||
}
|
||||
|
||||
@@ -97,6 +97,7 @@ class SceneLoaded extends FourSceneState {
|
||||
class SelectedSceneState extends FourSceneState {}
|
||||
|
||||
class SearchResultsState extends FourSceneState {}
|
||||
class SaveState extends FourSceneState {}
|
||||
|
||||
class SaveSelectionSuccessState extends FourSceneState {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user