mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-16 18:16:21 +00:00
Semi-implemented getting spaces feature
This commit is contained in:
@ -1,210 +1,23 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/ac_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/curtain_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/device_category_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/light_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/room_model.dart';
|
||||
import 'package:syncrow_app/features/devices/view/widgets/ACs/acs_view.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/lights/lights_view.dart';
|
||||
import 'package:syncrow_app/features/devices/view/widgets/smart_door/door_view.dart';
|
||||
import 'package:syncrow_app/generated/assets.dart';
|
||||
import 'package:syncrow_app/utils/resource_manager/constants.dart';
|
||||
import 'package:syncrow_app/services/api/network_exception.dart';
|
||||
import 'package:syncrow_app/services/api/spaces_api.dart';
|
||||
|
||||
part 'spaces_state.dart';
|
||||
|
||||
class SpacesCubit extends Cubit<SpacesState> {
|
||||
SpacesCubit() : super(SpacesInitial());
|
||||
SpacesCubit() : super(SpacesInitial()) {
|
||||
fetchSpaces();
|
||||
}
|
||||
|
||||
static SpacesCubit get(context) => BlocProvider.of(context);
|
||||
|
||||
static List<SpaceModel> spaces = [
|
||||
SpaceModel(
|
||||
id: '0',
|
||||
name: 'Home',
|
||||
rooms: [
|
||||
RoomModel(id: '0', name: 'Living Room', categories: [
|
||||
DevicesCategoryModel(
|
||||
devices: [
|
||||
ACModel(
|
||||
name: "Living Room AC",
|
||||
id: '0',
|
||||
status: false,
|
||||
temperature: 20,
|
||||
fanSpeed: 0,
|
||||
tempMode: 0,
|
||||
coolTo: 20,
|
||||
type: DeviceType.AC,
|
||||
image: '',
|
||||
timer: null,
|
||||
bounds: Bounds(
|
||||
min: 20,
|
||||
max: 30,
|
||||
),
|
||||
),
|
||||
],
|
||||
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: DeviceType.Lights,
|
||||
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: [
|
||||
CurtainModel(
|
||||
openPercentage: 10,
|
||||
id: "1",
|
||||
name: "Living Room Curtain",
|
||||
status: false,
|
||||
type: DeviceType.Curtain,
|
||||
image: '',
|
||||
timer: null,
|
||||
),
|
||||
],
|
||||
icon: Assets.iconsCurtain,
|
||||
name: 'Curtains',
|
||||
type: DeviceType.Curtain,
|
||||
page: const CurtainView(),
|
||||
),
|
||||
DevicesCategoryModel(
|
||||
devices: [],
|
||||
icon: Assets.iconsGateway,
|
||||
name: 'Gateway',
|
||||
type: DeviceType.Gateway,
|
||||
page: const GateWayView(),
|
||||
),
|
||||
]),
|
||||
RoomModel(id: '1', name: 'Bedroom', categories: [
|
||||
DevicesCategoryModel(
|
||||
devices: [
|
||||
ACModel(
|
||||
name: "Living Room AC",
|
||||
id: '0',
|
||||
status: false,
|
||||
temperature: 20,
|
||||
fanSpeed: 0,
|
||||
tempMode: 0,
|
||||
coolTo: 20,
|
||||
type: DeviceType.AC,
|
||||
image: '',
|
||||
timer: null,
|
||||
bounds: Bounds(
|
||||
min: 20,
|
||||
max: 30,
|
||||
),
|
||||
),
|
||||
],
|
||||
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: DeviceType.Lights,
|
||||
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: [
|
||||
CurtainModel(
|
||||
openPercentage: 10,
|
||||
id: "1",
|
||||
name: "Living Room Curtain",
|
||||
status: false,
|
||||
type: DeviceType.Curtain,
|
||||
image: '',
|
||||
timer: null,
|
||||
),
|
||||
],
|
||||
icon: Assets.iconsCurtain,
|
||||
name: 'Curtains',
|
||||
type: DeviceType.Curtain,
|
||||
page: const CurtainView(),
|
||||
),
|
||||
DevicesCategoryModel(
|
||||
devices: [],
|
||||
icon: Assets.iconsGateway,
|
||||
name: 'Gateway',
|
||||
type: DeviceType.Gateway,
|
||||
page: const GateWayView(),
|
||||
),
|
||||
]),
|
||||
],
|
||||
),
|
||||
SpaceModel(
|
||||
id: '1',
|
||||
name: 'Office',
|
||||
rooms: [],
|
||||
),
|
||||
SpaceModel(
|
||||
id: '2',
|
||||
name: 'Parent\'s House',
|
||||
rooms: [],
|
||||
),
|
||||
];
|
||||
static List<SpaceModel> spaces = [];
|
||||
|
||||
SpaceModel selectedSpace = spaces.first;
|
||||
SpaceModel? selectedSpace = spaces.isNotEmpty ? spaces.first : null;
|
||||
|
||||
RoomModel? selectedRoom;
|
||||
|
||||
@ -229,7 +42,7 @@ class SpacesCubit extends Cubit<SpacesState> {
|
||||
if (index == 0) {
|
||||
unselectRoom();
|
||||
} else {
|
||||
selectedRoom = selectedSpace.rooms[index - 1];
|
||||
selectedRoom = selectedSpace!.rooms![index - 1];
|
||||
}
|
||||
emit(RoomSelected(selectedRoom!));
|
||||
}
|
||||
@ -244,7 +57,7 @@ class SpacesCubit extends Cubit<SpacesState> {
|
||||
if (index == 0) {
|
||||
unselectRoom();
|
||||
} else {
|
||||
selectedRoom = selectedSpace.rooms[index - 1];
|
||||
selectedRoom = selectedSpace!.rooms![index - 1];
|
||||
}
|
||||
emit(RoomSelected(selectedRoom!));
|
||||
}
|
||||
@ -266,5 +79,16 @@ class SpacesCubit extends Cubit<SpacesState> {
|
||||
emit(RoomUnSelected());
|
||||
}
|
||||
|
||||
//TODO implement the methods to fetch the spaces from the API
|
||||
fetchSpaces() async {
|
||||
emit(SpacesLoading());
|
||||
try {
|
||||
spaces = await SpacesAPI.getSpaces();
|
||||
emit(SpacesLoaded(spaces));
|
||||
} on DioException catch (e) {
|
||||
emit(SpacesError(e.message ?? "Something went wrong"));
|
||||
throw ServerFailure.fromDioError(e);
|
||||
} catch (e) {
|
||||
emit(SpacesError(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user