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

View File

@ -18,62 +18,8 @@ part 'devices_state.dart';
class DevicesCubit extends Cubit<DevicesState> { class DevicesCubit extends Cubit<DevicesState> {
DevicesCubit() : super(DevicesInitial()); 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); 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 = [ static List<DevicesCategoryModel> categories = [
DevicesCategoryModel( DevicesCategoryModel(
devices: [ devices: [
@ -84,6 +30,7 @@ class DevicesCubit extends Cubit<DevicesState> {
temperature: 20, temperature: 20,
fanSpeed: 0, fanSpeed: 0,
tempMode: 0, tempMode: 0,
coolTo: 20,
type: '', type: '',
location: '', location: '',
image: '', image: '',
@ -96,6 +43,33 @@ class DevicesCubit extends Cubit<DevicesState> {
temperature: 20, temperature: 20,
fanSpeed: 0, fanSpeed: 0,
tempMode: 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: '', type: '',
location: '', location: '',
image: '', image: '',
@ -133,6 +107,42 @@ class DevicesCubit extends Cubit<DevicesState> {
location: '', location: '',
image: '', 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, icon: Assets.iconsLight,
name: 'Lights', name: 'Lights',
@ -178,6 +188,60 @@ class DevicesCubit extends Cubit<DevicesState> {
return null; 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) { void areAllDevicesOff(DevicesCategoryModel category) {
for (var device in category.devices) { for (var device in category.devices) {
if (device.status ?? false) { if (device.status ?? false) {

View File

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

View File

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

View File

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

View File

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

View File

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