How do I implement infinite scrolling in Combine with Swift?

Implementing infinite scrolling in a Swift application using Combine can enhance user experience by loading content dynamically as the user scrolls. Below is an example of how to achieve this.

Infinite Scrolling, Combine, Swift, SwiftUI, Pagination
This guide provides an overview of using Combine in Swift to implement infinite scrolling in your application, enabling smoother content loading.
import SwiftUI
import Combine

struct ContentView: View {
    @State private var items: [String] = []
    @State private var isLoading: Bool = false
    @State private var page: Int = 0

    var body: some View {
        List {
            ForEach(items, id: \.self) { item in
                Text(item)
            }
            if isLoading {
                ProgressView()
                    .onAppear(perform: loadMore)
            }
        }
        .onAppear(perform: loadMore)
    }

    func loadMore() {
        guard !isLoading else { return }
        isLoading = true

        let newPage = page + 1
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
            let newItems = (1...20).map { "Item \($0 + (newPage - 1) * 20)" }
            items.append(contentsOf: newItems)
            page = newPage
            isLoading = false
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Infinite Scrolling Combine Swift SwiftUI Pagination