Impressia/Vernissage/Widgets/UserProfileView/UserProfileHeader.swift

122 lines
4.5 KiB
Swift
Raw Normal View History

2023-01-09 10:06:21 +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-09 10:06:21 +01:00
struct UserProfileHeader: View {
@EnvironmentObject private var applicationState: ApplicationState
2023-02-03 15:16:30 +01:00
@EnvironmentObject private var client: Client
2023-01-23 18:01:27 +01:00
@EnvironmentObject private var routerPath: RouterPath
2023-01-09 10:06:21 +01:00
@State var account: Account
@State var relationship: Relationship? = nil
2023-01-09 10:06:21 +01:00
var body: some View {
VStack(alignment: .leading) {
HStack(alignment: .center) {
2023-01-24 20:38:21 +01:00
UserAvatar(accountAvatar: account.avatar, size: .profile)
2023-01-09 10:06:21 +01:00
Spacer()
VStack(alignment: .center) {
Text("\(account.statusesCount)")
.font(.title3)
Text("Posts")
.font(.subheadline)
.opacity(0.6)
}
Spacer()
2023-01-23 18:01:27 +01:00
NavigationLink(value: RouteurDestinations.accounts(entityId: account.id, listType: .followers)) {
2023-01-09 10:06:21 +01:00
VStack(alignment: .center) {
Text("\(account.followersCount)")
.font(.title3)
Text("Followers")
.font(.subheadline)
.opacity(0.6)
}
}.foregroundColor(.mainTextColor)
2023-01-23 18:01:27 +01:00
2023-01-09 10:06:21 +01:00
Spacer()
2023-01-23 18:01:27 +01:00
NavigationLink(value: RouteurDestinations.accounts(entityId: account.id, listType: .following)) {
2023-01-09 10:06:21 +01:00
VStack(alignment: .center) {
Text("\(account.followingCount)")
.font(.title3)
Text("Following")
.font(.subheadline)
.opacity(0.6)
}
}.foregroundColor(.mainTextColor)
}
HStack (alignment: .center) {
VStack(alignment: .leading) {
2023-01-18 18:41:42 +01:00
Text(account.displayNameWithoutEmojis)
2023-01-09 10:06:21 +01:00
.foregroundColor(.mainTextColor)
.font(.footnote)
.fontWeight(.bold)
Text("@\(account.acct)")
.foregroundColor(.lightGrayColor)
.font(.footnote)
}
Spacer()
if self.applicationState.account?.id != self.account.id {
2023-01-22 21:44:07 +01:00
self.otherAccountActionButtons()
}
2023-01-09 10:06:21 +01:00
}
if let note = account.note, !note.isEmpty {
2023-01-23 18:01:27 +01:00
MarkdownFormattedText(note.asMarkdown, withFontSize: 14, andWidth: Int(UIScreen.main.bounds.width) - 16)
.environment(\.openURL, OpenURLAction { url in
2023-02-03 15:16:30 +01:00
routerPath.handle(url: url)
2023-01-23 18:01:27 +01:00
})
2023-01-09 10:06:21 +01:00
}
Text("Joined \(account.createdAt.toRelative(.isoDateTimeMilliSec))")
.foregroundColor(.lightGrayColor.opacity(0.5))
.font(.footnote)
}
.padding()
}
2023-01-22 15:56:35 +01:00
@ViewBuilder
2023-01-22 21:44:07 +01:00
private func otherAccountActionButtons() -> some View {
ActionButton {
await onRelationshipButtonTap()
} label: {
HStack {
Image(systemName: relationship?.following == true ? "person.badge.minus" : "person.badge.plus")
Text(relationship?.following == true ? "Unfollow" : (relationship?.followedBy == true ? "Follow back" : "Follow"))
}
}
.buttonStyle(.borderedProminent)
.tint(relationship?.following == true ? .dangerColor : .accentColor)
2023-01-22 15:56:35 +01:00
}
private func onRelationshipButtonTap() async {
do {
if self.relationship?.following == true {
2023-02-03 15:16:30 +01:00
if let relationship = try await self.client.accounts?.unfollow(account: self.account.id) {
self.relationship = relationship
}
} else {
2023-02-03 15:16:30 +01:00
if let relationship = try await self.client.accounts?.follow(account: self.account.id) {
self.relationship = relationship
}
}
} catch {
2023-01-15 12:41:55 +01:00
ErrorService.shared.handle(error, message: "Relationship action failed.", showToastr: true)
}
}
2023-01-09 10:06:21 +01:00
}