From ef2d859dd219ac54212efc5b01942846ced0c1f0 Mon Sep 17 00:00:00 2001 From: talksik Date: Mon, 13 Dec 2021 17:13:18 -0800 Subject: [PATCH] commiting changes to user and such without the breaking changes --- nirvana-ios/ContentView.swift | 14 ++++----- nirvana-ios/Globals/SessionStore.swift | 30 +++++++++++++++++++ nirvana-ios/Models/User.swift | 17 +++++++++++ .../ChatsCarouselView/ChatsCarouselView.swift | 4 +-- .../ChatsCarouselViewModel.swift | 12 ++++---- 5 files changed, 61 insertions(+), 16 deletions(-) create mode 100644 nirvana-ios/Globals/SessionStore.swift diff --git a/nirvana-ios/ContentView.swift b/nirvana-ios/ContentView.swift index d0aa062..4b6297e 100644 --- a/nirvana-ios/ContentView.swift +++ b/nirvana-ios/ContentView.swift @@ -7,9 +7,8 @@ import SwiftUI -// dummy parent navigation page to test out all pages for now struct ContentView: View { -// @State private var selectedView: NavigationViews? = NavigationViews.welcomeView + @EnvironmentObject var authSession: AuthSessionStore var body: some View { NavigationView { @@ -21,18 +20,17 @@ struct ContentView: View { NavigationLink(destination: HomeView()) { Text("Home Page") } - - NavigationLink(destination: WelcomeView()) { - Text("Onboarding - 1") - } } .navigationBarTitle("View All Pages", displayMode: .inline) - - } + }.onAppear(perform: self.getUser) // .navigationViewStyle(StackNavigationViewStyle()) // .environmentObject()//can pass anything to send to all views within navigation view } + + func getUser () { + authSession.listen() + } } struct ContentView_Previews: PreviewProvider { diff --git a/nirvana-ios/Globals/SessionStore.swift b/nirvana-ios/Globals/SessionStore.swift new file mode 100644 index 0000000..01a89ae --- /dev/null +++ b/nirvana-ios/Globals/SessionStore.swift @@ -0,0 +1,30 @@ +// +// AuthSessionStore.swift +// nirvana-ios +// +// Created by Arjun Patel on 12/13/21. +// + +import Foundation +import Firebase +import Combine + +class AuthSessionStore : ObservableObject { + @Published public var firebaseUser : User? + private var handle: AuthStateDidChangeListenerHandle? + + func listen() { + // monitor authentication changes using firebase + handle = Auth.auth().addStateDidChangeListener { (auth, user) in + if let user = user { + // if we have a user, create a new user model + print("Got user: \(user)") + self.firebaseUser = User( + _uid: user.uid, _email: user.email, _displayName: user.displayName, _profilePic: user.photoURL, _phoneNumber: user.phoneNumber) + } else { + // if we don't have a user, set our session to nil + self.firebaseUser = nil + } + } + } +} diff --git a/nirvana-ios/Models/User.swift b/nirvana-ios/Models/User.swift index 68323aa..ea0a6a0 100644 --- a/nirvana-ios/Models/User.swift +++ b/nirvana-ios/Models/User.swift @@ -9,6 +9,23 @@ import Foundation import SwiftUI struct User: Identifiable, Hashable { + var id:String = UUID().uuidString + var email:String? + var displayName:String? + var profilePictureUrl:URL? + var phoneNumber:String? + + init(_uid:String = UUID().uuidString, _email:String?, _displayName:String?, _profilePic:URL?, _phoneNumber: String?) { + self.id = _uid + self.email = _email + self.profilePictureUrl = _profilePic + self.displayName = _displayName + self.phoneNumber = _phoneNumber + } +} + + +struct TestUser: Identifiable, Hashable { var id = UUID().uuidString var profilePictureUrl:String diff --git a/nirvana-ios/Views/HomeView/ChatsCarouselView/ChatsCarouselView.swift b/nirvana-ios/Views/HomeView/ChatsCarouselView/ChatsCarouselView.swift index 4927dd2..b8c92c3 100644 --- a/nirvana-ios/Views/HomeView/ChatsCarouselView/ChatsCarouselView.swift +++ b/nirvana-ios/Views/HomeView/ChatsCarouselView/ChatsCarouselView.swift @@ -11,9 +11,9 @@ struct ChatsCarouselView: View { @StateObject var viewModel:ChatsCarouselViewModel = ChatsCarouselViewModel() // current snapped/selected user @State var selectedUserId: String = "" - @State var selectedUser: User? + @State var selectedUser: TestUser? - func getSelectedUser(userId: String) -> User { + func getSelectedUser(userId: String) -> TestUser { return self.viewModel.carouselUsers.filter{user in return user.id == userId }[0] diff --git a/nirvana-ios/Views/HomeView/ChatsCarouselView/ChatsCarouselViewModel.swift b/nirvana-ios/Views/HomeView/ChatsCarouselView/ChatsCarouselViewModel.swift index acf9812..fe5031b 100644 --- a/nirvana-ios/Views/HomeView/ChatsCarouselView/ChatsCarouselViewModel.swift +++ b/nirvana-ios/Views/HomeView/ChatsCarouselView/ChatsCarouselViewModel.swift @@ -8,15 +8,15 @@ import Foundation final class ChatsCarouselViewModel: ObservableObject { - @Published var carouselUsers:[User] + @Published var carouselUsers:[TestUser] init() { self.carouselUsers = [ - User(_profilePic: "liam", _firstN: "Liam", _lastN: "Digregorio"), - User(_profilePic: "heran", _firstN: "Heran", _lastN: "Patel"), - User(_profilePic: "sarth", _firstN: "Sarth", _lastN: "Shah"), - User(_profilePic: "kevin", _firstN: "Kevin", _lastN: "Le"), - User(_profilePic: "rohan", _firstN: "Rohan", _lastN: "Chadha") + TestUser(_profilePic: "liam", _firstN: "Liam", _lastN: "Digregorio"), + TestUser(_profilePic: "heran", _firstN: "Heran", _lastN: "Patel"), + TestUser(_profilePic: "sarth", _firstN: "Sarth", _lastN: "Shah"), + TestUser(_profilePic: "kevin", _firstN: "Kevin", _lastN: "Le"), + TestUser(_profilePic: "rohan", _firstN: "Rohan", _lastN: "Chadha") ] } }