-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c6a1d3
commit 5dda354
Showing
16 changed files
with
674 additions
and
161 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import 'dart:async'; | ||
import 'package:appwrite_project/utils/Validators.dart'; | ||
import 'package:bloc/bloc.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:rxdart/rxdart.dart'; | ||
|
||
import '../../resources/user_repository.dart'; | ||
|
||
part 'registration_event.dart'; | ||
part 'registration_state.dart'; | ||
|
||
class RegistrationBloc extends Bloc<RegistrationEvent, RegistrationState> { | ||
final UserRepository _userRepository; | ||
final String name; | ||
final String phoneNumber; | ||
|
||
RegistrationBloc({ | ||
@required UserRepository userRepository, | ||
@required this.name, | ||
@required this.phoneNumber, | ||
}) : assert(userRepository != null), | ||
_userRepository = userRepository; | ||
|
||
@override | ||
RegistrationState get initialState => RegistrationState.empty(); | ||
|
||
@override | ||
Stream<Transition<RegistrationEvent, RegistrationState>> transformEvents( | ||
Stream<RegistrationEvent> events, | ||
TransitionFunction<RegistrationEvent, RegistrationState> transitionFn, | ||
) { | ||
final nonDebounceStream = events.where((event) { | ||
return (event is! EmailChanged && event is! PasswordChanged); | ||
}); | ||
final debounceStream = events.where((event) { | ||
return (event is EmailChanged || event is PasswordChanged); | ||
}).debounceTime(Duration(milliseconds: 300)); | ||
return super.transformEvents( | ||
nonDebounceStream.mergeWith([debounceStream]), | ||
transitionFn, | ||
); | ||
} | ||
|
||
@override | ||
Stream<RegistrationState> mapEventToState( | ||
RegistrationEvent event, | ||
) async* { | ||
if (event is EmailChanged) { | ||
yield* _mapEmailChangedToState(event.email); | ||
} else if (event is PasswordChanged) { | ||
yield* _mapPasswordChangedToState(event.password); | ||
} else if (event is RegistrationSubmitted) { | ||
yield* _mapFormSubmittedToState(event.email, event.password); | ||
} | ||
} | ||
|
||
Stream<RegistrationState> _mapEmailChangedToState(String email) async* { | ||
if (email.length > 0) { | ||
yield state.update( | ||
isEmailValid: Validators.isValidEmail(email), | ||
); | ||
} | ||
} | ||
|
||
Stream<RegistrationState> _mapPasswordChangedToState(String password) async* { | ||
if (password.length > 0) { | ||
yield state.update( | ||
isPasswordValid: Validators.isValidPassword(password), | ||
); | ||
} | ||
} | ||
|
||
Stream<RegistrationState> _mapFormSubmittedToState( | ||
String email, | ||
String password, | ||
) async* { | ||
yield RegistrationState.loading(); | ||
try { | ||
await _userRepository.signup( | ||
email: email, | ||
password: password, | ||
); | ||
yield RegistrationState.success(); | ||
} catch (e) { | ||
yield RegistrationState.failure(e.toString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
part of 'registration_bloc.dart'; | ||
|
||
abstract class RegistrationEvent extends Equatable { | ||
const RegistrationEvent(); | ||
|
||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
class EmailChanged extends RegistrationEvent { | ||
final String email; | ||
|
||
const EmailChanged({@required this.email}); | ||
|
||
@override | ||
List<Object> get props => [email]; | ||
|
||
@override | ||
String toString() => 'EmailChanged { email :$email }'; | ||
} | ||
|
||
class PasswordChanged extends RegistrationEvent { | ||
final String password; | ||
|
||
const PasswordChanged({@required this.password}); | ||
|
||
@override | ||
List<Object> get props => [password]; | ||
|
||
@override | ||
String toString() => 'PasswordChanged { password: $password }'; | ||
} | ||
|
||
class RegistrationSubmitted extends RegistrationEvent { | ||
final String email; | ||
final String password; | ||
// final String firstName; | ||
|
||
const RegistrationSubmitted({ | ||
@required this.email, | ||
@required this.password, | ||
// @required this.firstName | ||
}); | ||
|
||
@override | ||
List<Object> get props => [ | ||
email, | ||
password, | ||
]; | ||
|
||
@override | ||
String toString() { | ||
return 'Submitted { email: $email, password: $password,}'; | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
lib/authentication/registration/registration_state.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
part of 'registration_bloc.dart'; | ||
|
||
|
||
@immutable | ||
class RegistrationState { | ||
final bool isEmailValid; | ||
final bool isPasswordValid; | ||
final bool isSubmitting; | ||
final bool isSuccess; | ||
final bool isFailure; | ||
final String isError; | ||
|
||
bool get isFormValid => isEmailValid && isPasswordValid; | ||
|
||
RegistrationState( | ||
{@required this.isEmailValid, | ||
@required this.isPasswordValid, | ||
@required this.isSubmitting, | ||
@required this.isSuccess, | ||
@required this.isFailure, | ||
this.isError}); | ||
|
||
factory RegistrationState.empty() { | ||
return RegistrationState( | ||
isEmailValid: true, | ||
isPasswordValid: true, | ||
isSubmitting: false, | ||
isSuccess: false, | ||
isFailure: false, | ||
); | ||
} | ||
|
||
factory RegistrationState.loading() { | ||
return RegistrationState( | ||
isEmailValid: true, | ||
isPasswordValid: true, | ||
isSubmitting: true, | ||
isSuccess: false, | ||
isFailure: false, | ||
); | ||
} | ||
|
||
factory RegistrationState.failure(String error) { | ||
return RegistrationState( | ||
isEmailValid: true, | ||
isPasswordValid: true, | ||
isSubmitting: false, | ||
isSuccess: false, | ||
isFailure: true, | ||
isError: error); | ||
} | ||
|
||
factory RegistrationState.success() { | ||
return RegistrationState( | ||
isEmailValid: true, | ||
isPasswordValid: true, | ||
isSubmitting: false, | ||
isSuccess: true, | ||
isFailure: false, | ||
); | ||
} | ||
|
||
RegistrationState update({ | ||
bool isEmailValid, | ||
bool isPasswordValid, | ||
}) { | ||
return copyWith( | ||
isEmailValid: isEmailValid, | ||
isPasswordValid: isPasswordValid, | ||
isSubmitting: false, | ||
isSuccess: false, | ||
isFailure: false, | ||
); | ||
} | ||
|
||
RegistrationState copyWith({ | ||
bool isEmailValid, | ||
bool isPasswordValid, | ||
bool isSubmitEnabled, | ||
bool isSubmitting, | ||
bool isSuccess, | ||
bool isFailure, | ||
}) { | ||
return RegistrationState( | ||
isEmailValid: isEmailValid ?? this.isEmailValid, | ||
isPasswordValid: isPasswordValid ?? this.isPasswordValid, | ||
isSubmitting: isSubmitting ?? this.isSubmitting, | ||
isSuccess: isSuccess ?? this.isSuccess, | ||
isFailure: isFailure ?? this.isFailure, | ||
); | ||
} | ||
|
||
@override | ||
String toString() { | ||
return '''RegistrationState { | ||
isEmailValid: $isEmailValid, | ||
isPasswordValid: $isPasswordValid, | ||
isSubmitting: $isSubmitting, | ||
isSuccess: $isSuccess, | ||
isFailure: $isFailure, | ||
}'''; | ||
} | ||
} |
Oops, something went wrong.