mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-16 01:56:19 +00:00
111 lines
2.7 KiB
Dart
111 lines
2.7 KiB
Dart
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/room_model.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()) {
|
|
fetchSpaces().then((value) {
|
|
if (selectedSpace != null) {
|
|
fetchRooms(selectedSpace!);
|
|
}
|
|
});
|
|
}
|
|
|
|
static SpacesCubit get(context) => BlocProvider.of(context);
|
|
|
|
static List<SpaceModel> spaces = [];
|
|
|
|
SpaceModel? selectedSpace = spaces.isNotEmpty ? spaces.first : null;
|
|
|
|
RoomModel? selectedRoom;
|
|
|
|
PageController devicesPageController = PageController();
|
|
|
|
PageController roomsPageController = PageController();
|
|
|
|
var duration = const Duration(milliseconds: 300);
|
|
|
|
selectSpace(SpaceModel space) {
|
|
selectedSpace = space;
|
|
emit(SpacesSelected(space));
|
|
}
|
|
|
|
roomSliderPageChanged(int index) {
|
|
devicesPageController.animateToPage(
|
|
index,
|
|
duration: duration,
|
|
curve: Curves.linear,
|
|
);
|
|
|
|
if (index == 0) {
|
|
unselectRoom();
|
|
} else {
|
|
selectedRoom = selectedSpace!.rooms![index - 1];
|
|
}
|
|
emit(RoomSelected(selectedRoom!));
|
|
}
|
|
|
|
devicesPageChanged(int index) {
|
|
roomsPageController.animateToPage(
|
|
index,
|
|
duration: const Duration(milliseconds: 300),
|
|
curve: Curves.linear,
|
|
);
|
|
|
|
if (index == 0) {
|
|
unselectRoom();
|
|
} else {
|
|
selectedRoom = selectedSpace!.rooms![index - 1];
|
|
}
|
|
emit(RoomSelected(selectedRoom!));
|
|
}
|
|
|
|
unselectRoom() {
|
|
selectedRoom = null;
|
|
devicesPageController.animateToPage(
|
|
0,
|
|
duration: duration,
|
|
curve: Curves.linear,
|
|
);
|
|
|
|
roomsPageController.animateToPage(
|
|
0,
|
|
duration: duration,
|
|
curve: Curves.linear,
|
|
);
|
|
|
|
emit(RoomUnSelected());
|
|
}
|
|
|
|
fetchSpaces() async {
|
|
emit(SpacesLoading());
|
|
try {
|
|
spaces = await SpacesAPI.getSpaces();
|
|
spaces.isNotEmpty ? selectSpace(spaces.first) : null;
|
|
emit(SpacesLoaded(spaces));
|
|
} on DioException catch (e) {
|
|
emit(SpacesError(ServerFailure.fromDioError(e).errMessage));
|
|
}
|
|
}
|
|
|
|
fetchRooms(SpaceModel space) async {
|
|
emit(SpaceRoomsLoading());
|
|
try {
|
|
space.rooms = await SpacesAPI.getRooms(space.id!);
|
|
if (space.rooms != null) {
|
|
emit(SpaceRoomsLoaded(space.rooms!));
|
|
} else {
|
|
emit(SpaceRoomsError("No rooms found"));
|
|
}
|
|
} on DioException catch (e) {
|
|
emit(SpacesError(ServerFailure.fromDioError(e).errMessage));
|
|
}
|
|
}
|
|
}
|