Impressia/Vernissage/Views/UserProfileView.swift

167 lines
7.5 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
import MastodonSwift
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 statuses: [Status] = []
2023-01-06 13:05:21 +01:00
@State private var isDuringRelationshipAction = false
2023-01-05 11:55:20 +01:00
private static let initialColumns = 1
@State private var gridColumns = Array(repeating: GridItem(.flexible()), count: initialColumns)
2023-01-04 20:56:26 +01:00
var body: some View {
2023-01-05 11:55:20 +01:00
ScrollView {
2023-01-04 20:56:26 +01:00
if let account = self.account {
2023-01-05 11:55:20 +01:00
VStack(alignment: .leading) {
HStack(alignment: .center) {
2023-01-06 13:05:21 +01:00
UserAvatar(accountAvatar: account.avatar, width: 96, height: 96)
2023-01-05 11:55:20 +01:00
Spacer()
VStack(alignment: .center) {
Text("\(account.statusesCount)")
.font(.title3)
Text("Posts")
.font(.subheadline)
.opacity(0.6)
}
Spacer()
2023-01-05 21:08:19 +01:00
NavigationLink(destination: FollowersView(accountId: account.id)
.environmentObject(applicationState)
) {
VStack(alignment: .center) {
Text("\(account.followersCount)")
.font(.title3)
Text("Followers")
.font(.subheadline)
.opacity(0.6)
}
2023-01-06 13:05:21 +01:00
}.foregroundColor(.mainTextColor)
2023-01-05 11:55:20 +01:00
Spacer()
2023-01-05 21:08:19 +01:00
NavigationLink(destination: FollowingView(accountId: account.id)
.environmentObject(applicationState)
) {
VStack(alignment: .center) {
Text("\(account.followingCount)")
.font(.title3)
Text("Following")
.font(.subheadline)
.opacity(0.6)
}
2023-01-06 13:05:21 +01:00
}.foregroundColor(.mainTextColor)
2023-01-04 20:56:26 +01:00
}
2023-01-05 11:55:20 +01:00
HStack (alignment: .center) {
2023-01-05 21:08:19 +01:00
VStack(alignment: .leading) {
2023-01-06 13:05:21 +01:00
Text(account.displayName ?? account.acct)
.foregroundColor(.mainTextColor)
2023-01-05 21:08:19 +01:00
.font(.footnote)
.fontWeight(.bold)
2023-01-06 13:05:21 +01:00
Text("@\(account.acct)")
.foregroundColor(.lightGrayColor)
2023-01-05 21:08:19 +01:00
.font(.footnote)
}
2023-01-05 11:55:20 +01:00
Spacer()
2023-01-05 21:08:19 +01:00
if self.applicationState.accountData?.id != self.accountId {
Button {
Task {
2023-01-06 13:05:21 +01:00
Task { @MainActor in
self.isDuringRelationshipAction = false
}
HapticService.shared.touch()
self.isDuringRelationshipAction = true
2023-01-05 21:08:19 +01:00
do {
if let relationship = try await AccountService.shared.follow(
forAccountId: self.accountId,
andContext: self.applicationState.accountData
) {
self.relationship = relationship
}
} catch {
print("Error \(error.localizedDescription)")
2023-01-05 14:50:48 +01:00
}
2023-01-05 21:08:19 +01:00
}
} label: {
2023-01-06 13:05:21 +01:00
if isDuringRelationshipAction {
LoadingIndicator()
} else {
HStack {
Image(systemName: relationship?.following == true ? "person.badge.minus" : "person.badge.plus")
Text(relationship?.following == true ? "Unfollow" : (relationship?.followedBy == true ? "Follow back" : "Follow"))
}
2023-01-05 14:50:48 +01:00
}
}
2023-01-06 13:05:21 +01:00
.disabled(isDuringRelationshipAction)
2023-01-05 21:08:19 +01:00
.buttonStyle(.borderedProminent)
2023-01-06 13:05:21 +01:00
.tint(relationship?.following == true ? .dangerColor : .accentColor)
2023-01-05 11:55:20 +01:00
}
2023-01-04 20:56:26 +01:00
}
2023-01-05 21:08:19 +01:00
if let note = account.note, !note.isEmpty {
2023-01-05 11:55:20 +01:00
HTMLFormattedText(note, withFontSize: 14, andWidth: Int(UIScreen.main.bounds.width) - 16)
.padding(.top, -10)
.padding(.leading, -4)
2023-01-04 20:56:26 +01:00
}
2023-01-05 11:55:20 +01:00
Text("Joined \(account.createdAt.toRelative(.isoDateTimeMilliSec))")
2023-01-06 13:05:21 +01:00
.foregroundColor(.lightGrayColor.opacity(0.5))
2023-01-04 20:56:26 +01:00
.font(.footnote)
}
2023-01-05 11:55:20 +01:00
.padding()
2023-01-04 20:56:26 +01:00
2023-01-09 06:47:34 +01:00
ForEach(self.statuses, id: \.id) { item in
NavigationLink(destination: StatusView(statusId: item.id)
.environmentObject(applicationState)) {
ImageRowAsync(attachments: item.mediaAttachments)
}
2023-01-04 20:56:26 +01:00
}
} else {
2023-01-06 13:05:21 +01:00
LoadingIndicator()
2023-01-04 20:56:26 +01:00
}
}
.navigationBarTitle(self.accountDisplayName ?? self.accountUserName)
.onAppear {
Task {
do {
2023-01-05 11:55:20 +01:00
async let relationshipTask = AccountService.shared.getRelationship(withId: self.accountId, forUser: self.applicationState.accountData)
async let accountTask = AccountService.shared.getAccount(withId: self.accountId, and: self.applicationState.accountData)
2023-01-06 13:05:21 +01:00
// Wait for download account and relationships.
2023-01-05 11:55:20 +01:00
(self.relationship, self.account) = try await (relationshipTask, accountTask)
self.statuses = try await AccountService.shared.getStatuses(forAccountId: self.accountId, andContext: self.applicationState.accountData)
2023-01-04 20:56:26 +01:00
} catch {
print("Error \(error.localizedDescription)")
}
}
}
}
}
struct UserProfileView_Previews: PreviewProvider {
static var previews: some View {
UserProfileView(accountId: "", accountDisplayName: "", accountUserName: "")
}
}