mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-15 17:47:28 +00:00
330 lines
8.2 KiB
Dart
330 lines
8.2 KiB
Dart
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/device_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';
|
|
import 'package:syncrow_app/features/devices/view/widgets/smart_door/door_view.dart';
|
|
import 'package:syncrow_app/generated/assets.dart';
|
|
|
|
import '../model/device_category_model.dart';
|
|
import '../view/widgets/ACs/acs_view.dart';
|
|
import '../view/widgets/lights/lights_view.dart';
|
|
|
|
part 'devices_state.dart';
|
|
|
|
class DevicesCubit extends Cubit<DevicesState> {
|
|
DevicesCubit() : super(DevicesInitial());
|
|
|
|
static DevicesCubit get(context) => BlocProvider.of(context);
|
|
|
|
static List<DevicesCategoryModel> categories = [
|
|
DevicesCategoryModel(
|
|
devices: [
|
|
ACModel(
|
|
name: "Living Room AC",
|
|
id: '0',
|
|
status: false,
|
|
temperature: 20,
|
|
fanSpeed: 0,
|
|
tempMode: 0,
|
|
coolTo: 20,
|
|
type: '',
|
|
location: '',
|
|
image: '',
|
|
timer: null,
|
|
),
|
|
ACModel(
|
|
name: "Master Bedroom AC",
|
|
id: '1',
|
|
status: false,
|
|
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: '',
|
|
timer: null,
|
|
),
|
|
],
|
|
icon: Assets.iconsAC,
|
|
name: 'ACs',
|
|
type: DeviceType.AC,
|
|
page: const ACsView(),
|
|
),
|
|
DevicesCategoryModel(
|
|
devices: [
|
|
LightModel(
|
|
name: "Living Room Light",
|
|
id: '0',
|
|
status: false,
|
|
color: 0,
|
|
brightness: 20,
|
|
lightingMode: 1,
|
|
timer: null,
|
|
type: '',
|
|
location: '',
|
|
image: '',
|
|
recentColors: [
|
|
0xFF83D9FF,
|
|
0xFFFC3E81,
|
|
0xFFC0FF66,
|
|
0xFFFDC242,
|
|
],
|
|
),
|
|
LightModel(
|
|
name: "Master Bedroom Light",
|
|
id: '1',
|
|
status: false,
|
|
color: 2,
|
|
brightness: 40,
|
|
lightingMode: 1,
|
|
timer: null,
|
|
type: '',
|
|
location: '',
|
|
image: '',
|
|
recentColors: [
|
|
0xFF83D9FF,
|
|
0xFFFC3E81,
|
|
0xFFC0FF66,
|
|
0xFFFDC242,
|
|
],
|
|
),
|
|
LightModel(
|
|
name: "Kitchen Light",
|
|
id: '2',
|
|
status: false,
|
|
color: 1,
|
|
brightness: 60,
|
|
lightingMode: 1,
|
|
timer: null,
|
|
type: '',
|
|
location: '',
|
|
image: '',
|
|
recentColors: [
|
|
0xFF83D9FF,
|
|
0xFFFC3E81,
|
|
0xFFC0FF66,
|
|
0xFFFDC242,
|
|
],
|
|
),
|
|
LightModel(
|
|
name: "Bathroom Light",
|
|
id: '3',
|
|
status: false,
|
|
color: 3,
|
|
brightness: 80,
|
|
lightingMode: 1,
|
|
timer: null,
|
|
type: '',
|
|
location: '',
|
|
image: '',
|
|
recentColors: [
|
|
0xFF83D9FF,
|
|
0xFFFC3E81,
|
|
0xFFC0FF66,
|
|
0xFFFDC242,
|
|
],
|
|
),
|
|
LightModel(
|
|
name: "Balcony Light",
|
|
id: '4',
|
|
status: false,
|
|
color: 4,
|
|
brightness: 100,
|
|
lightingMode: 1,
|
|
timer: null,
|
|
type: '',
|
|
location: '',
|
|
image: '',
|
|
recentColors: [
|
|
0xFF83D9FF,
|
|
0xFFFC3E81,
|
|
0xFFC0FF66,
|
|
0xFFFDC242,
|
|
],
|
|
),
|
|
],
|
|
icon: Assets.iconsLight,
|
|
name: 'Lights',
|
|
type: DeviceType.Lights,
|
|
page: const LightsView(),
|
|
),
|
|
DevicesCategoryModel(
|
|
devices: [],
|
|
icon: Assets.iconsDoorLock,
|
|
name: 'Doors',
|
|
type: DeviceType.Door,
|
|
page: const DoorView(),
|
|
),
|
|
DevicesCategoryModel(
|
|
devices: [],
|
|
icon: Assets.iconsCurtain,
|
|
name: 'Curtains',
|
|
type: DeviceType.Curtain,
|
|
page: const CurtainView(),
|
|
),
|
|
DevicesCategoryModel(
|
|
devices: [],
|
|
icon: Assets.iconsScreen,
|
|
name: 'Screens',
|
|
type: DeviceType.Screens,
|
|
page: const ScreensView(),
|
|
),
|
|
DevicesCategoryModel(
|
|
devices: [],
|
|
icon: Assets.iconsGateway,
|
|
name: 'Gateway',
|
|
type: DeviceType.Gateway,
|
|
page: const GateWayView(),
|
|
),
|
|
];
|
|
|
|
void selectCategory(int index) {
|
|
for (var i = 0; i < categories.length; i++) {
|
|
if (i == index) {
|
|
categories[i].isSelected = true;
|
|
} else {
|
|
categories[i].isSelected = false;
|
|
}
|
|
}
|
|
emit(DevicesCategoryChanged());
|
|
}
|
|
|
|
Widget? get chosenCategoryView {
|
|
for (var category in categories) {
|
|
if (category.isSelected) {
|
|
return category.page;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
void selectDevice(DeviceModel device) {
|
|
for (var category in categories) {
|
|
for (var device in category.devices) {
|
|
if (device.isSelected) {
|
|
category.isSelected = false;
|
|
emit(DeviceSelected());
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
device.isSelected = !device.isSelected;
|
|
emit(DeviceSelected());
|
|
}
|
|
|
|
DeviceModel? getSelectedDevice() {
|
|
for (var category in categories) {
|
|
for (var device in category.devices) {
|
|
if (device.isSelected) {
|
|
return device;
|
|
}
|
|
}
|
|
}
|
|
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) {
|
|
category.devicesStatus = false;
|
|
emit(CategorySwitchChanged());
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void clearCategoriesSelection() {
|
|
for (var category in categories) {
|
|
category.isSelected = false;
|
|
for (var device in category.devices) {
|
|
device.isSelected = false;
|
|
}
|
|
}
|
|
}
|
|
}
|