IceCubesApp/Packages/Account/Sources/Account/AccountDetailView.swift

91 lines
2.5 KiB
Swift
Raw Normal View History

2022-11-29 11:18:06 +00:00
import SwiftUI
import Models
import Network
2022-12-18 19:30:19 +00:00
import Status
import Shimmer
import DesignSystem
2022-11-29 11:18:06 +00:00
public struct AccountDetailView: View {
@EnvironmentObject private var client: Client
@StateObject private var viewModel: AccountDetailViewModel
2022-12-20 08:37:07 +00:00
@State private var scrollOffset: CGFloat = 0
2022-11-29 11:18:06 +00:00
2022-12-20 15:08:09 +00:00
private let isCurrentUser: Bool
2022-11-29 11:18:06 +00:00
public init(accountId: String) {
_viewModel = StateObject(wrappedValue: .init(accountId: accountId))
2022-12-20 15:08:09 +00:00
isCurrentUser = false
2022-11-29 11:18:06 +00:00
}
2022-12-20 15:08:09 +00:00
public init(account: Account, isCurrentUser: Bool = false) {
2022-12-20 16:11:12 +00:00
_viewModel = StateObject(wrappedValue: .init(account: account,
isCurrentUser: isCurrentUser))
2022-12-20 15:08:09 +00:00
self.isCurrentUser = isCurrentUser
2022-12-17 12:37:46 +00:00
}
2022-11-29 11:18:06 +00:00
public var body: some View {
2022-12-20 08:37:07 +00:00
ScrollViewOffsetReader { offset in
self.scrollOffset = offset
} content: {
2022-12-17 12:37:46 +00:00
LazyVStack {
2022-12-18 19:30:19 +00:00
headerView
2022-12-20 08:37:07 +00:00
Divider()
.offset(y: -20)
2022-12-19 06:17:01 +00:00
StatusesListView(fetcher: viewModel)
2022-11-29 11:18:06 +00:00
}
}
.task {
viewModel.client = client
await viewModel.fetchAccount()
2022-12-20 15:08:09 +00:00
if viewModel.statuses.isEmpty {
await viewModel.fetchStatuses()
}
}
.refreshable {
Task {
await viewModel.fetchAccount()
await viewModel.fetchStatuses()
}
2022-12-18 19:30:19 +00:00
}
2022-12-20 08:37:07 +00:00
.edgesIgnoringSafeArea(.top)
.navigationTitle(Text(scrollOffset < -20 ? viewModel.title : ""))
2022-12-18 19:30:19 +00:00
}
@ViewBuilder
private var headerView: some View {
switch viewModel.state {
case .loading:
2022-12-20 16:11:12 +00:00
AccountDetailHeaderView(isCurrentUser: isCurrentUser,
account: .placeholder(),
relationship: .constant(.placeholder()),
following: .constant(false))
2022-12-18 19:30:19 +00:00
.redacted(reason: .placeholder)
case let .data(account):
2022-12-20 16:11:12 +00:00
AccountDetailHeaderView(isCurrentUser: isCurrentUser,
account: account,
relationship: $viewModel.relationship,
following:
.init(get: {
viewModel.relationship?.following ?? false
}, set: { following in
Task {
if following {
await viewModel.follow()
} else {
await viewModel.unfollow()
}
}
}))
2022-12-18 19:30:19 +00:00
case let .error(error):
Text("Error: \(error.localizedDescription)")
}
}
2022-12-17 12:37:46 +00:00
}
struct AccountDetailView_Previews: PreviewProvider {
static var previews: some View {
AccountDetailView(account: .placeholder())
2022-11-29 11:18:06 +00:00
}
}
2022-12-17 12:37:46 +00:00