mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-24 08:47:55 +00:00
Converted all the devices models to extends from DeviceModel for the purpose of unifying.
Initialized Lights feature.
This commit is contained in:
+23
-30
@@ -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<AcState> {
|
||||
AcCubit() : super(AcInitial()) {
|
||||
averageTempForAll();
|
||||
updateACsStatus();
|
||||
}
|
||||
|
||||
@@ -21,7 +19,7 @@ class AcCubit extends Cubit<AcState> {
|
||||
|
||||
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<AcState> {
|
||||
}
|
||||
|
||||
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<AcState> {
|
||||
}
|
||||
|
||||
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<AcState> {
|
||||
}
|
||||
|
||||
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<AcState> {
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -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<DevicesState> {
|
||||
|
||||
static DevicesCubit get(context) => BlocProvider.of(context);
|
||||
|
||||
static var categories = <DevicesCategoryModel>[
|
||||
static List<DevicesCategoryModel> categories = [
|
||||
DevicesCategoryModel(
|
||||
devices: [
|
||||
ACModel(
|
||||
@@ -38,6 +39,10 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
temperature: 20,
|
||||
fanSpeed: 0,
|
||||
tempMode: 0,
|
||||
type: '',
|
||||
location: '',
|
||||
image: '',
|
||||
timer: null,
|
||||
),
|
||||
ACModel(
|
||||
name: "Master Bedroom AC",
|
||||
@@ -46,6 +51,10 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
temperature: 20,
|
||||
fanSpeed: 0,
|
||||
tempMode: 0,
|
||||
type: '',
|
||||
location: '',
|
||||
image: '',
|
||||
timer: null,
|
||||
),
|
||||
],
|
||||
icon: Assets.iconsAC,
|
||||
@@ -54,7 +63,32 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
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<DevicesState> {
|
||||
|
||||
void areAllDevicesOff(DevicesCategoryModel category) {
|
||||
for (var device in category.devices) {
|
||||
if (device.status) {
|
||||
if (device.status ?? false) {
|
||||
category.devicesStatus = false;
|
||||
emit(CategorySwitchChanged());
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
part 'lights_state.dart';
|
||||
|
||||
class LightsCubit extends Cubit<LightsState> {
|
||||
LightsCubit() : super(LightsInitial());
|
||||
|
||||
static LightsCubit get(context) => BlocProvider.of(context);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
part of 'lights_cubit.dart';
|
||||
|
||||
abstract class LightsState {}
|
||||
|
||||
class LightsInitial extends LightsState {}
|
||||
Reference in New Issue
Block a user