tap filter

This commit is contained in:
mohammad
2024-08-22 16:52:41 +03:00
parent e4f8924e93
commit f5a7441b3c
16 changed files with 333 additions and 318 deletions

View File

@ -5,23 +5,25 @@ 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/constants/const.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<TabChangedEvent>(selectFilterTap);
on<SelectTime>(selectTime);
on<FilterDataEvent>(_filterData);
on<ResetSearch>(resetSearch);
on<TabChangedEvent>(onTabChanged);
}
String startTime = 'Start Time';
String endTime = 'End Time';
String startTime = 'Start Date';
String endTime = 'End Date';
int? effectiveTimeTimeStamp;
int? expirationTimeTimeStamp;
TextEditingController passwordName= TextEditingController();
List<PasswordModel> filteredData = []; // To store filtered data
List<PasswordModel> filteredData = [];
List<PasswordModel> data=[];
Future<void> _onFetchTableData(
@ -29,11 +31,26 @@ class AccessBloc extends Bloc<AccessEvent, AccessState> {
try {
emit(AccessLoaded());
data = await AccessMangApi().fetchVisitorPassword();
emit(TableLoaded(data));
filteredData= data;
updateTabsCount();
emit(TableLoaded(data));
} catch (e) {
emit(FailedState(e.toString()));
}
}
void updateTabsCount() {
// Count occurrences based on the type field
int toBeEffectiveCount = data.where((item) => item.passwordStatus.value== 'To Be Effective').length;
int effectiveCount = data.where((item) => item.passwordStatus.value == 'Effective').length;
int expiredCount = data.where((item) => item.passwordStatus.value == 'Expired').length;
// Update tab labels with counts
tabs[1] = 'To Be Effective ($toBeEffectiveCount)';
tabs[2] = 'Effective ($effectiveCount)';
tabs[3] = 'Expired ($expiredCount)';
}
int selectedIndex = 0;
@ -49,9 +66,8 @@ class AccessBloc extends Bloc<AccessEvent, AccessState> {
try {
emit(AccessLoaded());
selectedIndex= event.selectedIndex;
emit(AccessInitial());
emit(AccessInitial());
emit(TableLoaded(data));
} catch (e) {
emit(FailedState( e.toString()));
return;
@ -60,6 +76,7 @@ class AccessBloc extends Bloc<AccessEvent, AccessState> {
Future<void> selectTime(SelectTime event, Emitter<AccessState> emit) async {
final DateTime? picked = await showDatePicker(
context: event.context,
initialDate: DateTime.now(),
@ -120,27 +137,27 @@ class AccessBloc extends Bloc<AccessEvent, AccessState> {
}
}
}
emit(AccessInitial());
emit(TableLoaded(data));
emit(ChangeTimeState());
}
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
// Filter by password name if provided
if (event.passwordName != null && event.passwordName!.isNotEmpty) {
final bool matchesName = item.passwodName != null &&
item.passwodName.contains(event.passwordName);
final bool matchesName = item.passwordName != null &&
item.passwordName.contains(event.passwordName);
if (!matchesName) {
matchesCriteria = false;
}
}
// Check if the time range should be used for filtering
// Filter by date range if provided
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) {
@ -153,22 +170,31 @@ class AccessBloc extends Bloc<AccessEvent, AccessState> {
}
}
}
// Filter by tab selection
if (event.selectedTabIndex == 1 && item.passwordStatus.value != 'To Be Effective') {
matchesCriteria = false;
} else if (event.selectedTabIndex == 2 && item.passwordStatus.value != 'Effective') {
matchesCriteria = false;
} else if (event.selectedTabIndex == 3 && item.passwordStatus.value != 'Expired') {
matchesCriteria = false;
}
return matchesCriteria;
}).toList();
print('Filtered data: $filteredData');
emit(TableLoaded(filteredData));
} catch (e) {
print('Error occurred during filtering: $e');
emit(FailedState(e.toString()));
}
}
// ResetSearch
resetSearch(ResetSearch event, Emitter<AccessState> emit) async{
emit(AccessLoaded());
startTime = 'Start Time';
endTime = 'End Time';
passwordName.clear();
selectedIndex=0;
add(FetchTableData());
}
String timestampToDate(dynamic timestamp) {
@ -176,5 +202,38 @@ class AccessBloc extends Bloc<AccessEvent, AccessState> {
return "${dateTime.year}/${dateTime.month.toString().padLeft(2, '0')}/${dateTime.day.toString().padLeft(2, '0')}";
}
Future<void> onTabChanged(TabChangedEvent event, Emitter<AccessState> emit) async {
try {
emit(AccessLoaded());
selectedIndex = event.selectedIndex;
// Apply filtering based on selected tab
switch (selectedIndex) {
case 0: // All
filteredData = data;
break;
case 1: // To Be Effective
filteredData = data.where((item) => item.passwordStatus.value == "To Be Effective").toList();
break;
case 2: // Effective
filteredData = data.where((item) => item.passwordStatus.value == "Effective").toList();
break;
case 3: // Expired
filteredData = data.where((item) => item.passwordStatus.value == "Expired").toList();
break;
default:
filteredData = data;
}
add(FilterDataEvent(
selectedTabIndex: selectedIndex, // Pass the selected tab index
passwordName: passwordName.text.toLowerCase(),
startTime: effectiveTimeTimeStamp,
endTime: expirationTimeTimeStamp
));
emit(TableLoaded(filteredData));
} catch (e) {
emit(FailedState(e.toString()));
}
}
}

View File

@ -31,10 +31,16 @@ class FilterDataEvent extends AccessEvent {
final String? passwordName;
final int? startTime;
final int? endTime;
final int selectedTabIndex; // Add this field
const FilterDataEvent({
this.passwordName,
this.startTime,
this.endTime,
required this.selectedTabIndex, // Initialize this field
});
}