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

81 lines
2.2 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
import Shimmer
2023-01-17 10:36:01 +00:00
import SwiftUI
import Network
2022-12-19 06:17:01 +00:00
public struct StatusesListView<Fetcher>: View where Fetcher: StatusesFetcher {
@EnvironmentObject private var theme: Theme
2023-02-01 11:49:59 +00:00
2022-12-19 06:17:01 +00:00
@ObservedObject private var fetcher: Fetcher
private let isRemote: Bool
private let routerPath: RouterPath
private let client: Client
2023-01-17 10:36:01 +00:00
public init(fetcher: Fetcher, client: Client, routerPath: RouterPath, 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-01-17 10:36:01 +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",
buttonTitle: "action.retry") {
Task {
await fetcher.fetchStatuses()
2023-01-07 17:01:06 +00:00
}
}
.listRowBackground(theme.primaryBackgroundColor)
2023-01-31 07:16:36 +00:00
.listRowSeparator(.hidden)
2023-01-17 10:36:01 +00:00
case let .display(statuses, nextPageState):
ForEach(statuses, id: \.viewId) { status in
let viewModel = StatusRowViewModel(status: status, client: client, routerPath: routerPath, isRemote: isRemote)
if viewModel.filter?.filter.filterAction != .hide {
StatusRowView(viewModel: viewModel)
.id(status.id)
.onAppear {
2023-01-31 07:04:35 +00:00
fetcher.statusDidAppear(status: status)
}
.onDisappear {
fetcher.statusDidDisappear(status: status)
}
2022-12-19 06:17:01 +00:00
}
}
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-01-17 10:36:01 +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)
.listRowBackground(theme.primaryBackgroundColor)
2022-12-19 06:17:01 +00:00
}
}