Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: Update attendance and fix null-error #308

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/data/datasources/user_remote.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,16 @@ class UserRemoteDataImpl implements UserRemoteData {
if (jsonResponse["ROWS"] != null) {
Map<String, dynamic>.from(jsonResponse["ROWS"]).forEach((date, row) {
if (row.containsKey("START")) {
attendance.add(
AttendanceModel.fromJson(row["START"]).copyWith(date: date));
if (row["START"] != null) {
attendance.add(
AttendanceModel.fromJson(row["START"]).copyWith(date: date));
}
}
if (row.containsKey("END")) {
attendance
.add(AttendanceModel.fromJson(row["END"]).copyWith(date: date));
if (row["END"] != null) {
attendance.add(
AttendanceModel.fromJson(row["END"]).copyWith(date: date));
}
}
});
}
Expand Down
117 changes: 91 additions & 26 deletions lib/presentation/pages/profile/profile_attendance_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get/get.dart';
import 'package:rtu_mirea_app/domain/entities/attendance.dart';
import 'package:rtu_mirea_app/presentation/bloc/attendance_bloc/attendance_bloc.dart';
import 'package:rtu_mirea_app/presentation/bloc/user_bloc/user_bloc.dart';
import 'package:rtu_mirea_app/presentation/pages/profile/widgets/attendance_card.dart';
Expand Down Expand Up @@ -34,6 +36,94 @@ class _ProfileAttendancePageState extends State<ProfileAttendancePage> {
return res;
}

List<List<Attendance>> _groupAttendanceByDate(List<Attendance> attendance) {
final Map<String, List<Attendance>> groupedAttendance = {};
for (var element in attendance) {
if (groupedAttendance.containsKey(element.date)) {
groupedAttendance[element.date]!.add(element);
} else {
groupedAttendance[element.date] = [element];
}
}

final List<List<Attendance>> result = [];

final dateFormat = DateFormat('dd.MM.yyyy HH:mm:ss');

groupedAttendance.forEach((key, value) {
value.sort((a, b) {
final aDateTime = dateFormat.parse('${a.date} ${a.time}');
final bDateTime = dateFormat.parse('${b.date} ${b.time}');

return aDateTime.compareTo(bDateTime);
});

final List<Attendance> entryExitAttendance = [];

Attendance? entryAttendance;
Attendance? exitAttendance;

for (final element in value) {
if (element.eventType == 'Вход' && entryAttendance == null) {
entryAttendance = element;
} else if (element.eventType == 'Выход') {
exitAttendance = element;
}
}

if (entryAttendance != null) {
entryExitAttendance.add(entryAttendance!);
}
if (exitAttendance != null) {
entryExitAttendance.add(exitAttendance!);
}
result.add(entryExitAttendance);
});

return result;
}

Widget _buildAttendanceList(List<Attendance> attendance) {
final List<List<Attendance>> groupedAttendance =
_groupAttendanceByDate(attendance);
return Expanded(
child: Column(
children: [
const SizedBox(height: 8),
Text('Дней посещено: ${groupedAttendance.length}',
style: AppTextStyle.body),
const SizedBox(height: 8),
Expanded(
child: ListView.builder(
itemCount: groupedAttendance.length,
itemBuilder: (context, index) {
return Padding(
padding:
const EdgeInsets.symmetric(vertical: 16, horizontal: 24),
child: Column(children: [
AttendanceCard(
type: groupedAttendance[index][0].eventType,
date: groupedAttendance[index][0].date,
time: groupedAttendance[index][0].time,
),
if (groupedAttendance[index].length > 1) ...[
const SizedBox(height: 8),
AttendanceCard(
type: groupedAttendance[index][1].eventType,
date: groupedAttendance[index][1].date,
time: groupedAttendance[index][1].time,
),
],
]),
);
},
),
),
],
),
);
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -92,32 +182,7 @@ class _ProfileAttendancePageState extends State<ProfileAttendancePage> {
child: Center(child: CircularProgressIndicator()))
else if (state is AttendanceLoaded &&
state.attendance.isNotEmpty)
Expanded(
child: Column(
children: [
const SizedBox(height: 8),
Text('Дней посещено: ${state.visitsCount}',
style: AppTextStyle.body),
const SizedBox(height: 8),
Expanded(
child: ListView.builder(
itemCount: state.attendance.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.symmetric(
vertical: 16, horizontal: 24),
child: AttendanceCard(
type: state.attendance[index].eventType,
date: state.attendance[index].date,
time: state.attendance[index].time,
),
);
},
),
),
],
),
),
_buildAttendanceList(state.attendance),
if (state is AttendanceLoaded && state.attendance.isEmpty)
Expanded(
child: Center(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class AttendanceCard extends StatelessWidget {
alignment: Alignment.center,
child: type == "Вход"
? const Icon(FontAwesomeIcons.rightToBracket, size: 15)
: const Icon(FontAwesomeIcons.rightToBracket, size: 15),
: const Icon(FontAwesomeIcons.rightFromBracket, size: 15),
),
),
const SizedBox(width: 55.50),
Expand Down