mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-09 14:47:23 +00:00
now if user change end time into value before start time it prevent it with dialog information showing the error and if the init start date is null fill it with the needed value
This commit is contained in:
@ -21,7 +21,6 @@ import 'package:syncrow_web/utils/snack_bar.dart';
|
||||
|
||||
class VisitorPasswordBloc
|
||||
extends Bloc<VisitorPasswordEvent, VisitorPasswordState> {
|
||||
|
||||
VisitorPasswordBloc() : super(VisitorPasswordInitial()) {
|
||||
on<SelectUsageFrequency>(selectUsageFrequency);
|
||||
on<FetchDevice>(_onFetchDevice);
|
||||
@ -87,6 +86,9 @@ class VisitorPasswordBloc
|
||||
SelectTimeVisitorPassword event,
|
||||
Emitter<VisitorPasswordState> emit,
|
||||
) async {
|
||||
// Ensure expirationTimeTimeStamp has a value
|
||||
effectiveTimeTimeStamp ??= DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
||||
|
||||
final DateTime? picked = await showDatePicker(
|
||||
context: event.context,
|
||||
initialDate: DateTime.now(),
|
||||
@ -94,7 +96,8 @@ class VisitorPasswordBloc
|
||||
lastDate: DateTime.now().add(const Duration(days: 5095)),
|
||||
);
|
||||
|
||||
if (picked != null) {
|
||||
if (picked == null) return;
|
||||
|
||||
final TimeOfDay? timePicked = await showTimePicker(
|
||||
context: event.context,
|
||||
initialTime: TimeOfDay.now(),
|
||||
@ -105,18 +108,14 @@ class VisitorPasswordBloc
|
||||
primary: ColorsManager.primaryColor,
|
||||
onSurface: Colors.black,
|
||||
),
|
||||
buttonTheme: const ButtonThemeData(
|
||||
colorScheme: ColorScheme.light(
|
||||
primary: Colors.green,
|
||||
),
|
||||
),
|
||||
),
|
||||
child: child!,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
if (timePicked != null) {
|
||||
if (timePicked == null) return;
|
||||
|
||||
final selectedDateTime = DateTime(
|
||||
picked.year,
|
||||
picked.month,
|
||||
@ -124,57 +123,98 @@ class VisitorPasswordBloc
|
||||
timePicked.hour,
|
||||
timePicked.minute,
|
||||
);
|
||||
|
||||
final selectedTimestamp =
|
||||
selectedDateTime.millisecondsSinceEpoch ~/ 1000;
|
||||
final selectedTimestamp = selectedDateTime.millisecondsSinceEpoch ~/ 1000;
|
||||
final currentTimestamp = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
||||
|
||||
if (event.isStart) {
|
||||
// START TIME VALIDATION
|
||||
if (expirationTimeTimeStamp != null &&
|
||||
selectedTimestamp > expirationTimeTimeStamp!) {
|
||||
CustomSnackBar.displaySnackBar(
|
||||
'Effective Time cannot be later than Expiration Time.',
|
||||
);
|
||||
return;
|
||||
}
|
||||
if(selectedTimestamp < DateTime.now().millisecondsSinceEpoch ~/ 1000) {
|
||||
if(selectedTimestamp < DateTime.now().millisecondsSinceEpoch ~/ 1000) {
|
||||
await showDialog<void>(
|
||||
context: event.context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Effective Time cannot be earlier than current time.'),
|
||||
title: const Text(
|
||||
'Effective Time cannot be later than Expiration Time.',
|
||||
),
|
||||
actionsAlignment: MainAxisAlignment.center,
|
||||
content:
|
||||
FilledButton(
|
||||
content: FilledButton(
|
||||
onPressed: () {
|
||||
Navigator.of(event.context).pop();
|
||||
add(SelectTimeVisitorPassword(context: event.context, isStart: true, isRepeat: false));
|
||||
add(SelectTimeVisitorPassword(
|
||||
context: event.context,
|
||||
isStart: true,
|
||||
isRepeat: false,
|
||||
));
|
||||
},
|
||||
child: const Text('OK'),
|
||||
),
|
||||
|
||||
),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (selectedTimestamp < currentTimestamp) {
|
||||
await showDialog<void>(
|
||||
context: event.context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text(
|
||||
'Effective Time cannot be earlier than current time.',
|
||||
),
|
||||
actionsAlignment: MainAxisAlignment.center,
|
||||
content: FilledButton(
|
||||
onPressed: () {
|
||||
Navigator.of(event.context).pop();
|
||||
add(SelectTimeVisitorPassword(
|
||||
context: event.context,
|
||||
isStart: true,
|
||||
isRepeat: false,
|
||||
));
|
||||
},
|
||||
child: const Text('OK'),
|
||||
),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Save effective time
|
||||
effectiveTimeTimeStamp = selectedTimestamp;
|
||||
startTimeAccess = selectedDateTime.toString().split('.').first;
|
||||
} else {
|
||||
// END TIME VALIDATION
|
||||
if (effectiveTimeTimeStamp != null &&
|
||||
selectedTimestamp < effectiveTimeTimeStamp!) {
|
||||
CustomSnackBar.displaySnackBar(
|
||||
await showDialog<void>(
|
||||
context: event.context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text(
|
||||
'Expiration Time cannot be earlier than Effective Time.',
|
||||
),
|
||||
actionsAlignment: MainAxisAlignment.center,
|
||||
content: FilledButton(
|
||||
onPressed: () {
|
||||
Navigator.of(event.context).pop();
|
||||
add(SelectTimeVisitorPassword(
|
||||
context: event.context,
|
||||
isStart: false,
|
||||
isRepeat: false,
|
||||
));
|
||||
},
|
||||
child: const Text('OK'),
|
||||
),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Save expiration time
|
||||
expirationTimeTimeStamp = selectedTimestamp;
|
||||
endTimeAccess = selectedDateTime.toString().split('.').first;
|
||||
}
|
||||
|
||||
emit(ChangeTimeState());
|
||||
emit(VisitorPasswordInitial());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool toggleRepeat(
|
||||
ToggleRepeatEvent event, Emitter<VisitorPasswordState> emit) {
|
||||
|
Reference in New Issue
Block a user