merge dev to devices view

This commit is contained in:
ashrafzarkanisala
2024-08-24 14:21:10 +03:00
32 changed files with 1822 additions and 1095 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,14 +31,25 @@ 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() {
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;
tabs[1] = 'To Be Effective ($toBeEffectiveCount)';
tabs[2] = 'Effective ($effectiveCount)';
tabs[3] = 'Expired ($expiredCount)';
}
int selectedIndex = 0;
final List<String> tabs = [
'All',
'To Be Effective (0)',
@ -49,9 +62,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 +72,8 @@ class AccessBloc extends Bloc<AccessEvent, AccessState> {
Future<void> selectTime(SelectTime event, Emitter<AccessState> emit) async {
emit(AccessLoaded());
final DateTime? picked = await showDatePicker(
context: event.context,
initialDate: DateTime.now(),
@ -120,27 +134,23 @@ 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
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
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 +163,30 @@ class AccessBloc extends Bloc<AccessEvent, AccessState> {
}
}
}
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;
effectiveTimeTimeStamp=null;
expirationTimeTimeStamp=null;
add(FetchTableData());
}
String timestampToDate(dynamic timestamp) {
@ -176,5 +194,36 @@ 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;
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,
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
});
}

View File

@ -6,8 +6,8 @@ class PasswordModel {
final dynamic effectiveTime;
final dynamic passwordCreated;
final dynamic createdTime;
final dynamic passwodName; // New field
final dynamic passwordStatus;
final dynamic passwordName; // New field
final AccessStatus passwordStatus;
final AccessType passwordType;
final dynamic deviceUuid;
@ -17,8 +17,8 @@ class PasswordModel {
this.effectiveTime,
this.passwordCreated,
this.createdTime,
this.passwodName, // New field
this.passwordStatus,
this.passwordName, // New field
required this.passwordStatus,
required this.passwordType,
this.deviceUuid,
});
@ -30,9 +30,9 @@ class PasswordModel {
effectiveTime: json['effectiveTime'],
passwordCreated: json['passwordCreated'],
createdTime: json['createdTime'],
passwodName: json['passwodName']??'No name', // New field
passwordStatus: json['passwordStatus'],
passwordType:AccessTypeExtension.fromString(json['passwordType']) ,
passwordName: json['passwordName']??'No name', // New field
passwordStatus:AccessStatusExtension.fromString(json['passwordStatus']),
passwordType:AccessTypeExtension.fromString(json['passwordType']),
deviceUuid: json['deviceUuid'],
);
}
@ -44,13 +44,11 @@ class PasswordModel {
'effectiveTime': effectiveTime,
'passwordCreated': passwordCreated,
'createdTime': createdTime,
'passwodName': passwodName, // New field
'passwodName': passwordName, // New field
'passwordStatus': passwordStatus,
'passwordType': passwordType,
'deviceUuid': deviceUuid,
};
}
List<PasswordModel> parsePasswordList(List<dynamic> jsonList) {
return jsonList.map((json) => PasswordModel.fromJson(json)).toList();
}
}

View File

@ -9,6 +9,7 @@ import 'package:syncrow_web/pages/common/buttons/default_button.dart';
import 'package:syncrow_web/pages/common/filter/filter_widget.dart';
import 'package:syncrow_web/pages/visitor_password/view/visitor_password_dialog.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
import 'package:syncrow_web/utils/constants/const.dart';
import 'package:syncrow_web/utils/style.dart';
import 'package:syncrow_web/web_layout/web_scaffold.dart';
@ -41,270 +42,278 @@ class AccessManagementPage extends StatelessWidget {
create: (BuildContext context) =>
AccessBloc()..add(FetchTableData()),
child: BlocConsumer<AccessBloc, AccessState>(
listener: (context, state) {
if (state is FailedState) {
// CustomSnackBar.displaySnackBar(
// state.errorMessage
// );
}
}, builder: (context, state) {
final accessBloc = BlocProvider.of<AccessBloc>(context);
return Container(
padding: EdgeInsets.all(30),
height: size.height,
width: size.width,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
FilterWidget(
size: size,
tabs: accessBloc.tabs,
selectedIndex: accessBloc.selectedIndex,
onTabChanged: (index) {
accessBloc.add(TabChangedEvent(index));
},
),
const SizedBox(
height: 20,
),
Row(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
const Text('Name'),
Container(
width: size.width * 0.15,
decoration: containerDecoration,
child: TextFormField(
controller: accessBloc.passwordName,
style: const TextStyle(color: Colors.black),
decoration: textBoxDecoration()!
.copyWith(hintText: 'Please enter'),
)),
],
),
const SizedBox(
width: 15,
),
DateTimeWebWidget(
isRequired: false,
title: 'Access Time',
size: size,
endTime: () {
accessBloc.add(
SelectTime(context: context, isStart: false));
},
startTime: () {
accessBloc.add(
SelectTime(context: context, isStart: true));
},
firstString:
BlocProvider.of<AccessBloc>(context).startTime,
secondString:
BlocProvider.of<AccessBloc>(context).endTime,
),
const SizedBox(
width: 15,
),
SizedBox(
width: size.width * 0.06,
listener: (context, state) {},
builder: (context, state) {
final accessBloc = BlocProvider.of<AccessBloc>(context);
final filteredData = accessBloc.filteredData;
return state is AccessLoaded
? const Center(child: CircularProgressIndicator())
: Container(
padding: EdgeInsets.all(30),
height: size.height,
width: size.width,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(''),
Container(
decoration: containerDecoration,
child: DefaultButton(
onPressed: () {
accessBloc.add(FilterDataEvent(
passwordName:
accessBloc.passwordName.text,
startTime: accessBloc
.effectiveTimeTimeStamp,
endTime: accessBloc
.expirationTimeTimeStamp));
},
borderRadius: 9,
child: const Text('Search'))),
],
),
),
const SizedBox(
width: 10,
),
SizedBox(
width: size.width * 0.06,
child: Column(
children: [
Text(''),
Container(
decoration: containerDecoration,
child: DefaultButton(
onPressed: () {
accessBloc.add(ResetSearch());
},
backgroundColor: ColorsManager.whiteColors,
borderRadius: 9,
child: Text(
'Reset',
style: Theme.of(context)
.textTheme
.bodySmall!
.copyWith(color: Colors.black),
height: size.height * 0.05,
child: Flexible(
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount:
BlocProvider.of<AccessBloc>(context)
.tabs
.length,
shrinkWrap: true,
itemBuilder: (context, index) {
final isSelected = index ==
BlocProvider.of<AccessBloc>(context)
.selectedIndex;
return InkWell(
onTap: () {
BlocProvider.of<AccessBloc>(context)
.add(TabChangedEvent(index));
},
child: Container(
decoration: BoxDecoration(
color: ColorsManager.boxColor,
border: Border.all(
color: isSelected
? Colors.blue
: Colors.transparent,
width: 2.0,
),
borderRadius: index == 0
? const BorderRadius.only(
topLeft:
Radius.circular(10),
bottomLeft:
Radius.circular(10))
: index == 3
? const BorderRadius.only(
topRight:
Radius.circular(10),
bottomRight:
Radius.circular(10))
: null,
),
padding: const EdgeInsets.only(
left: 10, right: 10),
child: Center(
child: Text(
BlocProvider.of<AccessBloc>(
context)
.tabs[index],
style: TextStyle(
color: isSelected
? Colors.blue
: Colors.black,
),
),
),
),
);
},
),
),
),
const SizedBox(
height: 20,
),
Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.end,
textBaseline: TextBaseline.ideographic,
children: [
Column(
crossAxisAlignment:
CrossAxisAlignment.start,
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Text(
'Name',
style: Theme.of(context)
.textTheme
.bodySmall!
.copyWith(
color: Colors.black,
fontSize: 13),
),
const SizedBox(
height: 5,
),
Container(
height: 43,
width: size.width * 0.15,
decoration: containerDecoration,
child: TextFormField(
controller: accessBloc.passwordName,
style: const TextStyle(
color: Colors.black),
decoration: textBoxDecoration()!
.copyWith(
hintText: 'Please enter'),
)),
],
),
const SizedBox(
width: 15,
),
DateTimeWebWidget(
icon: Assets.calendarIcon,
isRequired: false,
title: 'Access Time',
size: size,
endTime: () {
accessBloc.add(SelectTime(
context: context, isStart: false));
},
startTime: () {
accessBloc.add(SelectTime(
context: context, isStart: true));
},
firstString:
BlocProvider.of<AccessBloc>(context)
.startTime,
secondString:
BlocProvider.of<AccessBloc>(context)
.endTime,
),
const SizedBox(
width: 15,
),
SizedBox(
height: 45,
width: size.width * 0.06,
child: Container(
decoration: containerDecoration,
child: DefaultButton(
onPressed: () {
accessBloc.add(FilterDataEvent(
selectedTabIndex: BlocProvider
.of<AccessBloc>(
context)
.selectedIndex, // Pass the selected tab index
passwordName: accessBloc
.passwordName.text
.toLowerCase(),
startTime: accessBloc
.effectiveTimeTimeStamp,
endTime: accessBloc
.expirationTimeTimeStamp));
},
borderRadius: 9,
child: const Text('Search'))),
),
const SizedBox(
width: 10,
),
SizedBox(
height: 45,
width: size.width * 0.06,
child: Container(
decoration: containerDecoration,
child: DefaultButton(
onPressed: () {
accessBloc.add(ResetSearch());
},
backgroundColor:
ColorsManager.whiteColors,
borderRadius: 9,
child: Text(
'Reset',
style: Theme.of(context)
.textTheme
.bodySmall!
.copyWith(color: Colors.black),
),
),
),
),
],
),
const SizedBox(
height: 20,
),
Wrap(
children: [
Container(
width: size.width * 0.15,
decoration: containerDecoration,
child: DefaultButton(
onPressed: () {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return const VisitorPasswordDialog();
},
).then((v) {
if (v != null) {
accessBloc.add(FetchTableData());
}
});
},
borderRadius: 8,
child: const Text(
'+ Create Visitor Password ')),
),
const SizedBox(
width: 10,
),
Container(
width: size.width * 0.12,
decoration: containerDecoration,
child: DefaultButton(
borderRadius: 8,
backgroundColor:
ColorsManager.whiteColors,
child: Text(
'Admin Password',
style: Theme.of(context)
.textTheme
.bodySmall!
.copyWith(color: Colors.black),
)))
],
),
const SizedBox(
height: 20,
),
Expanded(
child: DynamicTable(
isEmpty: filteredData.isEmpty,
withCheckBox: false,
size: size,
cellDecoration: containerDecoration,
headers: const [
'Name',
'Access Type',
'Access Period',
'Accessible Device',
'Authorizer',
'Authorization Date & Time',
'Access Status'
],
data: filteredData.map((item) {
return [
item.passwordName.toString(),
item.passwordType.value,
('${accessBloc.timestampToDate(item.effectiveTime)} - ${accessBloc.timestampToDate(item.invalidTime)}'),
item.deviceUuid.toString(),
'',
'',
item.passwordStatus.value
];
}).toList(),
)
// : const Center(child: CircularProgressIndicator()),
)
],
),
),
],
),
const SizedBox(
height: 20,
),
Wrap(
children: [
Container(
width: size.width * 0.15,
decoration: containerDecoration,
child: DefaultButton(
onPressed: () {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return const VisitorPasswordDialog();
},
);
},
borderRadius: 8,
child:
const Text('+ Create Visitor Password ')),
),
const SizedBox(
width: 10,
),
Container(
width: size.width * 0.12,
decoration: containerDecoration,
child: DefaultButton(
borderRadius: 8,
backgroundColor: ColorsManager.whiteColors,
child: Text(
'Admin Password',
style: Theme.of(context)
.textTheme
.bodySmall!
.copyWith(color: Colors.black),
)))
],
),
const SizedBox(
height: 20,
),
Expanded(
child: state is TableLoaded
? DynamicTable(
withCheckBox: false,
size: size,
cellDecoration: containerDecoration,
headers: const [
'Name',
'Access Type',
'Access Period',
'Device Id',
'Authorizer',
'Authorization Date & Time',
'Access Status'
],
data: state.data.map((item) {
return [
item.passwodName.toString(),
item.passwordType.value,
('${accessBloc.timestampToDate(item.effectiveTime)} - ${accessBloc.timestampToDate(item.invalidTime)}'),
item.deviceUuid.toString(),
'',
'',
''
];
}).toList(),
)
: const Center(child: CircularProgressIndicator()),
)
],
),
);
})));
);
})));
}
// Container TableWidget(Size size, TableLoaded state,AccessBloc accessBloc) {
// return Container(
// decoration: containerDecoration,
// width: size.width,
// child: Padding(
// padding: const EdgeInsets.all(10.0),
// child: ListView(
// scrollDirection: Axis.horizontal,
// children: [
// Container(
// width: size.width,
// height: size.height,
// child: Column(
// children: [
// Container(
// color: ColorsManager.boxColor,
// child: Row(
// children: [
// _buildTableHeaderCell('Password name'),
// _buildTableHeaderCell(' Password Type'),
// _buildTableHeaderCell('Start Time'),
// _buildTableHeaderCell('End Time'),
// _buildTableHeaderCell('Device Id'),
// // _buildTableHeaderCell('Authorization Source'),
// // _buildTableHeaderCell('Authorizer'),
// _buildTableHeaderCell('Password Created'),
// // _buildTableHeaderCell('Access Status'),
// _buildTableHeaderCell('Password Status'),
// ],
// ),
// ),
// Expanded(
// child: Container(
// width: size.width,
// color: ColorsManager.whiteColors,
// child: ListView(
// shrinkWrap: true,
// children: [
// Column(
// children: state.data.map((item) {
// return Row(
// children: [
// _buildTableCell(item.passwodName),
// _buildTableCell(item.passwordType),
//
// _buildTableCell(accessBloc.timestampToDateTime(item.effectiveTime).toString()),
// _buildTableCell(accessBloc.timestampToDateTime(item.invalidTime).toString()),
// _buildTableCell(item.deviceUuid.toString()),
// // _buildTableCell(item.authorizationSource),
// // _buildTableCell(item.authorizer),
// _buildTableCell(item.passwordCreated!=null?accessBloc.timestampToDateTime(item.passwordCreated).toString():'no data'),
// // _buildTableCell(item.accessStatus),
// _buildTableCell(item.passwordStatus.toString()),
// ],
// );
// }).toList(),
// ),
// ],
// ),
// ),
// ),
// ],
// ),
// ),
// ],
// ),
// ),
// );
// }
}