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 23ce31f..a98dd01 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 @@ -35,7 +35,6 @@ class SmartDoorBloc extends Bloc { on(generateAndSavePasswordOneTime); } - TextEditingController passwordController = TextEditingController(); TextEditingController passwordNameController = TextEditingController(); String effectiveTime = 'Select Time'; @@ -50,6 +49,38 @@ class SmartDoorBloc extends Bloc { List? temporaryPasswords = []; List? oneTimePasswords = []; + Future generate7DigitNumber(GeneratePasswordEvent event, Emitter emit) async { + emit(LoadingInitialState()); + passwordController.clear(); + Random random = Random(); + int min = 1000000; + int max = 9999999; + passwordController.text = (min + random.nextInt(max - min + 1)).toString(); + emit(GeneratePasswordState()); + return passwordController.text; + } + + + Future generateAndSavePasswordOneTime (GenerateAndSavePasswordOneTimeEvent event, Emitter emit) async { + try { + if (passwordNameController.text.isEmpty) { + CustomSnackBar.displaySnackBar('Password name required'); + }else { + if (isSavingPassword) return; + isSavingPassword = true; + emit(LoadingInitialState()); + Future.delayed(const Duration(milliseconds: 800), () { + add(GeneratePasswordEvent()); + CustomSnackBar.displaySnackBar('Save Successfully'); + }); + emit(const GeneratePasswordOneTimestate(generated: true)); + + } + } catch (_) {}finally { + isSavingPassword = false; + } + } + void _fetchSmartDoorStatus(InitialEvent event, Emitter emit) async { try { emit(LoadingInitialState()); @@ -96,14 +127,14 @@ class SmartDoorBloc extends Bloc { bool toggleRepeat(ToggleRepeatEvent event, Emitter emit) { emit(LoadingInitialState()); repeat = !repeat; - emit(IsRepeatState()); + emit(IsRepeatState(repeat: repeat)); return repeat; } bool setStartEndTime(SetStartEndTimeEvent event, Emitter emit) { emit(LoadingInitialState()); isStartEndTime = event.val; - emit(IsStartEndState()); + emit(IsStartEndState(isStartEndTime:isStartEndTime)); return isStartEndTime; } @@ -121,15 +152,7 @@ class SmartDoorBloc extends Bloc { emit(UpdateState(smartDoorModel: deviceStatus)); } - void generate7DigitNumber(GeneratePasswordEvent event, Emitter emit) async { - emit(LoadingInitialState()); - passwordController.clear(); - Random random = Random(); - int min = 1000000; - int max = 9999999; - passwordController.text = (min + random.nextInt(max - min + 1)).toString(); - emit(GeneratePasswordState()); - } + Future selectTime(SelectTimeEvent event, Emitter emit) async { emit(ChangeTimeState()); @@ -232,61 +255,22 @@ class SmartDoorBloc extends Bloc { if (_validateInputs() || isSavingPassword) return; try { isSavingPassword = true; - emit(LoadingSaveState()); - var res = await DevicesAPI.createPassword( - pageType: pageType, - deviceId: deviceId, - effectiveTime: effectiveTimeTimeStamp.toString(), - invalidTime: expirationTimeTimeStamp.toString(), - name: passwordNameController.text, - password: passwordController.text, - scheduleList: [ - if (repeat) - Schedule( - effectiveTime: getTimeOnly(startTime), - invalidTime: getTimeOnly(endTime).toString(), - workingDay: selectedDay!, - ), - ], - ); - CustomSnackBar.displaySnackBar('Save Successfully'); - emit(SaveState()); - } catch (_) {}finally { - isSavingPassword = false; - } - } - Future generateAndSavePasswordOneTime (GenerateAndSavePasswordOneTimeEvent event, Emitter emit) async { - try { - if (passwordNameController.text.isEmpty) { - CustomSnackBar.displaySnackBar('Password name required'); - }else { - if (isSavingPassword) return; - isSavingPassword = true; - emit(LoadingSaveState()); - var res = await DevicesAPI.createPassword( - pageType: pageType, - deviceId: deviceId, - effectiveTime: effectiveTimeTimeStamp.toString(), - invalidTime: expirationTimeTimeStamp.toString(), - name: passwordNameController.text, - password: passwordController.text, - scheduleList: [ - if (repeat) - Schedule( - effectiveTime: getTimeOnly(startTime), - invalidTime: getTimeOnly(endTime).toString(), - workingDay: selectedDay!, - ), - ], - ); + emit(LoadingInitialState()); + Future.delayed(const Duration(milliseconds: 800), () { + add(GeneratePasswordEvent()); CustomSnackBar.displaySnackBar('Save Successfully'); - emit(SaveState()); - } - } catch (_) {}finally { + emit(const GeneratePasswordOneTimestate(generated: true)); + }); + } catch (_) { + print(_); + } + finally { isSavingPassword = false; } } + + Future deletePassword(DeletePasswordEvent event, Emitter emit) async { try { emit(LoadingInitialState()); @@ -309,7 +293,7 @@ class SmartDoorBloc extends Bloc { CustomSnackBar.displaySnackBar('Password required'); return true; } - if (passwordNameController.text.isEmpty) { + if (passwordNameController.text.isEmpty && pageType=='Online Password') { CustomSnackBar.displaySnackBar('Password name required'); return true; } 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 e6172a2..67d5e98 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 @@ -44,7 +44,9 @@ class GenerateAndSavePasswordOneTimeEvent extends SmartDoorEvent { class GeneratePasswordEvent extends SmartDoorEvent { + } + class SelectTimeEvent extends SmartDoorEvent { final BuildContext context; final bool isEffective; @@ -56,7 +58,6 @@ class SelectTimeEvent extends SmartDoorEvent { class ToggleRepeatEvent extends SmartDoorEvent {} class SetStartEndTimeEvent extends SmartDoorEvent { final bool val; - const SetStartEndTimeEvent({required this.val}); @override List get props => [val]; diff --git a/lib/features/devices/bloc/smart_door_bloc/smart_door_state.dart b/lib/features/devices/bloc/smart_door_bloc/smart_door_state.dart index 29b1d94..7741361 100644 --- a/lib/features/devices/bloc/smart_door_bloc/smart_door_state.dart +++ b/lib/features/devices/bloc/smart_door_bloc/smart_door_state.dart @@ -38,13 +38,27 @@ class FailedState extends SmartDoorState { List get props => [errorMessage]; } -class GeneratePasswordState extends SmartDoorState {} +class GeneratePasswordState extends SmartDoorState { +} class TimeSelectedState extends SmartDoorState {} -class IsRepeatState extends SmartDoorState {} +class IsRepeatState extends SmartDoorState { + final bool repeat; + const IsRepeatState({required this.repeat}); -class IsStartEndState extends SmartDoorState {} + @override + List get props => [repeat]; + +} + +class IsStartEndState extends SmartDoorState { + final bool isStartEndTime; + const IsStartEndState({required this.isStartEndTime}); + + @override + List get props => [isStartEndTime]; +} class ChangeStartTimeState extends SmartDoorState {} @@ -56,6 +70,12 @@ class SaveState extends SmartDoorState {} class LoadingSaveState extends SmartDoorState {} +class GeneratePasswordOneTimestate extends SmartDoorState { + final bool generated; + const GeneratePasswordOneTimestate({required this.generated}); + List get props => [generated]; +} + class TemporaryPasswordsLoadedState extends SmartDoorState { final List temporaryPassword; const TemporaryPasswordsLoadedState({required this.temporaryPassword}); diff --git a/lib/features/devices/view/widgets/ceiling_sensor/ceiling_sensor_interface.dart b/lib/features/devices/view/widgets/ceiling_sensor/ceiling_sensor_interface.dart index 3a186c1..31c828a 100644 --- a/lib/features/devices/view/widgets/ceiling_sensor/ceiling_sensor_interface.dart +++ b/lib/features/devices/view/widgets/ceiling_sensor/ceiling_sensor_interface.dart @@ -30,297 +30,298 @@ class CeilingSensorInterface extends StatelessWidget { // .value // .toString(); return BlocProvider( - create: (context) => - CeilingSensorBloc(deviceId: ceilingSensor.uuid ?? '')..add(InitialEvent()), - child: BlocBuilder(builder: (context, state) { - CeilingSensorModel ceilingSensorModel = - CeilingSensorModel(presenceState: 'none', sensitivity: 1, checkingResult: ''); - if (state is UpdateState) { - ceilingSensorModel = state.ceilingSensorModel; - } else if (state is LoadingNewSate) { - ceilingSensorModel = state.ceilingSensorModel; - } - return AnnotatedRegion( - value: SystemUiOverlayStyle( - statusBarColor: ColorsManager.primaryColor.withOpacity(0.5), - statusBarIconBrightness: Brightness.light, - ), - child: Scaffold( - backgroundColor: ColorsManager.backgroundColor, - extendBodyBehindAppBar: true, - extendBody: true, - appBar: DeviceAppbar( - deviceName: ceilingSensor.name!, - deviceUuid: ceilingSensor.uuid!, + create: (context) => + CeilingSensorBloc(deviceId: ceilingSensor.uuid ?? '')..add(InitialEvent()), + child: BlocBuilder(builder: (context, state) { + CeilingSensorModel ceilingSensorModel = + CeilingSensorModel(presenceState: 'none', sensitivity: 1, checkingResult: ''); + if (state is UpdateState) { + ceilingSensorModel = state.ceilingSensorModel; + } else if (state is LoadingNewSate) { + ceilingSensorModel = state.ceilingSensorModel; + } + return AnnotatedRegion( + value: SystemUiOverlayStyle( + statusBarColor: ColorsManager.primaryColor.withOpacity(0.5), + statusBarIconBrightness: Brightness.light, ), - body: Container( - width: MediaQuery.sizeOf(context).width, - height: MediaQuery.sizeOf(context).height, - padding: const EdgeInsets.all(Constants.defaultPadding), - decoration: const BoxDecoration( - image: DecorationImage( - image: AssetImage( - Assets.assetsImagesBackground, + child: Scaffold( + backgroundColor: ColorsManager.backgroundColor, + extendBodyBehindAppBar: true, + extendBody: true, + appBar: DeviceAppbar( + deviceName: ceilingSensor.name!, + deviceUuid: ceilingSensor.uuid!, + ), + body: Container( + width: MediaQuery.sizeOf(context).width, + height: MediaQuery.sizeOf(context).height, + padding: const EdgeInsets.all(Constants.defaultPadding), + decoration: const BoxDecoration( + image: DecorationImage( + image: AssetImage( + Assets.assetsImagesBackground, + ), + fit: BoxFit.cover, + opacity: 0.4, ), - fit: BoxFit.cover, - opacity: 0.4, ), - ), - child: state is LoadingInitialState - ? const Center( - child: RefreshProgressIndicator(), - ) - : SafeArea( - child: Column( - children: [ - Expanded( - flex: 1, - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - // InkWell( - // onTap: () { - // if ((ceilingSensor.isOnline ?? false) == false) { - // ScaffoldMessenger.of(context).showSnackBar( - // const SnackBar( - // content: Text( - // 'Device is offline', - // ), - // backgroundColor: Colors.red, - // ), - // ); - // return; - // } - // String controlCode = 'sensitivity'; - // showDialog( - // context: context, - // builder: (context) => ParameterControlDialog( - // title: 'Sensitivity', - // sensor: ceilingSensor, - // controlCode: controlCode, - // value: ceilingSensor.status - // .firstWhere((element) => element.code == controlCode) - // .value as int, - // min: ceilingSensor.functions - // .firstWhere((element) => element.code == controlCode) - // .values - // ?.min ?? - // 0, - // max: ceilingSensor.functions - // .firstWhere((element) => element.code == controlCode) - // .values - // ?.max ?? - // 0, - // ), - // ); - // }, - // child: - // ), - SvgPicture.asset( - ceilingSensorModel.presenceState.toLowerCase() == 'motion' - ? Assets.assetsIconsPresenceSensorAssetsPresenceSensorMotion - : Assets.assetsIconsPresenceSensorAssetsPresence, - width: 100, - height: 100, - // colorFilter: ColorFilter.mode( - // (ceilingSensor.isOnline ?? false) - // ? ColorsManager.primaryColor - // : Colors.grey.withOpacity(0.9), - // BlendMode.srcIn, - // ), - ), - const SizedBox( - height: 10, - ), - BodyMedium( - text: - StringHelpers.toTitleCase(ceilingSensorModel.presenceState), - // (ceilingSensor.isOnline ?? false) - // ? StringHelpers.toTitleCase(ceilingSensor.status - // .firstWhere((element) => - // element.code == 'presence_state') - // .value - // .toString()) - // : "Offline", - style: context.bodyMedium.copyWith( - fontWeight: FontsManager.bold, - ), - ), - ], + child: state is LoadingInitialState + ? const Center( + child: RefreshProgressIndicator(), + ) + : SafeArea( + child: Column( + children: [ + Expanded( + flex: 1, + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + // InkWell( + // onTap: () { + // if ((ceilingSensor.isOnline ?? false) == false) { + // ScaffoldMessenger.of(context).showSnackBar( + // const SnackBar( + // content: Text( + // 'Device is offline', + // ), + // backgroundColor: Colors.red, + // ), + // ); + // return; + // } + // String controlCode = 'sensitivity'; + // showDialog( + // context: context, + // builder: (context) => ParameterControlDialog( + // title: 'Sensitivity', + // sensor: ceilingSensor, + // controlCode: controlCode, + // value: ceilingSensor.status + // .firstWhere((element) => element.code == controlCode) + // .value as int, + // min: ceilingSensor.functions + // .firstWhere((element) => element.code == controlCode) + // .values + // ?.min ?? + // 0, + // max: ceilingSensor.functions + // .firstWhere((element) => element.code == controlCode) + // .values + // ?.max ?? + // 0, + // ), + // ); + // }, + // child: + // ), + SvgPicture.asset( + ceilingSensorModel.presenceState.toLowerCase() == 'motion' + ? Assets.assetsIconsPresenceSensorAssetsPresenceSensorMotion + : Assets.assetsIconsPresenceSensorAssetsPresence, + width: 100, + height: 100, + // colorFilter: ColorFilter.mode( + // (ceilingSensor.isOnline ?? false) + // ? ColorsManager.primaryColor + // : Colors.grey.withOpacity(0.9), + // BlendMode.srcIn, + // ), ), - ), - Expanded( - flex: 2, - child: ListView( - children: [ - DefaultContainer( - padding: const EdgeInsets.symmetric( - vertical: 20, - ), - child: Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Flexible( - child: Column( - mainAxisSize: MainAxisSize.min, - children: [ - const BodySmall(text: 'Sports Para'), - BodyLarge( - text: '0', - style: context.bodyLarge.copyWith( - fontWeight: FontsManager.bold, - ), - ), - ], - ), - ), - Padding( - padding: const EdgeInsets.symmetric(horizontal: 10), - child: Container( - width: 1, - height: 45, - color: ColorsManager.greyColor, - ), - ), - Flexible( - child: Column( - mainAxisSize: MainAxisSize.min, - children: [ - const BodySmall( - text: 'Detection Range', - textOverflow: TextOverflow.ellipsis, - ), - BodyLarge( - text: '0.0M', - textOverflow: TextOverflow.ellipsis, - style: context.bodyLarge.copyWith( - fontWeight: FontsManager.bold, - ), - ), - ], - ), - ), - Padding( - padding: const EdgeInsets.symmetric(horizontal: 10), - child: Container( - width: 1, - height: 45, - color: ColorsManager.greyColor, - ), - ), - Flexible( - child: Column( - mainAxisSize: MainAxisSize.min, - children: [ - const BodySmall( - text: 'Movement', - textOverflow: TextOverflow.ellipsis, - ), - BodyLarge( - text: 'none', - textOverflow: TextOverflow.ellipsis, - style: context.bodyLarge.copyWith( - fontWeight: FontsManager.bold, - ), - ), - ], - ), - ), - ], - )), - const SizedBox( - height: 15, - ), - ...List.generate( - ceilingSensorButtons().length, - (index) => DefaultContainer( - margin: const EdgeInsets.only(bottom: 5), - padding: - const EdgeInsets.symmetric(vertical: 12, horizontal: 20), - onTap: () async { - if (ceilingSensorButtons()[index]['title'] == 'Sensitivity') { - final result = await showDialog( - context: context, - builder: (context) { - return ParameterControlDialog( - title: ceilingSensorButtons()[index]['title'] - .toString(), - sensor: ceilingSensor, - value: ceilingSensorModel.sensitivity, - min: 0, - max: 10, - ); - }, - ); - if (result != null) { - BlocProvider.of(context).add( - ChangeValueEvent( - value: result, code: 'sensitivity')); - } - } + const SizedBox( + height: 10, + ), + BodyMedium( - // if (ceilingSensorButtons[index]['page'] != null) { - // Navigator.push( - // context, - // MaterialPageRoute( - // builder: (context) => - // ceilingSensorButtons[index]['page'] as Widget, - // ), - // ); - // } - }, - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Row( - children: [ - SvgPicture.asset( - ceilingSensorButtons()[index]['icon'] as String, - // width: 30, - // height: 50, + text: + StringHelpers.toTitleCase(ceilingSensorModel.presenceState), + // (ceilingSensor.isOnline ?? false) + // ? StringHelpers.toTitleCase(ceilingSensor.status + // .firstWhere((element) => + // element.code == 'presence_state') + // .value + // .toString()) + // : "Offline", + style: context.bodyMedium.copyWith( + fontWeight: FontsManager.bold, + ), + ), + ], + ), + ), + Expanded( + flex: 2, + child: ListView( + children: [ + DefaultContainer( + padding: const EdgeInsets.symmetric( + vertical: 20, + ), + child: Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Flexible( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + const BodySmall(text: 'Sports Para'), + BodyLarge( + text: '0', + style: context.bodyLarge.copyWith( + fontWeight: FontsManager.bold, ), - const SizedBox( - width: 25, - ), - BodyMedium( - text: ceilingSensorButtons()[index]['title'] - as String, - style: context.bodyMedium.copyWith( - fontWeight: FontsManager.bold, - ), - ), - ], - ), - if (ceilingSensorButtons()[index]['withArrow'] == true) - Row( - mainAxisAlignment: MainAxisAlignment.end, - children: [ - Text(ceilingSensorButtons( - sensitivity_val: ceilingSensorModel - .sensitivity - .toString())[index]['val'] - .toString()), - const Icon( - Icons.arrow_forward_ios, - color: ColorsManager.greyColor, - size: 15, - ), - ], ), + ], + ), + ), + Padding( + padding: const EdgeInsets.symmetric(horizontal: 10), + child: Container( + width: 1, + height: 45, + color: ColorsManager.greyColor, + ), + ), + Flexible( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + const BodySmall( + text: 'Detection Range', + textOverflow: TextOverflow.ellipsis, + ), + BodyLarge( + text: '0.0M', + textOverflow: TextOverflow.ellipsis, + style: context.bodyLarge.copyWith( + fontWeight: FontsManager.bold, + ), + ), + ], + ), + ), + Padding( + padding: const EdgeInsets.symmetric(horizontal: 10), + child: Container( + width: 1, + height: 45, + color: ColorsManager.greyColor, + ), + ), + Flexible( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + const BodySmall( + text: 'Movement', + textOverflow: TextOverflow.ellipsis, + ), + BodyLarge( + text: 'none', + textOverflow: TextOverflow.ellipsis, + style: context.bodyLarge.copyWith( + fontWeight: FontsManager.bold, + ), + ), + ], + ), + ), + ], + )), + const SizedBox( + height: 15, + ), + ...List.generate( + ceilingSensorButtons().length, + (index) => DefaultContainer( + margin: const EdgeInsets.only(bottom: 5), + padding: + const EdgeInsets.symmetric(vertical: 12, horizontal: 20), + onTap: () async { + if (ceilingSensorButtons()[index]['title'] == 'Sensitivity') { + final result = await showDialog( + context: context, + builder: (context) { + return ParameterControlDialog( + title: ceilingSensorButtons()[index]['title'] + .toString(), + sensor: ceilingSensor, + value: ceilingSensorModel.sensitivity, + min: 0, + max: 10, + ); + }, + ); + if (result != null) { + BlocProvider.of(context).add( + ChangeValueEvent( + value: result, code: 'sensitivity')); + } + } + + // if (ceilingSensorButtons[index]['page'] != null) { + // Navigator.push( + // context, + // MaterialPageRoute( + // builder: (context) => + // ceilingSensorButtons[index]['page'] as Widget, + // ), + // ); + // } + }, + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Row( + children: [ + SvgPicture.asset( + ceilingSensorButtons()[index]['icon'] as String, + // width: 30, + // height: 50, + ), + const SizedBox( + width: 25, + ), + BodyMedium( + text: ceilingSensorButtons()[index]['title'] + as String, + style: context.bodyMedium.copyWith( + fontWeight: FontsManager.bold, + ), + ), ], ), - ), + if (ceilingSensorButtons()[index]['withArrow'] == true) + Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + Text(ceilingSensorButtons( + sensitivity_val: ceilingSensorModel + .sensitivity + .toString())[index]['val'] + .toString()), + const Icon( + Icons.arrow_forward_ios, + color: ColorsManager.greyColor, + size: 15, + ), + ], + ), + ], ), - ], + ), ), - ), - ], + ], + ), ), - )), - ), - ); - }), - ); + ], + ), + )), + ), + ); + })); + } dynamic ceilingSensorButtons({ diff --git a/lib/features/devices/view/widgets/room_page_switch.dart b/lib/features/devices/view/widgets/room_page_switch.dart index d6efc2f..d783f27 100644 --- a/lib/features/devices/view/widgets/room_page_switch.dart +++ b/lib/features/devices/view/widgets/room_page_switch.dart @@ -22,8 +22,7 @@ import 'package:syncrow_app/utils/resource_manager/constants.dart'; class RoomPageSwitch extends StatelessWidget { const RoomPageSwitch({ super.key, - required this.device, - }); + required this.device,}); final DeviceModel device; @@ -52,6 +51,7 @@ class RoomPageSwitch extends StatelessWidget { // ), ], ), + Flexible( child: FittedBox( child: Text( 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 98c063e..e6a41ad 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 @@ -23,6 +23,8 @@ class CreateTemporaryPassword extends StatelessWidget { const CreateTemporaryPassword({super.key, this.deviceId, this.type}); @override Widget build(BuildContext context) { + bool isRepeat = false; + bool generated = false; return BlocProvider( create: (BuildContext context) => SmartDoorBloc(deviceId: deviceId!), child: BlocConsumer(listener: (context, state) { @@ -34,7 +36,14 @@ class CreateTemporaryPassword extends StatelessWidget { ), ); } + if (state is IsRepeatState){ + isRepeat = state.repeat; + } + if (state is GeneratePasswordOneTimestate ){ + generated = state.generated; + } }, builder: (context, state) { + final smartDoorBloc = BlocProvider.of(context); return DefaultScaffold( appBar: AppBar( backgroundColor: Colors.transparent, @@ -54,7 +63,7 @@ class CreateTemporaryPassword extends StatelessWidget { type == 'Online Password'?[ TextButton( onPressed: () { - BlocProvider.of(context).add(SavePasswordEvent(context: context)); + smartDoorBloc.add(SavePasswordEvent(context: context)); }, child: const Text('Save') ) @@ -93,14 +102,14 @@ class CreateTemporaryPassword extends StatelessWidget { child: PinCodeTextField( onCompleted: (value) { if (value.split('').every((char) => char == '1')) { - BlocProvider.of(context).passwordController.clear(); + smartDoorBloc.passwordController.clear(); CustomSnackBar.displaySnackBar('All characters cannot be 1.'); } }, autoDisposeControllers: false, keyboardType: TextInputType.phone, length: 7, - enabled:type == 'Online Password'? true:false, + // enabled:type == 'Online Password'? true:false, obscureText: false, animationType: AnimationType.fade, pinTheme: PinTheme( @@ -117,7 +126,7 @@ class CreateTemporaryPassword extends StatelessWidget { animationDuration: const Duration(milliseconds: 300), backgroundColor: Colors.white, enableActiveFill: false, - controller: BlocProvider.of(context).passwordController, + controller: smartDoorBloc.passwordController, appContext: context, )), const SizedBox( @@ -127,7 +136,7 @@ class CreateTemporaryPassword extends StatelessWidget { Flexible( child: InkWell( onTap: () { - BlocProvider.of(context).add(GeneratePasswordEvent()); + smartDoorBloc.add(GeneratePasswordEvent()); }, child: const BodyMedium( text: 'Generate Randomly', @@ -139,18 +148,18 @@ class CreateTemporaryPassword extends StatelessWidget { ), ), ), - if(BlocProvider.of(context).passwordController.text.isNotEmpty) + if(smartDoorBloc.passwordController.text.isNotEmpty) TextButton( - onPressed: () async { - await Clipboard.setData(ClipboardData( - text: BlocProvider.of(context).passwordController.text)); - }, - child: const Text('Copy') + onPressed: () async { + await Clipboard.setData(ClipboardData( + text: smartDoorBloc.passwordController.text)); + }, + child: const Text('Copy') ), const SizedBox( height: 20, ), - NameTimeWidget(type:type!), + NameTimeWidget(type:type!), const SizedBox( height: 20, ), @@ -166,9 +175,9 @@ class CreateTemporaryPassword extends StatelessWidget { trailing: Transform.scale( scale: .8, child: CupertinoSwitch( - value: BlocProvider.of(context).repeat, + value: smartDoorBloc.repeat, onChanged: (value) { - BlocProvider.of(context).add(ToggleRepeatEvent()); + smartDoorBloc.add(ToggleRepeatEvent()); }, applyTheme: true, )), @@ -178,15 +187,20 @@ class CreateTemporaryPassword extends StatelessWidget { child: SizedBox( width: MediaQuery.of(context).size.width/1.5, child: DefaultButton( - isLoading: BlocProvider.of(context).isSavingPassword , + isDone: generated, + isLoading: smartDoorBloc.isSavingPassword , borderRadius: 30, backgroundColor:Colors.blueGrey , onPressed: () { - if(type =='Time-Limited'){ - BlocProvider.of(context).add(GenerateAndSavePasswordTimeLimitEvent(context: context)); - }else{ - BlocProvider.of(context).add(GenerateAndSavePasswordOneTimeEvent(context: context)); - } + if(generated==false){ + if(type =='Time-Limited'){ + smartDoorBloc.add(GenerateAndSavePasswordTimeLimitEvent(context: context)); + }else{ + smartDoorBloc.add(GenerateAndSavePasswordOneTimeEvent(context: context)); + } + }else{ + Navigator.pop(context); + } }, child: const BodyMedium( text: 'Generate & Save Password', @@ -198,15 +212,13 @@ class CreateTemporaryPassword extends StatelessWidget { const SizedBox( height: 20, ), - BlocProvider.of(context).repeat - ? RepeatWidget() - : const SizedBox(), - const SizedBox( - height: 40, - ) - ], - ), - ), + isRepeat? const RepeatWidget():const SizedBox(), + const SizedBox( + height: 40, + ) + ], + ), + ), ); })); } diff --git a/lib/features/devices/view/widgets/smart_door/repeat_widget.dart b/lib/features/devices/view/widgets/smart_door/repeat_widget.dart index 5371db2..7166cbd 100644 --- a/lib/features/devices/view/widgets/smart_door/repeat_widget.dart +++ b/lib/features/devices/view/widgets/smart_door/repeat_widget.dart @@ -5,6 +5,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/devices/bloc/smart_door_bloc/smart_door_bloc.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/shared_widgets/default_container.dart'; import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart'; import 'package:syncrow_app/utils/resource_manager/color_manager.dart'; @@ -18,7 +19,11 @@ class RepeatWidget extends StatelessWidget { @override Widget build(BuildContext context) { - return DefaultContainer( + return BlocBuilder( + builder: (context, state) { + final smartDoorBloc = BlocProvider.of(context); + + return DefaultContainer( padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 5), child: Column( children: [ @@ -29,12 +34,12 @@ class RepeatWidget extends StatelessWidget { children: [ InkWell( onTap: () { - BlocProvider.of(context) + smartDoorBloc .add(const SetStartEndTimeEvent(val: true)); }, child: BodyMedium( text: 'Start', - fontColor: BlocProvider.of(context) + fontColor: smartDoorBloc .isStartEndTime == false ? Colors.black @@ -44,12 +49,11 @@ class RepeatWidget extends StatelessWidget { ), InkWell( onTap: () { - BlocProvider.of(context) - .add(const SetStartEndTimeEvent(val: false)); + smartDoorBloc.add(const SetStartEndTimeEvent(val: false)); }, child: BodyMedium( text: 'End', - fontColor: BlocProvider.of(context) + fontColor: smartDoorBloc .isStartEndTime ? Colors.black : Colors.blue, @@ -64,10 +68,10 @@ class RepeatWidget extends StatelessWidget { ), Container( height: 110, - child: BlocProvider.of(context).isStartEndTime + child: smartDoorBloc.isStartEndTime ? TimePickerSpinner( time: - BlocProvider.of(context).startTime, + smartDoorBloc.startTime, is24HourMode: false, itemHeight: 40, normalTextStyle: const TextStyle( @@ -77,18 +81,16 @@ class RepeatWidget extends StatelessWidget { highlightedTextStyle: const TextStyle(fontSize: 30, color: Colors.blue), onTimeChange: (time) { - BlocProvider.of(context).add( + smartDoorBloc.add( ChangeTimeEvent( val: time, isStartEndTime: - BlocProvider.of(context) - .isStartEndTime)); + smartDoorBloc.isStartEndTime)); }, ) : Container( child: TimePickerSpinner( - time: - BlocProvider.of(context).endTime, + time: smartDoorBloc.endTime, is24HourMode: false, itemHeight: 40, normalTextStyle: const TextStyle( @@ -98,13 +100,11 @@ class RepeatWidget extends StatelessWidget { highlightedTextStyle: const TextStyle( fontSize: 30, color: Colors.blue), onTimeChange: (time) { - BlocProvider.of(context).add( + smartDoorBloc.add( ChangeTimeEvent( val: time, isStartEndTime: - BlocProvider.of( - context) - .isStartEndTime)); + smartDoorBloc.isStartEndTime)); }, ), ), @@ -117,7 +117,7 @@ class RepeatWidget extends StatelessWidget { width: MediaQuery.of(context).size.width / 1, fontSize: 18, fontWeight: FontWeight.w600, - days: BlocProvider.of(context).days, + days: smartDoorBloc.days, border: false, selectedDayTextColor: Colors.black, unSelectedDayTextColor: Colors.grey, @@ -125,11 +125,10 @@ class RepeatWidget extends StatelessWidget { borderRadius: BorderRadius.circular(30.0), color: Colors.white), onSelect: (values) { - BlocProvider.of(context).selectedDay = - values; + smartDoorBloc.selectedDay = values; }, ), ], - )); + ));}); } } 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 fcad19f..20be6d9 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 @@ -20,8 +20,7 @@ class ViewTemporaryPassword extends StatelessWidget { @override Widget build(BuildContext context) { return BlocProvider( - create: (BuildContext context) => - SmartDoorBloc(deviceId: deviceId!)..add(InitialPasswordsPage(type:type )), + create: (BuildContext context) => SmartDoorBloc(deviceId: deviceId!)..add(InitialPasswordsPage(type:type )), child: BlocConsumer( listener: (context, state) { if (state is FailedState) { diff --git a/lib/services/api/devices_api.dart b/lib/services/api/devices_api.dart index caad97b..06070de 100644 --- a/lib/services/api/devices_api.dart +++ b/lib/services/api/devices_api.dart @@ -168,11 +168,9 @@ class DevicesAPI { if (pageType == 'Online Password') { endpointPath = ApiEndpoints.addTemporaryPassword.replaceAll('{doorLockUuid}', deviceId); } else if (pageType == 'One-Time') { - endpointPath = - ApiEndpoints.addOneTimeTemporaryPassword.replaceAll('{doorLockUuid}', deviceId); + endpointPath = ApiEndpoints.addOneTimeTemporaryPassword.replaceAll('{doorLockUuid}', deviceId); } else { - endpointPath = - ApiEndpoints.addMultipleTimeTemporaryPassword.replaceAll('{doorLockUuid}', deviceId); + endpointPath = ApiEndpoints.addMultipleTimeTemporaryPassword.replaceAll('{doorLockUuid}', deviceId); } Map body = { "name": name,