mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-08-26 07:29:40 +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) {
|
||||
@ -377,4 +444,26 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
}
|
||||
}
|
||||
|
||||
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 {}
|
||||
|
||||
|
@ -25,6 +25,11 @@ class FourSceneProfilePage extends StatelessWidget {
|
||||
|
||||
return DefaultScaffold(
|
||||
title: 'Device Settings',
|
||||
leading: IconButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop(true);
|
||||
},
|
||||
icon: const Icon(Icons.arrow_back_ios)),
|
||||
child: BlocProvider(
|
||||
create: (context) => FourSceneBloc(fourSceneId: device?.uuid ?? '')
|
||||
..add(const FourSceneInitial())
|
||||
@ -93,7 +98,7 @@ class FourSceneProfilePage extends StatelessWidget {
|
||||
controller: _bloc.nameController,
|
||||
enabled: _bloc.editName,
|
||||
onEditingComplete: () {
|
||||
// sensor.add(SaveNameEvent(context: context));
|
||||
_bloc.add(const SaveNameEvent());
|
||||
},
|
||||
decoration: const InputDecoration(
|
||||
hintText: "Your Name",
|
||||
@ -131,19 +136,22 @@ class FourSceneProfilePage extends StatelessWidget {
|
||||
DefaultContainer(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
Navigator.of(context).push(
|
||||
onTap: () async {
|
||||
bool val = await Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => LocationFourScenePage(
|
||||
space: spaces!.first,
|
||||
deviceId: device?.uuid ?? '',
|
||||
)),
|
||||
);
|
||||
if (val == true) {
|
||||
_bloc.add(FourSceneInitialInfo());
|
||||
}
|
||||
},
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
SizedBox(
|
||||
const SizedBox(
|
||||
child: Text('Location'),
|
||||
),
|
||||
Row(
|
||||
@ -155,7 +163,7 @@ class FourSceneProfilePage extends StatelessWidget {
|
||||
fontColor: ColorsManager.textGray,
|
||||
),
|
||||
),
|
||||
Icon(
|
||||
const Icon(
|
||||
Icons.arrow_forward_ios,
|
||||
size: 15,
|
||||
color: ColorsManager.textGray,
|
||||
|
@ -69,14 +69,17 @@ class FourSceneSettings extends StatelessWidget {
|
||||
vertical: 10,
|
||||
),
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
Navigator.of(context).push(
|
||||
onTap: () async {
|
||||
bool val = await Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => FourSceneProfilePage(
|
||||
device: device,
|
||||
),
|
||||
),
|
||||
);
|
||||
if (val == true) {
|
||||
_bloc.add(FourSceneInitialInfo());
|
||||
}
|
||||
},
|
||||
child: Stack(
|
||||
children: [
|
||||
@ -161,7 +164,9 @@ class FourSceneSettings extends StatelessWidget {
|
||||
onTap: () {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => FourSceneInfoPage( device: device!,)),
|
||||
builder: (context) => FourSceneInfoPage(
|
||||
device: device!,
|
||||
)),
|
||||
);
|
||||
},
|
||||
text: 'Device Information',
|
||||
|
@ -41,6 +41,12 @@ class LocationFourScenePage extends StatelessWidget {
|
||||
scene_4: '',
|
||||
scene_id_group_id: '',
|
||||
switch_backlight: '');
|
||||
if (state is SaveSelectionSuccessState) {
|
||||
new Future.delayed(const Duration(microseconds: 500), () {
|
||||
_bloc.add(FourSceneInitialInfo());
|
||||
Navigator.of(context).pop(true);
|
||||
});
|
||||
}
|
||||
return state is FourSceneLoadingState
|
||||
? const Center(
|
||||
child: DefaultContainer(
|
||||
@ -60,6 +66,7 @@ class LocationFourScenePage extends StatelessWidget {
|
||||
? () {
|
||||
context.read<FourSceneBloc>().add(
|
||||
AssignRoomEvent(
|
||||
context: context,
|
||||
roomId: roomIdSelected,
|
||||
unit: space!));
|
||||
}
|
||||
@ -77,6 +84,7 @@ class LocationFourScenePage extends StatelessWidget {
|
||||
),
|
||||
const SizedBox(width: 20),
|
||||
],
|
||||
|
||||
child: RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
// sensor.add(const SosInitial());
|
||||
|
@ -31,6 +31,22 @@ class DevicesAPI {
|
||||
}
|
||||
}
|
||||
|
||||
static Future<Map<String, dynamic>> putDeviceName(
|
||||
{required String deviceId, required String deviceName}) async {
|
||||
try {
|
||||
final response = await _httpService.put(
|
||||
path: ApiEndpoints.deviceByUuid.replaceAll('{deviceUuid}', deviceId),
|
||||
body: {"deviceName": deviceName},
|
||||
expectedResponseModel: (json) {
|
||||
return json;
|
||||
},
|
||||
);
|
||||
return response;
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
static Future<Map<String, dynamic>> controlDevice(
|
||||
DeviceControlModel controlModel, String deviceId) async {
|
||||
try {
|
||||
|
Reference in New Issue
Block a user