home / skills / rshankras / claude-code-apple-skills / pagination

pagination skill

/skills/generators/pagination

This skill generates Swift pagination infrastructure with offset and cursor patterns, infinite scroll, and optional search to streamline paginated data loading.

npx playbooks add skill rshankras/claude-code-apple-skills --skill pagination

Review the files below or copy the command above to add this skill to your agents.

Files (3)
SKILL.md
6.3 KB
---
name: pagination
description: Generates pagination infrastructure with offset or cursor-based patterns, infinite scroll, and search support. Use when user wants to add paginated lists, infinite scrolling, or load-more functionality.
allowed-tools: [Read, Write, Edit, Glob, Grep, Bash, AskUserQuestion]
---

# Pagination Generator

Generate production pagination infrastructure supporting offset-based and cursor-based APIs, with infinite scroll SwiftUI views, state machine management, and optional search integration.

## When This Skill Activates

Use this skill when the user:
- Asks to "add pagination" or "paginate a list"
- Wants "infinite scroll" or "load more" functionality
- Mentions "cursor-based pagination" or "offset pagination"
- Asks about "paginated API" or "loading pages of data"
- Wants "search with pagination"

## Pre-Generation Checks

### 1. Project Context Detection
- [ ] Check Swift version (requires Swift 5.9+)
- [ ] Check deployment target (iOS 17+ / macOS 14+ for @Observable)
- [ ] Search for existing pagination implementations
- [ ] Identify source file locations

### 2. Networking Layer Detection
Search for existing networking code:
```
Glob: **/*API*.swift, **/*Client*.swift, **/*Endpoint*.swift
Grep: "APIClient" or "APIEndpoint"
```

If `networking-layer` generator was used, detect the `APIEndpoint` protocol and generate data sources that conform to it.

### 3. Conflict Detection
Search for existing pagination:
```
Glob: **/*Pagina*.swift, **/*LoadMore*.swift
Grep: "PaginationState" or "loadNextPage" or "hasMorePages"
```

If found, ask user whether to replace or extend.

## Configuration Questions

Ask user via AskUserQuestion:

1. **Pagination style?**
   - Offset-based (page number + page size) — most common for REST APIs
   - Cursor-based (opaque cursor token) — better for real-time data, social feeds

2. **Loading trigger?**
   - Infinite scroll (auto-load when near bottom) — recommended
   - Manual "Load More" button
   - Both (infinite scroll with manual fallback on error)

3. **Additional features?** (multi-select)
   - Search with pagination (debounced, resets on query change)
   - Pull-to-refresh
   - Empty/error/loading state views

4. **Data source pattern?**
   - Generic (works with any Codable model)
   - Protocol-based (define per-endpoint data sources)

## Generation Process

### Step 1: Read Templates
Read `pagination-patterns.md` for architecture guidance.
Read `templates.md` for production Swift code.

### Step 2: Create Core Files
Generate these files:
1. `PaginatedResponse.swift` — Generic response models for offset and cursor
2. `PaginationState.swift` — State machine (idle, loading, loaded, error, exhausted)
3. `PaginatedDataSource.swift` — Protocol endpoints conform to
4. `PaginationManager.swift` — @Observable manager with state transitions

### Step 3: Create Optional Files
Based on configuration:
- `SearchablePaginationManager.swift` — If search selected
- `Views/PaginatedList.swift` — Infinite scroll SwiftUI wrapper
- `Views/LoadMoreButton.swift` — Manual load-more button
- `Views/PaginationStateView.swift` — Empty/loading/error state views

### Step 4: Determine File Location
Check project structure:
- If `Sources/` exists → `Sources/Pagination/`
- If `App/` exists → `App/Pagination/`
- Otherwise → `Pagination/`

## Output Format

After generation, provide:

### Files Created
```
Pagination/
├── PaginatedResponse.swift           # Generic response models
├── PaginationState.swift             # State machine enum
├── PaginatedDataSource.swift         # Data source protocol
├── PaginationManager.swift           # @Observable manager
├── SearchablePaginationManager.swift # Optional: search + pagination
└── Views/
    ├── PaginatedList.swift           # Infinite scroll wrapper
    ├── LoadMoreButton.swift          # Manual load-more
    └── PaginationStateView.swift     # Empty/loading/error states
```

### Integration Steps

**Define a data source:**
```swift
struct UsersDataSource: PaginatedDataSource {
    typealias Item = User

    let apiClient: APIClient

    func fetch(page: PageRequest) async throws -> PaginatedResponse<User> {
        try await apiClient.request(UsersEndpoint(page: page.page, size: page.size))
    }
}
```

**Use PaginationManager in a view model:**
```swift
@Observable
final class UsersViewModel {
    let pagination: PaginationManager<UsersDataSource>

    init(apiClient: APIClient) {
        pagination = PaginationManager(
            dataSource: UsersDataSource(apiClient: apiClient)
        )
    }
}
```

**With SwiftUI (infinite scroll):**
```swift
struct UsersListView: View {
    @State private var viewModel = UsersViewModel()

    var body: some View {
        PaginatedList(manager: viewModel.pagination) { user in
            UserRow(user: user)
        }
        .task {
            await viewModel.pagination.loadFirstPage()
        }
    }
}
```

**With search:**
```swift
struct SearchableUsersView: View {
    @State private var searchManager = SearchablePaginationManager(
        dataSource: UsersDataSource()
    )

    var body: some View {
        PaginatedList(manager: searchManager.pagination) { user in
            UserRow(user: user)
        }
        .searchable(text: $searchManager.query)
    }
}
```

### Testing

```swift
@Test
func loadFirstPagePopulatesItems() async throws {
    let mockSource = MockDataSource(items: User.mockList(count: 20))
    let manager = PaginationManager(dataSource: mockSource, pageSize: 10)

    await manager.loadFirstPage()

    #expect(manager.items.count == 10)
    #expect(manager.state == .loaded)
    #expect(manager.hasMore == true)
}

@Test
func loadAllPagesReachesExhausted() async throws {
    let mockSource = MockDataSource(items: User.mockList(count: 15))
    let manager = PaginationManager(dataSource: mockSource, pageSize: 10)

    await manager.loadFirstPage()
    await manager.loadNextPage()

    #expect(manager.items.count == 15)
    #expect(manager.state == .exhausted)
}
```

## References

- **pagination-patterns.md** — Offset vs cursor comparison, state machine, threshold prefetching
- **templates.md** — All production Swift templates
- Related: `generators/networking-layer` — Base networking layer for data sources
- Related: `generators/http-cache` — Cache paginated responses

Overview

This skill generates production-ready pagination infrastructure for Swift projects, supporting offset-based and cursor-based APIs, infinite scroll, and load-more patterns. It creates a small set of reusable Swift files (state machine, data source protocol, manager, and SwiftUI views) and optional search integration so you can plug pagination into existing networking code quickly. Use it to add robust paging UX, debounced search resets, and clear loading/error states.

How this skill works

The generator inspects the project for Swift version, deployment target, and existing networking or pagination code, then asks a few configuration questions (pagination style, loading trigger, extra features). It outputs core files like PaginatedResponse, PaginationState, PaginatedDataSource, and PaginationManager plus optional searchable manager and SwiftUI wrappers. The produced manager is @Observable, implements state transitions, and exposes methods to load first/next pages, refresh, and report hasMore/load state for views.

When to use it

  • You need infinite scroll or a manual "Load More" button for large lists.
  • You want to add cursor-based pagination for feeds or offset-based for REST endpoints.
  • You require search that resets pagination and uses debounce.
  • You want a reusable PaginationManager and SwiftUI wrappers for consistent UX.
  • You're integrating pagination into an existing networking layer or APIClient.

Best practices

  • Prefer cursor-based pagination for feeds with frequent changes; use offset for simple REST endpoints.
  • Expose small page sizes for snappy UI and prefetch next page when near bottom (threshold prefetching).
  • Reset pagination on search query changes and debounce input to avoid excessive requests.
  • Detect and avoid double loads by guarding state transitions in PaginationManager.
  • Provide clear empty, loading, and error state views and a manual fallback for infinite scroll failures.

Example use cases

  • A social feed using cursor-based pagination and infinite scroll with threshold prefetching.
  • A REST-backed product list using offset pagination with a Load More button and pull-to-refresh.
  • A searchable user directory that resets pages when the search query changes and debounces requests.
  • A generic PaginationManager wired to an existing APIClient via a PaginatedDataSource conformance.
  • Unit tests that verify loadFirstPage/loadNextPage transitions and exhaustion behavior with a mock data source.

FAQ

Which pagination style should I pick?

Use cursor-based for feeds with rapidly changing data or large result sets; use offset-based for simple REST endpoints and server support for page/size parameters.

Does the generated manager handle duplicate loads and errors?

Yes. The state machine prevents concurrent loads, surfaces loading/error/exhausted states, and supports a manual retry or fallback load-more button.

Can I integrate this with an existing APIClient?

Yes. Implement PaginatedDataSource to wrap your APIClient or conform to detected APIEndpoint types; the manager calls your data source for pages.