IceCubesApp/Packages/DesignSystem/Sources/DesignSystem/Views/NextPageView.swift
Thomas Ricouard 1f858414d8 format .
2024-02-14 12:48:14 +01:00

53 lines
1.2 KiB
Swift

import SwiftUI
public struct NextPageView: View {
@State private var isLoadingNextPage: Bool = false
@State private var showRetry: Bool = false
let loadNextPage: () async throws -> Void
public init(loadNextPage: @escaping (() async throws -> Void)) {
self.loadNextPage = loadNextPage
}
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)
}
}
.frame(maxWidth: .infinity, alignment: .center)
.task {
await executeTask()
}
.listRowSeparator(.hidden, edges: .all)
}
private func executeTask() async {
showRetry = false
defer {
isLoadingNextPage = false
}
guard !isLoadingNextPage else { return }
isLoadingNextPage = true
do {
try await loadNextPage()
} catch {
showRetry = true
}
}
}