fix(authentication)!: use FutureOrResult extension (close #41) #48
| @ -3,7 +3,7 @@ | |||||||
| // ----- | // ----- | ||||||
| // File: app.dart | // File: app.dart | ||||||
| // Created Date: 19/08/2022 12:05:38 | // Created Date: 19/08/2022 12:05:38 | ||||||
| // Last Modified: Thu Nov 10 2022 | // Last Modified: Wed Nov 23 2022 | ||||||
| // ----- | // ----- | ||||||
| // Copyright (c) 2022 | // Copyright (c) 2022 | ||||||
| 
 | 
 | ||||||
| @ -21,7 +21,7 @@ import 'package:wyatt_form_bloc/wyatt_form_bloc.dart'; | |||||||
| import 'package:wyatt_type_utils/wyatt_type_utils.dart'; | import 'package:wyatt_type_utils/wyatt_type_utils.dart'; | ||||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||||
| 
 | 
 | ||||||
| FutureResult<int?> onSignUpSuccess( | FutureOrResult<int?> onSignUpSuccess( | ||||||
|   Account? account, |   Account? account, | ||||||
|   WyattForm form, |   WyattForm form, | ||||||
| ) async { | ) async { | ||||||
| @ -34,7 +34,7 @@ FutureResult<int?> onSignUpSuccess( | |||||||
|   return const Ok<int, AppException>(id); |   return const Ok<int, AppException>(id); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| FutureResult<int?> onAccountChanges(Account? account) async { | FutureOrResult<int?> onAccountChanges(Account? account) async { | ||||||
|   final id = Random().nextInt(1000); |   final id = Random().nextInt(1000); | ||||||
|   debugPrint('onAccountChanges: $account, generatedId: $id'); |   debugPrint('onAccountChanges: $account, generatedId: $id'); | ||||||
|   return Ok<int, AppException>(id); |   return Ok<int, AppException>(id); | ||||||
|  | |||||||
| @ -29,12 +29,12 @@ import 'package:wyatt_authentication_bloc/src/domain/repositories/authentication | |||||||
| import 'package:wyatt_form_bloc/wyatt_form_bloc.dart'; | import 'package:wyatt_form_bloc/wyatt_form_bloc.dart'; | ||||||
| import 'package:wyatt_type_utils/wyatt_type_utils.dart'; | import 'package:wyatt_type_utils/wyatt_type_utils.dart'; | ||||||
| 
 | 
 | ||||||
| typedef OnSignUpSuccess<T> = FutureResult<T?> Function( | typedef OnSignUpSuccess<T> = FutureOrResult<T?> Function( | ||||||
|   Account? account, |   Account? account, | ||||||
|   WyattForm form, |   WyattForm form, | ||||||
| ); | ); | ||||||
| 
 | 
 | ||||||
| typedef OnAuthChange<T> = FutureResult<T?> Function(Account? account); | typedef OnAuthChange<T> = FutureOrResult<T?> Function(Account? account); | ||||||
| 
 | 
 | ||||||
| class AuthenticationRepositoryImpl<T extends Object> | class AuthenticationRepositoryImpl<T extends Object> | ||||||
|     extends AuthenticationRepository<T> { |     extends AuthenticationRepository<T> { | ||||||
| @ -46,7 +46,7 @@ class AuthenticationRepositoryImpl<T extends Object> | |||||||
|   final OnSignUpSuccess<T>? _onSignUpSuccess; |   final OnSignUpSuccess<T>? _onSignUpSuccess; | ||||||
| 
 | 
 | ||||||
|   final OnAuthChange<T>? _onAccountChanges; |   final OnAuthChange<T>? _onAccountChanges; | ||||||
|   final StreamController<FutureResult<AccountWrapper<T>>> _signUpStream = |   final StreamController<FutureOrResult<AccountWrapper<T>>> _signUpStream = | ||||||
|       StreamController(); |       StreamController(); | ||||||
| 
 | 
 | ||||||
|   bool _pause = false; // Semaphore |   bool _pause = false; // Semaphore | ||||||
| @ -93,7 +93,7 @@ class AuthenticationRepositoryImpl<T extends Object> | |||||||
|   FormRepository get formRepository => _formRepository; |   FormRepository get formRepository => _formRepository; | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<Account> signInWithEmailAndPassword({ |   FutureOrResult<Account> signInWithEmailAndPassword({ | ||||||
|     required String email, |     required String email, | ||||||
|     required String password, |     required String password, | ||||||
|   }) => |   }) => | ||||||
| @ -111,7 +111,7 @@ class AuthenticationRepositoryImpl<T extends Object> | |||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<void> signOut() => |   FutureOrResult<void> signOut() => | ||||||
|       Result.tryCatchAsync<void, AppException, AppException>( |       Result.tryCatchAsync<void, AppException, AppException>( | ||||||
|         () async { |         () async { | ||||||
|           await _authenticationRemoteDataSource.signOut(); |           await _authenticationRemoteDataSource.signOut(); | ||||||
| @ -121,7 +121,7 @@ class AuthenticationRepositoryImpl<T extends Object> | |||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<Account> signUp({ |   FutureOrResult<Account> signUp({ | ||||||
|     required String email, |     required String email, | ||||||
|     required String password, |     required String password, | ||||||
|   }) => |   }) => | ||||||
| @ -157,28 +157,28 @@ class AuthenticationRepositoryImpl<T extends Object> | |||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<void> destroyCache() => |   FutureOrResult<void> destroyCache() => | ||||||
|       Result.tryCatchAsync<void, AppException, AppException>( |       Result.tryCatchAsync<void, AppException, AppException>( | ||||||
|         _authenticationLocalDataSource.destroy, |         _authenticationLocalDataSource.destroy, | ||||||
|         (error) => error, |         (error) => error, | ||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<AccountWrapper<T>> getCache() => |   FutureOrResult<AccountWrapper<T>> getCache() => | ||||||
|       Result.tryCatchAsync<AccountWrapper<T>, AppException, AppException>( |       Result.tryCatchAsync<AccountWrapper<T>, AppException, AppException>( | ||||||
|         _authenticationLocalDataSource.load, |         _authenticationLocalDataSource.load, | ||||||
|         (error) => error, |         (error) => error, | ||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<Account> getAccount() => |   FutureOrResult<Account> getAccount() => | ||||||
|       Result.tryCatchAsync<Account, AppException, AppException>( |       Result.tryCatchAsync<Account, AppException, AppException>( | ||||||
|         _authenticationLocalDataSource.loadAccount, |         _authenticationLocalDataSource.loadAccount, | ||||||
|         (error) => error, |         (error) => error, | ||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<void> setAccount( |   FutureOrResult<void> setAccount( | ||||||
|     Account account, |     Account account, | ||||||
|   ) => |   ) => | ||||||
|       Result.tryCatchAsync<void, AppException, AppException>( |       Result.tryCatchAsync<void, AppException, AppException>( | ||||||
| @ -189,14 +189,14 @@ class AuthenticationRepositoryImpl<T extends Object> | |||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<T> getData() => |   FutureOrResult<T> getData() => | ||||||
|       Result.tryCatchAsync<T, AppException, AppException>( |       Result.tryCatchAsync<T, AppException, AppException>( | ||||||
|         _authenticationLocalDataSource.loadData, |         _authenticationLocalDataSource.loadData, | ||||||
|         (error) => error, |         (error) => error, | ||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<void> setData( |   FutureOrResult<void> setData( | ||||||
|     T? data, |     T? data, | ||||||
|   ) => |   ) => | ||||||
|       Result.tryCatchAsync<void, AppException, AppException>( |       Result.tryCatchAsync<void, AppException, AppException>( | ||||||
| @ -207,14 +207,14 @@ class AuthenticationRepositoryImpl<T extends Object> | |||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<String> getIdentityToken() => |   FutureOrResult<String> getIdentityToken() => | ||||||
|       Result.tryCatchAsync<String, AppException, AppException>( |       Result.tryCatchAsync<String, AppException, AppException>( | ||||||
|         _authenticationRemoteDataSource.getIdentityToken, |         _authenticationRemoteDataSource.getIdentityToken, | ||||||
|         (error) => error, |         (error) => error, | ||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   Stream<FutureResult<AccountWrapper<T>>> streamAccount() => MergeStream([ |   Stream<FutureOrResult<AccountWrapper<T>>> streamAccount() => MergeStream([ | ||||||
|         _signUpStream.stream.asBroadcastStream(), |         _signUpStream.stream.asBroadcastStream(), | ||||||
|         _authenticationRemoteDataSource.streamAccount().map((account) async { |         _authenticationRemoteDataSource.streamAccount().map((account) async { | ||||||
|           if (_onAccountChanges.isNotNull && !_pause) { |           if (_onAccountChanges.isNotNull && !_pause) { | ||||||
| @ -231,7 +231,7 @@ class AuthenticationRepositoryImpl<T extends Object> | |||||||
|       ]); |       ]); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<void> confirmPasswordReset({ |   FutureOrResult<void> confirmPasswordReset({ | ||||||
|     required String code, |     required String code, | ||||||
|     required String newPassword, |     required String newPassword, | ||||||
|   }) => |   }) => | ||||||
| @ -246,7 +246,7 @@ class AuthenticationRepositoryImpl<T extends Object> | |||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<void> sendEmailVerification() => |   FutureOrResult<void> sendEmailVerification() => | ||||||
|       Result.tryCatchAsync<void, AppException, AppException>( |       Result.tryCatchAsync<void, AppException, AppException>( | ||||||
|         () async { |         () async { | ||||||
|           await _authenticationRemoteDataSource.sendEmailVerification(); |           await _authenticationRemoteDataSource.sendEmailVerification(); | ||||||
| @ -255,7 +255,7 @@ class AuthenticationRepositoryImpl<T extends Object> | |||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<void> sendPasswordResetEmail({required String email}) => |   FutureOrResult<void> sendPasswordResetEmail({required String email}) => | ||||||
|       Result.tryCatchAsync<void, AppException, AppException>( |       Result.tryCatchAsync<void, AppException, AppException>( | ||||||
|         () async { |         () async { | ||||||
|           await _authenticationRemoteDataSource.sendPasswordResetEmail( |           await _authenticationRemoteDataSource.sendPasswordResetEmail( | ||||||
| @ -266,7 +266,7 @@ class AuthenticationRepositoryImpl<T extends Object> | |||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<Account> signInAnonymously() => |   FutureOrResult<Account> signInAnonymously() => | ||||||
|       Result.tryCatchAsync<Account, AppException, AppException>( |       Result.tryCatchAsync<Account, AppException, AppException>( | ||||||
|         () async { |         () async { | ||||||
|           final account = |           final account = | ||||||
| @ -277,7 +277,7 @@ class AuthenticationRepositoryImpl<T extends Object> | |||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<bool> verifyPasswordResetCode({required String code}) => |   FutureOrResult<bool> verifyPasswordResetCode({required String code}) => | ||||||
|       Result.tryCatchAsync<bool, AppException, AppException>( |       Result.tryCatchAsync<bool, AppException, AppException>( | ||||||
|         () async { |         () async { | ||||||
|           final response = await _authenticationRemoteDataSource |           final response = await _authenticationRemoteDataSource | ||||||
| @ -288,7 +288,7 @@ class AuthenticationRepositoryImpl<T extends Object> | |||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<void> refresh() => |   FutureOrResult<void> refresh() => | ||||||
|       Result.tryCatchAsync<void, AppException, AppException>( |       Result.tryCatchAsync<void, AppException, AppException>( | ||||||
|         () async { |         () async { | ||||||
|           await _authenticationRemoteDataSource.refresh(); |           await _authenticationRemoteDataSource.refresh(); | ||||||
|  | |||||||
| @ -30,7 +30,7 @@ abstract class AuthenticationRepository<T> extends BaseRepository { | |||||||
|   /// Throws a SignUpWithEmailAndPasswordFailureInterface if |   /// Throws a SignUpWithEmailAndPasswordFailureInterface if | ||||||
|   /// an exception occurs. |   /// an exception occurs. | ||||||
|   /// {@endtemplate} |   /// {@endtemplate} | ||||||
|   FutureResult<Account> signUp({ |   FutureOrResult<Account> signUp({ | ||||||
|     required String email, |     required String email, | ||||||
|     required String password, |     required String password, | ||||||
|   }); |   }); | ||||||
| @ -40,21 +40,21 @@ abstract class AuthenticationRepository<T> extends BaseRepository { | |||||||
|   /// |   /// | ||||||
|   /// Throws a SendEmailVerificationFailureInterface if an exception occurs. |   /// Throws a SendEmailVerificationFailureInterface if an exception occurs. | ||||||
|   /// {@endtemplate} |   /// {@endtemplate} | ||||||
|   FutureResult<void> sendEmailVerification(); |   FutureOrResult<void> sendEmailVerification(); | ||||||
| 
 | 
 | ||||||
|   /// {@template send_password_reset_email} |   /// {@template send_password_reset_email} | ||||||
|   /// Sends a password reset email to the provided [email]. |   /// Sends a password reset email to the provided [email]. | ||||||
|   /// |   /// | ||||||
|   /// Throws a SendPasswordResetEmailFailureInterface if an exception occurs. |   /// Throws a SendPasswordResetEmailFailureInterface if an exception occurs. | ||||||
|   /// {@endtemplate} |   /// {@endtemplate} | ||||||
|   FutureResult<void> sendPasswordResetEmail({required String email}); |   FutureOrResult<void> sendPasswordResetEmail({required String email}); | ||||||
| 
 | 
 | ||||||
|   /// {@template confirm_password_reset} |   /// {@template confirm_password_reset} | ||||||
|   /// Confirms the password reset with the provided [newPassword] and [code]. |   /// Confirms the password reset with the provided [newPassword] and [code]. | ||||||
|   /// |   /// | ||||||
|   /// Throws a ConfirmPasswordResetFailureInterface if an exception occurs. |   /// Throws a ConfirmPasswordResetFailureInterface if an exception occurs. | ||||||
|   /// {@endtemplate} |   /// {@endtemplate} | ||||||
|   FutureResult<void> confirmPasswordReset({ |   FutureOrResult<void> confirmPasswordReset({ | ||||||
|     required String code, |     required String code, | ||||||
|     required String newPassword, |     required String newPassword, | ||||||
|   }); |   }); | ||||||
| @ -64,14 +64,14 @@ abstract class AuthenticationRepository<T> extends BaseRepository { | |||||||
|   /// |   /// | ||||||
|   /// Throws a VerifyPasswordResetCodeFailureInterface if an exception occurs. |   /// Throws a VerifyPasswordResetCodeFailureInterface if an exception occurs. | ||||||
|   /// {@endtemplate} |   /// {@endtemplate} | ||||||
|   FutureResult<bool> verifyPasswordResetCode({required String code}); |   FutureOrResult<bool> verifyPasswordResetCode({required String code}); | ||||||
| 
 | 
 | ||||||
|   /// {@template signin_anom} |   /// {@template signin_anom} | ||||||
|   /// Sign in anonymously. |   /// Sign in anonymously. | ||||||
|   /// |   /// | ||||||
|   /// Throws a SignInAnonymouslyFailureInterface if an exception occurs. |   /// Throws a SignInAnonymouslyFailureInterface if an exception occurs. | ||||||
|   /// {@endtemplate} |   /// {@endtemplate} | ||||||
|   FutureResult<Account> signInAnonymously(); |   FutureOrResult<Account> signInAnonymously(); | ||||||
| 
 | 
 | ||||||
|   /// {@template signin_pwd} |   /// {@template signin_pwd} | ||||||
|   /// Signs in with the provided [email] and [password]. |   /// Signs in with the provided [email] and [password]. | ||||||
| @ -79,7 +79,7 @@ abstract class AuthenticationRepository<T> extends BaseRepository { | |||||||
|   /// Throws a SignInWithEmailAndPasswordFailureInterface if |   /// Throws a SignInWithEmailAndPasswordFailureInterface if | ||||||
|   /// an exception occurs. |   /// an exception occurs. | ||||||
|   /// {@endtemplate} |   /// {@endtemplate} | ||||||
|   FutureResult<Account> signInWithEmailAndPassword({ |   FutureOrResult<Account> signInWithEmailAndPassword({ | ||||||
|     required String email, |     required String email, | ||||||
|     required String password, |     required String password, | ||||||
|   }); |   }); | ||||||
| @ -88,9 +88,9 @@ abstract class AuthenticationRepository<T> extends BaseRepository { | |||||||
|   /// Signs out the current user. |   /// Signs out the current user. | ||||||
|   /// It also clears the cache and the associated data. |   /// It also clears the cache and the associated data. | ||||||
|   /// {@endtemplate} |   /// {@endtemplate} | ||||||
|   FutureResult<void> signOut(); |   FutureOrResult<void> signOut(); | ||||||
| 
 | 
 | ||||||
|   FutureResult<void> refresh(); |   FutureOrResult<void> refresh(); | ||||||
| 
 | 
 | ||||||
|   /// {@template stream_account} |   /// {@template stream_account} | ||||||
|   /// Stream of [AccountWrapper] which will emit the current account when |   /// Stream of [AccountWrapper] which will emit the current account when | ||||||
| @ -99,16 +99,16 @@ abstract class AuthenticationRepository<T> extends BaseRepository { | |||||||
|   /// Emits [AccountWrapper] with null [Account] if the user is not  |   /// Emits [AccountWrapper] with null [Account] if the user is not  | ||||||
|   /// authenticated. |   /// authenticated. | ||||||
|   /// {@endtemplate} |   /// {@endtemplate} | ||||||
|   Stream<FutureResult<AccountWrapper<T>>> streamAccount(); |   Stream<FutureOrResult<AccountWrapper<T>>> streamAccount(); | ||||||
| 
 | 
 | ||||||
|   FutureResult<String> getIdentityToken(); |   FutureOrResult<String> getIdentityToken(); | ||||||
| 
 | 
 | ||||||
|   FutureResult<Account> getAccount(); |   FutureOrResult<Account> getAccount(); | ||||||
|   FutureResult<void> setAccount(Account account); |   FutureOrResult<void> setAccount(Account account); | ||||||
| 
 | 
 | ||||||
|   FutureResult<T> getData(); |   FutureOrResult<T> getData(); | ||||||
|   FutureResult<void> setData(T? data); |   FutureOrResult<void> setData(T? data); | ||||||
| 
 | 
 | ||||||
|   FutureResult<AccountWrapper<T>> getCache(); |   FutureOrResult<AccountWrapper<T>> getCache(); | ||||||
|   FutureResult<void> destroyCache(); |   FutureOrResult<void> destroyCache(); | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user