debugging phase of user creation process

This commit is contained in:
talksik
2021-12-18 17:28:11 -08:00
parent 23a2219863
commit 71f5aff78c
8 changed files with 154 additions and 36 deletions
+8
View File
@@ -64,6 +64,8 @@
89F18AE6276E881900115B1E /* FirebaseStorage in Frameworks */ = {isa = PBXBuildFile; productRef = 89F18AE5276E881900115B1E /* FirebaseStorage */; }; 89F18AE6276E881900115B1E /* FirebaseStorage in Frameworks */ = {isa = PBXBuildFile; productRef = 89F18AE5276E881900115B1E /* FirebaseStorage */; };
89F18AE8276E881900115B1E /* FirebaseStorageCombine-Community in Frameworks */ = {isa = PBXBuildFile; productRef = 89F18AE7276E881900115B1E /* FirebaseStorageCombine-Community */; }; 89F18AE8276E881900115B1E /* FirebaseStorageCombine-Community in Frameworks */ = {isa = PBXBuildFile; productRef = 89F18AE7276E881900115B1E /* FirebaseStorageCombine-Community */; };
89F18AEA276E881900115B1E /* FirebaseStorageSwift-Beta in Frameworks */ = {isa = PBXBuildFile; productRef = 89F18AE9276E881900115B1E /* FirebaseStorageSwift-Beta */; }; 89F18AEA276E881900115B1E /* FirebaseStorageSwift-Beta in Frameworks */ = {isa = PBXBuildFile; productRef = 89F18AE9276E881900115B1E /* FirebaseStorageSwift-Beta */; };
89F18AEC276E92BF00115B1E /* CustomExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89F18AEB276E92BF00115B1E /* CustomExtensions.swift */; };
89F18AEE276EB55C00115B1E /* ServiceState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89F18AED276EB55C00115B1E /* ServiceState.swift */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
@@ -109,6 +111,8 @@
89F05BA3276ACC060002E9C7 /* ContactService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactService.swift; sourceTree = "<group>"; }; 89F05BA3276ACC060002E9C7 /* ContactService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactService.swift; sourceTree = "<group>"; };
89F05BA5276ACD830002E9C7 /* ContactsPickerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactsPickerView.swift; sourceTree = "<group>"; }; 89F05BA5276ACD830002E9C7 /* ContactsPickerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactsPickerView.swift; sourceTree = "<group>"; };
89F1386C2767EB19006680ED /* SignInView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInView.swift; sourceTree = "<group>"; }; 89F1386C2767EB19006680ED /* SignInView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInView.swift; sourceTree = "<group>"; };
89F18AEB276E92BF00115B1E /* CustomExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomExtensions.swift; sourceTree = "<group>"; };
89F18AED276EB55C00115B1E /* ServiceState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServiceState.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
@@ -245,6 +249,7 @@
891A15FC276566B800B26659 /* constants.swift */, 891A15FC276566B800B26659 /* constants.swift */,
89BDCDE427695DB900577829 /* NirvanaImage.swift */, 89BDCDE427695DB900577829 /* NirvanaImage.swift */,
89BDCDE62769616600577829 /* UIImage+Extension.swift */, 89BDCDE62769616600577829 /* UIImage+Extension.swift */,
89F18AEB276E92BF00115B1E /* CustomExtensions.swift */,
); );
path = Globals; path = Globals;
sourceTree = "<group>"; sourceTree = "<group>";
@@ -305,6 +310,7 @@
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
89AD557B276DF86A001658E0 /* FirestoreService.swift */, 89AD557B276DF86A001658E0 /* FirestoreService.swift */,
89F18AED276EB55C00115B1E /* ServiceState.swift */,
); );
path = Repositories; path = Repositories;
sourceTree = "<group>"; sourceTree = "<group>";
@@ -479,6 +485,8 @@
891A16132765A44300B26659 /* ChatsCarouselViewModel.swift in Sources */, 891A16132765A44300B26659 /* ChatsCarouselViewModel.swift in Sources */,
89F05B9B276A87920002E9C7 /* ContactsViewModel.swift in Sources */, 89F05B9B276A87920002E9C7 /* ContactsViewModel.swift in Sources */,
89AD5573276DD790001658E0 /* SplashView.swift in Sources */, 89AD5573276DD790001658E0 /* SplashView.swift in Sources */,
89F18AEC276E92BF00115B1E /* CustomExtensions.swift in Sources */,
89F18AEE276EB55C00115B1E /* ServiceState.swift in Sources */,
891A15E127653A7000B26659 /* nirvana_iosApp.swift in Sources */, 891A15E127653A7000B26659 /* nirvana_iosApp.swift in Sources */,
89BCABED276D2B05009E9B10 /* WavesGlassBackgroundView.swift in Sources */, 89BCABED276D2B05009E9B10 /* WavesGlassBackgroundView.swift in Sources */,
89F05BA6276ACD830002E9C7 /* ContactsPickerView.swift in Sources */, 89F05BA6276ACD830002E9C7 /* ContactsPickerView.swift in Sources */,
@@ -0,0 +1,23 @@
//
// CustomExtensions.swift
// nirvana-ios
//
// Created by Arjun Patel on 12/18/21.
//
import Foundation
extension String {
// custom function to give (949)920-0392 and get out the right thing
func applyPatternOnNumbers(pattern: String, replacementCharacter: Character) -> String {
var pureNumber = self.replacingOccurrences( of: "[^0-9]", with: "", options: .regularExpression)
for index in 0 ..< pattern.count {
guard index < pureNumber.count else { return pureNumber }
let stringIndex = String.Index(utf16Offset: index, in: pattern)
let patternCharacter = pattern[stringIndex]
guard patternCharacter != replacementCharacter else { continue }
pureNumber.insert(patternCharacter, at: stringIndex)
}
return pureNumber
}
}
+8 -7
View File
@@ -6,15 +6,16 @@
// //
import Foundation import Foundation
import FirebaseFirestoreSwift
public struct Messages: Codable { struct Messages: Identifiable, Codable {
let id: String @DocumentID var id: String?
let senderId: String var senderId: String
let receiverId: String var receiverId: String
let sentTimestamp:Date @ServerTimestamp var sentTimestamp:Date?
let listenedTimestamp:Date @ServerTimestamp var listenedToTimestamp:Date?
let audioDataUrl:String var audioDataUrl:String?
} }
+20 -10
View File
@@ -6,18 +6,28 @@
// //
import Foundation import Foundation
import SwiftUI import FirebaseFirestoreSwift
public struct User: Codable { struct User: Identifiable, Codable {
let id: String @DocumentID var id: String?
let firstName: String var firstName: String?
let lastName: String? var lastName: String?
let phoneNumber: String? var phoneNumber: String?
let emailAddress:String? var emailAddress:String?
let avatar:String? var avatar:String?
let lastLoggedInTimestamp: Date @ServerTimestamp var lastLoggedInTimestamp: Date?
let createdTimestamp: Date @ServerTimestamp var createdTimestamp: Date?
enum CodingKeys: String, CodingKey {
case firstName
case lastName
case phoneNumber
case emailAddress
case avatar
case lastLoggedInTimestamp
case createdTimestamp
}
} }
struct TestUser: Identifiable, Hashable { struct TestUser: Identifiable, Hashable {
@@ -10,12 +10,58 @@ import Firebase
import FirebaseFirestore import FirebaseFirestore
class FirestoreService { class FirestoreService {
enum Collections: String { enum Collection: String {
case users = "users" case users = "users"
case messages = "messages" case messages = "messages"
case user_friends = "user_friends" // associating a user to case user_friends = "user_friends" // associating a user to
} }
// private var db = Firestore.firestore() private var db = Firestore.firestore()
func getUser(userId: String) -> User? {
let docRef = db.collection(Collection.users.rawValue).document(userId)
var user:User? = nil
docRef.getDocument { (document, error) in
if let document = document, document.exists {
user = try? document.data(as: User.self)
} else {
user = nil
}
}
return user
}
func createUser(user: User) {
do {
let _ = try db.collection(Collection.users.rawValue).addDocument(from: user)
} catch {
print("error in creating user \(error.localizedDescription)")
}
}
func updateUser(user: User) -> ServiceState {
do {
if user.id != nil {
let _ = try db.collection(Collection.users.rawValue).document(user.id!).setData(from: user)
return ServiceState.success("Updated user in firestore service")
} else {
return ServiceState.error(ServiceError(description: "No user id given to firestore service"))
}
} catch {
print("error in creating user \(error.localizedDescription)")
return ServiceState.error(ServiceError(description: error.localizedDescription))
}
}
func getCollectionRef(_ collectionName: Collection) -> CollectionReference {
return db.collection(collectionName.rawValue)
}
func getFirestoreServerTimestamp() -> FieldValue {
return FieldValue.serverTimestamp()
}
} }
@@ -0,0 +1,17 @@
//
// ServiceStates.swift
// nirvana-ios
//
// Created by Arjun Patel on 12/18/21.
//
import Foundation
enum ServiceState {
case error(ServiceError)
case success(String)
}
struct ServiceError {
let description: String
}
@@ -12,6 +12,7 @@ import Firebase
import FirebaseAuth import FirebaseAuth
struct PhoneCodeVerificationView: View { struct PhoneCodeVerificationView: View {
@ObservedObject private var phoneverificationViewModel = PhoneVerificationViewModel()
@EnvironmentObject private var navigationStack: NavigationStack @EnvironmentObject private var navigationStack: NavigationStack
@State var verificationCode = "" @State var verificationCode = ""
@@ -98,10 +99,17 @@ struct PhoneCodeVerificationView: View {
print("user id that was authenticated is: \(userId)") print("user id that was authenticated is: \(userId)")
print("user id that was authenticated is: \(userPhoneNumber)") print("user id that was authenticated is: \(userPhoneNumber)")
// get user from firestore using the firebase userid given, // if for some reason firebase couldn't get basic user details
// if not there, create if userId == nil || userPhoneNumber == nil {
self.toastText = "⚠️ Error with Verification"
self.toastSubMessage = "Code is invalid. Please try again or re-enter your phone number in the previous page."
self.showToast.toggle()
print((err?.localizedDescription)!)
return
}
self.phoneverificationViewModel.createOrUpdateUser(userId: userId!, phoneNumber: userPhoneNumber!)
// then sending to next page // then sending to next page
self.navigationStack.push(OnboardingTrioView()) // verify code page self.navigationStack.push(OnboardingTrioView()) // verify code page
@@ -9,26 +9,31 @@ import Foundation
import FirebaseAuth import FirebaseAuth
final class PhoneVerificationViewModel : ObservableObject { final class PhoneVerificationViewModel : ObservableObject {
private var firestoreService = FirestoreService()
// TODO: add alert message handler here that publishes changes
public func verifyPhoneAndSendSMS(phoneNumber: String) { public func verifyPhoneAndSendSMS(phoneNumber: String) {
// TODO: do some string validation here // TODO: do some string validation here
// do auth stuff from firebase // do auth stuff from firebase
} }
public func createOrUpdateUser(userId: String, phoneNumber: String) {
} // TODO: set all in a transaction or batch write
extension String { // get user - 1 result set cost
// custom function to give (949)920-0392 and get out the right thing var user = firestoreService.getUser(userId: userId)
func applyPatternOnNumbers(pattern: String, replacementCharacter: Character) -> String { print("user that was received from get: \(user?.id)")
var pureNumber = self.replacingOccurrences( of: "[^0-9]", with: "", options: .regularExpression)
for index in 0 ..< pattern.count { if user == nil {// if empty, create user - 1 result set cost..prolly higher cost
guard index < pureNumber.count else { return pureNumber } let newOrExistingUser = User(id: userId, phoneNumber: phoneNumber)
let stringIndex = String.Index(utf16Offset: index, in: pattern) firestoreService.createUser(user: newOrExistingUser)
let patternCharacter = pattern[stringIndex] } else { // if not, change last logged in value
guard patternCharacter != replacementCharacter else { continue } // assign to nil so that server can fill it in
pureNumber.insert(patternCharacter, at: stringIndex) user!.lastLoggedInTimestamp = nil
let _ = firestoreService.updateUser(user: user!)
} }
return pureNumber
} }
} }