This commit is contained in:
mohammad
2024-09-30 17:30:16 +03:00
parent fed39b7198
commit b8b6ec67c7
7 changed files with 74 additions and 48 deletions

View File

@ -1,6 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:firebase_database/firebase_database.dart'; import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.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'; import 'package:syncrow_app/features/devices/bloc/one_touch_bloc/one_touch_state.dart';
@ -21,9 +22,9 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
OneTouchModel deviceStatus = OneTouchModel( OneTouchModel deviceStatus = OneTouchModel(
firstSwitch: false, firstSwitch: false,
firstCountDown: 0, firstCountDown: 0,
light_mode: '', light_mode: lightStatus.off,
relay: status.off, relay: status.off,
relay_status_1: ''); relay_status_1: status.off);
Timer? _timer; Timer? _timer;
bool oneTouchGroup = false; bool oneTouchGroup = false;
@ -582,19 +583,22 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
void _changeStatus( void _changeStatus(
ChangeStatusEvent event, Emitter<OneTouchState> emit) async { ChangeStatusEvent event, Emitter<OneTouchState> emit) async {
// emit(LoadingNewState(oneTouchModel: deviceStatus)); emit(LoadingInitialState());
emit(UpdateState(oneTouchModel: deviceStatus)); emit(UpdateState(oneTouchModel: deviceStatus));
emit(LoadingNewSate(oneTouchModel: deviceStatus));
try { try {
emit(UpdateState(oneTouchModel: deviceStatus)); await _handleDeviceControl(event.deviceId, event.context);
await _handleDeviceControl(event.deviceId);
add(InitialEvent(groupScreen: oneTouchGroup)); add(InitialEvent(groupScreen: oneTouchGroup));
emit(UpdateState(oneTouchModel: deviceStatus));
} catch (error) { } catch (error) {
print('Error controlling device: $error'); print('Error controlling device: $error');
add(InitialEvent(groupScreen: oneTouchGroup)); add(InitialEvent(groupScreen: oneTouchGroup));
} }
} }
Future<void> _handleDeviceControl(String deviceId) async { Future<void> _handleDeviceControl(
String deviceId, BuildContext context) async {
final Map<String, Map<String, String>> controlMap = { final Map<String, Map<String, String>> controlMap = {
"relay_status": { "relay_status": {
'Power On': 'power_on', 'Power On': 'power_on',
@ -619,6 +623,10 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
DeviceControlModel( DeviceControlModel(
deviceId: oneTouchId, code: optionSelected, value: selectedControl), deviceId: oneTouchId, code: optionSelected, value: selectedControl),
oneTouchId, oneTouchId,
).then(
(value) {
Navigator.pop(context);
},
); );
} else { } else {
print('Invalid statusSelected or optionSelected'); print('Invalid statusSelected or optionSelected');

View File

@ -1,4 +1,5 @@
import 'package:equatable/equatable.dart'; import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
abstract class OneTouchEvent extends Equatable { abstract class OneTouchEvent extends Equatable {
const OneTouchEvent(); const OneTouchEvent();
@ -142,5 +143,6 @@ class ChangeFirstWizardSwitchStatusEvent extends OneTouchEvent {
class ChangeStatusEvent extends OneTouchEvent { class ChangeStatusEvent extends OneTouchEvent {
final String deviceId; final String deviceId;
const ChangeStatusEvent({this.deviceId = ''}); final BuildContext context;
const ChangeStatusEvent({this.deviceId = '',required this.context});
} }

View File

@ -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<StatusModel> 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/features/devices/model/status_model.dart';
import 'package:syncrow_app/utils/resource_manager/constants.dart'; import 'package:syncrow_app/utils/resource_manager/constants.dart';
@ -34,8 +7,8 @@ class OneTouchModel {
bool firstSwitch; bool firstSwitch;
int firstCountDown; int firstCountDown;
status relay; status relay;
String light_mode; lightStatus light_mode;
String relay_status_1; status relay_status_1;
OneTouchModel( OneTouchModel(
{required this.firstSwitch, {required this.firstSwitch,
@ -67,8 +40,10 @@ class OneTouchModel {
return OneTouchModel( return OneTouchModel(
firstSwitch: _switch, firstSwitch: _switch,
firstCountDown: _count, firstCountDown: _count,
light_mode: _light_mode, light_mode: lightStatusExtension.fromString(_light_mode) ,
relay: StatusExtension.fromString(_relay ) , relay: StatusExtension.fromString(_relay ) ,
relay_status_1: relay_status_1); relay_status_1: StatusExtension.fromString(relay_status_1 )
);
} }
} }

View File

@ -78,9 +78,9 @@ class OneTouchScreen extends StatelessWidget {
OneTouchModel oneTouchModel = OneTouchModel( OneTouchModel oneTouchModel = OneTouchModel(
firstSwitch: false, firstSwitch: false,
firstCountDown: 0, firstCountDown: 0,
light_mode: '', light_mode: lightStatus.off,
relay: status.off, relay: status.off,
relay_status_1: ''); relay_status_1: status.off);
List<GroupOneTouchModel> groupOneTouchModel = []; List<GroupOneTouchModel> groupOneTouchModel = [];
bool allSwitchesOn = false; bool allSwitchesOn = false;

View File

@ -66,7 +66,8 @@ class OneTouchSetting extends StatelessWidget {
}, },
confirmTab: () { confirmTab: () {
oneTouchBloc.add(ChangeStatusEvent( oneTouchBloc.add(ChangeStatusEvent(
deviceId: device!.uuid!)); deviceId: device!.uuid!,
context: context));
}, },
title: 'Restart Status', title: 'Restart Status',
label1: 'Power Off', label1: 'Power Off',
@ -137,7 +138,9 @@ class OneTouchSetting extends StatelessWidget {
}, },
confirmTab: () { confirmTab: () {
oneTouchBloc.add( oneTouchBloc.add(
const ChangeStatusEvent()); ChangeStatusEvent(
deviceId: device!.uuid!,
context: context));
}, },
title: 'Indicator Status', title: 'Indicator Status',
label1: 'Off', label1: 'Off',
@ -173,8 +176,8 @@ class OneTouchSetting extends StatelessWidget {
children: [ children: [
BodyMedium( BodyMedium(
fontSize: 13, fontSize: 13,
text: oneTouchBloc text: oneTouchBloc.deviceStatus
.deviceStatus.light_mode, .light_mode.value,
fontColor: ColorsManager.textGray, fontColor: ColorsManager.textGray,
), ),
const Icon( const Icon(
@ -204,7 +207,12 @@ class OneTouchSetting extends StatelessWidget {
cancelTab: () { cancelTab: () {
Navigator.of(context).pop(); Navigator.of(context).pop();
}, },
confirmTab: () {}, confirmTab: () {
oneTouchBloc.add(
ChangeStatusEvent(
deviceId: device!.uuid!,
context: context));
},
title: 'Restart Status 1', title: 'Restart Status 1',
label1: 'Power Off', label1: 'Power Off',
label2: 'Power On', label2: 'Power On',
@ -245,7 +253,7 @@ class OneTouchSetting extends StatelessWidget {
BodyMedium( BodyMedium(
fontSize: 13, fontSize: 13,
text: oneTouchBloc.deviceStatus text: oneTouchBloc.deviceStatus
.relay_status_1, .relay_status_1.value,
fontColor: fontColor:
ColorsManager.textGray, ColorsManager.textGray,
), ),

View File

@ -33,6 +33,7 @@ class DevicesAPI {
static Future<Map<String, dynamic>> controlDevice( static Future<Map<String, dynamic>> controlDevice(
DeviceControlModel controlModel, String deviceId) async { DeviceControlModel controlModel, String deviceId) async {
try { try {
print(controlModel.toJson());
final response = await _httpService.post( final response = await _httpService.post(
path: ApiEndpoints.controlDevice.replaceAll('{deviceUuid}', deviceId), path: ApiEndpoints.controlDevice.replaceAll('{deviceUuid}', deviceId),
body: controlModel.toJson(), body: controlModel.toJson(),

View File

@ -628,9 +628,9 @@ enum status {
extension StatusExtension on status { extension StatusExtension on status {
String get value { String get value {
switch (this) { switch (this) {
case status.on:
return "Power Off";
case status.off: case status.off:
return "Power Off";
case status.on:
return "Power On"; return "Power On";
case status.restart: case status.restart:
return "Restart Memory"; 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");
}
}
}