mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-17 02:25:16 +00:00
95 lines
2.2 KiB
Dart
95 lines
2.2 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();
|
|
}
|
|
|
|
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();
|
|
emit(SpacesLoaded(spaces));
|
|
} on DioException catch (e) {
|
|
emit(SpacesError(e.message ?? "Something went wrong"));
|
|
throw ServerFailure.fromDioError(e);
|
|
} catch (e) {
|
|
emit(SpacesError(e.toString()));
|
|
}
|
|
}
|
|
}
|