Last active
April 16, 2020 20:27
-
-
Save HerrNiklasRaab/45c0b0cc9a52d6838501cfcf0eba3818 to your computer and use it in GitHub Desktop.
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
abstract class User extends DataModel { | |
final Name name; | |
final Address address; | |
final Gender gender; | |
final Birthday birthDay; | |
final Email email; | |
final Weight weight; | |
final Length height; | |
final bool hasFinishedSignUp; | |
Iterable<String> get notificationTokens; | |
bool get needsEmailVerification; | |
bool get hasEmailSettings; | |
User finishSignUp(); | |
User updateAuthenticationProvider( | |
List<AuthenticationProvider> authenticationProvider); | |
@override | |
User copyWith( | |
{String id, | |
Email email, | |
Name name, | |
Address address, | |
Gender gender, | |
Birthday birthDay, | |
Weight weight, | |
Length height, | |
bool setNameNull = false, | |
bool setAddressNull = false, | |
bool setBirthdayNull = false, | |
bool setWeightNull = false, | |
bool setHeightNull = false, | |
bool setGenderNull = false}); | |
factory User(String id, AuthenticationProvider authenticationProvider) = | |
UserImpl.newUser; | |
} | |
@jsonSerializable | |
class UserImpl extends DataModel implements User { | |
@override | |
@JsonProperty(enumValues: Gender.values) | |
final Gender gender; | |
@override | |
@JsonProperty() | |
final Birthday birthDay; | |
@override | |
@JsonProperty() | |
final Email email; | |
@override | |
@JsonProperty() | |
final List<String> notificationTokens; | |
@JsonProperty() | |
final List<AuthenticationProvider> authenticationProvider; | |
@override | |
@JsonProperty() | |
final Name name; | |
@override | |
@JsonProperty() | |
final Address address; | |
@override | |
@JsonProperty() | |
final bool hasFinishedSignUp; | |
@override | |
@JsonProperty() | |
final Length height; | |
@override | |
@JsonProperty() | |
final Weight weight; | |
UserImpl({ | |
String id, | |
this.name, | |
this.birthDay, | |
this.email, | |
this.hasFinishedSignUp = false, | |
this.gender, | |
this.address, | |
this.authenticationProvider = const [], | |
this.notificationTokens = const [], | |
this.height, | |
this.weight, | |
}) : super(id: id); | |
factory UserImpl.newUser( | |
String id, AuthenticationProvider authenticationProvider) { | |
return UserImpl( | |
id: id, | |
).updateAuthenticationProvider([authenticationProvider]) as UserImpl; | |
} | |
@override | |
User copyWith( | |
{Email email, | |
Address address, | |
Gender gender, | |
Birthday birthDay, | |
Name name, | |
bool hasFinishedSignUp, | |
List<AuthenticationProvider> authenticationProvider, | |
List<String> notificationTokens, | |
Length height, | |
Weight weight, | |
bool setNameNull = false, | |
bool setAddressNull = false, | |
bool setBirthdayNull = false, | |
bool setWeightNull = false, | |
bool setHeightNull = false, | |
bool setGenderNull = false, | |
String id}) { | |
return UserImpl( | |
id: id ?? this.id, | |
email: email ?? this.email, | |
name: setNameNull ? null : name ?? this.name, | |
hasFinishedSignUp: hasFinishedSignUp ?? this.hasFinishedSignUp, | |
address: setAddressNull ? null : address ?? this.address, | |
gender: setGenderNull ? null : gender ?? this.gender, | |
birthDay: setBirthdayNull ? null : birthDay ?? this.birthDay, | |
authenticationProvider: | |
authenticationProvider ?? this.authenticationProvider, | |
notificationTokens: notificationTokens ?? this.notificationTokens, | |
height: setHeightNull ? null : height ?? this.height, | |
weight: setWeightNull ? null : weight ?? this.weight); | |
} | |
@override | |
User updateAuthenticationProvider( | |
List<AuthenticationProvider> newAuthenticationProvider) { | |
return copyWith(authenticationProvider: [ | |
...newAuthenticationProvider.toSet().toList() | |
]); | |
} | |
@override | |
bool get needsEmailVerification => authenticationProvider.every( | |
(authenticationProvider) => authenticationProvider.shouldValidateEmail); | |
@override | |
User finishSignUp() { | |
return copyWith(hasFinishedSignUp: true); | |
} | |
@override | |
bool get hasEmailSettings => | |
authenticationProvider.any((authenticationProvider) => | |
authenticationProvider.runtimeType == EmailAuthenticationProvider); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment