Enhance User model with optional fields and update authentication methods to include profile picture and phone number
This commit is contained in:
@@ -1,36 +1,49 @@
|
||||
import 'dart:convert';
|
||||
|
||||
/// Model representing a user in the travel mate application.
|
||||
///
|
||||
///
|
||||
/// This class encapsulates user information including personal details
|
||||
/// and provides methods for serialization/deserialization with Firebase
|
||||
/// and JSON operations.
|
||||
class User {
|
||||
/// Unique identifier for the user (usually Firebase UID).
|
||||
final String? id;
|
||||
|
||||
|
||||
/// User's last name.
|
||||
final String nom;
|
||||
|
||||
|
||||
/// User's first name.
|
||||
final String prenom;
|
||||
|
||||
|
||||
/// User's email address.
|
||||
final String email;
|
||||
|
||||
/// User's profile picture URL (optional).
|
||||
final String? profilePictureUrl;
|
||||
|
||||
/// User's phone number (optional).
|
||||
final String? phoneNumber;
|
||||
|
||||
/// Platform used for authentication (e.g., 'google', 'apple', 'email').
|
||||
final String platform;
|
||||
|
||||
/// Creates a new [User] instance.
|
||||
///
|
||||
/// [nom], [prenom], and [email] are required fields.
|
||||
///
|
||||
/// [nom], [prenom], [email] and [platform] are required fields.
|
||||
/// [profilePictureUrl] and [phoneNumber] are optional.
|
||||
/// [id] is optional and typically assigned by Firebase.
|
||||
User({
|
||||
this.id,
|
||||
required this.nom,
|
||||
required this.prenom,
|
||||
required this.email,
|
||||
this.profilePictureUrl,
|
||||
this.phoneNumber,
|
||||
required this.platform,
|
||||
});
|
||||
|
||||
/// Creates a [User] instance from a Map (useful for Firebase operations).
|
||||
///
|
||||
///
|
||||
/// Handles null values gracefully by providing empty string defaults.
|
||||
factory User.fromMap(Map<String, dynamic> map) {
|
||||
return User(
|
||||
@@ -38,11 +51,14 @@ class User {
|
||||
nom: map['nom'] ?? '',
|
||||
prenom: map['prenom'] ?? '',
|
||||
email: map['email'] ?? '',
|
||||
profilePictureUrl: map['profilePictureUrl'],
|
||||
phoneNumber: map['phoneNumber'],
|
||||
platform: map['platform'] ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
/// Creates a [User] instance from a JSON string.
|
||||
///
|
||||
///
|
||||
/// Parses the JSON and delegates to [fromMap] for object creation.
|
||||
factory User.fromJson(String jsonStr) {
|
||||
Map<String, dynamic> map = json.decode(jsonStr);
|
||||
@@ -50,7 +66,7 @@ class User {
|
||||
}
|
||||
|
||||
/// Converts the [User] instance to a Map (useful for Firebase operations).
|
||||
///
|
||||
///
|
||||
/// Returns a map with all user properties for database storage.
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
@@ -58,11 +74,14 @@ class User {
|
||||
'nom': nom,
|
||||
'prenom': prenom,
|
||||
'email': email,
|
||||
'profilePictureUrl': profilePictureUrl,
|
||||
'phoneNumber': phoneNumber,
|
||||
'platform': platform,
|
||||
};
|
||||
}
|
||||
|
||||
/// Converts the [User] instance to a JSON string.
|
||||
///
|
||||
///
|
||||
/// Useful for API communications and data serialization.
|
||||
String toJson() {
|
||||
return json.encode(toMap());
|
||||
@@ -72,7 +91,7 @@ class User {
|
||||
String get fullName => '$prenom $nom';
|
||||
|
||||
/// Creates a copy of this user with optionally modified properties.
|
||||
///
|
||||
///
|
||||
/// Allows updating specific fields while preserving others.
|
||||
/// Useful for state management and partial updates.
|
||||
User copyWith({
|
||||
@@ -80,12 +99,18 @@ class User {
|
||||
String? nom,
|
||||
String? prenom,
|
||||
String? email,
|
||||
String? profilePictureUrl,
|
||||
String? phoneNumber,
|
||||
String? platform,
|
||||
}) {
|
||||
return User(
|
||||
id: id ?? this.id,
|
||||
nom: nom ?? this.nom,
|
||||
prenom: prenom ?? this.prenom,
|
||||
email: email ?? this.email,
|
||||
profilePictureUrl: profilePictureUrl ?? this.profilePictureUrl,
|
||||
phoneNumber: phoneNumber ?? this.phoneNumber,
|
||||
platform: platform ?? this.platform,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -96,7 +121,7 @@ class User {
|
||||
}
|
||||
|
||||
/// Compares users based on email address.
|
||||
///
|
||||
///
|
||||
/// Two users are considered equal if they have the same email.
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
@@ -108,4 +133,3 @@ class User {
|
||||
@override
|
||||
int get hashCode => email.hashCode;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user