-
Notifications
You must be signed in to change notification settings - Fork 1
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
058d48c
commit ec8f80d
Showing
27 changed files
with
301 additions
and
371 deletions.
There are no files selected for viewing
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
52 changes: 0 additions & 52 deletions
52
lib/features/authentication/data/models/authenticated_user_model.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 |
---|---|---|
@@ -1,77 +1,25 @@ | ||
import 'package:coffeecard/features/authentication/domain/entities/authenticated_user.dart'; | ||
import 'package:fpdart/fpdart.dart'; | ||
|
||
class AuthenticatedUserModel extends AuthenticatedUser { | ||
const AuthenticatedUserModel({ | ||
required super.email, | ||
required super.token, | ||
required super.encodedPasscode, | ||
required super.lastLogin, | ||
required super.sessionTimeout, | ||
}); | ||
|
||
factory AuthenticatedUserModel.fromJson(Map<String, dynamic> json) { | ||
final lastLogin = | ||
_nullOrValue<DateTime>(json, 'last_login', DateTime.parse); | ||
final sessionTimeout = _nullOrValue<Duration>( | ||
json, | ||
'session_timeout', | ||
_parseDuration, | ||
); | ||
|
||
return AuthenticatedUserModel( | ||
email: json['email'] as String, | ||
token: json['token'] as String, | ||
encodedPasscode: json['passcode'] as String, | ||
lastLogin: lastLogin, | ||
sessionTimeout: sessionTimeout, | ||
); | ||
} | ||
|
||
static String? _optionToString<T>(Option<T> option) { | ||
return option.match(() => 'null', (value) => value.toString()); | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
return { | ||
'email': email, | ||
'token': token, | ||
'passcode': encodedPasscode, | ||
'last_login': _optionToString(lastLogin), | ||
'session_timeout': _optionToString(sessionTimeout), | ||
}; | ||
} | ||
|
||
static Option<T> _nullOrValue<T>( | ||
Map<String, dynamic> m, | ||
String key, | ||
T Function(String) callback, | ||
) { | ||
if (!m.containsKey(key)) { | ||
return none(); | ||
} | ||
|
||
final val = m[key] as String; | ||
|
||
if (val == 'null') { | ||
return none(); | ||
} | ||
|
||
return Some(callback(val)); | ||
} | ||
|
||
static Duration _parseDuration(String s) { | ||
int hours = 0; | ||
int minutes = 0; | ||
|
||
final parts = s.split(':'); | ||
if (parts.length > 2) { | ||
hours = int.parse(parts[parts.length - 3]); | ||
} | ||
if (parts.length > 1) { | ||
minutes = int.parse(parts[parts.length - 2]); | ||
} | ||
|
||
return Duration(hours: hours, minutes: minutes); | ||
} | ||
} |
7 changes: 0 additions & 7 deletions
7
lib/features/authentication/domain/entities/authenticated_user.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 |
---|---|---|
@@ -1,27 +1,20 @@ | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:fpdart/fpdart.dart'; | ||
|
||
class AuthenticatedUser extends Equatable { | ||
final String email; | ||
final String token; | ||
final String encodedPasscode; | ||
final Option<DateTime> lastLogin; | ||
final Option<Duration> sessionTimeout; | ||
|
||
const AuthenticatedUser({ | ||
required this.email, | ||
required this.token, | ||
required this.encodedPasscode, | ||
required this.lastLogin, | ||
required this.sessionTimeout, | ||
}); | ||
|
||
@override | ||
List<Object?> get props => [ | ||
email, | ||
token, | ||
encodedPasscode, | ||
lastLogin, | ||
sessionTimeout, | ||
]; | ||
} |
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
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
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
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
Oops, something went wrong.