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 '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<OneTouchEvent, OneTouchState> {
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<OneTouchEvent, OneTouchState> {
void _changeStatus(
ChangeStatusEvent event, Emitter<OneTouchState> 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<void> _handleDeviceControl(String deviceId) async {
Future<void> _handleDeviceControl(
String deviceId, BuildContext context) async {
final Map<String, Map<String, String>> controlMap = {
"relay_status": {
'Power On': 'power_on',
@ -619,6 +623,10 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
DeviceControlModel(
deviceId: oneTouchId, code: optionSelected, value: selectedControl),
oneTouchId,
).then(
(value) {
Navigator.pop(context);
},
);
} else {
print('Invalid statusSelected or optionSelected');

View File

@ -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});
}

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/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 )
);
}
}

View File

@ -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> groupOneTouchModel = [];
bool allSwitchesOn = false;

View File

@ -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,
),

View File

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

View File

@ -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");
}
}
}