diff --git a/lib/features/devices/bloc/ac_cubit.dart b/lib/features/devices/bloc/ac/ac_cubit.dart similarity index 68% rename from lib/features/devices/bloc/ac_cubit.dart rename to lib/features/devices/bloc/ac/ac_cubit.dart index e6a0a1f..a02e49d 100644 --- a/lib/features/devices/bloc/ac_cubit.dart +++ b/lib/features/devices/bloc/ac/ac_cubit.dart @@ -1,6 +1,5 @@ import 'package:flutter/cupertino.dart'; 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'; @@ -8,7 +7,6 @@ part 'ac_state.dart'; class AcCubit extends Cubit { AcCubit() : super(AcInitial()) { - averageTempForAll(); updateACsStatus(); } @@ -21,7 +19,7 @@ class AcCubit extends Cubit { ACModel? getSelectedAC() { for (var ac in DevicesCubit.categories[0].devices) { - if (ac.isSelected) { + if (ac is ACModel && ac.isSelected) { return ac; } } @@ -29,7 +27,7 @@ class AcCubit extends Cubit { } void turnACOn(ACModel model) { - if (!model.status) { + if (model.status == false) { model.status = true; updateACsStatus(); emit(ACTurnedOn()); @@ -37,7 +35,7 @@ class AcCubit extends Cubit { } void turnACOff(ACModel model) { - if (model.status) { + if (model.status == true) { model.status = false; updateACsStatus(); emit(ACTurnedOff()); @@ -45,7 +43,7 @@ class AcCubit extends Cubit { } void updateACsStatus() { - bool tempStatus = DevicesCubit.categories[0].devices[0].status; + 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) { @@ -75,49 +73,44 @@ class AcCubit extends Cubit { } void setTempToAll(double temperature) { - for (var ac in DevicesCubit.categories[0].devices) { - ac.temperature = temperature; + for (var element in DevicesCubit.categories[0].devices) { + if (element is ACModel) { + element.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)); + var device = DevicesCubit.categories[0].devices[index]; + if (device is ACModel) { + device.temperature += 0.5; + emit(ACsTempChanged(device.temperature)); + } } void decreaseACTemp(int index) { - DevicesCubit.categories[0].devices[index].temperature -= .5; - averageTempForAll(); - emit(ACsTempChanged(DevicesCubit.categories[0].devices[index].temperature)); + 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; - averageTempForAll(); emit(ACsTempChanged(temp)); } double getTemp(int index) { - return DevicesCubit.categories[0].devices[index].temperature; + 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; - 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 } diff --git a/lib/features/devices/bloc/ac_state.dart b/lib/features/devices/bloc/ac/ac_state.dart similarity index 100% rename from lib/features/devices/bloc/ac_state.dart rename to lib/features/devices/bloc/ac/ac_state.dart diff --git a/lib/features/devices/bloc/devices_cubit.dart b/lib/features/devices/bloc/devices_cubit.dart index 7fe69cf..452632e 100644 --- a/lib/features/devices/bloc/devices_cubit.dart +++ b/lib/features/devices/bloc/devices_cubit.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/devices/model/ac_model.dart'; +import 'package:syncrow_app/features/devices/model/light_model.dart'; import 'package:syncrow_app/features/devices/view/widgets/curtains/curtain_view.dart'; import 'package:syncrow_app/features/devices/view/widgets/gateway/gateway_view.dart'; import 'package:syncrow_app/features/devices/view/widgets/screens/screens_view.dart'; @@ -28,7 +29,7 @@ class DevicesCubit extends Cubit { static DevicesCubit get(context) => BlocProvider.of(context); - static var categories = [ + static List categories = [ DevicesCategoryModel( devices: [ ACModel( @@ -38,6 +39,10 @@ class DevicesCubit extends Cubit { temperature: 20, fanSpeed: 0, tempMode: 0, + type: '', + location: '', + image: '', + timer: null, ), ACModel( name: "Master Bedroom AC", @@ -46,6 +51,10 @@ class DevicesCubit extends Cubit { temperature: 20, fanSpeed: 0, tempMode: 0, + type: '', + location: '', + image: '', + timer: null, ), ], icon: Assets.iconsAC, @@ -54,7 +63,32 @@ class DevicesCubit extends Cubit { page: const ACsView(), ), DevicesCategoryModel( - devices: [], + devices: [ + LightModel( + name: "Living Room Light", + id: '0', + status: false, + color: 0, + brightness: 20, + lightingMode: 1, + timer: null, + type: '', + location: '', + image: '', + ), + LightModel( + name: "Master Bedroom Light", + id: '1', + status: false, + color: 2, + brightness: 40, + lightingMode: 1, + timer: null, + type: '', + location: '', + image: '', + ), + ], icon: Assets.iconsLight, name: 'Lights', type: DeviceType.Lights, @@ -101,7 +135,7 @@ class DevicesCubit extends Cubit { void areAllDevicesOff(DevicesCategoryModel category) { for (var device in category.devices) { - if (device.status) { + if (device.status ?? false) { category.devicesStatus = false; emit(CategorySwitchChanged()); return; diff --git a/lib/features/devices/bloc/lights/lights_cubit.dart b/lib/features/devices/bloc/lights/lights_cubit.dart new file mode 100644 index 0000000..0ce32e9 --- /dev/null +++ b/lib/features/devices/bloc/lights/lights_cubit.dart @@ -0,0 +1,9 @@ +import 'package:flutter_bloc/flutter_bloc.dart'; + +part 'lights_state.dart'; + +class LightsCubit extends Cubit { + LightsCubit() : super(LightsInitial()); + + static LightsCubit get(context) => BlocProvider.of(context); +} diff --git a/lib/features/devices/bloc/lights/lights_state.dart b/lib/features/devices/bloc/lights/lights_state.dart new file mode 100644 index 0000000..96b8e90 --- /dev/null +++ b/lib/features/devices/bloc/lights/lights_state.dart @@ -0,0 +1,5 @@ +part of 'lights_cubit.dart'; + +abstract class LightsState {} + +class LightsInitial extends LightsState {} diff --git a/lib/features/devices/model/ac_model.dart b/lib/features/devices/model/ac_model.dart index cf342a8..db012e8 100644 --- a/lib/features/devices/model/ac_model.dart +++ b/lib/features/devices/model/ac_model.dart @@ -1,21 +1,51 @@ -class ACModel { - final String name; - final String id; - late bool status; +import 'package:syncrow_app/features/devices/model/device_model.dart'; + +class ACModel extends DeviceModel { late double temperature; late int fanSpeed; late int tempMode; - bool isSelected = false; - ACModel({ - required this.name, - required this.id, - required this.status, required this.temperature, required this.fanSpeed, required this.tempMode, + required super.id, + required super.name, + required super.type, + required super.status, + required super.location, + required super.image, + required super.timer, }); + + Map toJson() { + return { + 'temperature': temperature, + 'fanSpeed': fanSpeed, + 'tempMode': tempMode, + 'id': id, + 'name': name, + 'status': status, + 'type': type, + 'location': location, + 'image': image, + }; + } + + factory ACModel.fromJson(Map json) { + return ACModel( + id: json['id'], + name: json['name'], + status: json['status'], + temperature: json['temperature'], + fanSpeed: json['fanSpeed'], + tempMode: json['tempMode'], + type: json['type'], + location: json['location'], + image: json['image'], + timer: json['timer'], + ); + } } diff --git a/lib/features/devices/model/device_category_model.dart b/lib/features/devices/model/device_category_model.dart index 172bcf4..b6ff99d 100644 --- a/lib/features/devices/model/device_category_model.dart +++ b/lib/features/devices/model/device_category_model.dart @@ -1,6 +1,5 @@ import 'package:flutter/cupertino.dart'; - -import 'ac_model.dart'; +import 'package:syncrow_app/features/devices/model/device_model.dart'; class DevicesCategoryModel { final String name; @@ -9,7 +8,7 @@ class DevicesCategoryModel { final Widget page; bool devicesStatus = false; - final List devices; + final List devices; final DeviceType type; bool isSelected; @@ -23,7 +22,7 @@ class DevicesCategoryModel { required this.devices}) { //sets the initial status of the devices if (devices.isNotEmpty) { - bool tempStatus = devices.first.status; + bool tempStatus = devices.first.status ?? false; for (var device in devices) { if (device.status != tempStatus) { devicesStatus = false; diff --git a/lib/features/devices/model/device_model.dart b/lib/features/devices/model/device_model.dart new file mode 100644 index 0000000..f4c5b9c --- /dev/null +++ b/lib/features/devices/model/device_model.dart @@ -0,0 +1,20 @@ +abstract class DeviceModel { + final String? id; + final String? name; + final String? type; + bool? status; + final String? location; + final String? image; + final double? timer; + bool isSelected = false; + + DeviceModel({ + required this.id, + required this.name, + required this.type, + required this.status, + required this.location, + required this.image, + required this.timer, + }); +} diff --git a/lib/features/devices/model/light_model.dart b/lib/features/devices/model/light_model.dart new file mode 100644 index 0000000..1d694d7 --- /dev/null +++ b/lib/features/devices/model/light_model.dart @@ -0,0 +1,51 @@ +import 'package:syncrow_app/features/devices/model/device_model.dart'; + +class LightModel extends DeviceModel { + final double brightness; + final int color; + + final int lightingMode; + + LightModel({ + required this.brightness, + required this.color, + required this.lightingMode, + required super.id, + required super.name, + required super.type, + required super.status, + required super.location, + required super.image, + required super.timer, + }); + + Map toJson() { + return { + 'luminance': brightness, + 'color': color, + 'lightingMode': lightingMode, + 'timer': timer, + 'id': id, + 'name': name, + 'status': status, + 'type': type, + 'location': location, + 'image': image, + }; + } + + factory LightModel.fromJson(Map json) { + return LightModel( + id: json['id'], + name: json['name'], + status: json['status'], + brightness: json['luminance'], + color: json['color'], + lightingMode: json['lightingMode'], + timer: json['timer'], + type: json['type'], + location: json['location'], + image: json['image'], + ); + } +} diff --git a/lib/features/devices/view/widgets/ACs/ac_interface_temp_unit.dart b/lib/features/devices/view/widgets/ACs/ac_interface_temp_unit.dart index 298689b..3859014 100644 --- a/lib/features/devices/view/widgets/ACs/ac_interface_temp_unit.dart +++ b/lib/features/devices/view/widgets/ACs/ac_interface_temp_unit.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:sleek_circular_slider/sleek_circular_slider.dart'; -import 'package:syncrow_app/features/devices/bloc/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/text_widgets/body_large.dart'; import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart'; diff --git a/lib/features/devices/view/widgets/ACs/ac_temp_widget.dart b/lib/features/devices/view/widgets/ACs/ac_temp_widget.dart index daa72c1..22e53b4 100644 --- a/lib/features/devices/view/widgets/ACs/ac_temp_widget.dart +++ b/lib/features/devices/view/widgets/ACs/ac_temp_widget.dart @@ -1,7 +1,7 @@ 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_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/text_widgets/body_large.dart'; import 'package:syncrow_app/utils/context_extension.dart'; diff --git a/lib/features/devices/view/widgets/ACs/acs_list.dart b/lib/features/devices/view/widgets/ACs/acs_list.dart index 00b7e4b..f9465de 100644 --- a/lib/features/devices/view/widgets/ACs/acs_list.dart +++ b/lib/features/devices/view/widgets/ACs/acs_list.dart @@ -1,5 +1,5 @@ import 'package:flutter/material.dart'; -import 'package:syncrow_app/features/devices/bloc/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/view/widgets/ACs/ac_mode_control_unit.dart'; import 'package:syncrow_app/features/devices/view/widgets/ACs/ac_temp_widget.dart'; @@ -8,6 +8,8 @@ import 'package:syncrow_app/features/devices/view/widgets/ACs/universal_ac_temp. import 'package:syncrow_app/features/shared_widgets/devices_default_switch.dart'; import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.dart'; +import '../../../model/ac_model.dart'; + class ACsList extends StatelessWidget { const ACsList({ super.key, @@ -43,11 +45,16 @@ class ACsList extends StatelessWidget { crossAxisAlignment: CrossAxisAlignment.end, children: [ BodySmall( - text: DevicesCubit.categories[0].devices[index].name), + text: + DevicesCubit.categories[0].devices[index].name ?? + ""), IconButton( onPressed: () { - AcCubit.get(context).selectAC( - DevicesCubit.categories[0].devices[index]); + var device = + DevicesCubit.categories[0].devices[index]; + if (device is ACModel) { + AcCubit.get(context).selectAC(device); + } }, icon: const Icon( Icons.arrow_forward_ios, @@ -63,17 +70,21 @@ class ACsList extends StatelessWidget { ], ), const SizedBox(height: 5), - DevicesDefaultSwitch( - model: DevicesCubit.categories[0].devices[index], - ), + if (DevicesCubit.categories[0].devices[index] is ACModel) + DevicesDefaultSwitch( + model: + DevicesCubit.categories[0].devices[index] as ACModel, + ), const SizedBox(height: 10), ACTempWidget( index, ), const SizedBox(height: 10), - ACModeControlUnit( - model: DevicesCubit.categories[0].devices[index], - ), + if (DevicesCubit.categories[0].devices[index] is ACModel) + ACModeControlUnit( + model: + DevicesCubit.categories[0].devices[index] as ACModel, + ), const SizedBox(height: 10), ], ); diff --git a/lib/features/devices/view/widgets/ACs/acs_view.dart b/lib/features/devices/view/widgets/ACs/acs_view.dart index 0df5a6e..a4ff23c 100644 --- a/lib/features/devices/view/widgets/ACs/acs_view.dart +++ b/lib/features/devices/view/widgets/ACs/acs_view.dart @@ -3,7 +3,7 @@ import 'package:flutter/services.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/app_layout/view/widgets/default_app_bar.dart'; import 'package:syncrow_app/features/app_layout/view/widgets/default_nav_bar.dart'; -import 'package:syncrow_app/features/devices/bloc/ac_cubit.dart'; +import 'package:syncrow_app/features/devices/bloc/AC/ac_cubit.dart'; import 'package:syncrow_app/features/devices/model/ac_model.dart'; import 'package:syncrow_app/features/devices/view/widgets/ACs/ac_interface.dart'; import 'package:syncrow_app/features/devices/view/widgets/ACs/acs_list.dart'; diff --git a/lib/features/devices/view/widgets/ACs/universal_ac_switch.dart b/lib/features/devices/view/widgets/ACs/universal_ac_switch.dart index 4de5c8e..1d3567d 100644 --- a/lib/features/devices/view/widgets/ACs/universal_ac_switch.dart +++ b/lib/features/devices/view/widgets/ACs/universal_ac_switch.dart @@ -4,7 +4,7 @@ import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart'; import '../../../../../utils/resource_manager/color_manager.dart'; import '../../../../shared_widgets/text_widgets/body_medium.dart'; -import '../../../bloc/ac_cubit.dart'; +import '../../../bloc/AC/ac_cubit.dart'; class UniversalACSwitch extends StatelessWidget { const UniversalACSwitch({ diff --git a/lib/features/devices/view/widgets/ACs/universal_ac_temp.dart b/lib/features/devices/view/widgets/ACs/universal_ac_temp.dart index fdb6ea2..aede043 100644 --- a/lib/features/devices/view/widgets/ACs/universal_ac_temp.dart +++ b/lib/features/devices/view/widgets/ACs/universal_ac_temp.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_svg/svg.dart'; -import 'package:syncrow_app/features/devices/bloc/ac_cubit.dart'; +import 'package:syncrow_app/features/devices/bloc/AC/ac_cubit.dart'; import 'package:syncrow_app/utils/context_extension.dart'; import '../../../../../generated/assets.dart'; diff --git a/lib/features/devices/view/widgets/lights/lights_view.dart b/lib/features/devices/view/widgets/lights/lights_view.dart index 6ddbae5..979d944 100644 --- a/lib/features/devices/view/widgets/lights/lights_view.dart +++ b/lib/features/devices/view/widgets/lights/lights_view.dart @@ -1,10 +1,20 @@ import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; + +import '../../../bloc/lights/lights_cubit.dart'; class LightsView extends StatelessWidget { const LightsView({super.key}); @override Widget build(BuildContext context) { - return const Placeholder(); + return BlocProvider( + create: (context) => LightsCubit(), + child: BlocBuilder( + builder: (context, state) => const Center( + child: Text('LightsView'), + ), + ), + ); } } diff --git a/lib/features/shared_widgets/devices_default_switch.dart b/lib/features/shared_widgets/devices_default_switch.dart index 5d2a875..02c3337 100644 --- a/lib/features/shared_widgets/devices_default_switch.dart +++ b/lib/features/shared_widgets/devices_default_switch.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:syncrow_app/features/devices/bloc/ac_cubit.dart'; +import 'package:syncrow_app/features/devices/bloc/AC/ac_cubit.dart'; import 'package:syncrow_app/features/devices/model/ac_model.dart'; import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart'; import 'package:syncrow_app/utils/resource_manager/color_manager.dart'; @@ -27,7 +27,7 @@ class DevicesDefaultSwitch extends StatelessWidget { child: Container( height: 60, decoration: BoxDecoration( - color: model.status + color: model.status ?? false ? ColorsManager.primaryColor : Colors.white, borderRadius: const BorderRadius.only( @@ -38,7 +38,7 @@ class DevicesDefaultSwitch extends StatelessWidget { child: Center( child: BodyMedium( text: "ON", - fontColor: model.status ? Colors.white : null, + fontColor: model.status ?? false ? Colors.white : null, fontWeight: FontWeight.bold, ), ), @@ -53,7 +53,7 @@ class DevicesDefaultSwitch extends StatelessWidget { child: Container( height: 60, decoration: BoxDecoration( - color: model.status + color: model.status ?? false ? Colors.white : ColorsManager.primaryColor, borderRadius: const BorderRadius.only( @@ -64,7 +64,7 @@ class DevicesDefaultSwitch extends StatelessWidget { child: Center( child: BodyMedium( text: "OFF", - fontColor: model.status ? null : Colors.white, + fontColor: model.status ?? false ? null : Colors.white, fontWeight: FontWeight.bold, ), ),