mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-24 21:07:54 +00:00
getting spaces and rooms from api {null checks}
This commit is contained in:
@@ -16,6 +16,13 @@ class NavCubit extends Cubit<NavState> {
|
||||
NavCubit() : super(NavInitial());
|
||||
|
||||
static NavCubit of(context) => BlocProvider.of<NavCubit>(context);
|
||||
|
||||
//functoin to do the important work when the user logs out
|
||||
static clear() {
|
||||
pageIndex = 0;
|
||||
pageController.jumpToPage(0);
|
||||
}
|
||||
|
||||
static int pageIndex = 0;
|
||||
|
||||
static Map<String, List<Widget>> appBarActions = {
|
||||
@@ -139,11 +146,10 @@ class NavCubit extends Cubit<NavState> {
|
||||
const MenuView(),
|
||||
];
|
||||
|
||||
final PageController pageController = PageController();
|
||||
static final PageController pageController = PageController();
|
||||
|
||||
void updatePageIndex(int index) {
|
||||
pageIndex = index;
|
||||
print('index: $index');
|
||||
pageController.animateToPage(index,
|
||||
duration: const Duration(milliseconds: 150), curve: Curves.easeIn);
|
||||
emit(NavChangePage());
|
||||
|
||||
@@ -10,7 +10,11 @@ part 'spaces_state.dart';
|
||||
|
||||
class SpacesCubit extends Cubit<SpacesState> {
|
||||
SpacesCubit() : super(SpacesInitial()) {
|
||||
fetchSpaces();
|
||||
fetchSpaces().then((value) {
|
||||
if (selectedSpace != null) {
|
||||
fetchRooms(selectedSpace!);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static SpacesCubit get(context) => BlocProvider.of(context);
|
||||
@@ -83,12 +87,24 @@ class SpacesCubit extends Cubit<SpacesState> {
|
||||
emit(SpacesLoading());
|
||||
try {
|
||||
spaces = await SpacesAPI.getSpaces();
|
||||
spaces.isNotEmpty ? selectSpace(spaces.first) : null;
|
||||
emit(SpacesLoaded(spaces));
|
||||
} on DioException catch (e) {
|
||||
emit(SpacesError(e.message ?? "Something went wrong"));
|
||||
throw ServerFailure.fromDioError(e);
|
||||
} catch (e) {
|
||||
emit(SpacesError(e.toString()));
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,18 @@ class SpacesError extends SpacesState {
|
||||
SpacesError(this.errMessage);
|
||||
}
|
||||
|
||||
class SpaceRoomsLoading extends SpacesLoading {}
|
||||
|
||||
class SpaceRoomsLoaded extends SpacesLoading {
|
||||
final List<RoomModel> rooms;
|
||||
|
||||
SpaceRoomsLoaded(this.rooms);
|
||||
}
|
||||
|
||||
class SpaceRoomsError extends SpacesError {
|
||||
SpaceRoomsError(super.errMessage);
|
||||
}
|
||||
|
||||
class SpacesSelected extends SpacesState {
|
||||
final SpaceModel space;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import 'package:syncrow_app/features/devices/model/room_model.dart';
|
||||
class SpaceModel {
|
||||
final int? id;
|
||||
final String? name;
|
||||
final List<RoomModel>? rooms;
|
||||
late List<RoomModel>? rooms;
|
||||
|
||||
SpaceModel({
|
||||
required this.id,
|
||||
|
||||
@@ -6,6 +6,7 @@ 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';
|
||||
import 'package:syncrow_app/navigation/routing_constants.dart';
|
||||
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
|
||||
|
||||
class AppLayout extends StatelessWidget {
|
||||
@@ -13,30 +14,51 @@ class AppLayout extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<NavCubit, NavState>(
|
||||
builder: (context, state) {
|
||||
return AnnotatedRegion(
|
||||
value: SystemUiOverlayStyle(
|
||||
statusBarColor: ColorsManager.primaryColor.withOpacity(0.5),
|
||||
statusBarIconBrightness: Brightness.light,
|
||||
),
|
||||
child: SafeArea(
|
||||
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,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
return BlocProvider(
|
||||
create: (context) => SpacesCubit(),
|
||||
child: BlocListener<SpacesCubit, SpacesState>(
|
||||
listener: (context, state) {
|
||||
if (state is SpacesError) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(state.errMessage),
|
||||
),
|
||||
);
|
||||
Navigator.of(context)
|
||||
.popUntil((route) => route.settings.name == Routes.authLogin);
|
||||
}
|
||||
},
|
||||
child: BlocBuilder<NavCubit, NavState>(
|
||||
builder: (context, state) {
|
||||
return AnnotatedRegion(
|
||||
value: SystemUiOverlayStyle(
|
||||
statusBarColor: ColorsManager.primaryColor.withOpacity(0.5),
|
||||
statusBarIconBrightness: Brightness.light,
|
||||
),
|
||||
child: SafeArea(
|
||||
child: BlocBuilder<SpacesCubit, SpacesState>(
|
||||
builder: (context, state) {
|
||||
return Scaffold(
|
||||
backgroundColor: ColorsManager.backgroundColor,
|
||||
extendBodyBehindAppBar: true,
|
||||
extendBody: true,
|
||||
appBar:
|
||||
state is! SpacesLoading || state is! SpaceRoomsLoading
|
||||
? const DefaultAppBar()
|
||||
: null,
|
||||
body: const AppBody(),
|
||||
bottomNavigationBar:
|
||||
state is! SpacesLoading || state is! SpaceRoomsLoading
|
||||
? const DefaultNavBar()
|
||||
: null,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,10 +28,10 @@ class AppBarHomeDropdown extends StatelessWidget {
|
||||
underline: const SizedBox.shrink(),
|
||||
padding: const EdgeInsets.all(0),
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
value: SpacesCubit.get(context).selectedSpace,
|
||||
value: SpacesCubit.get(context).selectedSpace!.id,
|
||||
items: SpacesCubit.spaces.map((space) {
|
||||
return DropdownMenuItem(
|
||||
value: space,
|
||||
value: space.id,
|
||||
child: SizedBox(
|
||||
width: 100,
|
||||
child: Row(
|
||||
@@ -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,
|
||||
@@ -64,7 +64,10 @@ class AppBarHomeDropdown extends StatelessWidget {
|
||||
);
|
||||
}).toList(),
|
||||
onChanged: (value) {
|
||||
SpacesCubit.get(context).selectSpace(value!);
|
||||
if (value != null) {
|
||||
SpacesCubit.get(context).selectSpace(SpacesCubit.spaces
|
||||
.firstWhere((element) => element.id == value));
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
@@ -15,39 +15,38 @@ class AppBody extends StatelessWidget {
|
||||
return BlocBuilder<NavCubit, NavState>(
|
||||
builder: (context, state) {
|
||||
return Container(
|
||||
width: MediaQuery.sizeOf(context).width,
|
||||
height: MediaQuery.sizeOf(context).height,
|
||||
decoration: const BoxDecoration(
|
||||
image: DecorationImage(
|
||||
image: AssetImage(
|
||||
Assets.imagesBackground,
|
||||
),
|
||||
fit: BoxFit.cover,
|
||||
opacity: 0.4,
|
||||
width: MediaQuery.sizeOf(context).width,
|
||||
height: MediaQuery.sizeOf(context).height,
|
||||
decoration: const BoxDecoration(
|
||||
image: DecorationImage(
|
||||
image: AssetImage(
|
||||
Assets.imagesBackground,
|
||||
),
|
||||
fit: BoxFit.cover,
|
||||
opacity: 0.4,
|
||||
),
|
||||
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,
|
||||
);
|
||||
),
|
||||
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 || state is! SpaceRoomsLoading
|
||||
? PageView(
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
controller: NavCubit.pageController,
|
||||
children: NavCubit.of(context).pages,
|
||||
)
|
||||
: const Center(child: CircularProgressIndicator());
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -4,9 +4,7 @@ import 'package:syncrow_app/features/app_layout/bloc/nav_cubit.dart';
|
||||
import 'package:syncrow_app/utils/resource_manager/constants.dart';
|
||||
|
||||
class DefaultAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||
const DefaultAppBar(this.context, {super.key});
|
||||
|
||||
final BuildContext context;
|
||||
const DefaultAppBar({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
Reference in New Issue
Block a user