From c0bfd24751971754ca3c0113d40a02da6f946fa8 Mon Sep 17 00:00:00 2001 From: Mohammad Salameh Date: Thu, 21 Mar 2024 13:58:20 +0300 Subject: [PATCH 1/4] Refactor device-related views and add error handling Refactor device-related views to improve code structure and readability. Add error handling for authentication token in the splash view. Remove unnecessary NavCubit from the app initialization. --- lib/features/app_layout/bloc/nav_cubit.dart | 167 ------------- lib/features/app_layout/bloc/nav_state.dart | 7 - .../app_layout/bloc/spaces_cubit.dart | 195 +++++++++++++-- .../app_layout/bloc/spaces_state.dart | 27 ++- lib/features/app_layout/view/app_layout.dart | 22 +- .../view/widgets/app_bar_home_dropdown.dart | 104 ++++---- .../app_layout/view/widgets/app_body.dart | 11 +- .../view/widgets/default_app_bar.dart | 41 ++-- .../view/widgets/default_nav_bar.dart | 8 +- lib/features/auth/bloc/auth_cubit.dart | 5 +- lib/features/devices/bloc/devices_cubit.dart | 4 +- lib/features/devices/view/devices_view.dart | 15 +- .../widgets/ACs/ac_interface_temp_unit.dart | 225 +++++++++--------- .../devices/view/widgets/ACs/acs_view.dart | 94 ++++---- .../view/widgets/curtains/curtain_view.dart | 67 +++--- .../view/widgets/devices_view_body.dart | 133 ++++++----- .../view/widgets/lights/lights_view.dart | 88 ++++--- .../lights_switches/light_switches.dart | 5 +- lib/features/splash/view/splash_view.dart | 9 +- lib/my_app.dart | 5 - 20 files changed, 607 insertions(+), 625 deletions(-) delete mode 100644 lib/features/app_layout/bloc/nav_cubit.dart delete mode 100644 lib/features/app_layout/bloc/nav_state.dart diff --git a/lib/features/app_layout/bloc/nav_cubit.dart b/lib/features/app_layout/bloc/nav_cubit.dart deleted file mode 100644 index b485ad8..0000000 --- a/lib/features/app_layout/bloc/nav_cubit.dart +++ /dev/null @@ -1,167 +0,0 @@ -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/view/widgets/app_bar_home_dropdown.dart'; -import 'package:syncrow_app/features/dashboard/view/dashboard_view.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/utils/resource_manager/color_manager.dart'; - -import '../../../generated/assets.dart'; - -part 'nav_state.dart'; - -class NavCubit extends Cubit { - NavCubit() : super(NavInitial()); - - static NavCubit of(context) => BlocProvider.of(context); - - static clear() { - pageIndex = 0; - } - - static int pageIndex = 0; - - static Map> 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 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, - ), - ), - }; - - 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 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, - ); -} diff --git a/lib/features/app_layout/bloc/nav_state.dart b/lib/features/app_layout/bloc/nav_state.dart deleted file mode 100644 index 558ef39..0000000 --- a/lib/features/app_layout/bloc/nav_state.dart +++ /dev/null @@ -1,7 +0,0 @@ -part of 'nav_cubit.dart'; - -abstract class NavState {} - -class NavInitial extends NavState {} - -class NavChangePage extends NavState {} diff --git a/lib/features/app_layout/bloc/spaces_cubit.dart b/lib/features/app_layout/bloc/spaces_cubit.dart index f2c5dbb..c763847 100644 --- a/lib/features/app_layout/bloc/spaces_cubit.dart +++ b/lib/features/app_layout/bloc/spaces_cubit.dart @@ -1,27 +1,42 @@ 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 'spaces_state.dart'; class SpacesCubit extends Cubit { SpacesCubit() : super(SpacesInitial()) { - fetchSpaces().then((value) { - if (selectedSpace != null) { - fetchRooms(selectedSpace!); + if (SpacesCubit.spaces != null) { + if (selectedSpace == null) { + fetchSpaces().then((value) { + if (selectedSpace != null) { + print('selectedSpace: ${selectedSpace!.name}'); + fetchRooms(selectedSpace!); + } + }); } - }); + } else { + fetchSpaces(); // this is for the first time + } } static SpacesCubit get(context) => BlocProvider.of(context); - static List spaces = []; + static List? spaces; - static SpaceModel? selectedSpace = spaces.isNotEmpty ? spaces.first : null; + static SpaceModel? selectedSpace; RoomModel? selectedRoom; @@ -33,7 +48,7 @@ class SpacesCubit extends Cubit { selectSpace(SpaceModel space) { selectedSpace = space; - emit(SpacesSelected(space)); + emit(SpaceSelected(space)); } roomSliderPageChanged(int index) { @@ -84,27 +99,177 @@ class SpacesCubit extends Cubit { } fetchSpaces() async { - emit(SpacesLoading()); + emit(GetSpacesLoading()); try { spaces = await SpacesAPI.getSpaces(); - spaces.isNotEmpty ? selectSpace(spaces.first) : null; - emit(SpacesLoaded(spaces)); + selectedSpace = spaces!.isNotEmpty ? selectSpace(spaces!.first) : null; + emit(GetSpacesLoaded(spaces!)); } on DioException catch (e) { - emit(SpacesError(ServerFailure.fromDioError(e).errMessage)); + emit(GetSpacesError(ServerFailure.fromDioError(e).errMessage)); } } fetchRooms(SpaceModel space) async { - emit(SpaceRoomsLoading()); + emit(GetSpaceRoomsLoading()); try { space.rooms = await SpacesAPI.getRooms(space.id!); if (space.rooms != null) { - emit(SpaceRoomsLoaded(space.rooms!)); + emit(GetSpaceRoomsLoaded(space.rooms!)); } else { - emit(SpaceRoomsError("No rooms found")); + emit(GetSpaceRoomsError("No rooms found")); } } on DioException catch (e) { - emit(SpacesError(ServerFailure.fromDioError(e).errMessage)); + emit(GetSpacesError(ServerFailure.fromDioError(e).errMessage)); } } + + ////////////////////////////////////////Nav//////////////////////////////////////// + + static clear() { + pageIndex = 0; + } + + static int pageIndex = 0; + + static Map> 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 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, + ), + ), + }; + + 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 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, + ); } diff --git a/lib/features/app_layout/bloc/spaces_state.dart b/lib/features/app_layout/bloc/spaces_state.dart index 8fd63d4..ddd144f 100644 --- a/lib/features/app_layout/bloc/spaces_state.dart +++ b/lib/features/app_layout/bloc/spaces_state.dart @@ -4,36 +4,37 @@ abstract class SpacesState {} class SpacesInitial extends SpacesState {} -class SpacesLoading extends SpacesState {} +class GetSpacesLoading extends SpacesState {} -class SpacesLoaded extends SpacesState { +class GetSpacesLoaded extends SpacesState { final List spaces; - SpacesLoaded(this.spaces); + GetSpacesLoaded(this.spaces); } -class SpacesError extends SpacesState { +class GetSpacesError extends SpacesState { final String errMessage; - SpacesError(this.errMessage); + GetSpacesError(this.errMessage); } -class SpaceRoomsLoading extends SpacesLoading {} +class GetSpaceRoomsLoading extends SpacesState {} -class SpaceRoomsLoaded extends SpacesLoading { +class GetSpaceRoomsLoaded extends SpacesState { final List rooms; - SpaceRoomsLoaded(this.rooms); + GetSpaceRoomsLoaded(this.rooms); } -class SpaceRoomsError extends SpacesError { - SpaceRoomsError(super.errMessage); +class GetSpaceRoomsError extends SpacesState { + final String errMessage; + GetSpaceRoomsError(this.errMessage); } -class SpacesSelected extends SpacesState { +class SpaceSelected extends SpacesState { final SpaceModel space; - SpacesSelected(this.space); + SpaceSelected(this.space); } class RoomSelected extends SpacesState { @@ -43,3 +44,5 @@ class RoomSelected extends SpacesState { } class RoomUnSelected extends SpacesState {} + +class NavChangePage extends SpacesState {} diff --git a/lib/features/app_layout/view/app_layout.dart b/lib/features/app_layout/view/app_layout.dart index dbb164c..4f8a9e2 100644 --- a/lib/features/app_layout/view/app_layout.dart +++ b/lib/features/app_layout/view/app_layout.dart @@ -1,7 +1,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'; @@ -26,7 +26,7 @@ class AppLayout extends StatelessWidget { ], child: BlocListener( listener: (context, state) { - if (state is SpacesError) { + if (state is GetSpacesError) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(state.errMessage), @@ -36,7 +36,7 @@ class AppLayout extends StatelessWidget { .popUntil((route) => route.settings.name == Routes.authLogin); } }, - child: BlocBuilder( + child: BlocBuilder( builder: (context, state) { return AnnotatedRegion( value: SystemUiOverlayStyle( @@ -50,15 +50,15 @@ class AppLayout extends StatelessWidget { backgroundColor: ColorsManager.backgroundColor, extendBodyBehindAppBar: true, extendBody: true, - appBar: - state is! SpacesLoading || state is! SpaceRoomsLoading - ? const DefaultAppBar() - : null, + appBar: state is! GetSpacesLoading || + state is! GetSpaceRoomsLoading + ? const DefaultAppBar() + : null, body: const AppBody(), - bottomNavigationBar: - state is! SpacesLoading || state is! SpaceRoomsLoading - ? const DefaultNavBar() - : null, + bottomNavigationBar: state is! GetSpacesLoading || + state is! GetSpaceRoomsLoading + ? const DefaultNavBar() + : null, ); }, ), diff --git a/lib/features/app_layout/view/widgets/app_bar_home_dropdown.dart b/lib/features/app_layout/view/widgets/app_bar_home_dropdown.dart index 797f2d5..aa2ac17 100644 --- a/lib/features/app_layout/view/widgets/app_bar_home_dropdown.dart +++ b/lib/features/app_layout/view/widgets/app_bar_home_dropdown.dart @@ -17,60 +17,62 @@ class AppBarHomeDropdown extends StatelessWidget { Widget build(BuildContext context) { return BlocBuilder( builder: (context, state) { - return Padding( - padding: const EdgeInsets.only(left: 10, right: 10), - child: DropdownButton( - icon: const Icon( - Icons.expand_more, - color: Colors.black, - size: 25, - ), - underline: const SizedBox.shrink(), - padding: const EdgeInsets.all(0), - borderRadius: BorderRadius.circular(20), - value: SpacesCubit.selectedSpace!.id, - items: SpacesCubit.spaces.map((space) { - return DropdownMenuItem( - value: space.id, - child: SizedBox( - width: 100, - child: Row( - mainAxisAlignment: MainAxisAlignment.start, - mainAxisSize: MainAxisSize.min, - children: [ - SvgPicture.asset( - Assets.iconsHome, - width: 25, - height: 25, - colorFilter: const ColorFilter.mode( - ColorsManager.textPrimaryColor, - BlendMode.srcIn, - ), - ), - const SizedBox(width: 5), - Expanded( - child: BodyMedium( - text: space.name ?? "??", - style: context.bodyMedium.copyWith( - fontSize: 15, - color: ColorsManager.textPrimaryColor, - overflow: TextOverflow.ellipsis, - ), - ), - ), - ], + return SpacesCubit.selectedSpace == null + ? const Center(child: BodyMedium(text: 'No Home Selected')) + : Padding( + padding: const EdgeInsets.only(left: 10, right: 10), + child: DropdownButton( + icon: const Icon( + Icons.expand_more, + color: Colors.black, + size: 25, ), + underline: const SizedBox.shrink(), + padding: const EdgeInsets.all(0), + borderRadius: BorderRadius.circular(20), + value: SpacesCubit.selectedSpace!.id, + items: SpacesCubit.spaces!.map((space) { + return DropdownMenuItem( + value: space.id, + child: SizedBox( + width: 100, + child: Row( + mainAxisAlignment: MainAxisAlignment.start, + mainAxisSize: MainAxisSize.min, + children: [ + SvgPicture.asset( + Assets.iconsHome, + width: 25, + height: 25, + colorFilter: const ColorFilter.mode( + ColorsManager.textPrimaryColor, + BlendMode.srcIn, + ), + ), + const SizedBox(width: 5), + Expanded( + child: BodyMedium( + text: space.name ?? "??", + style: context.bodyMedium.copyWith( + fontSize: 15, + color: ColorsManager.textPrimaryColor, + overflow: TextOverflow.ellipsis, + ), + ), + ), + ], + ), + ), + ); + }).toList(), + onChanged: (value) { + if (value != null) { + SpacesCubit.get(context).selectSpace(SpacesCubit.spaces! + .firstWhere((element) => element.id == value)); + } + }, ), ); - }).toList(), - onChanged: (value) { - if (value != null) { - SpacesCubit.get(context).selectSpace(SpacesCubit.spaces - .firstWhere((element) => element.id == value)); - } - }, - ), - ); }, ); } diff --git a/lib/features/app_layout/view/widgets/app_body.dart b/lib/features/app_layout/view/widgets/app_body.dart index 0ba7833..0e3ddbd 100644 --- a/lib/features/app_layout/view/widgets/app_body.dart +++ b/lib/features/app_layout/view/widgets/app_body.dart @@ -1,6 +1,6 @@ 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'; @@ -12,7 +12,7 @@ class AppBody extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocBuilder( + return BlocBuilder( builder: (context, state) { return Container( width: MediaQuery.sizeOf(context).width, @@ -28,7 +28,7 @@ class AppBody extends StatelessWidget { ), child: BlocConsumer( listener: (context, state) { - if (state is SpacesError) { + if (state is GetSpacesError) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(state.errMessage), @@ -37,8 +37,9 @@ class AppBody extends StatelessWidget { } }, builder: (context, state) { - return state is! SpacesLoading || state is! SpaceRoomsLoading - ? NavCubit.of(context).pages[NavCubit.pageIndex] + return state is! GetSpacesLoading || + state is! GetSpaceRoomsLoading + ? SpacesCubit.get(context).pages[SpacesCubit.pageIndex] : const Center(child: CircularProgressIndicator()); }, ), diff --git a/lib/features/app_layout/view/widgets/default_app_bar.dart b/lib/features/app_layout/view/widgets/default_app_bar.dart index 08924a6..78fb2ef 100644 --- a/lib/features/app_layout/view/widgets/default_app_bar.dart +++ b/lib/features/app_layout/view/widgets/default_app_bar.dart @@ -1,6 +1,6 @@ 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 'package:syncrow_app/utils/resource_manager/constants.dart'; @@ -9,27 +9,26 @@ class DefaultAppBar extends StatelessWidget implements PreferredSizeWidget { @override Widget build(BuildContext context) { - return BlocBuilder( + return BlocBuilder( builder: (context, state) { - return BlocBuilder( - builder: (context, state) { - return Padding( - padding: const EdgeInsets.only( - top: 20, - ), - child: AppBar( - backgroundColor: Colors.transparent, - leadingWidth: 150, - toolbarHeight: Constants.appBarHeight, - leading: SpacesCubit.spaces.isNotEmpty - ? NavCubit.appBarLeading[ - NavCubit().bottomNavItems[NavCubit.pageIndex].label] - : null, - actions: NavCubit.appBarActions[ - NavCubit().bottomNavItems[NavCubit.pageIndex].label], - ), - ); - }, + return Padding( + padding: const EdgeInsets.only( + top: 20, + ), + child: AppBar( + backgroundColor: Colors.transparent, + leadingWidth: 150, + toolbarHeight: Constants.appBarHeight, + leading: SpacesCubit.spaces != null + ? SpacesCubit.spaces!.isNotEmpty + ? SpacesCubit.appBarLeading[SpacesCubit() + .bottomNavItems[SpacesCubit.pageIndex] + .label] + : null + : null, + actions: SpacesCubit.appBarActions[ + SpacesCubit().bottomNavItems[SpacesCubit.pageIndex].label], + ), ); }, ); diff --git a/lib/features/app_layout/view/widgets/default_nav_bar.dart b/lib/features/app_layout/view/widgets/default_nav_bar.dart index 92515db..d44a39f 100644 --- a/lib/features/app_layout/view/widgets/default_nav_bar.dart +++ b/lib/features/app_layout/view/widgets/default_nav_bar.dart @@ -1,6 +1,6 @@ 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 'package:syncrow_app/features/devices/bloc/devices_cubit.dart'; import 'package:syncrow_app/utils/resource_manager/color_manager.dart'; @@ -13,9 +13,9 @@ class DefaultNavBar extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocBuilder( + return BlocBuilder( builder: (context, state) { - var cubit = NavCubit.of(context); + var cubit = SpacesCubit.get(context); return SizedBox( height: Constants.bottomNavBarHeight, child: BottomNavigationBar( @@ -29,7 +29,7 @@ class DefaultNavBar extends StatelessWidget { SpacesCubit.get(context).unselectRoom(); } }, - currentIndex: NavCubit.pageIndex, + currentIndex: SpacesCubit.pageIndex, selectedItemColor: ColorsManager.primaryColor, selectedLabelStyle: const TextStyle( color: ColorsManager.primaryColor, diff --git a/lib/features/auth/bloc/auth_cubit.dart b/lib/features/auth/bloc/auth_cubit.dart index cf98d3d..a5cdf2b 100644 --- a/lib/features/auth/bloc/auth_cubit.dart +++ b/lib/features/auth/bloc/auth_cubit.dart @@ -3,7 +3,8 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.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/auth/model/login_with_email_model.dart'; import 'package:syncrow_app/features/auth/model/token.dart'; import 'package:syncrow_app/features/auth/model/user_model.dart'; @@ -75,7 +76,7 @@ class AuthCubit extends Cubit { try { FlutterSecureStorage storage = const FlutterSecureStorage(); await storage.delete(key: Token.loginAccessTokenKey); - NavCubit.clear(); + SpacesCubit.clear(); emit(AuthLoggedOut()); } on DioException catch (e) { emit(AuthError(ServerFailure.fromDioError(e).errMessage)); diff --git a/lib/features/devices/bloc/devices_cubit.dart b/lib/features/devices/bloc/devices_cubit.dart index cebe17e..4376db0 100644 --- a/lib/features/devices/bloc/devices_cubit.dart +++ b/lib/features/devices/bloc/devices_cubit.dart @@ -22,7 +22,9 @@ part 'devices_state.dart'; class DevicesCubit extends Cubit { DevicesCubit() : super(DevicesInitial()) { - fetchGroups(SpacesCubit.selectedSpace!.id!); + if (SpacesCubit.selectedSpace != null) { + fetchGroups(SpacesCubit.selectedSpace!.id!); + } } bool _isClosed = false; diff --git a/lib/features/devices/view/devices_view.dart b/lib/features/devices/view/devices_view.dart index 18dc389..4bad0e1 100644 --- a/lib/features/devices/view/devices_view.dart +++ b/lib/features/devices/view/devices_view.dart @@ -8,15 +8,12 @@ class DevicesView extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocProvider( - create: (context) => DevicesCubit(), - child: BlocBuilder( - builder: (context, state) => Container( - padding: const EdgeInsets.all(8), - width: MediaQuery.sizeOf(context).width, - height: MediaQuery.sizeOf(context).height, - child: const DevicesViewBody(), - ), + return BlocBuilder( + builder: (context, state) => Container( + padding: const EdgeInsets.all(8), + width: MediaQuery.sizeOf(context).width, + height: MediaQuery.sizeOf(context).height, + child: const DevicesViewBody(), ), ); } diff --git a/lib/features/devices/view/widgets/ACs/ac_interface_temp_unit.dart b/lib/features/devices/view/widgets/ACs/ac_interface_temp_unit.dart index 7dfabbd..52c471a 100644 --- a/lib/features/devices/view/widgets/ACs/ac_interface_temp_unit.dart +++ b/lib/features/devices/view/widgets/ACs/ac_interface_temp_unit.dart @@ -22,130 +22,127 @@ class AcInterfaceTempUnit extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocProvider( - create: (context) => DevicesCubit(), - child: BlocBuilder( - builder: (context, state) { - return DefaultContainer( - child: Column( - children: [ - Expanded( - flex: 8, - child: SizedBox( - width: 300, - child: SleekCircularSlider( - appearance: CircularSliderAppearance( - customWidths: CustomSliderWidths( - progressBarWidth: 15, - trackWidth: 15, - shadowWidth: 0, + return BlocBuilder( + builder: (context, state) { + return DefaultContainer( + child: Column( + children: [ + Expanded( + flex: 8, + child: SizedBox( + width: 300, + child: SleekCircularSlider( + appearance: CircularSliderAppearance( + customWidths: CustomSliderWidths( + progressBarWidth: 15, + trackWidth: 15, + shadowWidth: 0, + ), + customColors: CustomSliderColors( + progressBarColor: + ColorsManager.primaryColor.withOpacity(.6), + trackColor: ColorsManager.greyColor, + dotColor: Colors.transparent, + ), + infoProperties: InfoProperties( + //TODO: move to strings manager + bottomLabelText: 'CURRENT TEMP', + bottomLabelStyle: context.bodyLarge.copyWith( + color: Colors.grey, + fontWeight: FontsManager.regular, ), - customColors: CustomSliderColors( - progressBarColor: - ColorsManager.primaryColor.withOpacity(.6), - trackColor: ColorsManager.greyColor, - dotColor: Colors.transparent, - ), - infoProperties: InfoProperties( - //TODO: move to strings manager - bottomLabelText: 'CURRENT TEMP', - bottomLabelStyle: context.bodyLarge.copyWith( - color: Colors.grey, - fontWeight: FontsManager.regular, - ), - modifier: (double value) { - // return '${DeviceModel.temperature.toStringAsFixed(1)}°C'; - return '${value.toStringAsFixed(1)}°C'; - }, - mainLabelStyle: context.titleLarge.copyWith( - height: 0, - letterSpacing: -3, - color: Colors.black, - fontSize: 60, - shadows: [ - Shadow( - color: Colors.black.withOpacity(0.5), - offset: const Offset(0, 3), - blurRadius: 6, - ), - ], - ), + modifier: (double value) { + // return '${DeviceModel.temperature.toStringAsFixed(1)}°C'; + return '${value.toStringAsFixed(1)}°C'; + }, + mainLabelStyle: context.titleLarge.copyWith( + height: 0, + letterSpacing: -3, + color: Colors.black, + fontSize: 60, + shadows: [ + Shadow( + color: Colors.black.withOpacity(0.5), + offset: const Offset(0, 3), + blurRadius: 6, + ), + ], ), ), - // min: DeviceModel.bounds.min, - // max: DeviceModel.bounds.max, - // initialValue: DeviceModel.temperature, - onChange: (value) { - String valueAsString = value.toStringAsFixed(1); - if (valueAsString.endsWith(".0") || - valueAsString.endsWith(".5")) { - value = double.parse(valueAsString); - // DevicesCubit.get(context) - // .setACTemp(DeviceModel, value); - } - }, ), + // min: DeviceModel.bounds.min, + // max: DeviceModel.bounds.max, + // initialValue: DeviceModel.temperature, + onChange: (value) { + String valueAsString = value.toStringAsFixed(1); + if (valueAsString.endsWith(".0") || + valueAsString.endsWith(".5")) { + value = double.parse(valueAsString); + // DevicesCubit.get(context) + // .setACTemp(DeviceModel, value); + } + }, ), ), - Expanded( - flex: 2, - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - SizedBox.square( - dimension: 24, - child: InkWell( - onTap: () { - // DevicesCubit.get(context) - // .setACTemp(DeviceModel, DeviceModel.coolTo); - // DeviceModel.coolTo -= .5; - }, - child: SvgPicture.asset( - Assets.iconsMinus, - ), + ), + Expanded( + flex: 2, + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + SizedBox.square( + dimension: 24, + child: InkWell( + onTap: () { + // DevicesCubit.get(context) + // .setACTemp(DeviceModel, DeviceModel.coolTo); + // DeviceModel.coolTo -= .5; + }, + child: SvgPicture.asset( + Assets.iconsMinus, ), ), - Column( - children: [ - BodyLarge( - // text: "${DeviceModel.coolTo}° C", - text: '24° C', - style: context.bodyLarge.copyWith( - color: - ColorsManager.primaryColor.withOpacity(0.6), - fontWeight: FontsManager.bold, - fontSize: 30, - height: 0), - ), - const BodyMedium( - text: 'COOL TO', - fontSize: 18, - ) - ], - ), - SizedBox.square( - dimension: 24, - child: InkWell( - onTap: () { - // DevicesCubit.get(context) - // .setACTemp(DeviceModel, DeviceModel.coolTo); - // DeviceModel.coolTo += .5; - }, - child: SvgPicture.asset( - Assets.iconsPlus, - height: 24, - width: 24, - ), + ), + Column( + children: [ + BodyLarge( + // text: "${DeviceModel.coolTo}° C", + text: '24° C', + style: context.bodyLarge.copyWith( + color: + ColorsManager.primaryColor.withOpacity(0.6), + fontWeight: FontsManager.bold, + fontSize: 30, + height: 0), + ), + const BodyMedium( + text: 'COOL TO', + fontSize: 18, + ) + ], + ), + SizedBox.square( + dimension: 24, + child: InkWell( + onTap: () { + // DevicesCubit.get(context) + // .setACTemp(DeviceModel, DeviceModel.coolTo); + // DeviceModel.coolTo += .5; + }, + child: SvgPicture.asset( + Assets.iconsPlus, + height: 24, + width: 24, ), ), - ], - ), - ) - ], - ), - ); - }, - ), + ), + ], + ), + ) + ], + ), + ); + }, ); } } diff --git a/lib/features/devices/view/widgets/ACs/acs_view.dart b/lib/features/devices/view/widgets/ACs/acs_view.dart index fdf0fb2..f22b8ed 100644 --- a/lib/features/devices/view/widgets/ACs/acs_view.dart +++ b/lib/features/devices/view/widgets/ACs/acs_view.dart @@ -16,61 +16,57 @@ class ACsView extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocProvider( - create: (context) => DevicesCubit(), - child: BlocBuilder( - builder: (context, state) { - return BlocBuilder( - builder: (context, state) { - DeviceModel? selectedAC; - if (DevicesCubit.get(context).getSelectedDevice() - is DeviceModel) { - selectedAC = DevicesCubit.get(context).getSelectedDevice() - as DeviceModel; - } - return AnnotatedRegion( - value: SystemUiOverlayStyle( - statusBarColor: ColorsManager.primaryColor.withOpacity(0.5), - statusBarIconBrightness: Brightness.light, - ), - child: SafeArea( - child: Scaffold( - backgroundColor: ColorsManager.backgroundColor, - extendBodyBehindAppBar: true, - extendBody: true, - appBar: const CategoryViewAppBar(), - body: 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, + return BlocBuilder( + builder: (context, state) { + return BlocBuilder( + builder: (context, state) { + DeviceModel? selectedAC; + if (DevicesCubit.get(context).getSelectedDevice() is DeviceModel) { + selectedAC = + DevicesCubit.get(context).getSelectedDevice() as DeviceModel; + } + return AnnotatedRegion( + value: SystemUiOverlayStyle( + statusBarColor: ColorsManager.primaryColor.withOpacity(0.5), + statusBarIconBrightness: Brightness.light, + ), + child: SafeArea( + child: Scaffold( + backgroundColor: ColorsManager.backgroundColor, + extendBodyBehindAppBar: true, + extendBody: true, + appBar: const CategoryViewAppBar(), + body: 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, ), - child: Padding( - padding: EdgeInsets.only( - top: Constants.appBarHeight, - left: Constants.defaultPadding, - right: Constants.defaultPadding, - ), - child: SizedBox.expand( - child: selectedAC != null - ? AcInterface(deviceModel: selectedAC) - : const ACsList(), - ), + ), + child: Padding( + padding: EdgeInsets.only( + top: Constants.appBarHeight, + left: Constants.defaultPadding, + right: Constants.defaultPadding, + ), + child: SizedBox.expand( + child: selectedAC != null + ? AcInterface(deviceModel: selectedAC) + : const ACsList(), ), ), ), ), - ); - }, - ); - }, - ), + ), + ); + }, + ); + }, ); } } diff --git a/lib/features/devices/view/widgets/curtains/curtain_view.dart b/lib/features/devices/view/widgets/curtains/curtain_view.dart index baef4d3..30d7e74 100644 --- a/lib/features/devices/view/widgets/curtains/curtain_view.dart +++ b/lib/features/devices/view/widgets/curtains/curtain_view.dart @@ -13,47 +13,44 @@ class CurtainView extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocProvider( - create: (context) => DevicesCubit(), - child: BlocBuilder( - builder: (context, state) { - return AnnotatedRegion( - value: SystemUiOverlayStyle( - statusBarColor: ColorsManager.primaryColor.withOpacity(0.5), - statusBarIconBrightness: Brightness.light, - ), - child: SafeArea( - child: Scaffold( - backgroundColor: ColorsManager.backgroundColor, - extendBodyBehindAppBar: true, - extendBody: true, - appBar: const CategoryViewAppBar(), - body: 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, + return BlocBuilder( + builder: (context, state) { + return AnnotatedRegion( + value: SystemUiOverlayStyle( + statusBarColor: ColorsManager.primaryColor.withOpacity(0.5), + statusBarIconBrightness: Brightness.light, + ), + child: SafeArea( + child: Scaffold( + backgroundColor: ColorsManager.backgroundColor, + extendBodyBehindAppBar: true, + extendBody: true, + appBar: const CategoryViewAppBar(), + body: 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, ), - child: Padding( - padding: EdgeInsets.only( - top: Constants.appBarHeight, - left: Constants.defaultPadding, - right: Constants.defaultPadding, - ), - child: const CurtainList(), + ), + child: Padding( + padding: EdgeInsets.only( + top: Constants.appBarHeight, + left: Constants.defaultPadding, + right: Constants.defaultPadding, ), + child: const CurtainList(), ), ), ), - ); - }, - ), + ), + ); + }, ); } } diff --git a/lib/features/devices/view/widgets/devices_view_body.dart b/lib/features/devices/view/widgets/devices_view_body.dart index c558633..0f4f339 100644 --- a/lib/features/devices/view/widgets/devices_view_body.dart +++ b/lib/features/devices/view/widgets/devices_view_body.dart @@ -6,6 +6,7 @@ import 'package:syncrow_app/features/devices/view/widgets/devices_view_header.da import 'package:syncrow_app/features/devices/view/widgets/room_page.dart'; import 'package:syncrow_app/features/devices/view/widgets/rooms_slider.dart'; import 'package:syncrow_app/features/devices/view/widgets/wizard_page.dart'; +import 'package:syncrow_app/features/shared_widgets/text_widgets/body_large.dart'; import 'package:syncrow_app/utils/resource_manager/constants.dart'; import '../../bloc/devices_cubit.dart'; @@ -17,73 +18,75 @@ class DevicesViewBody extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocProvider( - create: (context) => DevicesCubit(), - child: BlocBuilder( - builder: (context, state) { - return BlocBuilder( - builder: (context, state) { - //TODO : move to NavigationCubit - if (state is DevicesLoading) { - return const Center(child: CircularProgressIndicator()); - } else { - return Padding( - padding: EdgeInsets.only( - top: Constants.appBarHeight, - bottom: Constants.bottomNavBarHeight, - ), - child: Column( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - const DevicesViewHeader(), - const RoomsSlider(), - const SizedBox( - height: 10, + return BlocBuilder( + builder: (context, state) { + return BlocBuilder( + builder: (context, state) { + //TODO : move to NavigationCubit + if (state is DevicesLoading) { + return const Center(child: CircularProgressIndicator()); + } else { + return Padding( + padding: EdgeInsets.only( + top: Constants.appBarHeight, + bottom: Constants.bottomNavBarHeight, + ), + child: Column( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + const DevicesViewHeader(), + const RoomsSlider(), + const SizedBox( + height: 10, + ), + Expanded( + child: PageView( + controller: + SpacesCubit.get(context).devicesPageController, + onPageChanged: (index) { + SpacesCubit.get(context).devicesPageChanged(index); + }, + children: [ + const WizardPage(), + if (SpacesCubit.selectedSpace != null) + if (SpacesCubit.selectedSpace!.rooms != null) + ...SpacesCubit.selectedSpace!.rooms!.map( + (room) { + return RoomPage( + room: room, + ); + }, + ) + ], ), - Expanded( - child: PageView( - controller: - SpacesCubit.get(context).devicesPageController, - onPageChanged: (index) { - SpacesCubit.get(context).devicesPageChanged(index); - }, - children: [ - const WizardPage(), - if (SpacesCubit.selectedSpace != null) - if (SpacesCubit.selectedSpace!.rooms != null) - ...SpacesCubit.selectedSpace!.rooms!.map( - (room) { - return RoomPage( - room: room, - ); - }, - ) - ], - ), - ), - Padding( - padding: const EdgeInsets.symmetric( - vertical: 7, - ), - child: SmoothPageIndicator( - controller: - SpacesCubit.get(context).devicesPageController, - count: SpacesCubit.selectedSpace!.rooms!.length + 1, - effect: const WormEffect( - paintStyle: PaintingStyle.stroke, - dotHeight: 8, - dotWidth: 8, + ), + SpacesCubit.selectedSpace != null + ? Padding( + padding: const EdgeInsets.symmetric( + vertical: 7, + ), + child: SmoothPageIndicator( + controller: SpacesCubit.get(context) + .devicesPageController, + count: + SpacesCubit.selectedSpace!.rooms!.length + 1, + effect: const WormEffect( + paintStyle: PaintingStyle.stroke, + dotHeight: 8, + dotWidth: 8, + ), + ), + ) + : const Center( + child: BodyLarge(text: 'No Home Found'), ), - ), - ), - ], - ), - ); - } - }, - ); - }, - ), + ], + ), + ); + } + }, + ); + }, ); } } diff --git a/lib/features/devices/view/widgets/lights/lights_view.dart b/lib/features/devices/view/widgets/lights/lights_view.dart index 80d3a23..eaa20dc 100644 --- a/lib/features/devices/view/widgets/lights/lights_view.dart +++ b/lib/features/devices/view/widgets/lights/lights_view.dart @@ -14,58 +14,54 @@ class LightsView extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocProvider( - create: (context) => DevicesCubit(), - child: BlocBuilder( - builder: (context, state) { - return BlocBuilder( - builder: (context, state) { - DeviceModel? selectedLight; - if (DevicesCubit.get(context).getSelectedDevice() - is DeviceModel) { - selectedLight = DevicesCubit.get(context).getSelectedDevice() - as DeviceModel; + return BlocBuilder( + builder: (context, state) { + return BlocBuilder( + builder: (context, state) { + DeviceModel? selectedLight; + if (DevicesCubit.get(context).getSelectedDevice() is DeviceModel) { + selectedLight = + DevicesCubit.get(context).getSelectedDevice() as DeviceModel; + } + List lights = []; + if (DevicesCubit.allCategories![1].devices != null) { + for (var device in DevicesCubit.allCategories![1].devices!) { + lights.add(device); } - List lights = []; - if (DevicesCubit.allCategories![1].devices != null) { - for (var device in DevicesCubit.allCategories![1].devices!) { - lights.add(device); - } - } - return AnnotatedRegion( - value: SystemUiOverlayStyle( - statusBarColor: ColorsManager.primaryColor.withOpacity(0.5), - statusBarIconBrightness: Brightness.light, - ), - child: SafeArea( - child: Scaffold( - backgroundColor: ColorsManager.backgroundColor, - extendBodyBehindAppBar: true, - extendBody: true, - appBar: const CategoryViewAppBar(), - body: 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, + } + return AnnotatedRegion( + value: SystemUiOverlayStyle( + statusBarColor: ColorsManager.primaryColor.withOpacity(0.5), + statusBarIconBrightness: Brightness.light, + ), + child: SafeArea( + child: Scaffold( + backgroundColor: ColorsManager.backgroundColor, + extendBodyBehindAppBar: true, + extendBody: true, + appBar: const CategoryViewAppBar(), + body: 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, ), - child: selectedLight != null - ? LightInterface(light: selectedLight) - : LightsViewList(lights: lights), ), + child: selectedLight != null + ? LightInterface(light: selectedLight) + : LightsViewList(lights: lights), ), ), - ); - }, - ); - }, - ), + ), + ); + }, + ); + }, ); } } diff --git a/lib/features/devices/view/widgets/lights_switches/light_switches.dart b/lib/features/devices/view/widgets/lights_switches/light_switches.dart index 95e91e3..6e087b7 100644 --- a/lib/features/devices/view/widgets/lights_switches/light_switches.dart +++ b/lib/features/devices/view/widgets/lights_switches/light_switches.dart @@ -1,7 +1,8 @@ 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/devices/model/device_model.dart'; import 'package:syncrow_app/features/devices/model/function_model.dart'; import 'package:syncrow_app/features/devices/view/widgets/ACs/category_view_app_bar.dart'; @@ -26,7 +27,7 @@ class LightSwitchesView extends StatelessWidget { extendBodyBehindAppBar: true, extendBody: true, appBar: const CategoryViewAppBar(), - body: BlocBuilder( + body: BlocBuilder( builder: (context, state) { return Container( width: MediaQuery.sizeOf(context).width, diff --git a/lib/features/splash/view/splash_view.dart b/lib/features/splash/view/splash_view.dart index 99a826e..ed1fe2c 100644 --- a/lib/features/splash/view/splash_view.dart +++ b/lib/features/splash/view/splash_view.dart @@ -12,13 +12,13 @@ class SplashView extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocBuilder( - builder: (context, state) { + return BlocConsumer( + listener: (context, state) { if (state is AuthTokenSuccess) { Future.delayed(const Duration(seconds: 1), () { Navigator.pushReplacementNamed(context, Routes.homeRoute); }); - } else { + } else if (state is AuthTokenError) { Future.delayed(const Duration(seconds: 1), () { Navigator.pushReplacement( context, @@ -26,7 +26,8 @@ class SplashView extends StatelessWidget { ); }); } - + }, + builder: (context, state) { return Scaffold( body: Stack( alignment: Alignment.center, diff --git a/lib/my_app.dart b/lib/my_app.dart index 99375cc..928ddd9 100644 --- a/lib/my_app.dart +++ b/lib/my_app.dart @@ -1,6 +1,5 @@ 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/auth/bloc/auth_cubit.dart'; import 'package:syncrow_app/utils/resource_manager/color_manager.dart'; import 'package:syncrow_app/utils/resource_manager/constants.dart'; @@ -24,10 +23,6 @@ class MyApp extends StatelessWidget { BlocProvider( create: (context) => AuthCubit(), ), - BlocProvider( - lazy: false, - create: (context) => NavCubit(), - ), ], child: MaterialApp( debugShowCheckedModeBanner: false, From e17c0af144cd898803c52dd5484b0c445b058344 Mon Sep 17 00:00:00 2001 From: Mohammad Salameh Date: Sun, 24 Mar 2024 14:43:13 +0300 Subject: [PATCH 2/4] Add HomeCubit and HomeState classes This commit adds the HomeCubit class along with its corresponding HomeState class. It also includes necessary imports and updates references to the previously used SpacesCubit to the new HomeCubit in various files. handled the multible onCreate -- HomeCubit --- .../{spaces_cubit.dart => home_cubit.dart} | 12 +++++----- .../{spaces_state.dart => home_state.dart} | 2 +- lib/features/app_layout/view/app_layout.dart | 10 ++++---- .../view/widgets/app_bar_home_dropdown.dart | 12 +++++----- .../app_layout/view/widgets/app_body.dart | 8 +++---- .../view/widgets/default_app_bar.dart | 17 +++++++------ .../view/widgets/default_nav_bar.dart | 14 +++++------ lib/features/auth/bloc/auth_cubit.dart | 6 ++--- lib/features/devices/bloc/devices_cubit.dart | 6 ++--- .../devices/view/widgets/ACs/acs_list.dart | 2 +- .../view/widgets/devices_view_body.dart | 23 +++++++++--------- .../lights/light_interface_recent_color.dart | 4 ++-- .../lights_switches/light_switches.dart | 4 ++-- .../devices/view/widgets/rooms_slider.dart | 24 +++++++++---------- lib/features/splash/view/splash_view.dart | 5 ++-- 15 files changed, 72 insertions(+), 77 deletions(-) rename lib/features/app_layout/bloc/{spaces_cubit.dart => home_cubit.dart} (96%) rename lib/features/app_layout/bloc/{spaces_state.dart => home_state.dart} (96%) diff --git a/lib/features/app_layout/bloc/spaces_cubit.dart b/lib/features/app_layout/bloc/home_cubit.dart similarity index 96% rename from lib/features/app_layout/bloc/spaces_cubit.dart rename to lib/features/app_layout/bloc/home_cubit.dart index c763847..17cedeb 100644 --- a/lib/features/app_layout/bloc/spaces_cubit.dart +++ b/lib/features/app_layout/bloc/home_cubit.dart @@ -14,11 +14,11 @@ 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 'spaces_state.dart'; +part 'home_state.dart'; -class SpacesCubit extends Cubit { - SpacesCubit() : super(SpacesInitial()) { - if (SpacesCubit.spaces != null) { +class HomeCubit extends Cubit { + HomeCubit() : super(SpacesInitial()) { + if (HomeCubit.spaces != null) { if (selectedSpace == null) { fetchSpaces().then((value) { if (selectedSpace != null) { @@ -32,7 +32,7 @@ class SpacesCubit extends Cubit { } } - static SpacesCubit get(context) => BlocProvider.of(context); + static HomeCubit get(context) => BlocProvider.of(context); static List? spaces; @@ -236,7 +236,7 @@ class SpacesCubit extends Cubit { ), }; - var bottomNavItems = [ + static var bottomNavItems = [ defaultBottomNavBarItem(icon: Assets.iconsDashboard, label: 'Dashboard'), // defaultBottomNavBarItem(icon: Assets.iconslayout, label: 'Layout'), defaultBottomNavBarItem(icon: Assets.iconsDevices, label: 'Devices'), diff --git a/lib/features/app_layout/bloc/spaces_state.dart b/lib/features/app_layout/bloc/home_state.dart similarity index 96% rename from lib/features/app_layout/bloc/spaces_state.dart rename to lib/features/app_layout/bloc/home_state.dart index ddd144f..017fcf9 100644 --- a/lib/features/app_layout/bloc/spaces_state.dart +++ b/lib/features/app_layout/bloc/home_state.dart @@ -1,4 +1,4 @@ -part of 'spaces_cubit.dart'; +part of 'home_cubit.dart'; abstract class SpacesState {} diff --git a/lib/features/app_layout/view/app_layout.dart b/lib/features/app_layout/view/app_layout.dart index 4f8a9e2..6b7d0c4 100644 --- a/lib/features/app_layout/view/app_layout.dart +++ b/lib/features/app_layout/view/app_layout.dart @@ -2,7 +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/spaces_cubit.dart'; +import 'package:syncrow_app/features/app_layout/bloc/home_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'; @@ -18,13 +18,13 @@ class AppLayout extends StatelessWidget { return MultiBlocProvider( providers: [ BlocProvider( - create: (context) => SpacesCubit(), + create: (context) => HomeCubit(), ), BlocProvider( create: (context) => DevicesCubit(), ), ], - child: BlocListener( + child: BlocListener( listener: (context, state) { if (state is GetSpacesError) { ScaffoldMessenger.of(context).showSnackBar( @@ -36,7 +36,7 @@ class AppLayout extends StatelessWidget { .popUntil((route) => route.settings.name == Routes.authLogin); } }, - child: BlocBuilder( + child: BlocBuilder( builder: (context, state) { return AnnotatedRegion( value: SystemUiOverlayStyle( @@ -44,7 +44,7 @@ class AppLayout extends StatelessWidget { statusBarIconBrightness: Brightness.light, ), child: SafeArea( - child: BlocBuilder( + child: BlocBuilder( builder: (context, state) { return Scaffold( backgroundColor: ColorsManager.backgroundColor, diff --git a/lib/features/app_layout/view/widgets/app_bar_home_dropdown.dart b/lib/features/app_layout/view/widgets/app_bar_home_dropdown.dart index aa2ac17..edbb684 100644 --- a/lib/features/app_layout/view/widgets/app_bar_home_dropdown.dart +++ b/lib/features/app_layout/view/widgets/app_bar_home_dropdown.dart @@ -1,7 +1,7 @@ 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/bloc/spaces_cubit.dart'; +import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart'; import 'package:syncrow_app/utils/context_extension.dart'; import 'package:syncrow_app/utils/resource_manager/color_manager.dart'; @@ -15,9 +15,9 @@ class AppBarHomeDropdown extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocBuilder( + return BlocBuilder( builder: (context, state) { - return SpacesCubit.selectedSpace == null + return HomeCubit.selectedSpace == null ? const Center(child: BodyMedium(text: 'No Home Selected')) : Padding( padding: const EdgeInsets.only(left: 10, right: 10), @@ -30,8 +30,8 @@ class AppBarHomeDropdown extends StatelessWidget { underline: const SizedBox.shrink(), padding: const EdgeInsets.all(0), borderRadius: BorderRadius.circular(20), - value: SpacesCubit.selectedSpace!.id, - items: SpacesCubit.spaces!.map((space) { + value: HomeCubit.selectedSpace!.id, + items: HomeCubit.spaces!.map((space) { return DropdownMenuItem( value: space.id, child: SizedBox( @@ -67,7 +67,7 @@ class AppBarHomeDropdown extends StatelessWidget { }).toList(), onChanged: (value) { if (value != null) { - SpacesCubit.get(context).selectSpace(SpacesCubit.spaces! + HomeCubit.get(context).selectSpace(HomeCubit.spaces! .firstWhere((element) => element.id == value)); } }, diff --git a/lib/features/app_layout/view/widgets/app_body.dart b/lib/features/app_layout/view/widgets/app_body.dart index 0e3ddbd..e4ea499 100644 --- a/lib/features/app_layout/view/widgets/app_body.dart +++ b/lib/features/app_layout/view/widgets/app_body.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:syncrow_app/features/app_layout/bloc/spaces_cubit.dart'; +import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; import '../../../../generated/assets.dart'; @@ -12,7 +12,7 @@ class AppBody extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocBuilder( + return BlocBuilder( builder: (context, state) { return Container( width: MediaQuery.sizeOf(context).width, @@ -26,7 +26,7 @@ class AppBody extends StatelessWidget { opacity: 0.4, ), ), - child: BlocConsumer( + child: BlocConsumer( listener: (context, state) { if (state is GetSpacesError) { ScaffoldMessenger.of(context).showSnackBar( @@ -39,7 +39,7 @@ class AppBody extends StatelessWidget { builder: (context, state) { return state is! GetSpacesLoading || state is! GetSpaceRoomsLoading - ? SpacesCubit.get(context).pages[SpacesCubit.pageIndex] + ? HomeCubit.get(context).pages[HomeCubit.pageIndex] : const Center(child: CircularProgressIndicator()); }, ), diff --git a/lib/features/app_layout/view/widgets/default_app_bar.dart b/lib/features/app_layout/view/widgets/default_app_bar.dart index 78fb2ef..7812e22 100644 --- a/lib/features/app_layout/view/widgets/default_app_bar.dart +++ b/lib/features/app_layout/view/widgets/default_app_bar.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:syncrow_app/features/app_layout/bloc/spaces_cubit.dart'; +import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; import 'package:syncrow_app/utils/resource_manager/constants.dart'; class DefaultAppBar extends StatelessWidget implements PreferredSizeWidget { @@ -9,7 +9,7 @@ class DefaultAppBar extends StatelessWidget implements PreferredSizeWidget { @override Widget build(BuildContext context) { - return BlocBuilder( + return BlocBuilder( builder: (context, state) { return Padding( padding: const EdgeInsets.only( @@ -19,15 +19,14 @@ class DefaultAppBar extends StatelessWidget implements PreferredSizeWidget { backgroundColor: Colors.transparent, leadingWidth: 150, toolbarHeight: Constants.appBarHeight, - leading: SpacesCubit.spaces != null - ? SpacesCubit.spaces!.isNotEmpty - ? SpacesCubit.appBarLeading[SpacesCubit() - .bottomNavItems[SpacesCubit.pageIndex] - .label] + leading: HomeCubit.spaces != null + ? HomeCubit.spaces!.isNotEmpty + ? HomeCubit.appBarLeading[ + HomeCubit.bottomNavItems[HomeCubit.pageIndex].label] : null : null, - actions: SpacesCubit.appBarActions[ - SpacesCubit().bottomNavItems[SpacesCubit.pageIndex].label], + actions: HomeCubit.appBarActions[ + HomeCubit.bottomNavItems[HomeCubit.pageIndex].label], ), ); }, diff --git a/lib/features/app_layout/view/widgets/default_nav_bar.dart b/lib/features/app_layout/view/widgets/default_nav_bar.dart index d44a39f..21a01f4 100644 --- a/lib/features/app_layout/view/widgets/default_nav_bar.dart +++ b/lib/features/app_layout/view/widgets/default_nav_bar.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:syncrow_app/features/app_layout/bloc/spaces_cubit.dart'; +import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart'; import 'package:syncrow_app/utils/resource_manager/color_manager.dart'; import 'package:syncrow_app/utils/resource_manager/constants.dart'; @@ -13,9 +13,9 @@ class DefaultNavBar extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocBuilder( + return BlocBuilder( builder: (context, state) { - var cubit = SpacesCubit.get(context); + var cubit = HomeCubit.get(context); return SizedBox( height: Constants.bottomNavBarHeight, child: BottomNavigationBar( @@ -25,11 +25,11 @@ class DefaultNavBar extends StatelessWidget { if (DevicesCubit.get(context).chosenCategoryView != null) { DevicesCubit().clearCategoriesSelection(context); } - if (SpacesCubit.get(context).selectedRoom != null) { - SpacesCubit.get(context).unselectRoom(); + if (HomeCubit.get(context).selectedRoom != null) { + HomeCubit.get(context).unselectRoom(); } }, - currentIndex: SpacesCubit.pageIndex, + currentIndex: HomeCubit.pageIndex, selectedItemColor: ColorsManager.primaryColor, selectedLabelStyle: const TextStyle( color: ColorsManager.primaryColor, @@ -39,7 +39,7 @@ class DefaultNavBar extends StatelessWidget { unselectedItemColor: Colors.grey, type: BottomNavigationBarType.fixed, elevation: 0, - items: cubit.bottomNavItems, + items: HomeCubit.bottomNavItems, ), ); }, diff --git a/lib/features/auth/bloc/auth_cubit.dart b/lib/features/auth/bloc/auth_cubit.dart index a5cdf2b..d966e90 100644 --- a/lib/features/auth/bloc/auth_cubit.dart +++ b/lib/features/auth/bloc/auth_cubit.dart @@ -3,7 +3,7 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; -import 'package:syncrow_app/features/app_layout/bloc/spaces_cubit.dart'; +import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; import 'package:syncrow_app/features/auth/model/login_with_email_model.dart'; import 'package:syncrow_app/features/auth/model/token.dart'; @@ -42,8 +42,6 @@ class AuthCubit extends Cubit { static Token token = Token.emptyConstructor(); - // (FlutterSecureStorage().read(key :'token')); - login() async { emit(AuthLoading()); try { @@ -76,7 +74,7 @@ class AuthCubit extends Cubit { try { FlutterSecureStorage storage = const FlutterSecureStorage(); await storage.delete(key: Token.loginAccessTokenKey); - SpacesCubit.clear(); + HomeCubit.clear(); emit(AuthLoggedOut()); } on DioException catch (e) { emit(AuthError(ServerFailure.fromDioError(e).errMessage)); diff --git a/lib/features/devices/bloc/devices_cubit.dart b/lib/features/devices/bloc/devices_cubit.dart index 4376db0..ddb130b 100644 --- a/lib/features/devices/bloc/devices_cubit.dart +++ b/lib/features/devices/bloc/devices_cubit.dart @@ -3,7 +3,7 @@ import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:syncrow_app/features/app_layout/bloc/spaces_cubit.dart'; +import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; import 'package:syncrow_app/features/app_layout/model/space_model.dart'; import 'package:syncrow_app/features/devices/model/device_category_model.dart'; import 'package:syncrow_app/features/devices/model/device_control_model.dart'; @@ -22,8 +22,8 @@ part 'devices_state.dart'; class DevicesCubit extends Cubit { DevicesCubit() : super(DevicesInitial()) { - if (SpacesCubit.selectedSpace != null) { - fetchGroups(SpacesCubit.selectedSpace!.id!); + if (HomeCubit.selectedSpace != null) { + fetchGroups(HomeCubit.selectedSpace!.id!); } } bool _isClosed = false; diff --git a/lib/features/devices/view/widgets/ACs/acs_list.dart b/lib/features/devices/view/widgets/ACs/acs_list.dart index 362118c..f8175f6 100644 --- a/lib/features/devices/view/widgets/ACs/acs_list.dart +++ b/lib/features/devices/view/widgets/ACs/acs_list.dart @@ -43,7 +43,7 @@ class ACsList extends StatelessWidget { itemBuilder: (context, index) { DeviceModel ac = DevicesCubit.get(context) .chosenCategory! - .devices![index] as DeviceModel; + .devices![index]; return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ diff --git a/lib/features/devices/view/widgets/devices_view_body.dart b/lib/features/devices/view/widgets/devices_view_body.dart index 0f4f339..4345611 100644 --- a/lib/features/devices/view/widgets/devices_view_body.dart +++ b/lib/features/devices/view/widgets/devices_view_body.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:smooth_page_indicator/smooth_page_indicator.dart'; -import 'package:syncrow_app/features/app_layout/bloc/spaces_cubit.dart'; +import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; import 'package:syncrow_app/features/devices/view/widgets/devices_view_header.dart'; import 'package:syncrow_app/features/devices/view/widgets/room_page.dart'; import 'package:syncrow_app/features/devices/view/widgets/rooms_slider.dart'; @@ -18,7 +18,7 @@ class DevicesViewBody extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocBuilder( + return BlocBuilder( builder: (context, state) { return BlocBuilder( builder: (context, state) { @@ -42,15 +42,15 @@ class DevicesViewBody extends StatelessWidget { Expanded( child: PageView( controller: - SpacesCubit.get(context).devicesPageController, + HomeCubit.get(context).devicesPageController, onPageChanged: (index) { - SpacesCubit.get(context).devicesPageChanged(index); + HomeCubit.get(context).devicesPageChanged(index); }, children: [ const WizardPage(), - if (SpacesCubit.selectedSpace != null) - if (SpacesCubit.selectedSpace!.rooms != null) - ...SpacesCubit.selectedSpace!.rooms!.map( + if (HomeCubit.selectedSpace != null) + if (HomeCubit.selectedSpace!.rooms != null) + ...HomeCubit.selectedSpace!.rooms!.map( (room) { return RoomPage( room: room, @@ -60,16 +60,15 @@ class DevicesViewBody extends StatelessWidget { ], ), ), - SpacesCubit.selectedSpace != null + HomeCubit.selectedSpace != null ? Padding( padding: const EdgeInsets.symmetric( vertical: 7, ), child: SmoothPageIndicator( - controller: SpacesCubit.get(context) - .devicesPageController, - count: - SpacesCubit.selectedSpace!.rooms!.length + 1, + controller: + HomeCubit.get(context).devicesPageController, + count: HomeCubit.selectedSpace!.rooms!.length + 1, effect: const WormEffect( paintStyle: PaintingStyle.stroke, dotHeight: 8, diff --git a/lib/features/devices/view/widgets/lights/light_interface_recent_color.dart b/lib/features/devices/view/widgets/lights/light_interface_recent_color.dart index ccc58e7..614d09a 100644 --- a/lib/features/devices/view/widgets/lights/light_interface_recent_color.dart +++ b/lib/features/devices/view/widgets/lights/light_interface_recent_color.dart @@ -41,8 +41,8 @@ class LightInterfaceRecentColor extends StatelessWidget { margin: const EdgeInsets.only(right: 10), width: 40, height: 40, - decoration: ShapeDecoration( - shape: const CircleBorder(), + decoration: const ShapeDecoration( + shape: CircleBorder(), // color: Color(light.recentColors[index]), ), ), diff --git a/lib/features/devices/view/widgets/lights_switches/light_switches.dart b/lib/features/devices/view/widgets/lights_switches/light_switches.dart index 6e087b7..86e5f52 100644 --- a/lib/features/devices/view/widgets/lights_switches/light_switches.dart +++ b/lib/features/devices/view/widgets/lights_switches/light_switches.dart @@ -1,7 +1,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/spaces_cubit.dart'; +import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; import 'package:syncrow_app/features/devices/model/device_model.dart'; import 'package:syncrow_app/features/devices/model/function_model.dart'; @@ -27,7 +27,7 @@ class LightSwitchesView extends StatelessWidget { extendBodyBehindAppBar: true, extendBody: true, appBar: const CategoryViewAppBar(), - body: BlocBuilder( + body: BlocBuilder( builder: (context, state) { return Container( width: MediaQuery.sizeOf(context).width, diff --git a/lib/features/devices/view/widgets/rooms_slider.dart b/lib/features/devices/view/widgets/rooms_slider.dart index 0ed7b45..7c7add3 100644 --- a/lib/features/devices/view/widgets/rooms_slider.dart +++ b/lib/features/devices/view/widgets/rooms_slider.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:syncrow_app/features/app_layout/bloc/spaces_cubit.dart'; +import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; import 'package:syncrow_app/features/shared_widgets/text_widgets/title_medium.dart'; import 'package:syncrow_app/utils/context_extension.dart'; import 'package:syncrow_app/utils/resource_manager/color_manager.dart'; @@ -13,48 +13,48 @@ class RoomsSlider extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocBuilder( + return BlocBuilder( builder: (context, state) { return SizedBox( height: 40, child: PageView( - controller: SpacesCubit.get(context).roomsPageController, + controller: HomeCubit.get(context).roomsPageController, onPageChanged: (index) { - SpacesCubit.get(context).roomSliderPageChanged(index); + HomeCubit.get(context).roomSliderPageChanged(index); }, children: [ Padding( padding: const EdgeInsets.symmetric(horizontal: 15), child: InkWell( onTap: () { - SpacesCubit.get(context).unselectRoom(); + HomeCubit.get(context).unselectRoom(); }, child: TitleMedium( text: StringsManager.wizard, style: context.titleMedium.copyWith( fontSize: 25, - color: SpacesCubit.get(context).selectedRoom == null + color: HomeCubit.get(context).selectedRoom == null ? ColorsManager.textPrimaryColor : ColorsManager.textPrimaryColor.withOpacity(.2), ), ), ), ), - if (SpacesCubit.selectedSpace != null) - if (SpacesCubit.selectedSpace!.rooms != null) - ...SpacesCubit.selectedSpace!.rooms!.map( + if (HomeCubit.selectedSpace != null) + if (HomeCubit.selectedSpace!.rooms != null) + ...HomeCubit.selectedSpace!.rooms!.map( (room) => Padding( padding: const EdgeInsets.symmetric(horizontal: 15), child: InkWell( onTap: () { - SpacesCubit.get(context).roomSliderPageChanged( - SpacesCubit.selectedSpace!.rooms!.indexOf(room)); + HomeCubit.get(context).roomSliderPageChanged( + HomeCubit.selectedSpace!.rooms!.indexOf(room)); }, child: TitleMedium( text: room.name!, style: context.titleMedium.copyWith( fontSize: 25, - color: SpacesCubit.get(context).selectedRoom == room + color: HomeCubit.get(context).selectedRoom == room ? ColorsManager.textPrimaryColor : ColorsManager.textPrimaryColor .withOpacity(.2), diff --git a/lib/features/splash/view/splash_view.dart b/lib/features/splash/view/splash_view.dart index ed1fe2c..06180a9 100644 --- a/lib/features/splash/view/splash_view.dart +++ b/lib/features/splash/view/splash_view.dart @@ -15,9 +15,8 @@ class SplashView extends StatelessWidget { return BlocConsumer( listener: (context, state) { if (state is AuthTokenSuccess) { - Future.delayed(const Duration(seconds: 1), () { - Navigator.pushReplacementNamed(context, Routes.homeRoute); - }); + Navigator.pushNamedAndRemoveUntil( + context, Routes.homeRoute, (route) => false); } else if (state is AuthTokenError) { Future.delayed(const Duration(seconds: 1), () { Navigator.pushReplacement( From 58bf70ea1aee1f186bc7ea45d4b012b62eaadc2a Mon Sep 17 00:00:00 2001 From: Mohammad Salameh Date: Sun, 24 Mar 2024 14:58:20 +0300 Subject: [PATCH 3/4] Update DefaultAppBar leading widget to show 'Create Home' when spaces is empty --- .../view/widgets/default_app_bar.dart | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/features/app_layout/view/widgets/default_app_bar.dart b/lib/features/app_layout/view/widgets/default_app_bar.dart index 7812e22..0575441 100644 --- a/lib/features/app_layout/view/widgets/default_app_bar.dart +++ b/lib/features/app_layout/view/widgets/default_app_bar.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; +import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.dart'; import 'package:syncrow_app/utils/resource_manager/constants.dart'; class DefaultAppBar extends StatelessWidget implements PreferredSizeWidget { @@ -9,26 +10,25 @@ class DefaultAppBar extends StatelessWidget implements PreferredSizeWidget { @override Widget build(BuildContext context) { - return BlocBuilder( + return BlocBuilder( builder: (context, state) { return Padding( - padding: const EdgeInsets.only( - top: 20, - ), - child: AppBar( - backgroundColor: Colors.transparent, - leadingWidth: 150, - toolbarHeight: Constants.appBarHeight, - leading: HomeCubit.spaces != null - ? HomeCubit.spaces!.isNotEmpty - ? HomeCubit.appBarLeading[ - HomeCubit.bottomNavItems[HomeCubit.pageIndex].label] - : null - : null, - actions: HomeCubit.appBarActions[ - HomeCubit.bottomNavItems[HomeCubit.pageIndex].label], - ), - ); + padding: const EdgeInsets.only( + top: 20, + ), + child: AppBar( + backgroundColor: Colors.transparent, + leadingWidth: 150, + toolbarHeight: Constants.appBarHeight, + leading: HomeCubit.spaces != null + ? HomeCubit.spaces!.isNotEmpty + ? HomeCubit.appBarLeading[ + HomeCubit.bottomNavItems[HomeCubit.pageIndex].label] + : const Center(child: BodySmall(text: 'Create Home')) + : null, + actions: HomeCubit.appBarActions[ + HomeCubit.bottomNavItems[HomeCubit.pageIndex].label], + )); }, ); } From ee095d0fdce7f7dbcde064c4be7715f89a024336 Mon Sep 17 00:00:00 2001 From: Mohammad Salameh Date: Sun, 24 Mar 2024 14:59:29 +0300 Subject: [PATCH 4/4] bug_fixing 2 --- lib/features/app_layout/bloc/home_cubit.dart | 2 +- lib/features/app_layout/bloc/home_state.dart | 24 +++++++++---------- lib/features/app_layout/view/app_layout.dart | 20 ++++++---------- .../view/widgets/app_bar_home_dropdown.dart | 2 +- .../app_layout/view/widgets/app_body.dart | 4 ++-- .../view/widgets/default_nav_bar.dart | 2 +- .../view/widgets/devices_view_body.dart | 2 +- .../lights_switches/light_switches.dart | 2 +- .../devices/view/widgets/rooms_slider.dart | 2 +- 9 files changed, 27 insertions(+), 33 deletions(-) diff --git a/lib/features/app_layout/bloc/home_cubit.dart b/lib/features/app_layout/bloc/home_cubit.dart index 17cedeb..b0e1932 100644 --- a/lib/features/app_layout/bloc/home_cubit.dart +++ b/lib/features/app_layout/bloc/home_cubit.dart @@ -16,7 +16,7 @@ import 'package:syncrow_app/utils/resource_manager/color_manager.dart'; part 'home_state.dart'; -class HomeCubit extends Cubit { +class HomeCubit extends Cubit { HomeCubit() : super(SpacesInitial()) { if (HomeCubit.spaces != null) { if (selectedSpace == null) { diff --git a/lib/features/app_layout/bloc/home_state.dart b/lib/features/app_layout/bloc/home_state.dart index 017fcf9..e5880c7 100644 --- a/lib/features/app_layout/bloc/home_state.dart +++ b/lib/features/app_layout/bloc/home_state.dart @@ -1,48 +1,48 @@ part of 'home_cubit.dart'; -abstract class SpacesState {} +abstract class HomeState {} -class SpacesInitial extends SpacesState {} +class SpacesInitial extends HomeState {} -class GetSpacesLoading extends SpacesState {} +class GetSpacesLoading extends HomeState {} -class GetSpacesLoaded extends SpacesState { +class GetSpacesLoaded extends HomeState { final List spaces; GetSpacesLoaded(this.spaces); } -class GetSpacesError extends SpacesState { +class GetSpacesError extends HomeState { final String errMessage; GetSpacesError(this.errMessage); } -class GetSpaceRoomsLoading extends SpacesState {} +class GetSpaceRoomsLoading extends HomeState {} -class GetSpaceRoomsLoaded extends SpacesState { +class GetSpaceRoomsLoaded extends HomeState { final List rooms; GetSpaceRoomsLoaded(this.rooms); } -class GetSpaceRoomsError extends SpacesState { +class GetSpaceRoomsError extends HomeState { final String errMessage; GetSpaceRoomsError(this.errMessage); } -class SpaceSelected extends SpacesState { +class SpaceSelected extends HomeState { final SpaceModel space; SpaceSelected(this.space); } -class RoomSelected extends SpacesState { +class RoomSelected extends HomeState { final RoomModel room; RoomSelected(this.room); } -class RoomUnSelected extends SpacesState {} +class RoomUnSelected extends HomeState {} -class NavChangePage extends SpacesState {} +class NavChangePage extends HomeState {} diff --git a/lib/features/app_layout/view/app_layout.dart b/lib/features/app_layout/view/app_layout.dart index 6b7d0c4..dda35a1 100644 --- a/lib/features/app_layout/view/app_layout.dart +++ b/lib/features/app_layout/view/app_layout.dart @@ -24,7 +24,7 @@ class AppLayout extends StatelessWidget { create: (context) => DevicesCubit(), ), ], - child: BlocListener( + child: BlocListener( listener: (context, state) { if (state is GetSpacesError) { ScaffoldMessenger.of(context).showSnackBar( @@ -36,7 +36,7 @@ class AppLayout extends StatelessWidget { .popUntil((route) => route.settings.name == Routes.authLogin); } }, - child: BlocBuilder( + child: BlocBuilder( builder: (context, state) { return AnnotatedRegion( value: SystemUiOverlayStyle( @@ -44,21 +44,15 @@ class AppLayout extends StatelessWidget { statusBarIconBrightness: Brightness.light, ), child: SafeArea( - child: BlocBuilder( + child: BlocBuilder( builder: (context, state) { - return Scaffold( + return const Scaffold( backgroundColor: ColorsManager.backgroundColor, extendBodyBehindAppBar: true, extendBody: true, - appBar: state is! GetSpacesLoading || - state is! GetSpaceRoomsLoading - ? const DefaultAppBar() - : null, - body: const AppBody(), - bottomNavigationBar: state is! GetSpacesLoading || - state is! GetSpaceRoomsLoading - ? const DefaultNavBar() - : null, + appBar: DefaultAppBar(), + body: AppBody(), + bottomNavigationBar: DefaultNavBar(), ); }, ), diff --git a/lib/features/app_layout/view/widgets/app_bar_home_dropdown.dart b/lib/features/app_layout/view/widgets/app_bar_home_dropdown.dart index edbb684..2b89da6 100644 --- a/lib/features/app_layout/view/widgets/app_bar_home_dropdown.dart +++ b/lib/features/app_layout/view/widgets/app_bar_home_dropdown.dart @@ -15,7 +15,7 @@ class AppBarHomeDropdown extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocBuilder( + return BlocBuilder( builder: (context, state) { return HomeCubit.selectedSpace == null ? const Center(child: BodyMedium(text: 'No Home Selected')) diff --git a/lib/features/app_layout/view/widgets/app_body.dart b/lib/features/app_layout/view/widgets/app_body.dart index e4ea499..ef2a47d 100644 --- a/lib/features/app_layout/view/widgets/app_body.dart +++ b/lib/features/app_layout/view/widgets/app_body.dart @@ -12,7 +12,7 @@ class AppBody extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocBuilder( + return BlocBuilder( builder: (context, state) { return Container( width: MediaQuery.sizeOf(context).width, @@ -26,7 +26,7 @@ class AppBody extends StatelessWidget { opacity: 0.4, ), ), - child: BlocConsumer( + child: BlocConsumer( listener: (context, state) { if (state is GetSpacesError) { ScaffoldMessenger.of(context).showSnackBar( diff --git a/lib/features/app_layout/view/widgets/default_nav_bar.dart b/lib/features/app_layout/view/widgets/default_nav_bar.dart index 21a01f4..15aef6e 100644 --- a/lib/features/app_layout/view/widgets/default_nav_bar.dart +++ b/lib/features/app_layout/view/widgets/default_nav_bar.dart @@ -13,7 +13,7 @@ class DefaultNavBar extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocBuilder( + return BlocBuilder( builder: (context, state) { var cubit = HomeCubit.get(context); return SizedBox( diff --git a/lib/features/devices/view/widgets/devices_view_body.dart b/lib/features/devices/view/widgets/devices_view_body.dart index 4345611..7837d8b 100644 --- a/lib/features/devices/view/widgets/devices_view_body.dart +++ b/lib/features/devices/view/widgets/devices_view_body.dart @@ -18,7 +18,7 @@ class DevicesViewBody extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocBuilder( + return BlocBuilder( builder: (context, state) { return BlocBuilder( builder: (context, state) { diff --git a/lib/features/devices/view/widgets/lights_switches/light_switches.dart b/lib/features/devices/view/widgets/lights_switches/light_switches.dart index 86e5f52..cadbd20 100644 --- a/lib/features/devices/view/widgets/lights_switches/light_switches.dart +++ b/lib/features/devices/view/widgets/lights_switches/light_switches.dart @@ -27,7 +27,7 @@ class LightSwitchesView extends StatelessWidget { extendBodyBehindAppBar: true, extendBody: true, appBar: const CategoryViewAppBar(), - body: BlocBuilder( + body: BlocBuilder( builder: (context, state) { return Container( width: MediaQuery.sizeOf(context).width, diff --git a/lib/features/devices/view/widgets/rooms_slider.dart b/lib/features/devices/view/widgets/rooms_slider.dart index 7c7add3..7d5795a 100644 --- a/lib/features/devices/view/widgets/rooms_slider.dart +++ b/lib/features/devices/view/widgets/rooms_slider.dart @@ -13,7 +13,7 @@ class RoomsSlider extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocBuilder( + return BlocBuilder( builder: (context, state) { return SizedBox( height: 40,