Files
syncrow-app/lib/features/devices/bloc/ac/ac_cubit.dart
2024-03-02 21:57:11 +03:00

117 lines
2.9 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart';
import 'package:syncrow_app/features/devices/model/ac_model.dart';
part 'ac_state.dart';
class AcCubit extends Cubit<AcState> {
AcCubit() : super(AcInitial()) {
updateACsStatus();
}
static AcCubit get(context) => BlocProvider.of(context);
void selectAC(ACModel model) {
model.isSelected = !model.isSelected;
emit(ACSelected());
}
ACModel? getSelectedAC() {
for (var ac in DevicesCubit.categories[0].devices) {
if (ac is ACModel && ac.isSelected) {
return ac;
}
}
return null;
}
void turnACOn(ACModel model) {
if (model.status == false) {
model.status = true;
updateACsStatus();
emit(ACTurnedOn());
}
}
void turnACOff(ACModel model) {
if (model.status == true) {
model.status = false;
updateACsStatus();
emit(ACTurnedOff());
}
}
void updateACsStatus() {
bool tempStatus = DevicesCubit.categories[0].devices[0].status ?? false;
for (var AC in DevicesCubit.categories[0].devices) {
//check if there any AC have a different status than the initial ==> turn off the universal switch
if (AC.status != tempStatus) {
DevicesCubit.categories[0].devicesStatus = false;
emit(ACsStatusChanged());
return;
}
DevicesCubit.categories[0].devicesStatus = tempStatus;
emit(ACsStatusChanged());
}
}
void turnAllACsOff() {
for (var ac in DevicesCubit.categories[0].devices) {
ac.status = false;
}
updateACsStatus();
emit(SwitchACsOff());
}
void turnAllACsOn() {
for (var ac in DevicesCubit.categories[0].devices) {
ac.status = true;
}
updateACsStatus();
emit(SwitchACsOn());
}
void setTempToAll(double temperature) {
for (var element in DevicesCubit.categories[0].devices) {
if (element is ACModel) {
element.temperature = temperature;
}
}
emit(ACsTempChanged(temperature));
}
void increaseACTemp(int index) {
var device = DevicesCubit.categories[0].devices[index];
if (device is ACModel) {
device.temperature += 0.5;
emit(ACsTempChanged(device.temperature));
}
}
void decreaseACTemp(int index) {
var device = DevicesCubit.categories[0].devices[index];
if (device is ACModel) {
device.temperature -= 0.5;
emit(ACsTempChanged(device.temperature));
}
}
void setACTemp(ACModel model, double temp) {
model.temperature = temp;
emit(ACsTempChanged(temp));
}
double getTemp(int index) {
var device = DevicesCubit.categories[0].devices[index];
if (device is ACModel) {
return device.temperature;
}
return 0.0; // or any default value you prefer
}
static double averageTemp = 0;
/// implement the fan speed and temp mode change
}