Semi-implemented getting spaces feature

This commit is contained in:
Mohammad Salameh
2024-03-12 11:14:31 +03:00
parent 661d535960
commit 0f3cc453ce
25 changed files with 311 additions and 294 deletions

View File

@ -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()));
}
}
}

View File

@ -12,6 +12,12 @@ class SpacesLoaded extends SpacesState {
SpacesLoaded(this.spaces);
}
class SpacesError extends SpacesState {
final String errMessage;
SpacesError(this.errMessage);
}
class SpacesSelected extends SpacesState {
final SpaceModel space;

View File

@ -1,9 +1,9 @@
import 'package:syncrow_app/features/devices/model/room_model.dart';
class SpaceModel {
final String id;
final String name;
final List<RoomModel> rooms;
final int? id;
final String? name;
final List<RoomModel>? rooms;
SpaceModel({
required this.id,
@ -21,9 +21,9 @@ class SpaceModel {
factory SpaceModel.fromJson(Map<String, dynamic> json) {
return SpaceModel(
id: json['id'],
name: json['name'],
rooms: json['rooms'],
id: int.parse(json['homeId']),
name: json['homeName'],
rooms: [],
);
}
}

View File

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/app_layout/bloc/nav_cubit.dart';
import 'package:syncrow_app/features/app_layout/bloc/spaces_cubit.dart';
import 'package:syncrow_app/features/app_layout/view/widgets/app_body.dart';
import 'package:syncrow_app/features/app_layout/view/widgets/default_app_bar.dart';
import 'package:syncrow_app/features/app_layout/view/widgets/default_nav_bar.dart';
@ -20,13 +21,18 @@ class AppLayout extends StatelessWidget {
statusBarIconBrightness: Brightness.light,
),
child: SafeArea(
child: Scaffold(
backgroundColor: ColorsManager.backgroundColor,
extendBodyBehindAppBar: true,
extendBody: true,
appBar: DefaultAppBar(context),
body: const AppBody(),
bottomNavigationBar: const DefaultNavBar(),
child: BlocBuilder<SpacesCubit, SpacesState>(
builder: (context, state) {
return Scaffold(
backgroundColor: ColorsManager.backgroundColor,
extendBodyBehindAppBar: true,
extendBody: true,
appBar: state is SpacesLoaded ? DefaultAppBar(context) : null,
body: const AppBody(),
bottomNavigationBar:
state is SpacesLoaded ? const DefaultNavBar() : null,
);
},
),
),
);

View File

@ -50,7 +50,7 @@ class AppBarHomeDropdown extends StatelessWidget {
const SizedBox(width: 5),
Expanded(
child: BodyMedium(
text: space.name,
text: space.name ?? "",
style: context.bodyMedium.copyWith(
fontSize: 15,
color: ColorsManager.textPrimaryColor,

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/app_layout/bloc/nav_cubit.dart';
import 'package:syncrow_app/features/app_layout/bloc/spaces_cubit.dart';
import '../../../../generated/assets.dart';
@ -25,10 +26,25 @@ class AppBody extends StatelessWidget {
opacity: 0.4,
),
),
child: PageView(
physics: const NeverScrollableScrollPhysics(),
controller: NavCubit.of(context).pageController,
children: NavCubit.of(context).pages,
child: BlocConsumer<SpacesCubit, SpacesState>(
listener: (context, state) {
if (state is SpacesError) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(state.errMessage),
),
);
}
},
builder: (context, state) {
return state is! SpacesLoading
? PageView(
physics: const NeverScrollableScrollPhysics(),
controller: NavCubit.of(context).pageController,
children: NavCubit.of(context).pages,
)
: const Center(child: CircularProgressIndicator());
},
)
// NavCubit.of(context).currentPage,
);