Skip to content

Commit 0bd7587

Browse files
authored
Support Swift6 version to Birthday sample app. (#544)
1 parent 12cf374 commit 0bd7587

File tree

3 files changed

+39
-27
lines changed

3 files changed

+39
-27
lines changed

Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class GoogleSignInAuthenticator: ObservableObject {
2929

3030
/// Signs in the user based upon the selected account.'
3131
/// - note: Successful calls to this will set the `authViewModel`'s `state` property.
32-
func signIn() {
32+
@MainActor func signIn() {
3333
#if os(iOS)
3434
guard let rootViewController = UIApplication.shared.windows.first?.rootViewController else {
3535
print("There is no root view controller!")
@@ -98,7 +98,7 @@ final class GoogleSignInAuthenticator: ObservableObject {
9898
/// `addScopes(_:presenting:)` request.
9999
/// - note: Successful requests will update the `authViewModel.state` with a new current user that
100100
/// has the granted scope.
101-
func addBirthdayReadScope(completion: @escaping () -> Void) {
101+
@MainActor func addBirthdayReadScope(completion: @escaping () -> Void) {
102102
guard let currentUser = GIDSignIn.sharedInstance.currentUser else {
103103
fatalError("No user signed in!")
104104
}

Samples/Swift/DaysUntilBirthday/Shared/Services/UserProfileImageLoader.swift

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,50 @@ typealias GIDImage = NSImage
2222

2323
import Combine
2424
import SwiftUI
25-
import GoogleSignIn
25+
@preconcurrency import GoogleSignIn
2626

2727
/// An observable class for loading the current user's profile image.
28-
final class UserProfileImageLoader: ObservableObject {
28+
@MainActor final class UserProfileImageLoader: ObservableObject {
2929
private let userProfile: GIDProfileData
30-
private let imageLoaderQueue = DispatchQueue(label: "com.google.days-until-birthday")
30+
private var imageLoadingTask: Task<Void, Never>?
3131
/// A `UIImage` property containing the current user's profile image.
3232
/// - note: This will default to a placeholder, and updates will be published to subscribers.
3333
@Published var image = GIDImage(named: "PlaceholderAvatar")!
3434

3535
/// Creates an instance of this loader with provided user profile.
3636
/// - note: The instance will asynchronously fetch the image data upon creation.
37-
init(userProfile: GIDProfileData) {
38-
self.userProfile = userProfile
39-
guard userProfile.hasImage else {
40-
return
37+
init(userProfile: GIDProfileData) {
38+
self.userProfile = userProfile
39+
guard userProfile.hasImage else {
40+
return
41+
}
42+
imageLoadingTask = Task {
43+
await loadProfileImage()
44+
}
4145
}
4246

43-
imageLoaderQueue.async {
44-
#if os(iOS)
45-
let dimension = 45 * UIScreen.main.scale
46-
#elseif os(macOS)
47-
let dimension = 120
48-
#endif
49-
guard let url = userProfile.imageURL(withDimension: UInt(dimension)),
50-
let data = try? Data(contentsOf: url),
51-
let image = GIDImage(data: data) else {
52-
return
53-
}
54-
DispatchQueue.main.async {
55-
self.image = image
56-
}
47+
private func loadProfileImage() async {
48+
#if os(iOS)
49+
let dimension = 45 * UIScreen.main.scale
50+
#elseif os(macOS)
51+
let dimension = 120
52+
#endif
53+
54+
guard let url = userProfile.imageURL(withDimension: UInt(dimension)) else {
55+
return
56+
}
57+
58+
do {
59+
let (imageData, _) = try await URLSession.shared.data(from: url)
60+
if let image = GIDImage(data: imageData) {
61+
self.image = image
62+
}
63+
} catch {
64+
print("Image download failed:", error)
65+
}
66+
}
67+
68+
deinit {
69+
imageLoadingTask?.cancel()
5770
}
58-
}
5971
}

Samples/Swift/DaysUntilBirthday/Shared/ViewModels/AuthenticationViewModel.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class AuthenticationViewModel: ObservableObject {
4646
}
4747

4848
/// Signs the user in.
49-
func signIn() {
49+
@MainActor func signIn() {
5050
authenticator.signIn()
5151
}
5252

@@ -66,8 +66,8 @@ final class AuthenticationViewModel: ObservableObject {
6666

6767
/// Adds the requested birthday read scope.
6868
/// - parameter completion: An escaping closure that is called upon successful completion.
69-
func addBirthdayReadScope(completion: @escaping () -> Void) {
70-
authenticator.addBirthdayReadScope(completion: completion)
69+
@MainActor func addBirthdayReadScope(completion: @escaping () -> Void) {
70+
authenticator.addBirthdayReadScope(completion: completion)
7171
}
7272

7373
}

0 commit comments

Comments
 (0)