mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-11-27 20:54:55 +00:00
Added a new method in DevicesCubit to fetch devices by room ID and updated related classes and API calls to support this functionality.
288 lines
7.3 KiB
Dart
288 lines
7.3 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:flutter_svg/flutter_svg.dart';
|
|
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
|
|
import 'package:syncrow_app/features/app_layout/view/widgets/app_bar_home_dropdown.dart';
|
|
import 'package:syncrow_app/features/dashboard/view/dashboard_view.dart';
|
|
import 'package:syncrow_app/features/devices/model/room_model.dart';
|
|
import 'package:syncrow_app/features/devices/view/widgets/devices_view_body.dart';
|
|
import 'package:syncrow_app/features/menu/view/menu_view.dart';
|
|
import 'package:syncrow_app/features/scene/view/scene_view.dart';
|
|
import 'package:syncrow_app/generated/assets.dart';
|
|
import 'package:syncrow_app/services/api/network_exception.dart';
|
|
import 'package:syncrow_app/services/api/spaces_api.dart';
|
|
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
|
|
static HomeCubit? _instance;
|
|
HomeCubit._() : super(HomeInitial()) {
|
|
if (selectedSpace == null) {
|
|
fetchSpaces().then((value) {
|
|
if (selectedSpace != null) {
|
|
print('selectedSpace: ${selectedSpace!.name}');
|
|
fetchRooms(selectedSpace!);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
static HomeCubit getInstance() {
|
|
// If an instance already exists, return it
|
|
_instance ??= HomeCubit._();
|
|
return _instance!;
|
|
}
|
|
|
|
static HomeCubit get(context) => BlocProvider.of(context);
|
|
|
|
List<SpaceModel>? spaces;
|
|
|
|
SpaceModel? selectedSpace;
|
|
|
|
RoomModel? selectedRoom;
|
|
|
|
PageController devicesPageController = PageController();
|
|
|
|
PageController roomsPageController = PageController();
|
|
|
|
var duration = const Duration(milliseconds: 300);
|
|
|
|
// selectSpace(SpaceModel space) async {
|
|
// selectedSpace = space;
|
|
// emit(SpaceSelected(space));
|
|
// }
|
|
|
|
changeSelectedSpace(SpaceModel space) {
|
|
selectedSpace = space;
|
|
emit(SpaceSelected(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(GetSpacesLoading());
|
|
try {
|
|
spaces = await SpacesAPI.getSpaces();
|
|
selectedSpace = spaces!.isNotEmpty
|
|
?
|
|
// selectSpace(spaces!.first)
|
|
selectedSpace = spaces!.first
|
|
: null;
|
|
emit(GetSpacesLoaded(spaces!));
|
|
} on DioException catch (e) {
|
|
emit(GetSpacesError(ServerFailure.fromDioError(e).errMessage));
|
|
}
|
|
}
|
|
|
|
fetchRooms(SpaceModel space) async {
|
|
emit(GetSpaceRoomsLoading());
|
|
try {
|
|
space.rooms = await SpacesAPI.getRoomsBySpaceId(space.id!);
|
|
if (space.rooms != null) {
|
|
emit(GetSpaceRoomsLoaded(space.rooms!));
|
|
} else {
|
|
emit(GetSpaceRoomsError("No rooms found"));
|
|
}
|
|
} on DioException catch (e) {
|
|
emit(GetSpacesError(ServerFailure.fromDioError(e).errMessage));
|
|
}
|
|
}
|
|
|
|
////////////////////////////////////////Nav////////////////////////////////////////
|
|
|
|
static clear() {
|
|
pageIndex = 0;
|
|
}
|
|
|
|
static int pageIndex = 0;
|
|
|
|
static Map<String, List<Widget>> appBarActions = {
|
|
'Dashboard': [
|
|
IconButton(
|
|
icon: const Icon(
|
|
Icons.add,
|
|
size: 25,
|
|
),
|
|
style: ButtonStyle(
|
|
foregroundColor:
|
|
MaterialStateProperty.all(ColorsManager.textPrimaryColor),
|
|
),
|
|
onPressed: () {},
|
|
),
|
|
],
|
|
'Devices': [
|
|
// IconButton(
|
|
// icon: Image.asset(
|
|
// Assets.iconsFilter,
|
|
// height: 20,
|
|
// width: 20,
|
|
// ),
|
|
// onPressed: () {},
|
|
// ),
|
|
IconButton(
|
|
icon: const Icon(
|
|
Icons.add,
|
|
size: 25,
|
|
),
|
|
style: ButtonStyle(
|
|
foregroundColor:
|
|
MaterialStateProperty.all(ColorsManager.textPrimaryColor),
|
|
),
|
|
onPressed: () {},
|
|
),
|
|
IconButton(
|
|
icon: const Icon(
|
|
Icons.more_vert,
|
|
size: 25,
|
|
),
|
|
style: ButtonStyle(
|
|
foregroundColor:
|
|
MaterialStateProperty.all(ColorsManager.textPrimaryColor),
|
|
),
|
|
onPressed: () {},
|
|
),
|
|
],
|
|
'Routine': [
|
|
// IconButton(
|
|
// icon: Image.asset(
|
|
// Assets.iconsFilter,
|
|
// height: 20,
|
|
// width: 20,
|
|
// ),
|
|
// onPressed: () {},
|
|
// ),
|
|
IconButton(
|
|
icon: const Icon(
|
|
Icons.add,
|
|
size: 25,
|
|
),
|
|
style: ButtonStyle(
|
|
foregroundColor:
|
|
MaterialStateProperty.all(ColorsManager.textPrimaryColor),
|
|
),
|
|
onPressed: () {},
|
|
),
|
|
IconButton(
|
|
icon: const Icon(
|
|
Icons.more_vert,
|
|
size: 25,
|
|
),
|
|
style: ButtonStyle(
|
|
foregroundColor:
|
|
MaterialStateProperty.all(ColorsManager.textPrimaryColor),
|
|
),
|
|
onPressed: () {},
|
|
),
|
|
],
|
|
'Menu': [
|
|
IconButton(
|
|
icon: SvgPicture.asset(
|
|
Assets.iconsScan,
|
|
height: 20,
|
|
width: 20,
|
|
),
|
|
onPressed: () {},
|
|
),
|
|
],
|
|
};
|
|
|
|
static Map<String, Widget?> appBarLeading = {
|
|
'Dashboard': const AppBarHomeDropdown(),
|
|
'Devices': const AppBarHomeDropdown(),
|
|
'Routine': const AppBarHomeDropdown(),
|
|
'Menu': Padding(
|
|
padding: const EdgeInsets.only(left: 15),
|
|
child: Image.asset(
|
|
Assets.imagesLogoHorizontal,
|
|
height: 15,
|
|
width: 100,
|
|
fit: BoxFit.scaleDown,
|
|
),
|
|
),
|
|
};
|
|
|
|
static var bottomNavItems = [
|
|
defaultBottomNavBarItem(icon: Assets.iconsDashboard, label: 'Dashboard'),
|
|
// defaultBottomNavBarItem(icon: Assets.iconslayout, label: 'Layout'),
|
|
defaultBottomNavBarItem(icon: Assets.iconsDevices, label: 'Devices'),
|
|
defaultBottomNavBarItem(icon: Assets.iconsRoutines, label: 'Routine'),
|
|
defaultBottomNavBarItem(icon: Assets.iconsMenu, label: 'Menu'),
|
|
];
|
|
|
|
final List<Widget> pages = [
|
|
const DashboardView(),
|
|
// const LayoutPage(),
|
|
const DevicesViewBody(),
|
|
const SceneView(),
|
|
const MenuView(),
|
|
];
|
|
|
|
void updatePageIndex(int index) {
|
|
pageIndex = index;
|
|
|
|
emit(NavChangePage());
|
|
}
|
|
}
|
|
|
|
BottomNavigationBarItem defaultBottomNavBarItem(
|
|
{required String icon, required String label}) {
|
|
return BottomNavigationBarItem(
|
|
icon: SvgPicture.asset(icon),
|
|
activeIcon: SvgPicture.asset(
|
|
icon.replaceAll('.svg', '-fill.svg'),
|
|
colorFilter: const ColorFilter.mode(
|
|
ColorsManager.primaryColor,
|
|
BlendMode.srcIn,
|
|
),
|
|
),
|
|
label: label,
|
|
);
|
|
}
|