UserProfileView.swift 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import SwiftUI
  2. import GoogleSignIn
  3. struct UserProfileView: View {
  4. @EnvironmentObject var authViewModel: AuthenticationViewModel
  5. @StateObject var birthdayViewModel = BirthdayViewModel()
  6. private var user: GIDGoogleUser? {
  7. return GIDSignIn.sharedInstance.currentUser
  8. }
  9. var body: some View {
  10. return Group {
  11. if let userProfile = user?.profile {
  12. VStack(spacing: 10) {
  13. HStack(alignment: .top) {
  14. UserProfileImageView(userProfile: userProfile)
  15. .padding(.leading)
  16. VStack(alignment: .leading) {
  17. Text(userProfile.name)
  18. .font(.headline)
  19. Text(userProfile.email)
  20. }
  21. }
  22. Button(NSLocalizedString("Sign Out", comment: "Sign out button"), action: signOut)
  23. .background(Color.blue)
  24. .foregroundColor(Color.white)
  25. .cornerRadius(5)
  26. Button(NSLocalizedString("Disconnect", comment: "Disconnect button"), action: disconnect)
  27. .background(Color.blue)
  28. .foregroundColor(Color.white)
  29. .cornerRadius(5)
  30. Spacer()
  31. NavigationLink(NSLocalizedString("View Days Until Birthday", comment: "View birthday days"),
  32. destination: BirthdayView(birthdayViewModel: birthdayViewModel).onAppear {
  33. guard self.birthdayViewModel.birthday != nil else {
  34. if !self.authViewModel.hasBirthdayReadScope {
  35. self.authViewModel.addBirthdayReadScope {
  36. self.birthdayViewModel.fetchBirthday()
  37. }
  38. } else {
  39. self.birthdayViewModel.fetchBirthday()
  40. }
  41. return
  42. }
  43. })
  44. .background(Color.blue)
  45. .foregroundColor(Color.white)
  46. .cornerRadius(5)
  47. Spacer()
  48. }
  49. } else {
  50. Text(NSLocalizedString("Failed to get user profile!", comment: "Empty user profile text"))
  51. }
  52. }
  53. }
  54. func disconnect() {
  55. authViewModel.disconnect()
  56. }
  57. func signOut() {
  58. authViewModel.signOut()
  59. }
  60. }