IceCubesApp/Packages/DesignSystem/Sources/DesignSystem/Views/NextPageView.swift

53 lines
1.2 KiB
Swift
Raw Normal View History

import SwiftUI
public struct NextPageView: View {
@State private var isLoadingNextPage: Bool = false
@State private var showRetry: Bool = false
2024-02-14 11:48:14 +00:00
let loadNextPage: () async throws -> Void
public init(loadNextPage: @escaping (() async throws -> Void)) {
self.loadNextPage = loadNextPage
}
2024-02-14 11:48:14 +00:00
public var body: some View {
HStack {
if showRetry {
Button {
Task {
showRetry = false
await executeTask()
}
} label: {
Label("action.retry", systemImage: "arrow.clockwise")
}
.buttonStyle(.bordered)
} else {
Label("placeholder.loading.short", systemImage: "arrow.down")
.font(.footnote)
.foregroundStyle(.secondary)
.symbolEffect(.pulse, value: isLoadingNextPage)
}
}
2024-02-11 17:45:38 +00:00
.frame(maxWidth: .infinity, alignment: .center)
.task {
await executeTask()
}
.listRowSeparator(.hidden, edges: .all)
}
2024-02-14 11:48:14 +00:00
private func executeTask() async {
showRetry = false
defer {
isLoadingNextPage = false
}
guard !isLoadingNextPage else { return }
isLoadingNextPage = true
do {
try await loadNextPage()
} catch {
showRetry = true
}
}
}