diff --git a/lib/features/devices/bloc/devices_cubit.dart b/lib/features/devices/bloc/devices_cubit.dart index 47e22fa..5d1fb04 100644 --- a/lib/features/devices/bloc/devices_cubit.dart +++ b/lib/features/devices/bloc/devices_cubit.dart @@ -1,8 +1,10 @@ +import 'package:dio/dio.dart'; 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/curtain_model.dart'; import 'package:syncrow_app/features/devices/model/device_category_model.dart'; +import 'package:syncrow_app/features/devices/model/device_control_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/ACs/acs_view.dart'; @@ -12,6 +14,8 @@ import 'package:syncrow_app/features/devices/view/widgets/lights/lights_view.dar import 'package:syncrow_app/features/devices/view/widgets/lights_switches/light_switches.dart'; import 'package:syncrow_app/features/devices/view/widgets/smart_door/door_view.dart'; import 'package:syncrow_app/generated/assets.dart'; +import 'package:syncrow_app/services/api/devices_api.dart'; +import 'package:syncrow_app/services/api/network_exception.dart'; import 'package:syncrow_app/utils/resource_manager/constants.dart'; part 'devices_state.dart'; @@ -395,4 +399,27 @@ class DevicesCubit extends Cubit { emit(DevicesCategoryChanged()); } + + //fetchRooms(SpaceModel space) async { + // emit(SpaceRoomsLoading()); + // try { + // space.rooms = await SpacesAPI.getRooms(space.id!); + // if (space.rooms != null) { + // emit(SpaceRoomsLoaded(space.rooms!)); + // } else { + // emit(SpaceRoomsError("No rooms found")); + // } + // } on DioException catch (e) { + // emit(SpacesError(ServerFailure.fromDioError(e).errMessage)); + // } + // } + deviceControl(DeviceControlModel control) async { + emit(DeviceControlLoading()); + try { + var response = await DevicesAPI.controlDevice(control); + emit(DeviceControlSuccess()); + } on DioException catch (e) { + emit(DeviceControlError(ServerFailure.fromDioError(e).errMessage)); + } + } } diff --git a/lib/features/devices/bloc/devices_state.dart b/lib/features/devices/bloc/devices_state.dart index 4a6ba5d..d913d09 100644 --- a/lib/features/devices/bloc/devices_state.dart +++ b/lib/features/devices/bloc/devices_state.dart @@ -20,3 +20,13 @@ class CategorySwitchChanged extends DevicesState {} class DeviceSwitchChanged extends DevicesState {} class DeviceSelected extends DevicesState {} + +class DeviceControlLoading extends DevicesState {} + +class DeviceControlSuccess extends DevicesState {} + +class DeviceControlError extends DevicesState { + final String errorMsg; + + DeviceControlError(this.errorMsg); +} diff --git a/lib/features/devices/view/widgets/lights_switches/light_switch.dart b/lib/features/devices/view/widgets/lights_switches/light_switch.dart new file mode 100644 index 0000000..8963f57 --- /dev/null +++ b/lib/features/devices/view/widgets/lights_switches/light_switch.dart @@ -0,0 +1,70 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:flutter_svg/svg.dart'; +import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart'; +import 'package:syncrow_app/features/devices/model/device_control_model.dart'; +import 'package:syncrow_app/generated/assets.dart'; +import 'package:syncrow_app/utils/resource_manager/color_manager.dart'; + +class LightSwitch extends StatelessWidget { + const LightSwitch({ + super.key, + required this.control, + }); + + final DeviceControlModel control; + + @override + Widget build(BuildContext context) { + return BlocBuilder( + builder: (context, state) { + return state is DeviceControlLoading + ? const CircularProgressIndicator() + : InkWell( + overlayColor: MaterialStateProperty.all(Colors.transparent), + onTap: () { + DevicesCubit.get(context) + .deviceControl(control) + .then((value) { + if (control.value ?? true) { + control.value = false; + } else { + control.value = true; + } + }); + }, + child: Stack( + alignment: !control.value! + ? Alignment.topCenter + : Alignment.bottomCenter, + children: [ + Container( + decoration: BoxDecoration( + borderRadius: + const BorderRadius.all(Radius.circular(100.0)), + color: !control.value! + ? ColorsManager.primaryColorWithOpacity + : ColorsManager.switchOffColor, + ), + width: 60, + height: 115, + ), + Padding( + padding: const EdgeInsets.all(5.0), + child: SizedBox.square( + dimension: 60, + child: SvgPicture.asset( + !control.value! + ? Assets.iconsLightSwitchOn + : Assets.iconsLightSwitchOff, + fit: BoxFit.fill, + ), + ), + ), + ], + ), + ); + }, + ); + } +} diff --git a/lib/features/devices/view/widgets/lights_switches/light_switche.dart b/lib/features/devices/view/widgets/lights_switches/light_switche.dart deleted file mode 100644 index cabb568..0000000 --- a/lib/features/devices/view/widgets/lights_switches/light_switche.dart +++ /dev/null @@ -1,54 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:flutter_svg/svg.dart'; -import 'package:syncrow_app/generated/assets.dart'; -import 'package:syncrow_app/utils/resource_manager/color_manager.dart'; - -class LightSwitch extends StatefulWidget { - const LightSwitch({ - super.key, - }); - - @override - State createState() => _LightSwitchState(); -} - -class _LightSwitchState extends State { - bool isOn = false; - - @override - Widget build(BuildContext context) { - return InkWell( - overlayColor: MaterialStateProperty.all(Colors.transparent), - onTap: () { - setState(() { - isOn = !isOn; - }); - }, - child: Stack( - alignment: isOn ? Alignment.topCenter : Alignment.bottomCenter, - children: [ - Container( - decoration: BoxDecoration( - borderRadius: const BorderRadius.all(Radius.circular(100.0)), - color: isOn - ? ColorsManager.primaryColorWithOpacity - : ColorsManager.switchOffColor, - ), - width: 60, - height: 115, - ), - Padding( - padding: const EdgeInsets.all(5.0), - child: SizedBox.square( - dimension: 60, - child: SvgPicture.asset( - isOn ? Assets.iconsLightSwitchOn : Assets.iconsLightSwitchOff, - fit: BoxFit.fill, - ), - ), - ), - ], - ), - ); - } -} diff --git a/lib/features/devices/view/widgets/lights_switches/light_switches_body.dart b/lib/features/devices/view/widgets/lights_switches/light_switches_body.dart index 2c915b2..f5a20a3 100644 --- a/lib/features/devices/view/widgets/lights_switches/light_switches_body.dart +++ b/lib/features/devices/view/widgets/lights_switches/light_switches_body.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; +import 'package:syncrow_app/features/devices/model/device_control_model.dart'; import 'package:syncrow_app/features/devices/model/device_model.dart'; -import 'package:syncrow_app/features/devices/view/widgets/lights_switches/light_switche.dart'; +import 'package:syncrow_app/features/devices/view/widgets/lights_switches/light_switch.dart'; import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.dart'; import 'package:syncrow_app/utils/context_extension.dart'; import 'package:syncrow_app/utils/resource_manager/color_manager.dart'; @@ -20,13 +21,31 @@ class LightSwitchesBody extends StatelessWidget { crossAxisAlignment: CrossAxisAlignment.stretch, children: [ const Expanded(child: SizedBox.shrink()), - const Row( + Row( mainAxisAlignment: MainAxisAlignment.spaceAround, crossAxisAlignment: CrossAxisAlignment.center, children: [ - LightSwitch(), - LightSwitch(), - LightSwitch(), + LightSwitch( + control: DeviceControlModel( + deviceId: 'bfe10693d4fd263206ocq9', + code: 'switch_1', + value: true, + ), + ), + LightSwitch( + control: DeviceControlModel( + deviceId: 'bfe10693d4fd263206ocq9', + code: 'switch_2', + value: true, + ), + ), + LightSwitch( + control: DeviceControlModel( + deviceId: 'bfe10693d4fd263206ocq9', + code: 'switch_3', + value: true, + ), + ), ], ), Expanded( diff --git a/lib/services/api/api_links_endpoints.dart b/lib/services/api/api_links_endpoints.dart index 4233712..6e21740 100644 --- a/lib/services/api/api_links_endpoints.dart +++ b/lib/services/api/api_links_endpoints.dart @@ -13,4 +13,7 @@ abstract class ApiEndpoints { // Spaces static const String spaces = '$baseUrl/home'; static const String rooms = '$baseUrl/room'; + + // Devices + static const String control = '$baseUrl/device/control'; } diff --git a/lib/services/api/devices_api.dart b/lib/services/api/devices_api.dart new file mode 100644 index 0000000..2e74df5 --- /dev/null +++ b/lib/services/api/devices_api.dart @@ -0,0 +1,18 @@ +import 'package:syncrow_app/features/devices/model/device_control_model.dart'; +import 'package:syncrow_app/services/api/api_links_endpoints.dart'; +import 'package:syncrow_app/services/api/http_service.dart'; + +class DevicesAPI { + static Future> controlDevice( + DeviceControlModel controlModel) async { + final response = await HTTPService().post( + path: ApiEndpoints.control, + body: controlModel.toJson(), + showServerMessage: false, + expectedResponseModel: (json) { + return json; + }, + ); + return response; + } +}