IceCubesApp/Packages/Account/Sources/Account/AccountsList/AccountsListView.swift

123 lines
3.5 KiB
Swift
Raw Normal View History

2023-01-17 10:36:01 +00:00
import DesignSystem
2022-12-23 17:47:19 +00:00
import Env
2023-01-17 10:36:01 +00:00
import Models
import Network
2022-12-23 17:47:19 +00:00
import Shimmer
2023-01-17 10:36:01 +00:00
import SwiftUI
2022-12-23 17:47:19 +00:00
2023-09-18 19:03:52 +00:00
@MainActor
2022-12-23 17:47:19 +00:00
public struct AccountsListView: View {
2023-09-18 19:03:52 +00:00
@Environment(Theme.self) private var theme
@Environment(Client.self) private var client
@Environment(CurrentAccount.self) private var currentAccount
@State private var viewModel: AccountsListViewModel
2022-12-23 17:47:19 +00:00
@State private var didAppear: Bool = false
2023-01-17 10:36:01 +00:00
2022-12-24 12:41:25 +00:00
public init(mode: AccountsListMode) {
_viewModel = .init(initialValue: .init(mode: mode))
2022-12-23 17:47:19 +00:00
}
2023-01-17 10:36:01 +00:00
2022-12-23 17:47:19 +00:00
public var body: some View {
List {
switch viewModel.state {
case .loading:
2023-01-17 10:36:01 +00:00
ForEach(Account.placeholders()) { _ in
2022-12-23 17:47:19 +00:00
AccountsListRow(viewModel: .init(account: .placeholder(), relationShip: .placeholder()))
.redacted(reason: .placeholder)
2023-09-18 16:55:11 +00:00
.allowsHitTesting(false)
2022-12-23 17:47:19 +00:00
.shimmering()
2023-12-19 08:48:12 +00:00
#if !os(visionOS)
2022-12-29 09:39:34 +00:00
.listRowBackground(theme.primaryBackgroundColor)
2023-12-19 08:48:12 +00:00
#endif
2022-12-23 17:47:19 +00:00
}
case let .display(accounts, relationships, nextPageState):
if case .followers = viewModel.mode,
!currentAccount.followRequests.isEmpty
{
Section(
header: Text("account.follow-requests.pending-requests"),
footer: Text("account.follow-requests.instructions")
.font(.scaledFootnote)
.foregroundColor(.secondary)
.offset(y: -8)
) {
ForEach(currentAccount.followRequests) { account in
AccountsListRow(
viewModel: .init(account: account),
isFollowRequest: true,
requestUpdated: {
Task {
await viewModel.fetch()
}
}
)
2023-12-19 08:48:12 +00:00
#if !os(visionOS)
2023-01-17 10:36:01 +00:00
.listRowBackground(theme.primaryBackgroundColor)
2023-12-19 08:48:12 +00:00
#endif
}
}
}
Section {
ForEach(accounts) { account in
if let relationship = relationships.first(where: { $0.id == account.id }) {
AccountsListRow(viewModel: .init(account: account,
relationShip: relationship))
2023-12-19 08:48:12 +00:00
#if !os(visionOS)
.listRowBackground(theme.primaryBackgroundColor)
2023-12-19 08:48:12 +00:00
#endif
}
2022-12-23 17:47:19 +00:00
}
}
2023-01-17 10:36:01 +00:00
2022-12-23 17:47:19 +00:00
switch nextPageState {
case .hasNextPage:
loadingRow
2023-12-19 08:48:12 +00:00
#if !os(visionOS)
2022-12-29 09:39:34 +00:00
.listRowBackground(theme.primaryBackgroundColor)
2023-12-19 08:48:12 +00:00
#endif
2022-12-23 17:47:19 +00:00
.onAppear {
Task {
await viewModel.fetchNextPage()
}
}
2023-01-17 10:36:01 +00:00
2022-12-23 17:47:19 +00:00
case .loadingNextPage:
loadingRow
2023-12-19 08:48:12 +00:00
#if !os(visionOS)
2022-12-29 09:39:34 +00:00
.listRowBackground(theme.primaryBackgroundColor)
2023-12-19 08:48:12 +00:00
#endif
2022-12-23 17:47:19 +00:00
case .none:
EmptyView()
}
2023-01-17 10:36:01 +00:00
2022-12-23 17:47:19 +00:00
case let .error(error):
Text(error.localizedDescription)
2023-12-19 08:48:12 +00:00
#if !os(visionOS)
2022-12-29 09:39:34 +00:00
.listRowBackground(theme.primaryBackgroundColor)
2023-12-19 08:48:12 +00:00
#endif
2022-12-23 17:47:19 +00:00
}
}
2023-12-19 08:48:12 +00:00
#if !os(visionOS)
2022-12-29 09:39:34 +00:00
.scrollContentBackground(.hidden)
.background(theme.primaryBackgroundColor)
2023-12-19 08:48:12 +00:00
#endif
2022-12-23 17:47:19 +00:00
.listStyle(.plain)
2022-12-24 12:41:25 +00:00
.navigationTitle(viewModel.mode.title)
2022-12-23 17:47:19 +00:00
.navigationBarTitleDisplayMode(.inline)
.task {
viewModel.client = client
2023-01-17 10:36:01 +00:00
guard !didAppear else { return }
2022-12-23 17:47:19 +00:00
didAppear = true
await viewModel.fetch()
}
}
2023-01-17 10:36:01 +00:00
2022-12-23 17:47:19 +00:00
private var loadingRow: some View {
HStack {
Spacer()
ProgressView()
Spacer()
}
}
}