diff --git a/lib/features/devices/bloc/smart_door_bloc/smart_door_bloc.dart b/lib/features/devices/bloc/smart_door_bloc/smart_door_bloc.dart index 2cae2e9..0259855 100644 --- a/lib/features/devices/bloc/smart_door_bloc/smart_door_bloc.dart +++ b/lib/features/devices/bloc/smart_door_bloc/smart_door_bloc.dart @@ -3,7 +3,6 @@ import 'package:day_picker/model/day_in_week.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:intl/intl.dart'; -import 'package:pin_code_fields/pin_code_fields.dart'; import 'package:syncrow_app/features/devices/bloc/smart_door_bloc/smart_door_event.dart'; import 'package:syncrow_app/features/devices/bloc/smart_door_bloc/smart_door_state.dart'; import 'package:syncrow_app/features/devices/model/device_control_model.dart'; @@ -23,6 +22,13 @@ class SmartDoorBloc extends Bloc { on(_fetchSmartDoorStatus); on(getTemporaryPasswords); on(_updateLock); + on(savePassword); + on(toggleRepeat); + on(setStartEndTime); + on(changeTime); + on(generate7DigitNumber); + on(selectTime); + on(deletePassword); } void _fetchSmartDoorStatus( InitialEvent event, Emitter emit) async { @@ -78,26 +84,27 @@ class SmartDoorBloc extends Bloc { List? temporaryPasswords = []; List? oneTimePasswords = []; - changeTime(val, isStartEndTime) { + changeTime(ChangeTimeEvent event, Emitter emit) { emit(LoadingInitialState()); - if (isStartEndTime == true) { - startTime = val; + if (event.isStartEndTime == true) { + startTime = event.val; } else { - endTime = val; + endTime = event.val; } emit(changeTimeState()); } - bool toggleRepeat() { + bool toggleRepeat(ToggleRepeatEvent event, Emitter emit) { emit(LoadingInitialState()); repeat = !repeat; emit(IsRepeatState()); return repeat; } - bool setStartEndTime(bool val) { + + bool setStartEndTime(SetStartEndTimeEvent event, Emitter emit) { emit(LoadingInitialState()); - isStartEndTime = val; + isStartEndTime = event.val; emit(IsStartEndState()); return isStartEndTime; } @@ -119,7 +126,7 @@ class SmartDoorBloc extends Bloc { emit(UpdateState(smartDoorModel: deviceStatus)); } - void generate7DigitNumber() async { + void generate7DigitNumber(GeneratePasswordEvent event, Emitter emit) async { emit(LoadingInitialState()); passwordController.clear(); Random random = Random(); @@ -129,17 +136,16 @@ class SmartDoorBloc extends Bloc { emit(GeneratePasswordState()); } - Future selectTime(BuildContext context, - {required bool isEffective}) async { + Future selectTime(SelectTimeEvent event, Emitter emit) async { final DateTime? picked = await showDatePicker( - context: context, + context: event.context, initialDate: DateTime.now(), firstDate: DateTime(2015, 8), lastDate: DateTime(2101), ); if (picked != null) { final TimeOfDay? timePicked = await showTimePicker( - context: context, + context: event.context, initialTime: TimeOfDay.now(), ); if (timePicked != null) { @@ -150,12 +156,11 @@ class SmartDoorBloc extends Bloc { timePicked.hour, timePicked.minute, ); - if (isEffective) { + if (event.isEffective) { if (expirationTimeTimeStamp != null && selectedDateTime.millisecondsSinceEpoch > expirationTimeTimeStamp!) { - _showSnackBar(context, - 'Effective Time cannot be later than Expiration Time.'); + CustomSnackBar.displaySnackBar('Effective Time cannot be later than Expiration Time.'); } else { effectiveTime = selectedDateTime.toString(); effectiveTimeTimeStamp = selectedDateTime.millisecondsSinceEpoch; @@ -164,8 +169,7 @@ class SmartDoorBloc extends Bloc { if (effectiveTimeTimeStamp != null && selectedDateTime.millisecondsSinceEpoch < effectiveTimeTimeStamp!) { - _showSnackBar(context, - 'Expiration Time cannot be earlier than Effective Time.'); + CustomSnackBar.displaySnackBar('Expiration Time cannot be earlier than Effective Time.'); } else { expirationTime = selectedDateTime.toString(); expirationTimeTimeStamp = selectedDateTime.millisecondsSinceEpoch; @@ -176,11 +180,11 @@ class SmartDoorBloc extends Bloc { } } - Future savePassword(BuildContext context) async { - if (_validateInputs(context)) return; + Future savePassword(SavePasswordEvent event, Emitter emit) async { + if (_validateInputs()) return; try { emit(LoadingSaveState()); - var response = await DevicesAPI.createPassword( + var res = await DevicesAPI.createPassword( pageType: pageType, deviceId: deviceId, effectiveTime: effectiveTimeTimeStamp.toString(), @@ -195,21 +199,20 @@ class SmartDoorBloc extends Bloc { workingDay: selectedDay!, ), ], - ).then((value)async { - add(InitialPasswordsPage(type: pageType)); - },); + ); + Navigator.of(event.context).pop(true); CustomSnackBar.displaySnackBar('Save Successfully'); emit(SaveState()); - } catch (e) { - emit(FailedState(errorMessage: 'Failed to save password: $e')); + + } catch (_) { } } - void deletePassword(BuildContext context, passwordId) async { + Future deletePassword(DeletePasswordEvent event, Emitter emit) async { try { emit(LoadingInitialState()); var response = await DevicesAPI.deletePassword( - deviceId: deviceId, passwordId: passwordId) + deviceId: deviceId, passwordId: event.passwordId) .then((value) async { add(InitialPasswordsPage(type: pageType)); }); @@ -218,7 +221,7 @@ class SmartDoorBloc extends Bloc { } } - bool _validateInputs(BuildContext context) { + bool _validateInputs() { if (passwordController.text.length<7) { CustomSnackBar.displaySnackBar('Password less than 7'); return true; @@ -241,20 +244,13 @@ class SmartDoorBloc extends Bloc { } if (repeat == true && (endTime == null || startTime == null || selectedDay == null)) { - _showSnackBar(context, 'Start Time and End time and the days required '); + CustomSnackBar.displaySnackBar('Start Time and End time and the days required '); return true; } return false; } - void _showSnackBar(BuildContext context, String message) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text(message), - backgroundColor: Colors.red, - ), - ); - } + List days = [ DayInWeek("S", dayKey: 'Sun'), diff --git a/lib/features/devices/bloc/smart_door_bloc/smart_door_event.dart b/lib/features/devices/bloc/smart_door_bloc/smart_door_event.dart index 0a481ab..df138ae 100644 --- a/lib/features/devices/bloc/smart_door_bloc/smart_door_event.dart +++ b/lib/features/devices/bloc/smart_door_bloc/smart_door_event.dart @@ -1,4 +1,5 @@ import 'package:equatable/equatable.dart'; +import 'package:flutter/cupertino.dart'; abstract class SmartDoorEvent extends Equatable { const SmartDoorEvent(); @@ -21,4 +22,46 @@ class UpdateLockEvent extends SmartDoorEvent { List get props => [value]; } +class SavePasswordEvent extends SmartDoorEvent { + final BuildContext context; + const SavePasswordEvent({required this.context}); + @override + List get props => [context]; +} + +class GeneratePasswordEvent extends SmartDoorEvent {} +class SelectTimeEvent extends SmartDoorEvent { + final BuildContext context; + final bool isEffective; + const SelectTimeEvent({required this.context,required this.isEffective}); + @override + List get props => [context,isEffective]; +} + +class ToggleRepeatEvent extends SmartDoorEvent {} +class SetStartEndTimeEvent extends SmartDoorEvent { + final bool val; + + const SetStartEndTimeEvent({required this.val}); + @override + List get props => [val]; +} + +class DeletePasswordEvent extends SmartDoorEvent { + final String passwordId; + + const DeletePasswordEvent({required this.passwordId}); + @override + List get props => [passwordId]; +} + +class ChangeTimeEvent extends SmartDoorEvent { + final dynamic val; + final bool isStartEndTime; + + const ChangeTimeEvent({required this.val,required this.isStartEndTime}); + @override + List get props => [val,isStartEndTime]; +} + diff --git a/lib/features/devices/view/widgets/smart_door/create_temporary_password.dart b/lib/features/devices/view/widgets/smart_door/create_temporary_password.dart index 6fd500c..fd2af90 100644 --- a/lib/features/devices/view/widgets/smart_door/create_temporary_password.dart +++ b/lib/features/devices/view/widgets/smart_door/create_temporary_password.dart @@ -51,8 +51,7 @@ class CreateTemporaryPassword extends StatelessWidget { actions: [ TextButton( onPressed: () { - BlocProvider.of(context).savePassword(context); - SmartDoorBloc(deviceId: deviceId!).add(InitialPasswordsPage(type:type )); + BlocProvider.of(context).add(SavePasswordEvent(context: context)); }, child: const Text('Save')) ], @@ -84,9 +83,9 @@ class CreateTemporaryPassword extends StatelessWidget { child: Row( children: [ Expanded( - child: PinCodeTextField( - autoDisposeControllers: false, - keyboardType: TextInputType.phone, + child: PinCodeTextField( + autoDisposeControllers: false, + keyboardType: TextInputType.phone, length: 7, enabled: true, obscureText: false, @@ -114,7 +113,7 @@ class CreateTemporaryPassword extends StatelessWidget { ), InkWell( onTap: () { - BlocProvider.of(context).generate7DigitNumber(); + BlocProvider.of(context).add(GeneratePasswordEvent()); }, child: const BodyMedium( text: 'Generate Randomly', @@ -162,8 +161,8 @@ class CreateTemporaryPassword extends StatelessWidget { width: MediaQuery.of(context).size.width / 2, child: InkWell( onTap: () { - BlocProvider.of(context) - .selectTime(context, isEffective: true); + BlocProvider.of(context)..add(SelectTimeEvent(context: context, isEffective: true)); + // .selectTime(context, isEffective: true); }, child: Text( BlocProvider.of(context).effectiveTime @@ -183,9 +182,10 @@ class CreateTemporaryPassword extends StatelessWidget { width: MediaQuery.of(context).size.width / 2, child: InkWell( onTap: () { - BlocProvider.of(context) - .selectTime(context, - isEffective: false); + BlocProvider.of(context)..add(SelectTimeEvent(context: context, isEffective: false)); + // BlocProvider.of(context) + // .selectTime(context, + // isEffective: false); }, child: Text( BlocProvider.of(context) @@ -214,8 +214,7 @@ class CreateTemporaryPassword extends StatelessWidget { value: BlocProvider.of(context).repeat, onChanged: (value) { - BlocProvider.of(context) - .toggleRepeat(); + BlocProvider.of(context).add(ToggleRepeatEvent()); }, applyTheme: true, )), @@ -238,14 +237,15 @@ class CreateTemporaryPassword extends StatelessWidget { children: [ InkWell( onTap: () { - BlocProvider.of(context).setStartEndTime(true); + BlocProvider.of(context).add(const SetStartEndTimeEvent(val: true)); }, child: BodyMedium(text: 'Start',fontColor:BlocProvider.of(context).isStartEndTime==false? Colors.black:Colors.blue,fontSize: 18,), ), InkWell( onTap: () { - BlocProvider.of(context).setStartEndTime(false); + BlocProvider.of(context).add(const SetStartEndTimeEvent(val: false)); + }, child: BodyMedium(text: 'End',fontColor:BlocProvider.of(context).isStartEndTime? Colors.black:Colors.blue,fontSize: 18,), ) @@ -269,7 +269,9 @@ class CreateTemporaryPassword extends StatelessWidget { highlightedTextStyle: const TextStyle( fontSize: 30, color: Colors.blue), onTimeChange: (time) { - BlocProvider.of(context).changeTime(time, BlocProvider.of(context).isStartEndTime); + + // BlocProvider.of(context).changeTime(time, BlocProvider.of(context).isStartEndTime); + BlocProvider.of(context).add(ChangeTimeEvent(val: time, isStartEndTime: BlocProvider.of(context).isStartEndTime)); }, ): Container( child: TimePickerSpinner( @@ -282,7 +284,9 @@ class CreateTemporaryPassword extends StatelessWidget { highlightedTextStyle: const TextStyle( fontSize: 30, color: Colors.blue), onTimeChange: (time) { - BlocProvider.of(context).changeTime(time, BlocProvider.of(context).isStartEndTime); + BlocProvider.of(context).add(ChangeTimeEvent(val: time, isStartEndTime: BlocProvider.of(context).isStartEndTime)); + + // BlocProvider.of(context).changeTime(time, BlocProvider.of(context).isStartEndTime); }, ), ), diff --git a/lib/features/devices/view/widgets/smart_door/view_temporary_password.dart b/lib/features/devices/view/widgets/smart_door/view_temporary_password.dart index f778931..75350e4 100644 --- a/lib/features/devices/view/widgets/smart_door/view_temporary_password.dart +++ b/lib/features/devices/view/widgets/smart_door/view_temporary_password.dart @@ -42,7 +42,7 @@ class ViewTemporaryPassword extends StatelessWidget { Navigator.of(context).push(MaterialPageRoute( builder: (context) => CreateTemporaryPassword(deviceId: deviceId, type: type), )).then((result) { - if (result != null) { + if (result != null && result) { smartDoorBloc.add(InitialPasswordsPage(type:type )); } }); @@ -83,7 +83,7 @@ class ViewTemporaryPassword extends StatelessWidget { }, ); if(result=='delete'){ - smartDoorBloc.deletePassword(context, smartDoorBloc.temporaryPasswords![index].id.toString()); + smartDoorBloc.add(DeletePasswordEvent(passwordId: smartDoorBloc.temporaryPasswords![index].id.toString())); } }, trailing: const Icon( diff --git a/lib/services/api/devices_api.dart b/lib/services/api/devices_api.dart index a9a7b82..0cd1862 100644 --- a/lib/services/api/devices_api.dart +++ b/lib/services/api/devices_api.dart @@ -158,7 +158,7 @@ class DevicesAPI { required String pageType, List? scheduleList, }) async { - try { + String endpointPath; if (pageType == 'Online Password') { endpointPath = ApiEndpoints.addTemporaryPassword @@ -187,7 +187,7 @@ class DevicesAPI { expectedResponseModel: (json) => json, ); return response; - } catch (e) {} + }