Impressia/Vernissage/Views/UserProfileView.swift

144 lines
6.0 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 {
@EnvironmentObject var applicationState: ApplicationState
@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] = []
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) {
AsyncImage(url: account.avatar) { image in
image
.resizable()
.clipShape(Circle())
.aspectRatio(contentMode: .fit)
} placeholder: {
Image(systemName: "person.circle")
.resizable()
.foregroundColor(Color.mainTextColor)
}
.frame(width: 96.0, height: 96.0)
Spacer()
VStack(alignment: .center) {
Text("\(account.statusesCount)")
.font(.title3)
Text("Posts")
.font(.subheadline)
.opacity(0.6)
}
Spacer()
VStack(alignment: .center) {
Text("\(account.followersCount)")
.font(.title3)
Text("Followers")
.font(.subheadline)
.opacity(0.6)
}
Spacer()
VStack(alignment: .center) {
Text("\(account.followingCount)")
.font(.title3)
Text("Following")
.font(.subheadline)
.opacity(0.6)
}
2023-01-04 20:56:26 +01:00
}
2023-01-05 11:55:20 +01:00
HStack (alignment: .center) {
Text(account.displayName ?? account.username)
.foregroundColor(Color.mainTextColor)
.font(.footnote)
.fontWeight(.bold)
Text("@\(account.username)")
.foregroundColor(Color.lightGrayColor)
.font(.footnote)
Spacer()
Button {
// TODO: Folllow/Unfollow.
} 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 ? Color.dangerColor : .accentColor)
2023-01-04 20:56:26 +01:00
}
2023-01-05 11:55:20 +01:00
if let note = account.note {
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))")
.foregroundColor(Color.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-05 11:55:20 +01:00
LazyVGrid(columns: gridColumns) {
ForEach(self.statuses, id: \.id) { item in
NavigationLink(destination: DetailsView(statusId: item.id)
.environmentObject(applicationState)) {
ImageRowAsync(attachments: item.mediaAttachments)
}
}
2023-01-04 20:56:26 +01:00
}
} else {
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
}
}
.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)
(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: "")
}
}