feat(App): update Finlytic Flutter app UI and blocs
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:finlytic_app/core/network/api_client.dart';
|
||||
import 'package:finlytic_app/core/services/secure_storage_service.dart';
|
||||
import 'package:finlytic_app/features/auth/repositories/auth_repository.dart';
|
||||
import 'auth_event.dart';
|
||||
import 'auth_state.dart';
|
||||
|
||||
export 'auth_event.dart';
|
||||
export 'auth_state.dart';
|
||||
|
||||
class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
final AuthRepository repository;
|
||||
|
||||
AuthBloc({
|
||||
required ApiClient apiClient,
|
||||
required SecureStorageService storageService,
|
||||
AuthRepository? repository,
|
||||
}) : repository = repository ?? AuthRepository(apiClient: apiClient, storageService: storageService),
|
||||
super(AuthInitial()) {
|
||||
on<CheckAuthStatus>(_onCheckAuthStatus);
|
||||
on<LoginRequested>(_onLoginRequested);
|
||||
on<RegisterRequested>(_onRegisterRequested);
|
||||
on<LogoutRequested>(_onLogoutRequested);
|
||||
}
|
||||
|
||||
Future<void> _onCheckAuthStatus(CheckAuthStatus event, Emitter<AuthState> emit) async {
|
||||
final user = await repository.checkAuthStatus();
|
||||
if (user != null) {
|
||||
emit(Authenticated(user));
|
||||
} else {
|
||||
emit(Unauthenticated());
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onLoginRequested(LoginRequested event, Emitter<AuthState> emit) async {
|
||||
emit(AuthLoading());
|
||||
try {
|
||||
final user = await repository.login(event.email, event.password);
|
||||
emit(Authenticated(user));
|
||||
} on RequiresPasswordChangeException catch (e) {
|
||||
emit(AuthRequiresPasswordChange(e.userId));
|
||||
} catch (e) {
|
||||
emit(AuthFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onRegisterRequested(RegisterRequested event, Emitter<AuthState> emit) async {
|
||||
emit(AuthLoading());
|
||||
try {
|
||||
final user = await repository.register(event.email, event.password, event.fullName);
|
||||
emit(Authenticated(user));
|
||||
} catch (e) {
|
||||
emit(AuthFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onLogoutRequested(LogoutRequested event, Emitter<AuthState> emit) async {
|
||||
await repository.logout();
|
||||
emit(Unauthenticated());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
abstract class AuthEvent extends Equatable {
|
||||
const AuthEvent();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class CheckAuthStatus extends AuthEvent {}
|
||||
|
||||
class LoginRequested extends AuthEvent {
|
||||
final String email;
|
||||
final String password;
|
||||
|
||||
const LoginRequested(this.email, this.password);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [email, password];
|
||||
}
|
||||
|
||||
class RegisterRequested extends AuthEvent {
|
||||
final String email;
|
||||
final String password;
|
||||
final String fullName;
|
||||
|
||||
const RegisterRequested(this.email, this.password, this.fullName);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [email, password, fullName];
|
||||
}
|
||||
|
||||
class LogoutRequested extends AuthEvent {}
|
||||
@@ -0,0 +1,41 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:finlytic_app/features/auth/models/user_model.dart';
|
||||
|
||||
abstract class AuthState extends Equatable {
|
||||
const AuthState();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class AuthInitial extends AuthState {}
|
||||
|
||||
class AuthLoading extends AuthState {}
|
||||
|
||||
class Authenticated extends AuthState {
|
||||
final UserModel user;
|
||||
|
||||
const Authenticated(this.user);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [user];
|
||||
}
|
||||
|
||||
class Unauthenticated extends AuthState {}
|
||||
|
||||
class AuthRequiresPasswordChange extends AuthState {
|
||||
final String userId;
|
||||
|
||||
const AuthRequiresPasswordChange(this.userId);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [userId];
|
||||
}
|
||||
class AuthFailure extends AuthState {
|
||||
final String error;
|
||||
|
||||
const AuthFailure(this.error);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [error];
|
||||
}
|
||||
Reference in New Issue
Block a user