UserProfileView.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. Text(userProfile.email)
  35. }
  36. }
  37. NavigationLink(NSLocalizedString("View Days Until Birthday", comment: "View birthday days"),
  38. destination: BirthdayView(birthdayViewModel: birthdayViewModel).onAppear {
  39. guard self.birthdayViewModel.birthday != nil else {
  40. if !self.authViewModel.hasBirthdayReadScope {
  41. self.authViewModel.addBirthdayReadScope {
  42. self.birthdayViewModel.fetchBirthday()
  43. }
  44. } else {
  45. self.birthdayViewModel.fetchBirthday()
  46. }
  47. return
  48. }
  49. })
  50. Spacer()
  51. }
  52. .toolbar {
  53. ToolbarItemGroup(placement: .navigationBarTrailing) {
  54. Button(NSLocalizedString("Disconnect", comment: "Disconnect button"), action: disconnect)
  55. Button(NSLocalizedString("Sign Out", comment: "Sign out button"), action: signOut)
  56. }
  57. }
  58. } else {
  59. Text(NSLocalizedString("Failed to get user profile!", comment: "Empty user profile text"))
  60. }
  61. }
  62. }
  63. func disconnect() {
  64. authViewModel.disconnect()
  65. }
  66. func signOut() {
  67. authViewModel.signOut()
  68. }
  69. }