Blade is a pagination framework that simplifies the integration of pagination into the application.
Blade provides two libraries for working with pagination: Blade and BladeTCA. BladeTCA is an extension designed for working with Composable Architecture. Both support working with offset and cursor-based paginations.
First, you need to implement page loader for whether cursor or offset based pagination:
import Blade
/// Offset pagination loader
finalclassOffsetPageLoader<Element:Equatable&Decodable>:IOffsetPageLoader{func loadPage(request:OffsetPaginationRequest)asyncthrows->Page<Element>{
// Implementation here
}}
/// Cursor pagination loader
finalclassCursorPageLoader<Element:Equatable&Decodable&Identifiable>:ICursorPageLoader{func loadPage(request:CursorPaginationRequest<Element>)asyncthrows->Page<Element>{
// Implementation here
}}Second, create a Paginator instance:
import Blade
/// Offset-based pagination
letpaginator=Paginator(configuration:PaginationLimitOffset(firstPage:.zero, limit:20), offsetPageLoader:OffsetPageLoader())
/// Cursor-based pagination
letpaginator=Paginator(configuration:PaginationCursorSeek(id: #id_here), offsetPageLoader:CursorPageLoader())Third, the paginator is capable of requesting the first page, the next page, and resetting its state, like this:
/// Request an initial page
letpage=tryawait paginator.refresh()
/// Request next page
letnextPage=tryawait paginator.nextPage()
/// Reset state
await paginator.reset()If your app uses the Composable Architecture, Blade can be easily integrated.
import BladeTCA
// MARK: Reducer
@ReducerstructSomeFeature{
// MARK: Types
structState:Equatable{varpaginator:PaginatorState<ArticleView.ViewModel>}enumAction{case child(PaginatorAction<ArticleView.ViewModel,Never>)}
// MARK: Reducer
varbody:someReducerOf<Self>{Reduce{ state, action inswitch state {case.clild:return.none
}}.paginator(
state: \SomeFeature.State.paginator,
action:/SomeFeature.Action.child,
loadPage:{ request, state in
// Load page here
})}}
// MARK: View
import ComposableArchitecture
import SwiftUI
// MARK: - SomeView
structSomeView:View{
// MARK: Properties
privateletstore:StoreOf<SomeFeature>
// MARK: Initialization
init(store:StoreOf<SomeFeature>){self.store = store
}
// MARK: View
varbody:someView{PaginatorListView(
store: store.scope(state: \.paginator, action:{.child($0)})){ state in // Implement UI here
}}- iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 7.0+ / visionOS 1.0+
- Xcode 15.0
- Swift 5.7
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It is in early development, but blade does support its use on supported platforms.
Once you have your Swift package set up, adding blade as a dependency is as easy as adding it to the dependencies value of your Package.swift.
dependencies:[.package(url:"https://github.com/space-code/blade.git",.upToNextMajor(from:"1.0.0"))]- If you found a bug, open an issue.
- If you have a feature request, open an issue.
- If you want to contribute, submit a pull request.
Bootstrapping development environment
make bootstrap
Please feel free to help out with this project! If you see something that could be made better or want a new feature, open up an issue or send a Pull Request!
Nikita Vasilev, nv3212@gmail.com
blade is available under the MIT license. See the LICENSE file for more info.
