mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
add fetchViewer async func
This commit is contained in:
parent
5d0a5112d6
commit
ea2ba678c0
2 changed files with 52 additions and 15 deletions
|
|
@ -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<AnyCancellable>()
|
||||
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -5,6 +5,47 @@ import SwiftGraphQL
|
|||
import Utils
|
||||
|
||||
public extension DataService {
|
||||
func fetchViewer() async throws -> Viewer {
|
||||
let selection = Selection<Viewer, Objects.User> {
|
||||
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<Viewer, BasicError> {
|
||||
internalViewerPublisher()
|
||||
.handleEvents(receiveOutput: {
|
||||
|
|
|
|||
Loading…
Reference in a new issue