From ea2ba678c09b0c96d204eb206d1e2e6059a911c6 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Thu, 14 Apr 2022 13:35:28 -0700 Subject: [PATCH] add fetchViewer async func --- .../App/Views/Profile/ProfileView.swift | 26 +++++------- .../DataService/Queries/ViewerPublisher.swift | 41 +++++++++++++++++++ 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/Profile/ProfileView.swift b/apple/OmnivoreKit/Sources/App/Views/Profile/ProfileView.swift index 80d6eef94..aab2f65ab 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Profile/ProfileView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Profile/ProfileView.swift @@ -5,12 +5,10 @@ import SwiftUI import Utils import Views -final class ProfileContainerViewModel: ObservableObject { +@MainActor final class ProfileContainerViewModel: ObservableObject { @Published var isLoading = false @Published var profileCardData = ProfileCardData() - var subscriptions = Set() - var appVersionString: String { if let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String { return "Omnivore Version \(appVersion)" @@ -19,18 +17,14 @@ final class ProfileContainerViewModel: ObservableObject { } } - func loadProfileData(dataService: DataService) { - dataService.viewerPublisher().sink( - receiveCompletion: { _ in }, - receiveValue: { [weak self] viewer in - self?.profileCardData = ProfileCardData( - name: viewer.name, - username: viewer.username, - imageURL: viewer.profileImageURL.flatMap { URL(string: $0) } - ) - } + func loadProfileData(dataService: DataService) async { + guard let viewer = try? await dataService.fetchViewer() else { return } + + profileCardData = ProfileCardData( + name: viewer.name, + username: viewer.username, + imageURL: viewer.profileImageURL.flatMap { URL(string: $0) } ) - .store(in: &subscriptions) } } @@ -59,7 +53,9 @@ struct ProfileView: View { Group { Section { ProfileCard(data: viewModel.profileCardData) - .onAppear { viewModel.loadProfileData(dataService: dataService) } + .task { + await viewModel.loadProfileData(dataService: dataService) + } } Section { diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Queries/ViewerPublisher.swift b/apple/OmnivoreKit/Sources/Services/DataService/Queries/ViewerPublisher.swift index 7b91e2c50..39961b7cb 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Queries/ViewerPublisher.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Queries/ViewerPublisher.swift @@ -5,6 +5,47 @@ import SwiftGraphQL import Utils public extension DataService { + func fetchViewer() async throws -> Viewer { + let selection = Selection { + Viewer( + username: try $0.profile( + selection: .init { try $0.username() } + ), + name: try $0.name(), + profileImageURL: try $0.profile( + selection: .init { try $0.pictureUrl() } + ), + userID: try $0.id() + ) + } + + let query = Selection.Query { + try $0.me(selection: selection.nonNullOrFail) + } + + let path = appEnvironment.graphqlPath + let headers = networker.defaultHeaders + + return try await withCheckedThrowingContinuation { continuation in + send(query, to: path, headers: headers) { [weak self] result in + switch result { + case let .success(payload): + self?.currentViewer = payload.data + if UserDefaults.standard.string(forKey: Keys.userIdKey) == nil { + UserDefaults.standard.setValue(payload.data.userID, forKey: Keys.userIdKey) + DataService.registerIntercomUser?(payload.data.userID) + } + continuation.resume(returning: payload.data) + case .failure: + continuation.resume(throwing: BasicError.message(messageText: "http error")) + } + } + } + } +} + +public extension DataService { + @available(*, deprecated, message: "use async version instead") func viewerPublisher() -> AnyPublisher { internalViewerPublisher() .handleEvents(receiveOutput: {