mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-25 22:07:55 +00:00
Stabilized UI elements across multiple devices
Synchronized ACs Status functionality
This commit is contained in:
@@ -1,21 +1,46 @@
|
||||
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<AcState> {
|
||||
AcCubit() : super(AcInitial());
|
||||
AcCubit() : super(AcInitial()) {
|
||||
averageTempForAll();
|
||||
updateACsStatus();
|
||||
}
|
||||
|
||||
static AcCubit get(context) => BlocProvider.of(context);
|
||||
|
||||
bool areAllACsOff() {
|
||||
for (var ac in DevicesCubit.categories[0].devices) {
|
||||
if (ac.status) {
|
||||
return false;
|
||||
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());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void turnAllACsOff() {
|
||||
@@ -36,21 +61,45 @@ class AcCubit extends Cubit<AcState> {
|
||||
for (var ac in DevicesCubit.categories[0].devices) {
|
||||
ac.temperature = temperature;
|
||||
}
|
||||
emit(ACsTempChanged());
|
||||
averageTempForAll();
|
||||
emit(ACsTempChanged(temperature));
|
||||
}
|
||||
|
||||
double averageTempForAll() {
|
||||
double allTemp = 0;
|
||||
for (var ac in DevicesCubit.categories[0].devices) {
|
||||
allTemp += ac.temperature;
|
||||
}
|
||||
emit(ACsTempChanged());
|
||||
// void setACTemp(int index, double temperature) {
|
||||
// DevicesCubit.categories[0].devices[index].temperature = temperature;
|
||||
// averageTempForAll();
|
||||
// emit(ACsTempChanged(temperature));
|
||||
// }
|
||||
|
||||
double averageTemp = allTemp / DevicesCubit.categories[0].devices.length;
|
||||
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;
|
||||
|
||||
return averageTemp;
|
||||
emit(ACsAverageTemp());
|
||||
}
|
||||
|
||||
/// implement the fan speed and temp mode change
|
||||
|
||||
@@ -5,12 +5,24 @@ abstract class AcState {}
|
||||
|
||||
class AcInitial extends AcState {}
|
||||
|
||||
class ACsStatusChanged extends AcState {}
|
||||
|
||||
class SwitchACsOff extends AcState {}
|
||||
|
||||
class SwitchACsOn extends AcState {}
|
||||
|
||||
class ACsTempChanged extends AcState {}
|
||||
class ACsTempChanged extends AcState {
|
||||
final double temperature;
|
||||
|
||||
ACsTempChanged(this.temperature);
|
||||
}
|
||||
|
||||
class ACsAverageTemp extends AcState {}
|
||||
|
||||
class ACsFanSpeedChanged extends AcState {}
|
||||
|
||||
class ACsTempModeChanged extends AcState {}
|
||||
|
||||
class ACTurnedOn extends AcState {}
|
||||
|
||||
class ACTurnedOff extends AcState {}
|
||||
|
||||
@@ -18,18 +18,13 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
// getCategories();
|
||||
}
|
||||
|
||||
/// separate the cubit into different cubits based on devices type
|
||||
static bool ACSwitchValue = false;
|
||||
static bool lightsSwitchValue = false;
|
||||
static bool doorSwitchValue = false;
|
||||
static bool curtainSwitchValue = false;
|
||||
static bool screensSwitchValue = false;
|
||||
static bool gatewaySwitchValue = false;
|
||||
|
||||
void changeCategorySwitchValue(DevicesCategoryModel category) {
|
||||
category.switchValue = !category.switchValue;
|
||||
category.devicesStatus = !category.devicesStatus;
|
||||
for (var device in category.devices) {
|
||||
device.status = category.devicesStatus;
|
||||
}
|
||||
emit(CategorySwitchChanged());
|
||||
print('${category.name} switch value: ${category.switchValue} ');
|
||||
print('${category.name} switch value: ${category.devicesStatus} ');
|
||||
}
|
||||
|
||||
static DevicesCubit get(context) => BlocProvider.of(context);
|
||||
@@ -56,7 +51,6 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
],
|
||||
icon: Assets.iconsAC,
|
||||
name: 'ACs',
|
||||
switchValue: ACSwitchValue,
|
||||
type: DeviceType.AC,
|
||||
page: const ACView(),
|
||||
),
|
||||
@@ -64,7 +58,6 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
devices: [],
|
||||
icon: Assets.iconsLight,
|
||||
name: 'Lights',
|
||||
switchValue: lightsSwitchValue,
|
||||
type: DeviceType.Lights,
|
||||
page: const LightsView(),
|
||||
),
|
||||
@@ -72,7 +65,6 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
devices: [],
|
||||
icon: Assets.iconsDoorLock,
|
||||
name: 'Doors',
|
||||
switchValue: doorSwitchValue,
|
||||
type: DeviceType.Door,
|
||||
page: const DoorView(),
|
||||
),
|
||||
@@ -80,7 +72,6 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
devices: [],
|
||||
icon: Assets.iconsCurtain,
|
||||
name: 'Curtains',
|
||||
switchValue: curtainSwitchValue,
|
||||
type: DeviceType.Curtain,
|
||||
page: const CurtainView(),
|
||||
),
|
||||
@@ -88,7 +79,6 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
devices: [],
|
||||
icon: Assets.iconsScreen,
|
||||
name: 'Screens',
|
||||
switchValue: screensSwitchValue,
|
||||
type: DeviceType.Screens,
|
||||
page: const ScreensView(),
|
||||
),
|
||||
@@ -96,13 +86,12 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
devices: [],
|
||||
icon: Assets.iconsGateway,
|
||||
name: 'Gateway',
|
||||
switchValue: gatewaySwitchValue,
|
||||
type: DeviceType.Gateway,
|
||||
page: const GateWayView(),
|
||||
),
|
||||
];
|
||||
|
||||
Widget? get chosenCategory {
|
||||
Widget? get chosenCategoryView {
|
||||
for (var category in categories) {
|
||||
if (category.isSelected) {
|
||||
return category.page;
|
||||
@@ -111,7 +100,17 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
return null;
|
||||
}
|
||||
|
||||
void updateCategory(int index) {
|
||||
void areAllDevicesOff(DevicesCategoryModel category) {
|
||||
for (var device in category.devices) {
|
||||
if (device.status) {
|
||||
category.devicesStatus = false;
|
||||
emit(CategorySwitchChanged());
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void selectCategory(int index) {
|
||||
for (var i = 0; i < categories.length; i++) {
|
||||
if (i == index) {
|
||||
categories[i].isSelected = true;
|
||||
|
||||
Reference in New Issue
Block a user