UserProfileView.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 2021 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import SwiftUI
  17. import GoogleSignIn
  18. struct UserProfileView: View {
  19. @EnvironmentObject var authViewModel: AuthenticationViewModel
  20. @StateObject var birthdayViewModel = BirthdayViewModel()
  21. private var user: GIDGoogleUser? {
  22. return GIDSignIn.sharedInstance.currentUser
  23. }
  24. var body: some View {
  25. return Group {
  26. if let userProfile = user?.profile {
  27. VStack {
  28. HStack(alignment: .top) {
  29. UserProfileImageView(userProfile: userProfile)
  30. .padding(.leading)
  31. VStack(alignment: .leading) {
  32. Text(userProfile.name)
  33. .accessibilityLabel(Text("User name."))
  34. Text(userProfile.email)
  35. .accessibilityLabel(Text("User email."))
  36. .foregroundColor(.gray)
  37. }
  38. Spacer()
  39. }
  40. NavigationLink(NSLocalizedString("View Days Until Birthday", comment: "View birthday days"),
  41. destination: BirthdayView(birthdayViewModel: birthdayViewModel).onAppear {
  42. guard self.birthdayViewModel.birthday != nil else {
  43. if !self.authViewModel.hasBirthdayReadScope {
  44. self.authViewModel.addBirthdayReadScope {
  45. self.birthdayViewModel.fetchBirthday()
  46. }
  47. } else {
  48. self.birthdayViewModel.fetchBirthday()
  49. }
  50. return
  51. }
  52. })
  53. .accessibilityLabel(Text("View days until birthday."))
  54. Spacer()
  55. }
  56. .toolbar {
  57. ToolbarItemGroup(placement: .navigationBarTrailing) {
  58. Button(NSLocalizedString("Disconnect", comment: "Disconnect button"), action: disconnect)
  59. .accessibilityLabel(Text("Disconnect scope button."))
  60. Button(NSLocalizedString("Sign Out", comment: "Sign out button"), action: signOut)
  61. .accessibilityLabel(Text("Sign out button"))
  62. }
  63. }
  64. } else {
  65. Text(NSLocalizedString("Failed to get user profile!", comment: "Empty user profile text"))
  66. .accessibilityLabel(Text("Failed to get user profile"))
  67. }
  68. }
  69. }
  70. func disconnect() {
  71. authViewModel.disconnect()
  72. }
  73. func signOut() {
  74. authViewModel.signOut()
  75. }
  76. }