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

@ -11,11 +11,6 @@ class AcCubit extends Cubit<AcState> {
static AcCubit get(context) => BlocProvider.of(context);
void selectAC(ACModel model) {
model.isSelected = !model.isSelected;
emit(ACSelected());
}
ACModel? getSelectedAC() {
for (var ac in DevicesCubit.categories[0].devices) {
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 {
for (var category in categories) {
if (category.isSelected) {
@ -188,6 +199,31 @@ class DevicesCubit extends Cubit<DevicesState> {
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) {
if (category.devicesStatus != null) {
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() {
for (var category in categories) {
category.isSelected = false;

View File

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

View File

@ -1,5 +1,4 @@
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/model/device_category_model.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 ?? ""),
IconButton(
onPressed: () {
var device = category.devices[index];
if (device is ACModel) {
AcCubit.get(context).selectAC(device);
}
DevicesCubit.get(context).selectDevice(ac);
},
icon: const Icon(
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_nav_bar.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/view/widgets/ACs/ac_interface.dart';
import 'package:syncrow_app/features/devices/view/widgets/ACs/acs_list.dart';
@ -20,7 +21,11 @@ class ACsView extends StatelessWidget {
create: (context) => AcCubit(),
child: BlocBuilder<AcCubit, AcState>(
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(
value: SystemUiOverlayStyle(
statusBarColor: ColorsManager.primaryColor.withOpacity(0.5),

View File

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

View File

@ -19,6 +19,7 @@ class DevicesViewBody extends StatelessWidget {
create: (context) => DevicesCubit(),
child: BlocBuilder<DevicesCubit, DevicesState>(
builder: (context, state) {
//TODO : move to NavigationCubit
PageController pageController = PageController();
return state is DevicesLoading
? 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:syncrow_app/features/devices/bloc/devices_cubit.dart';
import 'package:syncrow_app/features/devices/view/widgets/lights/light_brightness.dart';
import '../../../../shared_widgets/devices_default_switch.dart';
@ -31,7 +32,7 @@ class LightsList extends StatelessWidget {
BodySmall(text: lights[index].name ?? ""),
IconButton(
onPressed: () {
// LightsCubit.get(context).selectAC(device);
DevicesCubit.get(context).selectDevice(lights[index]);
},
icon: const Icon(
Icons.arrow_forward_ios,

View File

@ -1,17 +1,16 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.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 '../../../../../utils/resource_manager/color_manager.dart';
import '../../../../app_layout/view/widgets/default_app_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/lights/lights_cubit.dart';
import '../../../model/light_model.dart';
import '../universal_switch.dart';
import 'light_interface.dart';
class LightsView extends StatelessWidget {
const LightsView({super.key});
@ -22,6 +21,11 @@ class LightsView extends StatelessWidget {
create: (context) => LightsCubit(),
child: BlocBuilder<LightsCubit, LightsState>(
builder: (context, state) {
LightModel? selectedLight;
if (DevicesCubit.get(context).getSelectedDevice() is LightModel) {
selectedLight =
DevicesCubit.get(context).getSelectedDevice() as LightModel;
}
List<LightModel> lights = [];
for (var device in DevicesCubit.categories[1].devices) {
if (device is LightModel) {
@ -51,24 +55,9 @@ class LightsView extends StatelessWidget {
opacity: 0.4,
),
),
child: 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),
],
),
),
),
),
child: selectedLight != null
? LightInterface(light: selectedLight)
: LightsViewList(lights: lights),
),
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),
],
),
),
),
);
}
}