diff --git a/lib/features/devices/bloc/ac/ac_cubit.dart b/lib/features/devices/bloc/ac/ac_cubit.dart index dcb8509..99b44c0 100644 --- a/lib/features/devices/bloc/ac/ac_cubit.dart +++ b/lib/features/devices/bloc/ac/ac_cubit.dart @@ -6,9 +6,7 @@ import 'package:syncrow_app/features/devices/model/ac_model.dart'; part 'ac_state.dart'; class AcCubit extends Cubit { - AcCubit() : super(AcInitial()) { - updateACsStatus(); - } + AcCubit() : super(AcInitial()); static AcCubit get(context) => BlocProvider.of(context); @@ -26,52 +24,6 @@ class AcCubit extends Cubit { return null; } - void turnACOn(ACModel model) { - if (model.status == false) { - model.status = true; - updateACsStatus(); - emit(ACTurnedOn()); - } - } - - void turnACOff(ACModel model) { - if (model.status == true) { - model.status = false; - updateACsStatus(); - emit(ACTurnedOff()); - } - } - - void updateACsStatus() { - bool? tempStatus = DevicesCubit.categories[0].devices[0].status; - for (var AC in DevicesCubit.categories[0].devices) { - //check if there any AC have a different status than the initial ==> turn off the universal switch - if (AC.status != tempStatus) { - DevicesCubit.categories[0].devicesStatus = null; - emit(ACsStatusChanged()); - return; - } - DevicesCubit.categories[0].devicesStatus = tempStatus; - emit(ACsStatusChanged()); - } - } - - void turnAllACsOff() { - for (var ac in DevicesCubit.categories[0].devices) { - ac.status = false; - } - updateACsStatus(); - emit(SwitchACsOff()); - } - - void turnAllACsOn() { - for (var ac in DevicesCubit.categories[0].devices) { - ac.status = true; - } - updateACsStatus(); - emit(SwitchACsOn()); - } - void setTempToAll(double temperature) { for (var element in DevicesCubit.categories[0].devices) { if (element is ACModel) { diff --git a/lib/features/devices/bloc/devices_cubit.dart b/lib/features/devices/bloc/devices_cubit.dart index 39cf86c..27b5fa0 100644 --- a/lib/features/devices/bloc/devices_cubit.dart +++ b/lib/features/devices/bloc/devices_cubit.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/devices/model/ac_model.dart'; +import 'package:syncrow_app/features/devices/model/device_model.dart'; import 'package:syncrow_app/features/devices/model/light_model.dart'; import 'package:syncrow_app/features/devices/view/widgets/curtains/curtain_view.dart'; import 'package:syncrow_app/features/devices/view/widgets/gateway/gateway_view.dart'; @@ -15,9 +16,7 @@ import '../view/widgets/lights/lights_view.dart'; part 'devices_state.dart'; class DevicesCubit extends Cubit { - DevicesCubit() : super(DevicesInitial()) { - // getCategories(); - } + DevicesCubit() : super(DevicesInitial()); void changeCategorySwitchValue(DevicesCategoryModel category) { if (category.devicesStatus != null) { @@ -37,6 +36,44 @@ class DevicesCubit extends Cubit { static DevicesCubit get(context) => BlocProvider.of(context); + void turnOnOffDevice(DeviceModel device) { + device.status = !device.status!; + DevicesCategoryModel category = + categories.firstWhere((category) => category.devices.contains(device)); + updateDevicesStatus(category); + emit(DeviceSwitchChanged()); + } + + void updateDevicesStatus(DevicesCategoryModel category) { + bool? tempStatus = category.devices[0].status; + for (var AC in category.devices) { + //check if there any AC have a different status than the initial ==> turn off the universal switch + if (AC.status != tempStatus) { + category.devicesStatus = null; + emit(DeviceSwitchChanged()); + return; + } + category.devicesStatus = tempStatus; + emit(DeviceSwitchChanged()); + } + } + + void turnAllDevicesOff(DevicesCategoryModel category) { + for (var device in category.devices) { + device.status = false; + } + updateDevicesStatus(category); + emit(CategorySwitchChanged()); + } + + void turnAllDevicesOn(DevicesCategoryModel category) { + for (var device in category.devices) { + device.status = true; + } + updateDevicesStatus(category); + emit(CategorySwitchChanged()); + } + static List categories = [ DevicesCategoryModel( devices: [ diff --git a/lib/features/devices/bloc/devices_state.dart b/lib/features/devices/bloc/devices_state.dart index f68db64..a25c3f6 100644 --- a/lib/features/devices/bloc/devices_state.dart +++ b/lib/features/devices/bloc/devices_state.dart @@ -17,8 +17,4 @@ class DevicesCategoryChanged extends DevicesState {} class CategorySwitchChanged extends DevicesState {} -class SwitchACsOff extends DevicesState {} - -class SwitchACsOn extends DevicesState {} - -class SetACsTemp extends DevicesState {} +class DeviceSwitchChanged extends DevicesState {} diff --git a/lib/features/devices/view/widgets/ACs/acs_list.dart b/lib/features/devices/view/widgets/ACs/acs_list.dart index f9465de..cf5c03b 100644 --- a/lib/features/devices/view/widgets/ACs/acs_list.dart +++ b/lib/features/devices/view/widgets/ACs/acs_list.dart @@ -3,8 +3,8 @@ 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/view/widgets/ACs/ac_mode_control_unit.dart'; import 'package:syncrow_app/features/devices/view/widgets/ACs/ac_temp_widget.dart'; -import 'package:syncrow_app/features/devices/view/widgets/ACs/universal_ac_switch.dart'; import 'package:syncrow_app/features/devices/view/widgets/ACs/universal_ac_temp.dart'; +import 'package:syncrow_app/features/devices/view/widgets/universal_switch.dart'; import 'package:syncrow_app/features/shared_widgets/devices_default_switch.dart'; import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.dart'; @@ -25,7 +25,9 @@ class ACsList extends StatelessWidget { const SizedBox(height: 10), const BodySmall(text: "All ACs"), const SizedBox(height: 5), - const UniversalACSwitch(), + UniversalSwitch( + category: DevicesCubit.categories[0], + ), const SizedBox(height: 10), const UniversalACTemp(), const SizedBox(height: 10), diff --git a/lib/features/devices/view/widgets/lights/lights_list.dart b/lib/features/devices/view/widgets/lights/lights_list.dart new file mode 100644 index 0000000..bdedcbf --- /dev/null +++ b/lib/features/devices/view/widgets/lights/lights_list.dart @@ -0,0 +1,56 @@ +import 'package:flutter/material.dart'; + +import '../../../../shared_widgets/devices_default_switch.dart'; +import '../../../../shared_widgets/text_widgets/body_small.dart'; +import '../../../model/light_model.dart'; + +class LightsList extends StatelessWidget { + const LightsList({ + super.key, + required this.lights, + }); + + final List lights; + + @override + Widget build(BuildContext context) { + return ListView.builder( + shrinkWrap: true, + physics: const NeverScrollableScrollPhysics(), + padding: const EdgeInsets.all(0), + itemCount: lights.length, + itemBuilder: (context, index) { + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + crossAxisAlignment: CrossAxisAlignment.end, + children: [ + BodySmall(text: lights[index].name ?? ""), + IconButton( + onPressed: () { + var device = lights[index]; + // LightsCubit.get(context).selectAC(device); + }, + icon: const Icon( + Icons.arrow_forward_ios, + ), + style: ButtonStyle( + padding: MaterialStateProperty.all( + const EdgeInsets.all(0), + ), + iconSize: MaterialStateProperty.all(15), + alignment: Alignment.bottomRight, + ), + ), + ], + ), + const SizedBox(height: 5), + DevicesDefaultSwitch(model: lights[index]), + ], + ); + }, + ); + } +} diff --git a/lib/features/devices/view/widgets/lights/lights_view.dart b/lib/features/devices/view/widgets/lights/lights_view.dart index 979d944..8354efe 100644 --- a/lib/features/devices/view/widgets/lights/lights_view.dart +++ b/lib/features/devices/view/widgets/lights/lights_view.dart @@ -1,7 +1,17 @@ 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 '../../../../../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'; class LightsView extends StatelessWidget { const LightsView({super.key}); @@ -11,9 +21,60 @@ class LightsView extends StatelessWidget { return BlocProvider( create: (context) => LightsCubit(), child: BlocBuilder( - builder: (context, state) => const Center( - child: Text('LightsView'), - ), + builder: (context, state) { + List lights = []; + for (var device in DevicesCubit.categories[1].devices) { + if (device is LightModel) { + 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 DefaultAppBar(), + 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: const EdgeInsets.only( + top: 60, 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), + ], + ), + ), + ), + ), + ), + bottomNavigationBar: const DefaultNavBar(), + ), + ), + ); + }, ), ); } diff --git a/lib/features/devices/view/widgets/ACs/universal_ac_switch.dart b/lib/features/devices/view/widgets/universal_switch.dart similarity index 81% rename from lib/features/devices/view/widgets/ACs/universal_ac_switch.dart rename to lib/features/devices/view/widgets/universal_switch.dart index a6abd5a..34a726d 100644 --- a/lib/features/devices/view/widgets/ACs/universal_ac_switch.dart +++ b/lib/features/devices/view/widgets/universal_switch.dart @@ -1,28 +1,31 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.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/utils/resource_manager/strings_manager.dart'; -import '../../../../../utils/resource_manager/color_manager.dart'; -import '../../../../shared_widgets/text_widgets/body_medium.dart'; -import '../../../bloc/AC/ac_cubit.dart'; +import '../../../../utils/resource_manager/color_manager.dart'; +import '../../../shared_widgets/text_widgets/body_medium.dart'; -class UniversalACSwitch extends StatelessWidget { - const UniversalACSwitch({ +class UniversalSwitch extends StatelessWidget { + const UniversalSwitch({ super.key, + required this.category, }); + final DevicesCategoryModel category; + @override Widget build(BuildContext context) { - return BlocBuilder( + return BlocBuilder( builder: (context, state) { - bool? status = DevicesCubit.categories[0].devicesStatus; + bool? status = category.devicesStatus; return Row( children: [ Expanded( child: InkWell( onTap: () { - AcCubit.get(context).turnAllACsOn(); + DevicesCubit.get(context).turnAllDevicesOn(category); }, child: Container( height: 60, @@ -54,7 +57,7 @@ class UniversalACSwitch extends StatelessWidget { Expanded( child: InkWell( onTap: () { - AcCubit.get(context).turnAllACsOff(); + DevicesCubit.get(context).turnAllDevicesOff(category); }, child: Container( height: 60, diff --git a/lib/features/shared_widgets/devices_default_switch.dart b/lib/features/shared_widgets/devices_default_switch.dart index 02c3337..2d45c10 100644 --- a/lib/features/shared_widgets/devices_default_switch.dart +++ b/lib/features/shared_widgets/devices_default_switch.dart @@ -1,28 +1,29 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:syncrow_app/features/devices/bloc/AC/ac_cubit.dart'; -import 'package:syncrow_app/features/devices/model/ac_model.dart'; +import 'package:syncrow_app/features/devices/model/device_model.dart'; import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart'; import 'package:syncrow_app/utils/resource_manager/color_manager.dart'; +import '../devices/bloc/devices_cubit.dart'; + class DevicesDefaultSwitch extends StatelessWidget { const DevicesDefaultSwitch({ super.key, required this.model, }); - final ACModel model; + final DeviceModel model; @override Widget build(BuildContext context) { - return BlocBuilder( + return BlocBuilder( builder: (context, state) { return Row( children: [ Expanded( child: InkWell( onTap: () { - AcCubit.get(context).turnACOn(model); + DevicesCubit.get(context).turnOnOffDevice(model); }, child: Container( height: 60, @@ -48,7 +49,7 @@ class DevicesDefaultSwitch extends StatelessWidget { Expanded( child: InkWell( onTap: () { - AcCubit.get(context).turnACOff(model); + DevicesCubit.get(context).turnOnOffDevice(model); }, child: Container( height: 60,