code refactoring

This commit is contained in:
Mohammad Salameh
2024-03-03 19:07:24 +03:00
parent ad511fe3af
commit d9a3f9e2a0
14 changed files with 144 additions and 48 deletions

View File

@ -23,7 +23,7 @@ class AppLayout extends StatelessWidget {
child: Scaffold( child: Scaffold(
backgroundColor: ColorsManager.backgroundColor, backgroundColor: ColorsManager.backgroundColor,
extendBodyBehindAppBar: true, extendBodyBehindAppBar: true,
// extendBody: true, extendBody: true,
appBar: DefaultAppBar(), appBar: DefaultAppBar(),
body: AppBody(), body: AppBody(),
bottomNavigationBar: DefaultNavBar(), bottomNavigationBar: DefaultNavBar(),

View File

@ -26,7 +26,8 @@ class AppBody extends StatelessWidget {
), ),
), ),
child: Padding( child: Padding(
padding: const EdgeInsets.only(top: 60, right: 15, left: 15), padding:
const EdgeInsets.only(top: 60, right: 15, left: 15, bottom: 80),
child: NavCubit.of(context).currentPage, child: NavCubit.of(context).currentPage,
), ),
); );

View File

@ -11,11 +11,6 @@ class AcCubit extends Cubit<AcState> {
static AcCubit get(context) => BlocProvider.of(context); static AcCubit get(context) => BlocProvider.of(context);
void selectAC(ACModel model) {
model.isSelected = !model.isSelected;
emit(ACSelected());
}
ACModel? getSelectedAC() { ACModel? getSelectedAC() {
for (var ac in DevicesCubit.categories[0].devices) { for (var ac in DevicesCubit.categories[0].devices) {
if (ac is ACModel && ac.isSelected) { if (ac is ACModel && ac.isSelected) {

View File

@ -179,6 +179,17 @@ class DevicesCubit extends Cubit<DevicesState> {
), ),
]; ];
void selectCategory(int index) {
for (var i = 0; i < categories.length; i++) {
if (i == index) {
categories[i].isSelected = true;
} else {
categories[i].isSelected = false;
}
}
emit(DevicesCategoryChanged());
}
Widget? get chosenCategoryView { Widget? get chosenCategoryView {
for (var category in categories) { for (var category in categories) {
if (category.isSelected) { if (category.isSelected) {
@ -188,6 +199,31 @@ class DevicesCubit extends Cubit<DevicesState> {
return null; return null;
} }
void selectDevice(DeviceModel device) {
for (var category in categories) {
for (var device in category.devices) {
if (device.isSelected) {
category.isSelected = false;
emit(DeviceSelected());
return;
}
}
}
device.isSelected = !device.isSelected;
emit(DeviceSelected());
}
DeviceModel? getSelectedDevice() {
for (var category in categories) {
for (var device in category.devices) {
if (device.isSelected) {
return device;
}
}
}
return null;
}
void changeCategorySwitchValue(DevicesCategoryModel category) { void changeCategorySwitchValue(DevicesCategoryModel category) {
if (category.devicesStatus != null) { if (category.devicesStatus != null) {
category.devicesStatus = !category.devicesStatus!; category.devicesStatus = !category.devicesStatus!;
@ -252,17 +288,6 @@ class DevicesCubit extends Cubit<DevicesState> {
} }
} }
void selectCategory(int index) {
for (var i = 0; i < categories.length; i++) {
if (i == index) {
categories[i].isSelected = true;
} else {
categories[i].isSelected = false;
}
}
emit(DevicesCategoryChanged());
}
static void clearCategoriesSelection() { static void clearCategoriesSelection() {
for (var category in categories) { for (var category in categories) {
category.isSelected = false; category.isSelected = false;

View File

@ -18,3 +18,5 @@ class DevicesCategoryChanged extends DevicesState {}
class CategorySwitchChanged extends DevicesState {} class CategorySwitchChanged extends DevicesState {}
class DeviceSwitchChanged extends DevicesState {} class DeviceSwitchChanged extends DevicesState {}
class DeviceSelected extends DevicesState {}

View File

@ -1,5 +1,4 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:syncrow_app/features/devices/bloc/AC/ac_cubit.dart';
import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart'; import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart';
import 'package:syncrow_app/features/devices/model/device_category_model.dart'; import 'package:syncrow_app/features/devices/model/device_category_model.dart';
import 'package:syncrow_app/features/devices/view/widgets/ACs/ac_mode_control_unit.dart'; import 'package:syncrow_app/features/devices/view/widgets/ACs/ac_mode_control_unit.dart';
@ -52,10 +51,7 @@ class ACsList extends StatelessWidget {
BodySmall(text: category.devices[index].name ?? ""), BodySmall(text: category.devices[index].name ?? ""),
IconButton( IconButton(
onPressed: () { onPressed: () {
var device = category.devices[index]; DevicesCubit.get(context).selectDevice(ac);
if (device is ACModel) {
AcCubit.get(context).selectAC(device);
}
}, },
icon: const Icon( icon: const Icon(
Icons.arrow_forward_ios, Icons.arrow_forward_ios,

View File

@ -4,6 +4,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/app_layout/view/widgets/default_app_bar.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/features/app_layout/view/widgets/default_nav_bar.dart';
import 'package:syncrow_app/features/devices/bloc/AC/ac_cubit.dart'; import 'package:syncrow_app/features/devices/bloc/AC/ac_cubit.dart';
import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart';
import 'package:syncrow_app/features/devices/model/ac_model.dart'; import 'package:syncrow_app/features/devices/model/ac_model.dart';
import 'package:syncrow_app/features/devices/view/widgets/ACs/ac_interface.dart'; import 'package:syncrow_app/features/devices/view/widgets/ACs/ac_interface.dart';
import 'package:syncrow_app/features/devices/view/widgets/ACs/acs_list.dart'; import 'package:syncrow_app/features/devices/view/widgets/ACs/acs_list.dart';
@ -20,7 +21,11 @@ class ACsView extends StatelessWidget {
create: (context) => AcCubit(), create: (context) => AcCubit(),
child: BlocBuilder<AcCubit, AcState>( child: BlocBuilder<AcCubit, AcState>(
builder: (context, state) { builder: (context, state) {
ACModel? selectedAC = AcCubit.get(context).getSelectedAC(); ACModel? selectedAC;
if (DevicesCubit.get(context).getSelectedDevice() is ACModel) {
selectedAC =
DevicesCubit.get(context).getSelectedDevice() as ACModel;
}
return AnnotatedRegion( return AnnotatedRegion(
value: SystemUiOverlayStyle( value: SystemUiOverlayStyle(
statusBarColor: ColorsManager.primaryColor.withOpacity(0.5), statusBarColor: ColorsManager.primaryColor.withOpacity(0.5),

View File

@ -11,7 +11,7 @@ class DevicesCategoriesView extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Column( return const Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Expanded( Expanded(

View File

@ -19,6 +19,7 @@ class DevicesViewBody extends StatelessWidget {
create: (context) => DevicesCubit(), create: (context) => DevicesCubit(),
child: BlocBuilder<DevicesCubit, DevicesState>( child: BlocBuilder<DevicesCubit, DevicesState>(
builder: (context, state) { builder: (context, state) {
//TODO : move to NavigationCubit
PageController pageController = PageController(); PageController pageController = PageController();
return state is DevicesLoading return state is DevicesLoading
? const Center(child: CircularProgressIndicator()) ? const Center(child: CircularProgressIndicator())

View File

@ -0,0 +1,44 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:syncrow_app/features/devices/model/light_model.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
class LightInterface extends StatelessWidget {
const LightInterface({super.key, required this.light});
final LightModel light;
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const SizedBox(height: 20),
Container(
constraints: const BoxConstraints(
maxHeight: 65,
),
margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
child: const DefaultContainer(child: SizedBox.expand()),
),
Container(
constraints: const BoxConstraints(
maxHeight: 310,
),
margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
child: const SizedBox.expand(),
),
Container(
constraints: const BoxConstraints(
maxHeight: 65,
),
margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
child: const SizedBox.expand(),
),
],
),
);
}
}

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart';
import 'package:syncrow_app/features/devices/view/widgets/lights/light_brightness.dart'; import 'package:syncrow_app/features/devices/view/widgets/lights/light_brightness.dart';
import '../../../../shared_widgets/devices_default_switch.dart'; import '../../../../shared_widgets/devices_default_switch.dart';
@ -31,7 +32,7 @@ class LightsList extends StatelessWidget {
BodySmall(text: lights[index].name ?? ""), BodySmall(text: lights[index].name ?? ""),
IconButton( IconButton(
onPressed: () { onPressed: () {
// LightsCubit.get(context).selectAC(device); DevicesCubit.get(context).selectDevice(lights[index]);
}, },
icon: const Icon( icon: const Icon(
Icons.arrow_forward_ios, Icons.arrow_forward_ios,

View File

@ -1,17 +1,16 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/view/widgets/lights/lights_list.dart'; import 'package:syncrow_app/features/devices/view/widgets/lights/lights_view_list.dart';
import '../../../../../generated/assets.dart'; import '../../../../../generated/assets.dart';
import '../../../../../utils/resource_manager/color_manager.dart'; import '../../../../../utils/resource_manager/color_manager.dart';
import '../../../../app_layout/view/widgets/default_app_bar.dart'; import '../../../../app_layout/view/widgets/default_app_bar.dart';
import '../../../../app_layout/view/widgets/default_nav_bar.dart'; import '../../../../app_layout/view/widgets/default_nav_bar.dart';
import '../../../../shared_widgets/text_widgets/body_small.dart';
import '../../../bloc/devices_cubit.dart'; import '../../../bloc/devices_cubit.dart';
import '../../../bloc/lights/lights_cubit.dart'; import '../../../bloc/lights/lights_cubit.dart';
import '../../../model/light_model.dart'; import '../../../model/light_model.dart';
import '../universal_switch.dart'; import 'light_interface.dart';
class LightsView extends StatelessWidget { class LightsView extends StatelessWidget {
const LightsView({super.key}); const LightsView({super.key});
@ -22,6 +21,11 @@ class LightsView extends StatelessWidget {
create: (context) => LightsCubit(), create: (context) => LightsCubit(),
child: BlocBuilder<LightsCubit, LightsState>( child: BlocBuilder<LightsCubit, LightsState>(
builder: (context, state) { builder: (context, state) {
LightModel? selectedLight;
if (DevicesCubit.get(context).getSelectedDevice() is LightModel) {
selectedLight =
DevicesCubit.get(context).getSelectedDevice() as LightModel;
}
List<LightModel> lights = []; List<LightModel> lights = [];
for (var device in DevicesCubit.categories[1].devices) { for (var device in DevicesCubit.categories[1].devices) {
if (device is LightModel) { if (device is LightModel) {
@ -51,24 +55,9 @@ class LightsView extends StatelessWidget {
opacity: 0.4, opacity: 0.4,
), ),
), ),
child: Padding( child: selectedLight != null
padding: const EdgeInsets.only( ? LightInterface(light: selectedLight)
top: 70, right: 15, left: 15, bottom: 80), : LightsViewList(lights: lights),
child: SizedBox.expand(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const BodySmall(text: "All Lights"),
UniversalSwitch(
category: DevicesCubit.categories[1],
),
LightsList(lights: lights),
],
),
),
),
),
), ),
bottomNavigationBar: const DefaultNavBar(), bottomNavigationBar: const DefaultNavBar(),
), ),

View File

@ -0,0 +1,37 @@
import 'package:flutter/material.dart';
import 'package:syncrow_app/features/devices/view/widgets/lights/lights_list.dart';
import '../../../../shared_widgets/text_widgets/body_small.dart';
import '../../../bloc/devices_cubit.dart';
import '../../../model/light_model.dart';
import '../universal_switch.dart';
class LightsViewList extends StatelessWidget {
const LightsViewList({
super.key,
required this.lights,
});
final List<LightModel> lights;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 70, right: 15, left: 15, bottom: 80),
child: SizedBox.expand(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const BodySmall(text: "All Lights"),
UniversalSwitch(
category: DevicesCubit.categories[1],
),
LightsList(lights: lights),
],
),
),
),
);
}
}

View File

@ -13,7 +13,7 @@ class SceneCubit extends Cubit<SceneState> {
void getScenes() { void getScenes() {
emit(SceneLoading()); emit(SceneLoading());
//TODO: remove this it's causing the Bad State because its being after the cubit is closed //TODO: remove this it's causing the Bad State because its being after the cubit is closed
Future.delayed(const Duration(seconds: 5), () { Future.delayed(const Duration(milliseconds: 50), () {
emit(SceneSuccess()); emit(SceneSuccess());
}); });
} }