IceCubesApp/Packages/Status/Sources/Status/List/StatusesListView.swift

87 lines
2.3 KiB
Swift
Raw Normal View History

2023-01-17 10:36:01 +00:00
import DesignSystem
2023-02-04 16:17:38 +00:00
import Env
2022-12-19 06:17:01 +00:00
import Models
2023-02-18 06:26:48 +00:00
import Network
2022-12-19 06:17:01 +00:00
import Shimmer
2023-01-17 10:36:01 +00:00
import SwiftUI
2022-12-19 06:17:01 +00:00
public struct StatusesListView<Fetcher>: View where Fetcher: StatusesFetcher {
@EnvironmentObject private var theme: Theme
2023-03-13 12:38:28 +00:00
2022-12-19 06:17:01 +00:00
@ObservedObject private var fetcher: Fetcher
// Whether this status is on a remote local timeline (many actions are unavailable if so)
private let isRemote: Bool
private let routerPath: RouterPath
private let client: Client
2023-03-13 12:38:28 +00:00
public init(fetcher: Fetcher,
client: Client,
routerPath: RouterPath,
2023-03-13 12:38:28 +00:00
isRemote: Bool = false)
{
2022-12-19 06:17:01 +00:00
self.fetcher = fetcher
self.isRemote = isRemote
self.client = client
self.routerPath = routerPath
2022-12-19 06:17:01 +00:00
}
2023-03-13 12:38:28 +00:00
2022-12-19 06:17:01 +00:00
public var body: some View {
switch fetcher.statusesState {
case .loading:
ForEach(Status.placeholders()) { status in
StatusRowView(viewModel: { .init(status: status, client: client, routerPath: routerPath) })
.redacted(reason: .placeholder)
}
case .error:
ErrorView(title: "status.error.title",
message: "status.error.loading.message",
2023-03-13 12:38:28 +00:00
buttonTitle: "action.retry")
{
Task {
2023-02-25 09:10:27 +00:00
await fetcher.fetchNewestStatuses()
2023-01-07 17:01:06 +00:00
}
}
2023-03-13 12:38:28 +00:00
.listRowSeparator(.hidden)
case let .display(statuses, nextPageState):
ForEach(statuses, id: \.viewId) { status in
StatusRowView(viewModel: { StatusRowViewModel(status: status,
client: client,
routerPath: routerPath,
isRemote: isRemote)
2023-03-13 12:38:28 +00:00
})
.id(status.id)
.onAppear {
fetcher.statusDidAppear(status: status)
}
.onDisappear {
fetcher.statusDidDisappear(status: status)
}
}
switch nextPageState {
case .hasNextPage:
loadingRow
.onAppear {
Task {
await fetcher.fetchNextPage()
2022-12-19 06:17:01 +00:00
}
}
case .loadingNextPage:
loadingRow
case .none:
EmptyView()
2022-12-19 06:17:01 +00:00
}
}
}
2023-03-13 12:38:28 +00:00
2022-12-19 06:17:01 +00:00
private var loadingRow: some View {
HStack {
Spacer()
ProgressView()
Spacer()
}
2023-01-03 17:22:08 +00:00
.padding(.horizontal, .layoutPadding)
2022-12-19 06:17:01 +00:00
}
}