fixed Universal AC temperature control

This commit is contained in:
Mohammad Salameh
2024-03-03 16:17:00 +03:00
parent 4cfae85f9c
commit 24ccb243d4
7 changed files with 156 additions and 101 deletions

View File

@ -2,6 +2,7 @@ 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';
import 'package:syncrow_app/features/devices/model/device_model.dart';
part 'ac_state.dart';
@ -25,30 +26,15 @@ class AcCubit extends Cubit<AcState> {
}
void setTempToAll(double temperature) {
for (var element in DevicesCubit.categories[0].devices) {
if (element is ACModel) {
element.temperature = temperature;
for (DeviceModel ac in DevicesCubit.categories[0].devices) {
if (ac is ACModel) {
setACTemp(ac, temperature);
}
}
universalACTemp = 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));
@ -62,7 +48,5 @@ class AcCubit extends Cubit<AcState> {
return 0.0; // or any default value you prefer
}
static double averageTemp = 0;
/// implement the fan speed and temp mode change
static double universalACTemp = 20;
}

View File

@ -18,62 +18,8 @@ part 'devices_state.dart';
class DevicesCubit extends Cubit<DevicesState> {
DevicesCubit() : super(DevicesInitial());
void changeCategorySwitchValue(DevicesCategoryModel category) {
if (category.devicesStatus != null) {
category.devicesStatus = !category.devicesStatus!;
for (var device in category.devices) {
device.status = category.devicesStatus;
}
} else {
category.devicesStatus = true;
for (var device in category.devices) {
device.status = true;
}
}
emit(CategorySwitchChanged());
}
static DevicesCubit get(context) => BlocProvider.of(context);
void turnOnOffDevice(DeviceModel device) {
device.status = !device.status!;
DevicesCategoryModel category =
categories.firstWhere((category) => category.devices.contains(device));
updateDevicesStatus(category);
emit(DeviceSwitchChanged());
}
void updateDevicesStatus(DevicesCategoryModel category) {
bool? tempStatus = category.devices[0].status;
for (var ac in category.devices) {
//check if there any ac have a different status than the initial ==> turn off the universal switch
if (ac.status != tempStatus) {
category.devicesStatus = null;
emit(DeviceSwitchChanged());
return;
}
category.devicesStatus = tempStatus;
emit(DeviceSwitchChanged());
}
}
void turnAllDevicesOff(DevicesCategoryModel category) {
for (var device in category.devices) {
device.status = false;
}
updateDevicesStatus(category);
emit(CategorySwitchChanged());
}
void turnAllDevicesOn(DevicesCategoryModel category) {
for (var device in category.devices) {
device.status = true;
}
updateDevicesStatus(category);
emit(CategorySwitchChanged());
}
static List<DevicesCategoryModel> categories = [
DevicesCategoryModel(
devices: [
@ -84,6 +30,7 @@ class DevicesCubit extends Cubit<DevicesState> {
temperature: 20,
fanSpeed: 0,
tempMode: 0,
coolTo: 20,
type: '',
location: '',
image: '',
@ -96,6 +43,33 @@ class DevicesCubit extends Cubit<DevicesState> {
temperature: 20,
fanSpeed: 0,
tempMode: 0,
coolTo: 20,
type: '',
location: '',
image: '',
timer: null,
),
ACModel(
name: "Kitchen AC",
id: '2',
status: false,
temperature: 20,
fanSpeed: 0,
tempMode: 0,
coolTo: 20,
type: '',
location: '',
image: '',
timer: null,
),
ACModel(
name: "Bathroom AC",
id: '3',
status: false,
temperature: 20,
fanSpeed: 0,
tempMode: 0,
coolTo: 20,
type: '',
location: '',
image: '',
@ -133,6 +107,42 @@ class DevicesCubit extends Cubit<DevicesState> {
location: '',
image: '',
),
LightModel(
name: "Kitchen Light",
id: '2',
status: false,
color: 1,
brightness: 60,
lightingMode: 1,
timer: null,
type: '',
location: '',
image: '',
),
LightModel(
name: "Bathroom Light",
id: '3',
status: false,
color: 3,
brightness: 80,
lightingMode: 1,
timer: null,
type: '',
location: '',
image: '',
),
LightModel(
name: "Balcony Light",
id: '4',
status: false,
color: 4,
brightness: 100,
lightingMode: 1,
timer: null,
type: '',
location: '',
image: '',
),
],
icon: Assets.iconsLight,
name: 'Lights',
@ -178,6 +188,60 @@ class DevicesCubit extends Cubit<DevicesState> {
return null;
}
void changeCategorySwitchValue(DevicesCategoryModel category) {
if (category.devicesStatus != null) {
category.devicesStatus = !category.devicesStatus!;
for (var device in category.devices) {
device.status = category.devicesStatus;
}
} else {
category.devicesStatus = true;
for (var device in category.devices) {
device.status = true;
}
}
emit(CategorySwitchChanged());
}
void turnOnOffDevice(DeviceModel device) {
device.status = !device.status!;
DevicesCategoryModel category =
categories.firstWhere((category) => category.devices.contains(device));
updateDevicesStatus(category);
emit(DeviceSwitchChanged());
}
void updateDevicesStatus(DevicesCategoryModel category) {
bool? tempStatus = category.devices[0].status;
for (var ac in category.devices) {
//check if there any ac have a different status than the initial ==> turn off the universal switch
if (ac.status != tempStatus) {
category.devicesStatus = null;
emit(DeviceSwitchChanged());
return;
}
category.devicesStatus = tempStatus;
emit(DeviceSwitchChanged());
}
}
void turnAllDevicesOff(DevicesCategoryModel category) {
for (var device in category.devices) {
device.status = false;
}
updateDevicesStatus(category);
emit(CategorySwitchChanged());
}
void turnAllDevicesOn(DevicesCategoryModel category) {
for (var device in category.devices) {
device.status = true;
}
updateDevicesStatus(category);
emit(CategorySwitchChanged());
}
void areAllDevicesOff(DevicesCategoryModel category) {
for (var device in category.devices) {
if (device.status ?? false) {

View File

@ -7,10 +7,13 @@ class ACModel extends DeviceModel {
late int tempMode;
late double coolTo;
ACModel({
required this.temperature,
required this.fanSpeed,
required this.tempMode,
required this.coolTo,
required super.id,
required super.name,
required super.type,
@ -25,6 +28,7 @@ class ACModel extends DeviceModel {
'temperature': temperature,
'fanSpeed': fanSpeed,
'tempMode': tempMode,
'coolTo': coolTo,
'id': id,
'name': name,
'status': status,
@ -46,6 +50,7 @@ class ACModel extends DeviceModel {
location: json['location'],
image: json['image'],
timer: json['timer'],
coolTo: json['coolTo'],
);
}
}

View File

@ -23,6 +23,7 @@ class AcInterfaceTempUnit extends StatelessWidget {
@override
Widget build(BuildContext context) {
//TODO: use the coolTo value from the model
double coolTo = acModel.temperature;
return BlocBuilder<AcCubit, AcState>(
builder: (context, state) {

View File

@ -1,21 +1,21 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_app/features/devices/bloc/AC/ac_cubit.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/devices/bloc/AC/ac_cubit.dart';import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_large.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
import '../../../../../generated/assets.dart';
import '../../../model/ac_model.dart';
class ACTempWidget extends StatelessWidget {
const ACTempWidget(
this.index, {
this.acModel, {
super.key,
});
final int index;
final ACModel acModel;
@override
Widget build(BuildContext context) {
@ -30,7 +30,8 @@ class ACTempWidget extends StatelessWidget {
dimension: 24,
child: InkWell(
onTap: () {
AcCubit.get(context).decreaseACTemp(index);
AcCubit.get(context)
.setACTemp(acModel, acModel.temperature - 0.5);
},
child: SvgPicture.asset(
Assets.iconsMinus,
@ -38,7 +39,7 @@ class ACTempWidget extends StatelessWidget {
),
),
BodyLarge(
text: "${AcCubit.get(context).getTemp(index)}° C",
text: "${acModel.temperature}° C",
style: context.bodyLarge.copyWith(
color: ColorsManager.primaryColor.withOpacity(0.6),
fontSize: 23,
@ -48,7 +49,8 @@ class ACTempWidget extends StatelessWidget {
dimension: 24,
child: InkWell(
onTap: () {
AcCubit.get(context).increaseACTemp(index);
AcCubit.get(context)
.setACTemp(acModel, acModel.temperature + 0.5);
},
child: SvgPicture.asset(
Assets.iconsPlus,

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:syncrow_app/features/devices/bloc/AC/ac_cubit.dart';
import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart';
import 'package:syncrow_app/features/devices/model/device_category_model.dart';
import 'package:syncrow_app/features/devices/view/widgets/ACs/ac_mode_control_unit.dart';
import 'package:syncrow_app/features/devices/view/widgets/ACs/ac_temp_widget.dart';
import 'package:syncrow_app/features/devices/view/widgets/ACs/universal_ac_temp.dart';
@ -17,6 +18,7 @@ class ACsList extends StatelessWidget {
@override
Widget build(BuildContext context) {
DevicesCategoryModel category = DevicesCubit.categories[0];
return SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
@ -26,7 +28,7 @@ class ACsList extends StatelessWidget {
const BodySmall(text: "All ACs"),
const SizedBox(height: 5),
UniversalSwitch(
category: DevicesCubit.categories[0],
category: category,
),
const SizedBox(height: 10),
const UniversalACTemp(),
@ -37,8 +39,9 @@ class ACsList extends StatelessWidget {
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
padding: const EdgeInsets.all(0),
itemCount: DevicesCubit.categories[0].devices.length,
itemCount: category.devices.length,
itemBuilder: (context, index) {
ACModel ac = category.devices[index] as ACModel;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
@ -46,14 +49,10 @@ class ACsList extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
BodySmall(
text:
DevicesCubit.categories[0].devices[index].name ??
""),
BodySmall(text: category.devices[index].name ?? ""),
IconButton(
onPressed: () {
var device =
DevicesCubit.categories[0].devices[index];
var device = category.devices[index];
if (device is ACModel) {
AcCubit.get(context).selectAC(device);
}
@ -72,20 +71,18 @@ class ACsList extends StatelessWidget {
],
),
const SizedBox(height: 5),
if (DevicesCubit.categories[0].devices[index] is ACModel)
if (category.devices[index] is ACModel)
DevicesDefaultSwitch(
model:
DevicesCubit.categories[0].devices[index] as ACModel,
model: ac,
),
const SizedBox(height: 10),
ACTempWidget(
index,
ac,
),
const SizedBox(height: 10),
if (DevicesCubit.categories[0].devices[index] is ACModel)
if (category.devices[index] is ACModel)
ACModeControlUnit(
model:
DevicesCubit.categories[0].devices[index] as ACModel,
model: ac,
),
const SizedBox(height: 10),
],

View File

@ -27,7 +27,8 @@ class UniversalACTemp extends StatelessWidget {
dimension: 24,
child: InkWell(
onTap: () {
AcCubit.get(context).setTempToAll(AcCubit.averageTemp + .5);
AcCubit.get(context)
.setTempToAll(AcCubit.universalACTemp - .5);
},
child: SvgPicture.asset(
Assets.iconsMinus,
@ -35,7 +36,7 @@ class UniversalACTemp extends StatelessWidget {
),
),
BodyLarge(
text: "${AcCubit.averageTemp}° C",
text: "${AcCubit.universalACTemp}° C",
style: context.bodyLarge.copyWith(
color: ColorsManager.primaryColor.withOpacity(0.6),
fontSize: 23,
@ -45,7 +46,8 @@ class UniversalACTemp extends StatelessWidget {
dimension: 24,
child: InkWell(
onTap: () {
AcCubit.get(context).setTempToAll(AcCubit.averageTemp + .5);
AcCubit.get(context)
.setTempToAll(AcCubit.universalACTemp + .5);
},
child: SvgPicture.asset(
Assets.iconsPlus,