mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-09 22:57:21 +00:00
182 lines
6.0 KiB
Dart
182 lines
6.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:syncrow_web/pages/access_management/bloc/access_event.dart';
|
|
import 'package:syncrow_web/pages/access_management/bloc/access_state.dart';
|
|
import 'package:syncrow_web/pages/access_management/model/password_model.dart';
|
|
import 'package:syncrow_web/services/access_mang_api.dart';
|
|
import 'package:syncrow_web/utils/color_manager.dart';
|
|
import 'package:syncrow_web/utils/snack_bar.dart';
|
|
|
|
class AccessBloc extends Bloc<AccessEvent, AccessState> {
|
|
AccessBloc() : super((AccessInitial())) {
|
|
on<FetchTableData>(_onFetchTableData);
|
|
on<TabChangedEvent>(selectFilterTap);
|
|
on<SelectTime>(selectTime);
|
|
on<FilterDataEvent>(_filterData);
|
|
on<ResetSearch>(resetSearch);
|
|
}
|
|
String startTime = 'Start Time';
|
|
String endTime = 'End Time';
|
|
|
|
int? effectiveTimeTimeStamp;
|
|
int? expirationTimeTimeStamp;
|
|
TextEditingController passwordName= TextEditingController();
|
|
List<PasswordModel> filteredData = []; // To store filtered data
|
|
List<PasswordModel> data=[];
|
|
|
|
Future<void> _onFetchTableData(
|
|
FetchTableData event, Emitter<AccessState> emit) async {
|
|
try {
|
|
emit(AccessLoaded());
|
|
data = await AccessMangApi().fetchVisitorPassword();
|
|
emit(TableLoaded(data));
|
|
} catch (e) {
|
|
emit(FailedState(e.toString()));
|
|
}
|
|
}
|
|
|
|
int selectedIndex = 0;
|
|
|
|
final List<String> tabs = [
|
|
'All',
|
|
'To Be Effective (0)',
|
|
'Effective (0)',
|
|
'Expired'
|
|
];
|
|
|
|
|
|
Future selectFilterTap(TabChangedEvent event, Emitter<AccessState> emit) async {
|
|
try {
|
|
emit(AccessLoaded());
|
|
selectedIndex= event.selectedIndex;
|
|
emit(AccessInitial());
|
|
emit(TableLoaded(data));
|
|
|
|
} catch (e) {
|
|
emit(FailedState( e.toString()));
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
Future<void> selectTime(SelectTime event, Emitter<AccessState> emit) async {
|
|
final DateTime? picked = await showDatePicker(
|
|
context: event.context,
|
|
initialDate: DateTime.now(),
|
|
firstDate: DateTime(2015, 8),
|
|
lastDate: DateTime(2101),
|
|
);
|
|
if (picked != null) {
|
|
final TimeOfDay? timePicked = await showTimePicker(
|
|
context: event.context,
|
|
initialTime: TimeOfDay.now(),
|
|
|
|
builder: (context, child) {
|
|
return Theme(
|
|
data: ThemeData.light().copyWith(
|
|
colorScheme: const ColorScheme.light(
|
|
primary: ColorsManager.primaryColor,
|
|
onSurface: Colors.black,
|
|
),
|
|
buttonTheme: const ButtonThemeData(
|
|
colorScheme: ColorScheme.light(
|
|
primary: Colors.green,
|
|
),
|
|
),
|
|
),
|
|
child: child!,
|
|
);
|
|
},
|
|
);
|
|
if (timePicked != null) {
|
|
final selectedDateTime = DateTime(
|
|
picked.year,
|
|
picked.month,
|
|
picked.day,
|
|
timePicked.hour,
|
|
timePicked.minute,
|
|
);
|
|
final selectedTimestamp = DateTime(
|
|
selectedDateTime.year,
|
|
selectedDateTime.month,
|
|
selectedDateTime.day,
|
|
selectedDateTime.hour,
|
|
selectedDateTime.minute,
|
|
).millisecondsSinceEpoch ~/ 1000; // Divide by 1000 to remove milliseconds
|
|
if (event.isStart) {
|
|
if (expirationTimeTimeStamp != null && selectedTimestamp > expirationTimeTimeStamp!) {
|
|
CustomSnackBar.displaySnackBar('Effective Time cannot be later than Expiration Time.');
|
|
} else {
|
|
startTime = selectedDateTime.toString().split('.').first; // Remove seconds and milliseconds
|
|
effectiveTimeTimeStamp = selectedTimestamp;
|
|
}
|
|
} else {
|
|
if (effectiveTimeTimeStamp != null && selectedTimestamp < effectiveTimeTimeStamp!) {
|
|
CustomSnackBar.displaySnackBar('Expiration Time cannot be earlier than Effective Time.');
|
|
} else {
|
|
endTime = selectedDateTime.toString().split('.').first; // Remove seconds and milliseconds
|
|
expirationTimeTimeStamp = selectedTimestamp;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
emit(AccessInitial());
|
|
emit(TableLoaded(data));
|
|
}
|
|
|
|
Future<void> _filterData(FilterDataEvent event, Emitter<AccessState> emit) async {
|
|
emit(AccessLoaded());
|
|
try {
|
|
// Filter the data based on the provided criteria
|
|
filteredData = data.where((item) {
|
|
bool matchesCriteria = true;
|
|
// Check if the password name should be used for filtering
|
|
if (event.passwordName != null && event.passwordName!.isNotEmpty) {
|
|
final bool matchesName = item.passwodName != null &&
|
|
item.passwodName.contains(event.passwordName!);
|
|
if (!matchesName) {
|
|
matchesCriteria = false;
|
|
}
|
|
}
|
|
// Check if the time range should be used for filtering
|
|
if (event.startTime != null && event.endTime != null) {
|
|
// Ensure effectiveTime and invalidTime are treated as integers
|
|
final int? effectiveTime = int.tryParse(item.effectiveTime.toString());
|
|
final int? invalidTime = int.tryParse(item.invalidTime.toString());
|
|
if (effectiveTime == null || invalidTime == null) {
|
|
matchesCriteria = false;
|
|
} else {
|
|
final bool matchesStartTime = effectiveTime >= event.startTime!;
|
|
final bool matchesEndTime = invalidTime <= event.endTime!;
|
|
if (!matchesStartTime || !matchesEndTime) {
|
|
matchesCriteria = false;
|
|
}
|
|
}
|
|
}
|
|
return matchesCriteria;
|
|
}).toList();
|
|
print('Filtered data: $filteredData'); // Print to debug filtered data
|
|
emit(TableLoaded(filteredData));
|
|
} catch (e) {
|
|
print('Error occurred during filtering: $e');
|
|
}
|
|
}
|
|
// ResetSearch
|
|
resetSearch(ResetSearch event, Emitter<AccessState> emit) async{
|
|
emit(AccessLoaded());
|
|
startTime = 'Start Time';
|
|
endTime = 'End Time';
|
|
passwordName.clear();
|
|
add(FetchTableData());
|
|
|
|
}
|
|
|
|
|
|
DateTime timestampToDateTime(dynamic timestamp) {
|
|
return DateTime.fromMillisecondsSinceEpoch(int.parse(timestamp) * 1000);
|
|
}
|
|
|
|
|
|
|
|
}
|