fix: rework presentation features

This commit is contained in:
Hugo Pointcheval 2022-11-09 01:47:57 -05:00
parent d2d8acdafb
commit 0d00a67b7c
Signed by: hugo
GPG Key ID: A9E8E9615379254F
14 changed files with 89 additions and 445 deletions

View File

@ -16,7 +16,8 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wyatt_authentication_bloc/src/domain/entities/user.dart';
import 'package:wyatt_authentication_bloc/src/core/enums/authentication_status.dart';
import 'package:wyatt_authentication_bloc/src/domain/entities/account_wrapper.dart';
import 'package:wyatt_authentication_bloc/src/features/authentication/cubit/authentication_cubit.dart';
class AuthenticationBuilder<Extra> extends StatelessWidget {
@ -29,8 +30,7 @@ class AuthenticationBuilder<Extra> extends StatelessWidget {
final Widget Function(
BuildContext context,
User user,
Extra? extra,
AccountWrapper<Extra> accountWrapper,
) authenticated;
final Widget Function(BuildContext context) unauthenticated;
final Widget Function(BuildContext context) unknown;
@ -40,8 +40,8 @@ class AuthenticationBuilder<Extra> extends StatelessWidget {
BlocBuilder<AuthenticationCubit<Extra>, AuthenticationState<Extra>>(
builder: (context, state) {
if (state.status == AuthenticationStatus.authenticated) {
if (state.user != null) {
return authenticated(context, state.user!, state.extra);
if (state.accountWrapper != null) {
return authenticated(context, state.accountWrapper!);
} else {
return unauthenticated(context);
}

View File

@ -18,83 +18,46 @@ import 'dart:async';
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wyatt_authentication_bloc/src/core/enum/auth_cubit_status.dart';
import 'package:wyatt_authentication_bloc/src/domain/entities/user.dart';
import 'package:wyatt_authentication_bloc/src/core/enums/authentication_status.dart';
import 'package:wyatt_authentication_bloc/src/domain/entities/account_wrapper.dart';
import 'package:wyatt_authentication_bloc/src/domain/repositories/authentication_repository.dart';
import 'package:wyatt_type_utils/wyatt_type_utils.dart';
part 'authentication_state.dart';
class AuthenticationCubit<Extra> extends Cubit<AuthenticationState<Extra>> {
final AuthenticationRepository _authenticationRepository;
late final StreamSubscription<AuthCubitStatus> _statusSubscription;
StreamSubscription<User>? _userSubscription;
final Future<Extra?> Function(User user)? _onAuthSuccess;
final AuthenticationRepository<Extra> _authenticationRepository;
AuthenticationCubit({
required AuthenticationRepository authenticationRepository,
Future<Extra?> Function(User user)? onAuthSuccess,
required AuthenticationRepository<Extra> authenticationRepository,
}) : _authenticationRepository = authenticationRepository,
_onAuthSuccess = onAuthSuccess,
super(const AuthenticationState.unknown()) {
_subscribeStatus();
_listenForAuthenticationChanges();
}
Future<AuthCubitStatus> get status async =>
_authenticationRepository.cubitStatus.last;
void _subscribeStatus() {
try {
_statusSubscription = _authenticationRepository.cubitStatus.listen(
(status) {
switch (status) {
case AuthCubitStatus.started:
start();
break;
case AuthCubitStatus.stoped:
stop();
break;
void _listenForAuthenticationChanges() {
_authenticationRepository.streamAccount().listen((accountFutureResult) {
accountFutureResult.fold(
(value) {
if (value.account.isNotNull) {
emit(AuthenticationState.authenticated(value));
return;
}
_authenticationRepository.destroyCache();
emit(const AuthenticationState.unauthenticated());
return;
},
(error) {
_authenticationRepository.destroyCache();
emit(const AuthenticationState.unauthenticated());
return;
},
);
} catch (_) {}
});
}
Future<void> init() async {
final firstUser = await _authenticationRepository.user.first;
_authenticationRepository.changeCubitStatus(AuthCubitStatus.started);
return changeStatus(firstUser);
}
bool start() {
_userSubscription = _authenticationRepository.user.listen(changeStatus);
return true;
}
bool stop() {
_userSubscription?.cancel();
return true;
}
Future<void> changeStatus(User user) async {
if (user.isNotEmpty) {
final Extra? extra = await _onAuthSuccess?.call(user);
emit(AuthenticationState.authenticated(user, extra));
} else {
_authenticationRepository.changeCubitStatus(AuthCubitStatus.stoped);
emit(const AuthenticationState.unauthenticated());
}
}
void logOut() {
unawaited(_authenticationRepository.signOut());
}
@override
Future<void> close() {
_userSubscription?.cancel();
_statusSubscription.cancel();
return super.close();
FutureOr<void> signOut() {
// TODO(hpcl): maybe force unauthenticated by emitting an event
_authenticationRepository.signOut();
}
}

View File

@ -16,42 +16,28 @@
part of 'authentication_cubit.dart';
enum AuthenticationStatus {
unknown,
authenticated,
unauthenticated,
}
class AuthenticationState<Extra> extends Equatable {
final AuthenticationStatus status;
final User? user;
final Extra? extra;
final AccountWrapper<Extra>? accountWrapper;
const AuthenticationState._({
required this.status,
this.user,
this.extra,
});
const AuthenticationState._({required this.status, this.accountWrapper});
const AuthenticationState.unknown()
: this._(status: AuthenticationStatus.unknown);
const AuthenticationState.authenticated(
User user,
Extra? extra,
) : this._(
const AuthenticationState.authenticated(AccountWrapper<Extra> accountWrapper)
: this._(
status: AuthenticationStatus.authenticated,
user: user,
extra: extra,
accountWrapper: accountWrapper,
);
const AuthenticationState.unauthenticated()
: this._(status: AuthenticationStatus.unauthenticated);
@override
List<Object?> get props => [status, user, extra];
List<Object?> get props => [status];
@override
String toString() =>
'AuthenticationState(status: $status, user: $user, extra: $extra)';
'AuthenticationState(status: $status, accountWrapper: $accountWrapper)';
}

View File

@ -1,71 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wyatt_authentication_bloc/src/core/exceptions/exceptions.dart';
import 'package:wyatt_authentication_bloc/src/domain/repositories/authentication_repository.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart' show FormStatus;
part 'email_verification_state.dart';
class EmailVerificationCubit extends Cubit<EmailVerificationState> {
final AuthenticationRepository _authenticationRepository;
EmailVerificationCubit({
required AuthenticationRepository authenticationRepository,
}) : _authenticationRepository = authenticationRepository,
super(const EmailVerificationState());
Future<void> sendEmailVerification() async {
emit(state.copyWith(status: FormStatus.submissionInProgress));
try {
await _authenticationRepository.sendEmailVerification();
emit(state.copyWith(status: FormStatus.submissionSuccess));
} on SendEmailVerificationFailureInterface catch (e) {
emit(
state.copyWith(
errorMessage: e.message,
status: FormStatus.submissionFailure,
),
);
} catch (_) {
emit(state.copyWith(status: FormStatus.submissionFailure));
}
}
Future<void> checkEmailVerification() async {
emit(state.copyWith(status: FormStatus.submissionInProgress));
try {
await _authenticationRepository.refresh();
emit(
state.copyWith(
isVerified: _authenticationRepository.currentUser.emailVerified,
status: FormStatus.submissionSuccess,
),
);
} on RefreshFailureInterface catch (e) {
emit(
state.copyWith(
errorMessage: e.message,
status: FormStatus.submissionFailure,
),
);
} catch (_) {
emit(state.copyWith(status: FormStatus.submissionFailure));
}
}
}

View File

@ -1,42 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
part of 'email_verification_cubit.dart';
class EmailVerificationState extends Equatable {
final bool isVerified;
final FormStatus status;
final String? errorMessage;
const EmailVerificationState({
this.isVerified = false,
this.status = FormStatus.pure,
this.errorMessage,
});
EmailVerificationState copyWith({
bool? isVerified,
FormStatus? status,
String? errorMessage,
}) => EmailVerificationState(
isVerified: isVerified ?? this.isVerified,
status: status ?? this.status,
errorMessage: errorMessage ?? this.errorMessage,
);
@override
List<Object> get props => [isVerified, status];
}

View File

@ -1,17 +1,19 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
export 'cubit/email_verification_cubit.dart';
export 'authentication/authentication.dart';
export 'sign_in/sign_in.dart';
export 'sign_up/sign_up.dart';

View File

@ -1,68 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wyatt_authentication_bloc/src/core/exceptions/exceptions.dart';
import 'package:wyatt_authentication_bloc/src/domain/repositories/authentication_repository.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
part 'password_reset_state.dart';
class PasswordResetCubit extends Cubit<PasswordResetState> {
final AuthenticationRepository _authenticationRepository;
final FormValidator _validationStrategy;
PasswordResetCubit({
required AuthenticationRepository authenticationRepository,
FormValidator validationStrategy = const EveryInputValidator(),
}) : _authenticationRepository = authenticationRepository,
_validationStrategy = validationStrategy,
super(const PasswordResetState());
void emailChanged(String value) {
final Email email = Email.dirty(value);
emit(
state.copyWith(
email: email,
status: _validationStrategy.rawValidate([email]),
),
);
}
Future<void> sendPasswordResetEmail() async {
if (!state.status.isValidated) {
return;
}
emit(state.copyWith(status: FormStatus.submissionInProgress));
try {
await _authenticationRepository.sendPasswordResetEmail(
email: state.email.value,
);
emit(state.copyWith(status: FormStatus.submissionSuccess));
} on SendPasswordResetEmailFailureInterface catch (e) {
emit(
state.copyWith(
errorMessage: e.message,
status: FormStatus.submissionFailure,
),
);
} catch (_) {
emit(state.copyWith(status: FormStatus.submissionFailure));
}
}
}

View File

@ -1,42 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
part of 'password_reset_cubit.dart';
class PasswordResetState extends Equatable {
final Email email;
final FormStatus status;
final String? errorMessage;
const PasswordResetState({
this.email = const Email.pure(),
this.status = FormStatus.pure,
this.errorMessage,
});
PasswordResetState copyWith({
Email? email,
FormStatus? status,
String? errorMessage,
}) => PasswordResetState(
email: email ?? this.email,
status: status ?? this.status,
errorMessage: errorMessage ?? this.errorMessage,
);
@override
List<Object> get props => [email, status];
}

View File

@ -1,17 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
export 'cubit/password_reset_cubit.dart';

View File

@ -16,20 +16,18 @@
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wyatt_authentication_bloc/src/core/enum/auth_cubit_status.dart';
import 'package:wyatt_authentication_bloc/src/core/exceptions/exceptions.dart';
import 'package:wyatt_authentication_bloc/src/domain/repositories/authentication_repository.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
part 'sign_in_state.dart';
class SignInCubit extends Cubit<SignInState> {
final AuthenticationRepository _authenticationRepository;
class SignInCubit<Extra> extends Cubit<SignInState> {
final AuthenticationRepository<Extra> _authenticationRepository;
final FormValidator _validationStrategy;
SignInCubit({
required AuthenticationRepository authenticationRepository,
required AuthenticationRepository<Extra> authenticationRepository,
FormValidator validationStrategy = const EveryInputValidator(),
}) : _authenticationRepository = authenticationRepository,
_validationStrategy = validationStrategy,
@ -55,71 +53,26 @@ class SignInCubit extends Cubit<SignInState> {
);
}
Future<void> signInAnonymously() async {
if (state.status.isSubmissionInProgress) {
return;
}
emit(state.copyWith(status: FormStatus.submissionInProgress));
try {
await _authenticationRepository.signInAnonymously();
_authenticationRepository.changeCubitStatus(AuthCubitStatus.started);
emit(state.copyWith(status: FormStatus.submissionSuccess));
} on SignInAnonymouslyFailureInterface catch (e) {
emit(
state.copyWith(
errorMessage: e.message,
status: FormStatus.submissionFailure,
),
);
} catch (_) {
emit(state.copyWith(status: FormStatus.submissionFailure));
}
}
Future<void> signInWithGoogle() async {
if (state.status.isSubmissionInProgress) {
return;
}
emit(state.copyWith(status: FormStatus.submissionInProgress));
try {
await _authenticationRepository.signInWithGoogle();
_authenticationRepository.changeCubitStatus(AuthCubitStatus.started);
emit(state.copyWith(status: FormStatus.submissionSuccess));
} on SignInWithGoogleFailureInterface catch (e) {
emit(
state.copyWith(
errorMessage: e.message,
status: FormStatus.submissionFailure,
),
);
} catch (_) {
emit(state.copyWith(status: FormStatus.submissionFailure));
}
}
Future<void> signInWithEmailAndPassword() async {
if (!state.status.isValidated) {
return;
}
emit(state.copyWith(status: FormStatus.submissionInProgress));
try {
await _authenticationRepository.signInWithEmailAndPassword(
email: state.email.value,
password: state.password.value,
);
_authenticationRepository.changeCubitStatus(AuthCubitStatus.started);
emit(state.copyWith(status: FormStatus.submissionSuccess));
} on SignInWithEmailAndPasswordFailureInterface catch (e) {
emit(
state.copyWith(
errorMessage: e.message,
final uid = await _authenticationRepository.signInWithEmailAndPassword(
email: state.email.value,
password: state.password.value,
);
emit(
uid.fold(
(value) => state.copyWith(status: FormStatus.submissionSuccess),
(error) => state.copyWith(
errorMessage: error.message,
status: FormStatus.submissionFailure,
),
);
} catch (_) {
emit(state.copyWith(status: FormStatus.submissionFailure));
}
),
);
}
}

View File

@ -1,16 +1,16 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

View File

@ -17,28 +17,22 @@
import 'dart:async';
import 'package:equatable/equatable.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wyatt_authentication_bloc/src/core/enum/auth_cubit_status.dart';
import 'package:wyatt_authentication_bloc/src/core/exceptions/exceptions.dart';
import 'package:wyatt_authentication_bloc/src/domain/repositories/authentication_repository.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
part 'sign_up_state.dart';
class SignUpCubit extends Cubit<SignUpState> {
final AuthenticationRepository _authenticationRepository;
class SignUpCubit<Extra> extends Cubit<SignUpState> {
final AuthenticationRepository<Extra> _authenticationRepository;
final Future<void> Function(SignUpState state, String? uid)? _onSignUpSuccess;
final FormValidator _validationStrategy;
SignUpCubit({
required AuthenticationRepository authenticationRepository,
required AuthenticationRepository<Extra> authenticationRepository,
required FormData formData,
FormValidator validationStrategy = const EveryInputValidator(),
Future<void> Function(SignUpState state, String? uid)? onSignUpSuccess,
}) : _authenticationRepository = authenticationRepository,
_onSignUpSuccess = onSignUpSuccess,
_validationStrategy = validationStrategy,
super(SignUpState(data: formData));
@ -144,24 +138,20 @@ class SignUpCubit extends Cubit<SignUpState> {
return;
}
emit(state.copyWith(status: FormStatus.submissionInProgress));
try {
final uid = await _authenticationRepository.signUp(
email: state.email.value,
password: state.password.value,
);
await _onSignUpSuccess?.call(state, uid);
_authenticationRepository.changeCubitStatus(AuthCubitStatus.started);
emit(state.copyWith(status: FormStatus.submissionSuccess));
} on SignUpWithEmailAndPasswordFailureInterface catch (e) {
emit(
state.copyWith(
errorMessage: e.message,
final uid = await _authenticationRepository.signUp(
email: state.email.value,
password: state.password.value,
);
emit(
uid.fold(
(value) => state.copyWith(status: FormStatus.submissionSuccess),
(error) => state.copyWith(
errorMessage: error.message,
status: FormStatus.submissionFailure,
),
);
} catch (e) {
debugPrint(e.toString());
emit(state.copyWith(status: FormStatus.submissionFailure));
}
),
);
}
}

View File

@ -1,16 +1,16 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

View File

@ -1,30 +1,20 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
export 'core/enum/auth_cubit_status.dart';
export 'core/exceptions/exceptions.dart';
export 'core/exceptions/exceptions_firebase.dart';
export 'core/extensions/firebase_auth_user_x.dart';
export 'core/utils/cryptography.dart';
export 'data/models/user_firebase.dart';
export 'data/repositories/authentication_repository_firebase.dart';
export 'domain/entities/user.dart';
export 'domain/repositories/authentication_repository.dart';
export 'features/authentication/authentication.dart';
export 'features/email_verification/email_verification.dart';
export 'features/password_reset/password_reset.dart';
export 'features/sign_in/sign_in.dart';
export 'features/sign_up/sign_up.dart';
export 'core/core.dart';
export 'data/data.dart';
export 'domain/domain.dart';
export 'features/features.dart';