{"id":17648767,"url":"https://github.com/chris-swift-dev/ListPagination","last_synced_at":"2025-04-19T14:31:49.950Z","repository":{"id":63907253,"uuid":"202496194","full_name":"chris-swift-dev/ListPagination","owner":"chris-swift-dev","description":"Swift package providing extensions of RandomAccessCollection to support List pagination in SwiftUI","archived":false,"fork":false,"pushed_at":"2022-04-11T19:12:04.000Z","size":1331,"stargazers_count":33,"open_issues_count":1,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T20:07:15.478Z","etag":null,"topics":["catalyst","ios","mac-os","macos","pagination","pagination-functionality","pagination-support","pagination-swiftui","swift","swift-package","swiftui","swiftui-components","swiftui-example","swiftui-lists"],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chris-swift-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-15T07:38:31.000Z","updated_at":"2025-02-10T00:33:37.000Z","dependencies_parsed_at":"2022-11-29T13:21:14.352Z","dependency_job_id":null,"html_url":"https://github.com/chris-swift-dev/ListPagination","commit_stats":null,"previous_names":["chris-swift-dev/listpagination","crelies/listpagination"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chris-swift-dev%2FListPagination","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chris-swift-dev%2FListPagination/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chris-swift-dev%2FListPagination/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chris-swift-dev%2FListPagination/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chris-swift-dev","download_url":"https://codeload.github.com/chris-swift-dev/ListPagination/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249714804,"owners_count":21314917,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["catalyst","ios","mac-os","macos","pagination","pagination-functionality","pagination-support","pagination-swiftui","swift","swift-package","swiftui","swiftui-components","swiftui-example","swiftui-lists"],"created_at":"2024-10-23T11:20:26.590Z","updated_at":"2025-04-19T14:31:48.878Z","avatar_url":"https://github.com/chris-swift-dev.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ListPagination\n\n[![Swift 5.3](https://img.shields.io/badge/swift5.3-compatible-green.svg?longCache=true\u0026style=flat-square)](https://developer.apple.com/swift)\n[![Platform](https://img.shields.io/badge/platform-iOS%20%7C%20macOS%20%7C%20tvOS-lightgrey.svg?longCache=true\u0026style=flat-square)](https://www.apple.com)\n[![License](https://img.shields.io/badge/license-MIT-lightgrey.svg?longCache=true\u0026style=flat-square)](https://en.wikipedia.org/wiki/MIT_License)\n\nThis Swift package provides extensions of **RandomAccessCollection** which help you add pagination support to your **SwiftUI** `List view`. It is already integrated in my [AdvancedList view (Wrapper around SwiftUI's List view)](https://github.com/crelies/AdvancedList).\n\n## Installation\n\nAdd this Swift package in Xcode using its Github repository url. (File \u003e Swift Packages \u003e Add Package Dependency...)\n\n## How to use\n\nYou can add pagination with two different approaches to your `List`: **Last item approach** and **Threshold item approach**.\n\nThat's way this package adds two functions to **RandomAccessCollection**:\n\n### isLastItem\n\nUse this function to check if the item in the current `List item iteration` is the last item of your collection.\n\n### isThresholdItem\n\nWith this function you can find out if the item of the current `List item iteration` is the item at your defined threshold.\nPass an offset (distance to the last item) to the function so the threshold item can be determined.\n\n## Example\n\nBoth example code snippets below require a simple extension of **String**:\n\n```swift\n/*\n    If you want to display an array of strings\n    in the List view you have to specify a key path,\n    so each string can be uniquely identified.\n    With this extension you don't have to do that anymore.\n */\nextension String: Identifiable {\n    public var id: String {\n        return self\n    }\n}\n```\n\n### Last item approach\n\n```swift\nstruct ListPaginationExampleView: View {\n    @State private var items: [String] = Array(0...24).map { \"Item \\($0)\" }\n    @State private var isLoading: Bool = false\n    @State private var page: Int = 0\n    private let pageSize: Int = 25\n    \n    var body: some View {\n        NavigationView {\n            List(items) { item in\n                VStack(alignment: .leading) {\n                    Text(item)\n                    \n                    if self.isLoading \u0026\u0026 self.items.isLastItem(item) {\n                        Divider()\n                        Text(\"Loading ...\")\n                            .padding(.vertical)\n                    }\n                }.onAppear {\n                    self.listItemAppears(item)\n                }\n            }\n            .navigationBarTitle(\"List of items\")\n            .navigationBarItems(trailing: Text(\"Page index: \\(page)\"))\n        }\n    }\n}\n\nextension ListPaginationExampleView {\n    private func listItemAppears\u003cItem: Identifiable\u003e(_ item: Item) {\n        if items.isLastItem(item) {\n            isLoading = true\n            \n            /*\n                Simulated async behaviour:\n                Creates items for the next page and\n                appends them to the list after a short delay\n             */\n            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {\n                self.page += 1\n                let moreItems = self.getMoreItems(forPage: self.page, pageSize: self.pageSize)\n                self.items.append(contentsOf: moreItems)\n                \n                self.isLoading = false\n            }\n        }\n    }\n}\n```\n\n### Threshold item approach\n\n```swift\nstruct ListPaginationThresholdExampleView: View {\n    @State private var items: [String] = Array(0...24).map { \"Item \\($0)\" }\n    @State private var isLoading: Bool = false\n    @State private var page: Int = 0\n    private let pageSize: Int = 25\n    private let offset: Int = 10\n    \n    var body: some View {\n        NavigationView {\n            List(items) { item in\n                VStack(alignment: .leading) {\n                    Text(item)\n                    \n                    if self.isLoading \u0026\u0026 self.items.isLastItem(item) {\n                        Divider()\n                        Text(\"Loading ...\")\n                            .padding(.vertical)\n                    }\n                }.onAppear {\n                    self.listItemAppears(item)\n                }\n            }\n            .navigationBarTitle(\"List of items\")\n            .navigationBarItems(trailing: Text(\"Page index: \\(page)\"))\n        }\n    }\n}\n\nextension ListPaginationThresholdExampleView {\n    private func listItemAppears\u003cItem: Identifiable\u003e(_ item: Item) {\n        if items.isThresholdItem(offset: offset,\n                                 item: item) {\n            isLoading = true\n            \n            /*\n                Simulated async behaviour:\n                Creates items for the next page and\n                appends them to the list after a short delay\n             */\n            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {\n                self.page += 1\n                let moreItems = self.getMoreItems(forPage: self.page, pageSize: self.pageSize)\n                self.items.append(contentsOf: moreItems)\n                \n                self.isLoading = false\n            }\n        }\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchris-swift-dev%2FListPagination","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchris-swift-dev%2FListPagination","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchris-swift-dev%2FListPagination/lists"}