User Model Definition
Overview
The User model is a Plain Old Java Object (POJO) that serves as the central data structure for identity management in the RideEase application. It is designed for seamless serialization and deserialization with Firebase Firestore, representing both "Rider" and "Driver" entities through a unified schema.
Data Schema
The model captures essential profile information, contact details, and authentication metadata.
| Property | Type | Description |
| :--- | :--- | :--- |
| userId | String | The unique identifier provided by Firebase Authentication (UID). |
| userType | String | Defines the user's role: "rider" or "driver". |
| fullName | String | The display name or legal name of the user. |
| email | String | The registered email address used for login and verification. |
| phoneNumber | String | The verified mobile number (including country code). |
| profilePictureUrl| String | A URL pointing to the user's profile image hosted on Firebase Storage. |
| isPhoneVerified | boolean | Indicates if the user has successfully completed the OTP verification flow. |
Serialization and Firestore Integration
The User class is specifically optimized for Firebase Firestore. It includes a required no-argument constructor, which allows the Firestore SDK to automatically map document fields to the object properties.
Reading Data
When fetching a user profile from the database, use the toObject() method:
// Example: Converting a Firestore Document to a User object
DocumentSnapshot doc = ...;
User profile = doc.toObject(User.class);
if (profile != null) {
String name = profile.getFullName();
String type = profile.getUserType();
}
Writing Data
To update or create a profile, you can pass the model instance directly to a Firestore reference:
User newUser = new User(
"auth_uid_123",
"rider",
"John Doe",
"john@example.com",
"+91 9876543210",
"https://storage.link/profile.jpg",
true
);
firestore.collection("users")
.document(newUser.getUserId())
.set(newUser);
Usage in ViewModels
The User model is frequently utilized within AuthViewModel to manage the UI state of a logged-in user. It is typically exposed via LiveData to allow Activities and Fragments to reactively update the UI when profile data changes.
// AuthViewModel.java usage pattern
private final MutableLiveData<User> userProfile = new MutableLiveData<>();
public LiveData<User> getUserProfile() {
return userProfile;
}
// Update the UI based on the model
viewModel.getUserProfile().observe(this, user -> {
binding.userNameText.setText(user.getFullName());
binding.userRoleBadge.setText(user.getUserType());
});
Validation Rules
While the model holds the data, validation is enforced during the construction or update phase in the UI/ViewModel layer:
- Email: Must match standard
Patterns.EMAIL_ADDRESS. - Phone Number: Must include a valid country code (e.g.,
+91) followed by a 10-digit numeric string. - User Type: Restricted to
"rider"or"driver". Selecting an incorrect type during login results in an authentication failure.