From b8b6ec67c7edb7b191adcdeb4729b42c3964bed4 Mon Sep 17 00:00:00 2001 From: mohammad Date: Mon, 30 Sep 2024 17:30:16 +0300 Subject: [PATCH] setting --- .../bloc/one_touch_bloc/one_touch_bloc.dart | 20 +++++++--- .../bloc/one_touch_bloc/one_touch_event.dart | 4 +- .../devices/model/one_touch_model.dart | 37 +++---------------- .../widgets/one_touch/one_touch_screen.dart | 4 +- .../widgets/one_touch/one_touch_setting.dart | 20 +++++++--- lib/services/api/devices_api.dart | 1 + lib/utils/resource_manager/constants.dart | 36 +++++++++++++++++- 7 files changed, 74 insertions(+), 48 deletions(-) diff --git a/lib/features/devices/bloc/one_touch_bloc/one_touch_bloc.dart b/lib/features/devices/bloc/one_touch_bloc/one_touch_bloc.dart index 4cc0efd..9be349b 100644 --- a/lib/features/devices/bloc/one_touch_bloc/one_touch_bloc.dart +++ b/lib/features/devices/bloc/one_touch_bloc/one_touch_bloc.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:dio/dio.dart'; import 'package:firebase_database/firebase_database.dart'; +import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; import 'package:syncrow_app/features/devices/bloc/one_touch_bloc/one_touch_state.dart'; @@ -21,9 +22,9 @@ class OneTouchBloc extends Bloc { OneTouchModel deviceStatus = OneTouchModel( firstSwitch: false, firstCountDown: 0, - light_mode: '', + light_mode: lightStatus.off, relay: status.off, - relay_status_1: ''); + relay_status_1: status.off); Timer? _timer; bool oneTouchGroup = false; @@ -582,19 +583,22 @@ class OneTouchBloc extends Bloc { void _changeStatus( ChangeStatusEvent event, Emitter emit) async { - // emit(LoadingNewState(oneTouchModel: deviceStatus)); + emit(LoadingInitialState()); emit(UpdateState(oneTouchModel: deviceStatus)); + emit(LoadingNewSate(oneTouchModel: deviceStatus)); + try { - emit(UpdateState(oneTouchModel: deviceStatus)); - await _handleDeviceControl(event.deviceId); + await _handleDeviceControl(event.deviceId, event.context); add(InitialEvent(groupScreen: oneTouchGroup)); + emit(UpdateState(oneTouchModel: deviceStatus)); } catch (error) { print('Error controlling device: $error'); add(InitialEvent(groupScreen: oneTouchGroup)); } } - Future _handleDeviceControl(String deviceId) async { + Future _handleDeviceControl( + String deviceId, BuildContext context) async { final Map> controlMap = { "relay_status": { 'Power On': 'power_on', @@ -619,6 +623,10 @@ class OneTouchBloc extends Bloc { DeviceControlModel( deviceId: oneTouchId, code: optionSelected, value: selectedControl), oneTouchId, + ).then( + (value) { + Navigator.pop(context); + }, ); } else { print('Invalid statusSelected or optionSelected'); diff --git a/lib/features/devices/bloc/one_touch_bloc/one_touch_event.dart b/lib/features/devices/bloc/one_touch_bloc/one_touch_event.dart index c93b2d2..68d1fa5 100644 --- a/lib/features/devices/bloc/one_touch_bloc/one_touch_event.dart +++ b/lib/features/devices/bloc/one_touch_bloc/one_touch_event.dart @@ -1,4 +1,5 @@ import 'package:equatable/equatable.dart'; +import 'package:flutter/material.dart'; abstract class OneTouchEvent extends Equatable { const OneTouchEvent(); @@ -142,5 +143,6 @@ class ChangeFirstWizardSwitchStatusEvent extends OneTouchEvent { class ChangeStatusEvent extends OneTouchEvent { final String deviceId; - const ChangeStatusEvent({this.deviceId = ''}); + final BuildContext context; + const ChangeStatusEvent({this.deviceId = '',required this.context}); } diff --git a/lib/features/devices/model/one_touch_model.dart b/lib/features/devices/model/one_touch_model.dart index 350385c..da15264 100644 --- a/lib/features/devices/model/one_touch_model.dart +++ b/lib/features/devices/model/one_touch_model.dart @@ -1,31 +1,4 @@ -// import 'package:syncrow_app/features/devices/model/status_model.dart'; -// class OneTouchModel { -// bool firstSwitch; -// int firstCountDown; - -// OneTouchModel({ -// required this.firstSwitch, -// required this.firstCountDown, -// }); - -// factory OneTouchModel.fromJson(List jsonList) { -// late bool _switch; -// late int _count; - -// for (int i = 0; i < jsonList.length; i++) { -// if (jsonList[i].code == 'switch_1') { -// _switch = jsonList[i].value ?? false; -// } else if (jsonList[i].code == 'countdown_1') { -// _count = jsonList[i].value ?? 0; -// } -// } -// return OneTouchModel( -// firstSwitch: _switch, -// firstCountDown: _count, -// ); -// } -// } import 'package:syncrow_app/features/devices/model/status_model.dart'; import 'package:syncrow_app/utils/resource_manager/constants.dart'; @@ -34,8 +7,8 @@ class OneTouchModel { bool firstSwitch; int firstCountDown; status relay; - String light_mode; - String relay_status_1; + lightStatus light_mode; + status relay_status_1; OneTouchModel( {required this.firstSwitch, @@ -67,8 +40,10 @@ class OneTouchModel { return OneTouchModel( firstSwitch: _switch, firstCountDown: _count, - light_mode: _light_mode, + light_mode: lightStatusExtension.fromString(_light_mode) , relay: StatusExtension.fromString(_relay ) , - relay_status_1: relay_status_1); + relay_status_1: StatusExtension.fromString(relay_status_1 ) + + ); } } diff --git a/lib/features/devices/view/widgets/one_touch/one_touch_screen.dart b/lib/features/devices/view/widgets/one_touch/one_touch_screen.dart index b594f83..b8399d7 100644 --- a/lib/features/devices/view/widgets/one_touch/one_touch_screen.dart +++ b/lib/features/devices/view/widgets/one_touch/one_touch_screen.dart @@ -78,9 +78,9 @@ class OneTouchScreen extends StatelessWidget { OneTouchModel oneTouchModel = OneTouchModel( firstSwitch: false, firstCountDown: 0, - light_mode: '', + light_mode: lightStatus.off, relay: status.off, - relay_status_1: ''); + relay_status_1: status.off); List groupOneTouchModel = []; bool allSwitchesOn = false; diff --git a/lib/features/devices/view/widgets/one_touch/one_touch_setting.dart b/lib/features/devices/view/widgets/one_touch/one_touch_setting.dart index 36ff477..0eb09ba 100644 --- a/lib/features/devices/view/widgets/one_touch/one_touch_setting.dart +++ b/lib/features/devices/view/widgets/one_touch/one_touch_setting.dart @@ -66,7 +66,8 @@ class OneTouchSetting extends StatelessWidget { }, confirmTab: () { oneTouchBloc.add(ChangeStatusEvent( - deviceId: device!.uuid!)); + deviceId: device!.uuid!, + context: context)); }, title: 'Restart Status', label1: 'Power Off', @@ -137,7 +138,9 @@ class OneTouchSetting extends StatelessWidget { }, confirmTab: () { oneTouchBloc.add( - const ChangeStatusEvent()); + ChangeStatusEvent( + deviceId: device!.uuid!, + context: context)); }, title: 'Indicator Status', label1: 'Off', @@ -173,8 +176,8 @@ class OneTouchSetting extends StatelessWidget { children: [ BodyMedium( fontSize: 13, - text: oneTouchBloc - .deviceStatus.light_mode, + text: oneTouchBloc.deviceStatus + .light_mode.value, fontColor: ColorsManager.textGray, ), const Icon( @@ -204,7 +207,12 @@ class OneTouchSetting extends StatelessWidget { cancelTab: () { Navigator.of(context).pop(); }, - confirmTab: () {}, + confirmTab: () { + oneTouchBloc.add( + ChangeStatusEvent( + deviceId: device!.uuid!, + context: context)); + }, title: 'Restart Status 1', label1: 'Power Off', label2: 'Power On', @@ -245,7 +253,7 @@ class OneTouchSetting extends StatelessWidget { BodyMedium( fontSize: 13, text: oneTouchBloc.deviceStatus - .relay_status_1, + .relay_status_1.value, fontColor: ColorsManager.textGray, ), diff --git a/lib/services/api/devices_api.dart b/lib/services/api/devices_api.dart index a92a027..c7305db 100644 --- a/lib/services/api/devices_api.dart +++ b/lib/services/api/devices_api.dart @@ -33,6 +33,7 @@ class DevicesAPI { static Future> controlDevice( DeviceControlModel controlModel, String deviceId) async { try { + print(controlModel.toJson()); final response = await _httpService.post( path: ApiEndpoints.controlDevice.replaceAll('{deviceUuid}', deviceId), body: controlModel.toJson(), diff --git a/lib/utils/resource_manager/constants.dart b/lib/utils/resource_manager/constants.dart index 86db344..cac66a9 100644 --- a/lib/utils/resource_manager/constants.dart +++ b/lib/utils/resource_manager/constants.dart @@ -628,9 +628,9 @@ enum status { extension StatusExtension on status { String get value { switch (this) { - case status.on: - return "Power Off"; case status.off: + return "Power Off"; + case status.on: return "Power On"; case status.restart: return "Restart Memory"; @@ -650,3 +650,35 @@ extension StatusExtension on status { } } } + +enum lightStatus { + off, + on_off, + switchPosition, +} + +extension lightStatusExtension on lightStatus { + String get value { + switch (this) { + case lightStatus.off: + return "Off"; + case lightStatus.on_off: + return "On/Off Status"; + case lightStatus.switchPosition: + return "Restart Memory"; + } + } + + static lightStatus fromString(String value) { + switch (value) { + case "none": + return lightStatus.off; + case "relay": + return lightStatus.on_off; + case "pos": + return lightStatus.switchPosition; + default: + throw ArgumentError("Invalid access type: $value"); + } + } +}