Skip to content

Commit

Permalink
feat: Remove Scores table and update UI
Browse files Browse the repository at this point in the history
  • Loading branch information
0niel committed Jan 13, 2024
1 parent bf9f14a commit 8f59567
Show file tree
Hide file tree
Showing 20 changed files with 781 additions and 2,273 deletions.
2 changes: 1 addition & 1 deletion lib/presentation/bloc/nfc_pass_bloc/nfc_pass_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class NfcPassBloc extends Bloc<NfcPassEvent, NfcPassState> {
(failure) => studentId = null,
(userData) {
// Prevent multiple fetches of user profile data
userBloc.add(UserEvent.setAuntificatedData(user: userData));
userBloc.add(SetAuthenticatedData(user: userData));

var student = userData.students
.firstWhereOrNull((element) => element.status == 'активный');
Expand Down
42 changes: 21 additions & 21 deletions lib/presentation/bloc/user_bloc/user_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:get/get.dart';
import 'package:rtu_mirea_app/domain/entities/student.dart';
import 'package:rtu_mirea_app/domain/entities/user.dart';
Expand All @@ -12,7 +12,6 @@ import 'package:sentry_flutter/sentry_flutter.dart';

part 'user_event.dart';
part 'user_state.dart';
part 'user_bloc.freezed.dart';

class UserBloc extends Bloc<UserEvent, UserState> {
final LogIn logIn;
Expand All @@ -25,19 +24,19 @@ class UserBloc extends Bloc<UserEvent, UserState> {
required this.logOut,
required this.getUserData,
required this.getAuthToken,
}) : super(const _Unauthorized()) {
on<_LogIn>(_onLogInEvent);
on<_LogOut>(_onLogOutEvent);
on<_Started>(_onGetUserDataEvent);
on<_GetUserData>(_onGetUserDataEvent);
on<_SetAuntificatedData>(_onSetAuntificatedDataEvent);
}) : super(const UserState()) {
on<LogInEvent>(_onLogInEvent);
on<LogOutEvent>(_onLogOutEvent);
on<Started>(_onGetUserDataEvent);
on<GetUserDataEvent>(_onGetUserDataEvent);
on<SetAuthenticatedData>(_onSetAuntificatedDataEvent);
}

void _onSetAuntificatedDataEvent(
_SetAuntificatedData event,
SetAuthenticatedData event,
Emitter<UserState> emit,
) async {
emit(_LogInSuccess(event.user));
emit(state.copyWith(status: UserStatus.authorized, user: event.user));
}

void _setSentryUserIdentity(String id, String email, String group) {
Expand All @@ -49,7 +48,7 @@ class UserBloc extends Bloc<UserEvent, UserState> {
UserEvent event,
Emitter<UserState> emit,
) async {
if (state is _LogInSuccess) return;
if (state.status == UserStatus.authorized) return;

// We use oauth2 to get token. So we don't need to pass login and password
// to the server. We just need to pass them to the oauth2 server.
Expand All @@ -59,26 +58,25 @@ class UserBloc extends Bloc<UserEvent, UserState> {
final logInRes = await logIn();

logInRes.fold(
(failure) => emit(_LogInError(
failure.cause ?? "Ошибка при авторизации. Повторите попытку")),
(failure) => emit(state.copyWith(status: UserStatus.authorizeError)),
(res) {
loggedIn = true;
},
);

if (loggedIn) {
emit(const _Loading());
emit(state.copyWith(status: UserStatus.loading));
final user = await getUserData();

user.fold(
(failure) => emit(const _Unauthorized()),
(failure) => emit(state.copyWith(status: UserStatus.unauthorized)),
(user) {
FirebaseAnalytics.instance.logLogin();
var student = getActiveStudent(user);

_setSentryUserIdentity(
user.id.toString(), user.login, student.academicGroup);
emit(_LogInSuccess(user));
emit(state.copyWith(status: UserStatus.authorized, user: user));
},
);
}
Expand All @@ -89,7 +87,7 @@ class UserBloc extends Bloc<UserEvent, UserState> {
Emitter<UserState> emit,
) async {
final res = await logOut();
res.fold((failure) => null, (r) => emit(const _Unauthorized()));
res.fold((failure) => null, (r) => emit(state.copyWith()));
}

static Student getActiveStudent(User user) {
Expand All @@ -103,7 +101,7 @@ class UserBloc extends Bloc<UserEvent, UserState> {
UserEvent event,
Emitter<UserState> emit,
) async {
if (state is _LogInSuccess) return;
if (state.status == UserStatus.authorized) return;

final token = await getAuthToken();

Expand All @@ -113,14 +111,16 @@ class UserBloc extends Bloc<UserEvent, UserState> {

// If token in the storage, user is authorized at least once and we can
// try to get user data
emit(const _Loading());
emit(state.copyWith(status: UserStatus.loading));
final user = await getUserData();

user.fold((failure) => emit(const _Unauthorized()), (r) {
user.fold(
(failure) => emit(state.copyWith(status: UserStatus.unauthorized)),
(r) {
var student = getActiveStudent(r);

_setSentryUserIdentity(r.id.toString(), r.login, student.academicGroup);
emit(_LogInSuccess(r));
emit(state.copyWith(status: UserStatus.authorized, user: r));
});
}
}
Loading

0 comments on commit 8f59567

Please sign in to comment.