import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../screens/SignUpScreen.dart';
class UserModel {
String userID;
String firstName;
String lastName;
String email;
String imageURL;
bool emailVerified;
bool profileCompleted;
String fcmToken;
String osType;
String country;
String flagCode;
int dateOfBirth;
String bio;
Map<String, dynamic> geo; // New geo field
bool onlineStatus; // New onlineStatus field
// Default constructor with named parameters
UserModel({
this.userID = '',
this.firstName = '',
this.lastName = '',
this.email = '',
this.imageURL = '',
this.emailVerified = false,
this.profileCompleted = false,
this.fcmToken = '',
this.osType = '',
this.country = '',
this.flagCode = '',
this.dateOfBirth = 0,
this.bio = '',
this.geo = const {}, // Initialize the geo field with an empty map
this.onlineStatus = false, // Initialize the onlineStatus field
});
// Factory constructor to create UserModel from Firestore document
factory UserModel.fromDocument(DocumentSnapshot document) {
return UserModel(
userID: document['userID'] ?? '',
firstName: document['firstName'] ?? '',
lastName: document['lastName'] ?? '',
email: document['email'] ?? '',
imageURL: document['imageURL'] ?? '',
emailVerified: document['emailVerified'] ?? false,
profileCompleted: document['profileCompleted'] ?? false,
fcmToken: document['fcmToken'] ?? '',
osType: document['osType'] ?? '',
country: document['country'] ?? '',
flagCode: document['flagCode'] ?? '',
dateOfBirth: document['dateOfBirth'] ?? 0,
bio: document['bio'] ?? '',
geo: Map<String, dynamic>.from(document['geo'] ?? {}), // New geo field
onlineStatus: document['onlineStatus'] ?? false, // New onlineStatus field
);
}
// Factory constructor to create UserModel from a map
factory UserModel.fromLinkedMap(Map<String, dynamic> map) {
return UserModel(
userID: map['userID'] ?? '',
firstName: map['firstName'] ?? '',
lastName: map['lastName'] ?? '',
email: map['email'] ?? '',
imageURL: map['imageURL'] ?? '',
emailVerified: map['emailVerified'] ?? false,
profileCompleted: map['profileCompleted'] ?? false,
fcmToken: map['fcmToken'] ?? '',
osType: map['osType'] ?? '',
country: map['country'] ?? '',
flagCode: map['flagCode'] ?? '',
dateOfBirth: map['dateOfBirth'] ?? 0,
bio: map['bio'] ?? '',
geo: Map<String, dynamic>.from(map['geo'] ?? {}), // New geo field
onlineStatus: map['onlineStatus'] ?? false, // New onlineStatus field
);
}
// Method to convert UserModel to a map
Map<String, dynamic> toMap() {
return {
'userID': userID,
'firstName': firstName,
'lastName': lastName,
'email': email,
'imageURL': imageURL,
'emailVerified': emailVerified,
'profileCompleted': profileCompleted,
'fcmToken': fcmToken,
'osType': osType,
'country': country,
'flagCode': flagCode,
'dateOfBirth': dateOfBirth,
'bio': bio,
'geo': geo, // New geo field
'onlineStatus': onlineStatus, // New onlineStatus field
};
}
static Future<UserModel?> fetchCurrentUser(String userId, {bool fetchSnapshot = true}) async {
User? user = FirebaseAuth.instance.currentUser;
if (user == null) {
print('User not logged in');
return null;
}
if (userId.isEmpty) {
userId = user.uid;
}
try {
if (fetchSnapshot) {
DocumentSnapshot documentSnapshot = await FirebaseFirestore.instance
.collection('Users')
.doc(userId)
.get();
if (documentSnapshot.exists) {
return UserModel.fromDocument(documentSnapshot);
} else {
print('No document found');
return null;
}
} else {
// Fetch essential user details if fetchSnapshot is false
DocumentSnapshot documentSnapshot = await FirebaseFirestore.instance
.collection('Users')
.doc(userId)
.get();
if (documentSnapshot.exists) {
return UserModel.fromDocument(documentSnapshot);
} else {
print('No document found');
return null;
}
}
} catch (e) {
print('Failed to fetch document: $e');
return null;
}
}
logOut(context) async {
try {
await FirebaseAuth.instance.signOut();
Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (context) => const SignUpScreen()), (route) => false);
} catch (e) {
print('Logout failed: $e');
}
}
}