UserProfileView.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 signInResult =
  50. try await authViewModel.addBirthdayReadScope(window: window)
  51. self.authViewModel.state = .signedIn(signInResult.user)
  52. self.birthdayViewModel.fetchBirthday()
  53. } catch {
  54. print("Failed to fetch birthday: \(error)")
  55. }
  56. }
  57. } else {
  58. self.birthdayViewModel.fetchBirthday()
  59. }
  60. return
  61. }
  62. })
  63. .background(Color.blue)
  64. .foregroundColor(Color.white)
  65. .cornerRadius(5)
  66. Spacer()
  67. }
  68. } else {
  69. Text(NSLocalizedString("Failed to get user profile!", comment: "Empty user profile text"))
  70. }
  71. }
  72. }
  73. }