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
|
2023-01-12 13:59:13 +01:00
|
|
|
@State private var firstLoadFinished = false
|
2023-01-04 20:56:26 +01:00
|
|
|
|
|
|
|
var body: some View {
|
2023-01-09 10:56:20 +01:00
|
|
|
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 {
|
2023-01-09 10:56:20 +01:00
|
|
|
Spacer()
|
2023-01-06 13:05:21 +01:00
|
|
|
LoadingIndicator()
|
2023-01-09 10:56:20 +01:00
|
|
|
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 {
|
2023-01-12 13:59:13 +01:00
|
|
|
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.
|
2023-01-12 13:59:13 +01:00
|
|
|
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
|
|
|
}
|