UserProfileView.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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(
  23. NSLocalizedString("Sign Out", comment: "Sign out button"),
  24. action: authViewModel.signOut
  25. )
  26. .background(Color.blue)
  27. .foregroundColor(Color.white)
  28. .cornerRadius(5)
  29. Button(
  30. NSLocalizedString("Disconnect", comment: "Disconnect button"),
  31. action: authViewModel.disconnect
  32. )
  33. .background(Color.blue)
  34. .foregroundColor(Color.white)
  35. .cornerRadius(5)
  36. Spacer()
  37. NavigationLink(
  38. NSLocalizedString("View Days Until Birthday", comment: "View birthday days"),
  39. destination: BirthdayView(birthdayViewModel: birthdayViewModel)
  40. .onAppear {
  41. guard self.birthdayViewModel.birthday != nil else {
  42. if !self.authViewModel.hasBirthdayReadScope {
  43. guard let window = NSApplication.shared.windows.first else {
  44. print("There was no presenting window")
  45. return
  46. }
  47. Task { @MainActor in
  48. do {
  49. let user = try await authViewModel.addBirthdayReadScope(window: window)
  50. self.authViewModel.state = .signedIn(user)
  51. self.birthdayViewModel.fetchBirthday()
  52. } catch {
  53. print("Failed to fetch birthday: \(error)")
  54. }
  55. }
  56. } else {
  57. self.birthdayViewModel.fetchBirthday()
  58. }
  59. return
  60. }
  61. })
  62. .background(Color.blue)
  63. .foregroundColor(Color.white)
  64. .cornerRadius(5)
  65. Spacer()
  66. }
  67. } else {
  68. Text(NSLocalizedString("Failed to get user profile!", comment: "Empty user profile text"))
  69. }
  70. }
  71. }
  72. }