import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:meta/meta.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 { AcCubit() : super(AcInitial()) { averageTempForAll(); updateACsStatus(); } static AcCubit get(context) => BlocProvider.of(context); void turnACOn(ACModel model) { if (!model.status) { model.status = true; updateACsStatus(); emit(ACTurnedOn()); } } void turnACOff(ACModel model) { if (model.status) { model.status = false; updateACsStatus(); emit(ACTurnedOff()); } } void updateACsStatus() { bool tempStatus = DevicesCubit.categories[0].devices[0].status; 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; } emit(SwitchACsOff()); } void turnAllACsOn() { for (var ac in DevicesCubit.categories[0].devices) { ac.status = true; } emit(SwitchACsOn()); } void setTempToAll(double temperature) { for (var ac in DevicesCubit.categories[0].devices) { ac.temperature = temperature; } averageTempForAll(); emit(ACsTempChanged(temperature)); } // void setACTemp(int index, double temperature) { // DevicesCubit.categories[0].devices[index].temperature = temperature; // averageTempForAll(); // emit(ACsTempChanged(temperature)); // } void increaseACTemp(int index) { DevicesCubit.categories[0].devices[index].temperature += .5; averageTempForAll(); emit(ACsTempChanged(DevicesCubit.categories[0].devices[index].temperature)); } void decreaseACTemp(int index) { DevicesCubit.categories[0].devices[index].temperature -= .5; averageTempForAll(); emit(ACsTempChanged(DevicesCubit.categories[0].devices[index].temperature)); } double getTemp(int index) { return DevicesCubit.categories[0].devices[index].temperature; } static double averageTemp = 0; void averageTempForAll() { double tempSum = 0; for (var ac in DevicesCubit.categories[0].devices) { tempSum += ac.temperature; } averageTemp = tempSum / DevicesCubit.categories[0].devices.length; averageTemp = (averageTemp * 2).round() / 2; emit(ACsAverageTemp()); } /// implement the fan speed and temp mode change }