Impressia/Vernissage/Views/UserProfileView.swift

56 lines
1.9 KiB
Swift
Raw Normal View History

2023-01-04 20:56:26 +01:00
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
// Licensed under the MIT License.
//
import SwiftUI
2023-01-10 08:04:25 +01:00
import MastodonKit
2023-01-04 20:56:26 +01:00
struct UserProfileView: View {
2023-01-06 13:05:21 +01:00
@EnvironmentObject private var applicationState: ApplicationState
2023-01-04 20:56:26 +01:00
@State public var accountId: String
@State public var accountDisplayName: String?
@State public var accountUserName: String
@State private var account: Account? = nil
2023-01-05 11:55:20 +01:00
@State private var relationship: Relationship? = nil
@State private var firstLoadFinished = false
2023-01-04 20:56:26 +01:00
var body: some View {
VStack {
if let account = self.account {
ScrollView {
UserProfileHeader(account: account, relationship: relationship)
UserProfileStatuses(accountId: account.id)
}
2023-01-04 20:56:26 +01:00
} else {
Spacer()
2023-01-06 13:05:21 +01:00
LoadingIndicator()
Spacer()
2023-01-04 20:56:26 +01:00
}
}
.navigationBarTitle(self.accountDisplayName ?? self.accountUserName)
2023-01-11 13:16:43 +01:00
.task {
do {
try await self.loadData()
} catch {
2023-01-20 13:47:38 +01:00
ErrorService.shared.handle(error, message: "Error during download account from server.", showToastr: !Task.isCancelled)
2023-01-04 20:56:26 +01:00
}
}
}
2023-01-09 08:29:55 +01:00
private func loadData() async throws {
guard firstLoadFinished == false else {
return
}
2023-01-22 21:44:07 +01:00
async let relationshipTask = AccountService.shared.relationships(withId: self.accountId, forUser: self.applicationState.accountData)
async let accountTask = AccountService.shared.account(withId: self.accountId, and: self.applicationState.accountData)
2023-01-09 08:29:55 +01:00
// Wait for download account and relationships.
self.firstLoadFinished = true
2023-01-09 08:29:55 +01:00
(self.relationship, self.account) = try await (relationshipTask, accountTask)
}
2023-01-04 20:56:26 +01:00
}