Refactor device control logic and add temperature and fan speed enums

- Refactor device control logic in the app to improve readability and maintainability.
- Add temperature modes (hot, cold, wind) and fan speeds (auto, low, middle, high) enums.
- Update icon mappings and utility functions for temperature modes and fan speeds.
This commit is contained in:
Mohammad Salameh
2024-04-03 18:54:21 +03:00
parent 6577652702
commit bff4b9493c
30 changed files with 183 additions and 107 deletions

View File

@ -18,8 +18,6 @@ import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
part 'home_state.dart';
class HomeCubit extends Cubit<HomeState> {
// Create a private static instance variable
HomeCubit._() : super(HomeInitial()) {
if (selectedSpace == null) {
fetchSpaces().then((value) {
@ -37,6 +35,13 @@ class HomeCubit extends Cubit<HomeState> {
return _instance!;
}
void emitSafe(HomeState newState) {
final cubit = this;
if (!cubit.isClosed) {
cubit.emit(newState);
}
}
static HomeCubit get(context) => BlocProvider.of(context);
List<SpaceModel>? spaces;
@ -58,7 +63,7 @@ class HomeCubit extends Cubit<HomeState> {
changeSelectedSpace(SpaceModel space) {
selectedSpace = space;
emit(SpaceSelected(space));
emitSafe(SpaceSelected(space));
}
roomSliderPageChanged(int index) {
@ -72,7 +77,7 @@ class HomeCubit extends Cubit<HomeState> {
unselectRoom();
} else {
selectedRoom = selectedSpace!.rooms![index - 1];
emit(RoomSelected(selectedRoom!));
emitSafe(RoomSelected(selectedRoom!));
}
}
@ -87,7 +92,7 @@ class HomeCubit extends Cubit<HomeState> {
unselectRoom();
} else {
selectedRoom = selectedSpace!.rooms![index - 1];
emit(RoomSelected(selectedRoom!));
emitSafe(RoomSelected(selectedRoom!));
}
}
@ -105,11 +110,11 @@ class HomeCubit extends Cubit<HomeState> {
curve: Curves.linear,
);
emit(RoomUnSelected());
emitSafe(RoomUnSelected());
}
fetchSpaces() async {
emit(GetSpacesLoading());
emitSafe(GetSpacesLoading());
try {
spaces = await SpacesAPI.getSpaces();
selectedSpace = spaces!.isNotEmpty
@ -117,23 +122,23 @@ class HomeCubit extends Cubit<HomeState> {
// selectSpace(spaces!.first)
selectedSpace = spaces!.first
: null;
emit(GetSpacesLoaded(spaces!));
emitSafe(GetSpacesLoaded(spaces!));
} on DioException catch (e) {
emit(GetSpacesError(ServerFailure.fromDioError(e).errMessage));
emitSafe(GetSpacesError(ServerFailure.fromDioError(e).errMessage));
}
}
fetchRooms(SpaceModel space) async {
emit(GetSpaceRoomsLoading());
emitSafe(GetSpaceRoomsLoading());
try {
space.rooms = await SpacesAPI.getRoomsBySpaceId(space.id!);
if (space.rooms != null) {
emit(GetSpaceRoomsLoaded(space.rooms!));
emitSafe(GetSpaceRoomsLoaded(space.rooms!));
} else {
emit(GetSpaceRoomsError("No rooms found"));
emitSafe(GetSpaceRoomsError("No rooms found"));
}
} on DioException catch (e) {
emit(GetSpacesError(ServerFailure.fromDioError(e).errMessage));
emitSafe(GetSpacesError(ServerFailure.fromDioError(e).errMessage));
}
}
@ -262,7 +267,7 @@ class HomeCubit extends Cubit<HomeState> {
const DashboardView(),
// const LayoutPage(),
BlocProvider(
create: (context) => DevicesCubit(),
create: (context) => DevicesCubit.getInstance(),
child: const DevicesViewBody(),
),
const SceneView(),
@ -272,7 +277,7 @@ class HomeCubit extends Cubit<HomeState> {
void updatePageIndex(int index) {
pageIndex = index;
emit(NavChangePage());
emitSafe(NavChangePage());
}
}