Skip to content

A Swift package for handling pagination with support for both numbered and keyset pagination.

License

Notifications You must be signed in to change notification settings

Blejator90/SwiftPagination

Repository files navigation

SwiftPagination

Swift Package CI

iOS macOS tvOS watchOS visionOS

SwiftPagination is a simple and flexible Swift library for handling pagination with support for both numbered and keyset pagination. This library makes it easy to implement pagination in your iOS and macOS applications.

Features

  • Supports both numbered and keyset pagination.
  • Simple and intuitive API.
  • Asynchronous loading of pages.
  • Handles state and error management.

Documentation

You can find the full API documentation here: SwiftPagination Documentation

Installation

Swift Package Manager

Add SwiftPagination to your Package.swift:

dependencies: [
    .package(url: "https://github.com/blejator90/SwiftPagination.git", from: "0.1.0")
]

Then, add it as a dependency in your target:

.target(
    name: "YourTarget",
    dependencies: [
        "SwiftPagination"
    ]
)

Usage

Numbered Pagination

Here's an example of how to use SwiftPagination with numbered pagination:

import SwiftPagination

struct MyItem: Decodable, Identifiable {
    let id: Int
    let name: String
}

let pagination = Pagination<MyItem>(
    pageSize: 10,
    initialPage: 1,
    fetchNumberedPage: { currentPage, pageSize in
        // Replace with your API call
        let items = (1...100).map { MyItem(id: $0, name: "Item \($0)") }
        let start = (currentPage - 1) * pageSize
        let end = min(start + pageSize, items.count)
        return Array(items[start..<end])
    }
)

// Load the first page
Task {
    do {
        let items = try await pagination.load()
        print("Loaded items: \(items)")
    } catch {
        print("Failed to load items: \(error)")
    }
}

Keyset Pagination

Here's an example of how to use SwiftPagination with keyset pagination:

import SwiftPagination

struct MyItem: Decodable, Identifiable, PaginationKey {
    let id: Int
    let name: String
    let key: String
}

let pagination = Pagination<MyItem>(
    pageSize: 10,
    initialKey: nil,
    fetchKeysetPage: { lastKey, pageSize in
        // Replace with your API call
        let items = (1...100).map { MyItem(id: $0, name: "Item \($0)", key: "\($0)") }
        guard let lastKey = lastKey else {
            return Array(items.prefix(pageSize))
        }
        let startIndex = items.firstIndex { $0.key == lastKey } ?? 0
        let endIndex = min(startIndex + pageSize, items.count)
        return Array(items[startIndex..<endIndex])
    }
)

// Load the first page
Task {
    do {
        let items = try await pagination.load()
        print("Loaded items: \(items)")
    } catch {
        print("Failed to load items: \(error)")
    }
}

Loading More Pages

To load more pages, simply call the loadMore method:

Task {
    do {
        let moreItems = try await pagination.loadMore()
        print("Loaded more items: \(moreItems)")
    } catch {
        print("Failed to load more items: \(error)")
    }
}

Contributions and Ideas

Contributions and ideas are welcome! If you have suggestions for improvements or new features, please open an issue or submit a pull request. I appreciate your feedback and help in making SwiftPagination better.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A Swift package for handling pagination with support for both numbered and keyset pagination.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages