UserProfileView.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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(spacing: 10) {
  28. HStack(alignment: .top) {
  29. UserProfileImageView(userProfile: userProfile)
  30. .padding(.leading)
  31. VStack(alignment: .leading) {
  32. Text(userProfile.name)
  33. .font(.headline)
  34. .accessibilityLabel(Text("User name."))
  35. Text(userProfile.email)
  36. .accessibilityLabel(Text("User email."))
  37. }
  38. }
  39. NavigationLink(NSLocalizedString("View Days Until Birthday", comment: "View birthday days"),
  40. destination: BirthdayView(birthdayViewModel: birthdayViewModel).onAppear {
  41. guard self.birthdayViewModel.birthday != nil else {
  42. if !self.authViewModel.hasBirthdayReadScope {
  43. self.authViewModel.addBirthdayReadScope {
  44. self.birthdayViewModel.fetchBirthday()
  45. }
  46. } else {
  47. self.birthdayViewModel.fetchBirthday()
  48. }
  49. return
  50. }
  51. })
  52. Spacer()
  53. }
  54. .toolbar {
  55. ToolbarItemGroup(placement: .navigationBarTrailing) {
  56. Button(NSLocalizedString("Disconnect", comment: "Disconnect button"), action: disconnect)
  57. .accessibilityLabel(Text("Disconnect scope button."))
  58. Button(NSLocalizedString("Sign Out", comment: "Sign out button"), action: signOut)
  59. .accessibilityLabel(Text("Sign out button"))
  60. }
  61. }
  62. } else {
  63. Text(NSLocalizedString("Failed to get user profile!", comment: "Empty user profile text"))
  64. .accessibilityLabel(Text("Failed to get user profile"))
  65. }
  66. }
  67. }
  68. func disconnect() {
  69. authViewModel.disconnect()
  70. }
  71. func signOut() {
  72. authViewModel.signOut()
  73. }
  74. }