UserProfileView.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. .accessibilityLabel(Text("User name."))
  20. Text(userProfile.email)
  21. .accessibilityLabel(Text("User email."))
  22. }
  23. }
  24. Button(NSLocalizedString("Sign Out", comment: "Sign out button"), action: signOut)
  25. .background(Color.blue)
  26. .foregroundColor(Color.white)
  27. .cornerRadius(5)
  28. .accessibilityLabel(Text("Sign out button"))
  29. Button(NSLocalizedString("Disconnect", comment: "Disconnect button"), action: disconnect)
  30. .background(Color.blue)
  31. .foregroundColor(Color.white)
  32. .cornerRadius(5)
  33. .accessibilityLabel(Text("Disconnect scope button."))
  34. Spacer()
  35. NavigationLink(NSLocalizedString("View Days Until Birthday", comment: "View birthday days"),
  36. destination: BirthdayView(birthdayViewModel: birthdayViewModel).onAppear {
  37. guard self.birthdayViewModel.birthday != nil else {
  38. if !self.authViewModel.hasBirthdayReadScope {
  39. self.authViewModel.addBirthdayReadScope {
  40. self.birthdayViewModel.fetchBirthday()
  41. }
  42. } else {
  43. self.birthdayViewModel.fetchBirthday()
  44. }
  45. return
  46. }
  47. })
  48. .background(Color.blue)
  49. .foregroundColor(Color.white)
  50. .cornerRadius(5)
  51. .accessibilityLabel(Text("View days until birthday."))
  52. Spacer()
  53. }
  54. } else {
  55. Text(NSLocalizedString("Failed to get user profile!", comment: "Empty user profile text"))
  56. .accessibilityLabel(Text("Failed to get user profile"))
  57. }
  58. }
  59. }
  60. func disconnect() {
  61. authViewModel.disconnect()
  62. }
  63. func signOut() {
  64. authViewModel.signOut()
  65. }
  66. }