-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
cursor
& offset
paginations
- Loading branch information
1 parent
cb42125
commit ca475a3
Showing
22 changed files
with
388 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...sses/COre/Reducers/Internal/Builders/PositionBuilders/CursorPositionBuilderStrategy.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// Blade | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A cursor-based paginator position builder. | ||
struct CursorPositionBuilderStrategy<State: Equatable & Identifiable>: IPositionBuilderStrategy { | ||
/// Creates a next position. | ||
/// | ||
/// - Parameter state: The current state of the paginator. | ||
/// | ||
/// - Returns: The next position offset based on the strategy. | ||
func next(state: PaginatorState<State, State.ID>) -> State.ID { | ||
state.items.last?.id ?? state.position | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...sses/COre/Reducers/Internal/Builders/PositionBuilders/OffsetPositionBuilderStrategy.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// Blade | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import Blade | ||
|
||
/// A offset-based paginator position builder. | ||
struct OffsetPositionBuilderStrategy<State: Equatable & Identifiable>: IPositionBuilderStrategy { | ||
/// Creates a next position. | ||
/// | ||
/// - Parameter state: The current state of the paginator. | ||
/// | ||
/// - Returns: The next position offset based on the strategy. | ||
func next(state: PaginatorState<State, Int>) -> Int { | ||
state.items.count | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...COre/Reducers/Internal/Builders/PositionBuilders/Protocols/IPositionBuilderStrategy.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// Blade | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// This protocol defines the interface for a strategy used in building positions based on a given state. | ||
protocol IPositionBuilderStrategy<State, PositionType> { | ||
associatedtype State: Equatable | ||
associatedtype PositionType: Equatable | ||
|
||
/// Takes a state as input and returns the corresponding position. | ||
/// | ||
/// - Parameter state: The state for which the next position needs to be built. | ||
/// | ||
/// - Returns: The resulting position based on the provided state. | ||
func next(state: State) -> PositionType | ||
} |
20 changes: 20 additions & 0 deletions
20
...lasses/COre/Reducers/Internal/Builders/RequestBuilders/CursorRequestBuilderStrategy.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// Blade | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import Blade | ||
|
||
/// A request builder strategy for cursor-based pagination. | ||
struct CursorRequestBuilderStrategy<State: Equatable & Identifiable>: IRequestBuilderStrategy { | ||
// MARK: IRequestBuilderStrategy | ||
|
||
/// Constructs a pagination request based on the provided state. | ||
/// | ||
/// - Parameter state: The current state of the paginator. | ||
/// | ||
/// - Returns: A CursorPaginationRequest with the cursor position from the provided state. | ||
func makeRequest(state: PaginatorState<State, State.ID>) -> CursorPaginationRequest<State> { | ||
CursorPaginationRequest(id: state.position) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...lasses/COre/Reducers/Internal/Builders/RequestBuilders/OffsetRequestBuilderStrategy.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// Blade | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import Blade | ||
import Foundation | ||
|
||
/// A request builder strategy for offset-based pagination. | ||
struct OffsetRequestBuilderStrategy<State: Equatable & Identifiable>: IRequestBuilderStrategy { | ||
// MARK: Properties | ||
|
||
/// The maximum number of items to be included in the pagination request. | ||
private let limit: Int | ||
|
||
// MARK: Initialization | ||
|
||
/// Initializes the OffsetRequestBuilderStrategy with a specified limit. | ||
/// | ||
/// - Parameter limit: The maximum number of items to be included in each pagination request. | ||
init(limit: Int) { | ||
self.limit = limit | ||
} | ||
|
||
// MARK: IRequestBuilderStrategy | ||
|
||
/// Constructs a pagination request based on the provided state. | ||
/// | ||
/// - Parameter state: The current state of the paginator. | ||
/// | ||
/// - Returns: An OffsetPaginationRequest with the offset position and specified limit from the provided state. | ||
func makeRequest(state: PaginatorState<State, Int>) -> OffsetPaginationRequest { | ||
OffsetPaginationRequest(limit: limit, offset: state.position) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...s/COre/Reducers/Internal/Builders/RequestBuilders/Protocols/IRequestBuilderStrategy.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// Blade | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A protocol for defining request builder strategies in a paginator. | ||
protocol IRequestBuilderStrategy<State, Request> { | ||
/// The state type associated with the strategy, conforming to Equatable. | ||
associatedtype State: Equatable | ||
|
||
/// The request type associated with the strategy, conforming to Equatable. | ||
associatedtype Request: Equatable | ||
|
||
/// Makes a request. | ||
/// | ||
/// - Parameter state: The current state for which a request needs to be built. | ||
/// | ||
/// - Returns: The constructed request based on the provided state. | ||
func makeRequest(state: State) -> Request | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
Sources/BladeTCA/Classes/Models/State/CursorPaginatorState.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// Blade | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import ComposableArchitecture | ||
|
||
/// Represents the state of a paginator for cursor-based pagination. | ||
public struct CursorPaginatorState<State: Equatable & Identifiable>: Equatable, IPaginatorState { | ||
// MARK: Properties | ||
|
||
/// The array of identifiable items managed by the paginator. | ||
public var items: IdentifiedArrayOf<State> | ||
|
||
/// A Boolean value indicating whether the paginator is currently loading more data. | ||
var isLoading: Bool | ||
|
||
/// The offset or position in the data set from where the paginator should load more items. | ||
var id: State.ID | ||
|
||
/// A Boolean value indicating whether there is more data available to be loaded. | ||
var hasMoreData: Bool | ||
|
||
// MARK: Initialization | ||
|
||
/// Initializes a paginator state with an array of identifiable items. | ||
/// | ||
/// - Parameters: | ||
/// - items: The array of identifiable items to be managed by the paginator. | ||
public init(items: IdentifiedArrayOf<State>, id: State.ID) { | ||
self.items = items | ||
isLoading = false | ||
hasMoreData = true | ||
self.id = id | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.