IceCubesApp/Packages/Account/Sources/Account/AccountDetailHeaderView.swift

153 lines
4.1 KiB
Swift
Raw Normal View History

2022-12-17 12:37:46 +00:00
import SwiftUI
import Models
2022-12-18 19:30:19 +00:00
import DesignSystem
2022-12-22 09:53:36 +00:00
import Env
2022-12-17 12:37:46 +00:00
struct AccountDetailHeaderView: View {
2022-12-22 09:56:24 +00:00
@EnvironmentObject private var quickLook: QuickLook
2022-12-20 08:37:07 +00:00
@EnvironmentObject private var routeurPath: RouterPath
2022-12-17 12:37:46 +00:00
@Environment(\.redactionReasons) private var reasons
2022-12-20 16:11:12 +00:00
let isCurrentUser: Bool
2022-12-20 08:37:07 +00:00
let account: Account
2022-12-20 16:11:12 +00:00
@Binding var relationship: Relationshionship?
@Binding var following: Bool
2022-12-21 11:47:07 +00:00
@Binding var scrollOffset: CGFloat
private var bannerHeight: CGFloat {
200 + (scrollOffset > 0 ? scrollOffset * 2 : 0)
}
2022-12-20 08:37:07 +00:00
2022-12-17 12:37:46 +00:00
var body: some View {
VStack(alignment: .leading) {
headerImageView
accountInfoView
}
}
private var headerImageView: some View {
2022-12-20 08:37:07 +00:00
GeometryReader { proxy in
2022-12-20 16:11:12 +00:00
ZStack(alignment: .bottomTrailing) {
AsyncImage(
url: account.header,
content: { image in
image.resizable()
.aspectRatio(contentMode: .fill)
2022-12-21 11:47:07 +00:00
.frame(height: bannerHeight)
2022-12-20 16:11:12 +00:00
.frame(width: proxy.frame(in: .local).width)
.clipped()
},
placeholder: {
Color.gray
2022-12-21 11:47:07 +00:00
.frame(height: bannerHeight)
2022-12-20 16:11:12 +00:00
}
)
if relationship?.followedBy == true {
Text("Follows You")
.font(.footnote)
.fontWeight(.semibold)
.padding(4)
.background(.ultraThinMaterial)
.cornerRadius(4)
.padding(8)
2022-12-20 08:37:07 +00:00
}
2022-12-20 16:11:12 +00:00
}
2022-12-20 08:37:07 +00:00
.background(Color.gray)
}
2022-12-21 11:47:07 +00:00
.frame(height: bannerHeight)
.offset(y: scrollOffset > 0 ? -scrollOffset : 0)
2022-12-20 08:37:07 +00:00
.contentShape(Rectangle())
.onTapGesture {
2022-12-22 09:56:24 +00:00
Task {
await quickLook.prepareFor(urls: [account.header], selectedURL: account.header)
}
2022-12-20 08:37:07 +00:00
}
2022-12-17 12:37:46 +00:00
}
private var accountAvatarView: some View {
HStack {
AsyncImage(
url: account.avatar,
content: { image in
image.resizable()
.aspectRatio(contentMode: .fit)
.cornerRadius(4)
.frame(maxWidth: 80, maxHeight: 80)
.overlay(
RoundedRectangle(cornerRadius: 4)
.stroke(.white, lineWidth: 1)
)
},
placeholder: {
ProgressView()
.frame(maxWidth: 80, maxHeight: 80)
}
)
2022-12-20 08:37:07 +00:00
.contentShape(Rectangle())
.onTapGesture {
2022-12-22 09:56:24 +00:00
Task {
await quickLook.prepareFor(urls: [account.avatar], selectedURL: account.avatar)
}
2022-12-20 08:37:07 +00:00
}
2022-12-17 12:37:46 +00:00
Spacer()
Group {
makeCustomInfoLabel(title: "Posts", count: account.statusesCount)
makeCustomInfoLabel(title: "Following", count: account.followingCount)
2022-12-17 12:37:46 +00:00
makeCustomInfoLabel(title: "Followers", count: account.followersCount)
}.offset(y: 20)
}
}
private var accountInfoView: some View {
Group {
accountAvatarView
2022-12-20 16:11:12 +00:00
HStack {
VStack(alignment: .leading, spacing: 0) {
2022-12-21 16:39:48 +00:00
account.displayNameWithEmojis
2022-12-20 16:11:12 +00:00
.font(.headline)
Text(account.acct)
.font(.callout)
.foregroundColor(.gray)
}
Spacer()
if relationship != nil && !isCurrentUser {
Button {
following.toggle()
} label: {
if relationship?.requested == true {
Text("Requested")
} else {
Text(following ? "Following" : "Follow")
}
}.buttonStyle(.bordered)
}
}
2022-12-17 12:37:46 +00:00
Text(account.note.asSafeAttributedString)
.font(.body)
.padding(.top, 8)
}
2022-12-18 19:30:19 +00:00
.padding(.horizontal, DS.Constants.layoutPadding)
2022-12-17 12:37:46 +00:00
.offset(y: -40)
}
private func makeCustomInfoLabel(title: String, count: Int) -> some View {
VStack {
2022-12-18 19:30:19 +00:00
Text("\(count)")
.font(.headline)
2022-12-17 12:37:46 +00:00
Text(title)
.font(.footnote)
.foregroundColor(.gray)
}
}
}
struct AccountDetailHeaderView_Previews: PreviewProvider {
static var previews: some View {
2022-12-20 16:11:12 +00:00
AccountDetailHeaderView(isCurrentUser: false,
account: .placeholder(),
relationship: .constant(.placeholder()),
2022-12-21 11:47:07 +00:00
following: .constant(true),
scrollOffset: .constant(0))
2022-12-17 12:37:46 +00:00
}
}