door lock changes

This commit is contained in:
mohammad
2024-07-03 14:27:50 +03:00
parent 1fd6e7947b
commit 992b13b063
9 changed files with 418 additions and 404 deletions
@@ -35,7 +35,6 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
on<GenerateAndSavePasswordOneTimeEvent>(generateAndSavePasswordOneTime);
}
TextEditingController passwordController = TextEditingController();
TextEditingController passwordNameController = TextEditingController();
String effectiveTime = 'Select Time';
@@ -50,6 +49,38 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
List<TemporaryPassword>? temporaryPasswords = [];
List<TemporaryPassword>? oneTimePasswords = [];
Future generate7DigitNumber(GeneratePasswordEvent event, Emitter<SmartDoorState> 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<void> generateAndSavePasswordOneTime (GenerateAndSavePasswordOneTimeEvent event, Emitter<SmartDoorState> 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<SmartDoorState> emit) async {
try {
emit(LoadingInitialState());
@@ -96,14 +127,14 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
bool toggleRepeat(ToggleRepeatEvent event, Emitter<SmartDoorState> emit) {
emit(LoadingInitialState());
repeat = !repeat;
emit(IsRepeatState());
emit(IsRepeatState(repeat: repeat));
return repeat;
}
bool setStartEndTime(SetStartEndTimeEvent event, Emitter<SmartDoorState> emit) {
emit(LoadingInitialState());
isStartEndTime = event.val;
emit(IsStartEndState());
emit(IsStartEndState(isStartEndTime:isStartEndTime));
return isStartEndTime;
}
@@ -121,15 +152,7 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
emit(UpdateState(smartDoorModel: deviceStatus));
}
void generate7DigitNumber(GeneratePasswordEvent event, Emitter<SmartDoorState> 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<void> selectTime(SelectTimeEvent event, Emitter<SmartDoorState> emit) async {
emit(ChangeTimeState());
@@ -232,61 +255,22 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
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<void> generateAndSavePasswordOneTime (GenerateAndSavePasswordOneTimeEvent event, Emitter<SmartDoorState> 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<void> deletePassword(DeletePasswordEvent event, Emitter<SmartDoorState> emit) async {
try {
emit(LoadingInitialState());
@@ -309,7 +293,7 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
CustomSnackBar.displaySnackBar('Password required');
return true;
}
if (passwordNameController.text.isEmpty) {
if (passwordNameController.text.isEmpty && pageType=='Online Password') {
CustomSnackBar.displaySnackBar('Password name required');
return true;
}
@@ -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<Object> get props => [val];
@@ -38,13 +38,27 @@ class FailedState extends SmartDoorState {
List<Object> 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<Object> get props => [repeat];
}
class IsStartEndState extends SmartDoorState {
final bool isStartEndTime;
const IsStartEndState({required this.isStartEndTime});
@override
List<Object> 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<Object> get props => [generated];
}
class TemporaryPasswordsLoadedState extends SmartDoorState {
final List<TemporaryPassword> temporaryPassword;
const TemporaryPasswordsLoadedState({required this.temporaryPassword});