Skip to content

Commit

Permalink
Add 'apollo-ios-pagination/' from commit 'e78c22f26139b7c8a990efba8dd…
Browse files Browse the repository at this point in the history
…5730bc625999c'

git-subtree-dir: apollo-ios-pagination
git-subtree-mainline: 0fee469
git-subtree-split: e78c22f
  • Loading branch information
BobaFetters committed Oct 10, 2023
2 parents 0fee469 + e78c22f commit 27486ea
Show file tree
Hide file tree
Showing 17 changed files with 707 additions and 0 deletions.
3 changes: 3 additions & 0 deletions apollo-ios-pagination/.github/renovate.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json"
}
93 changes: 93 additions & 0 deletions apollo-ios-pagination/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## User settings
xcuserdata/

## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
*.xcscmblueprint
*.xccheckout

## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
build/
DerivedData/
*.moved-aside
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3

## Obj-C/Swift specific
*.hmap

## App packaging
*.ipa
*.dSYM.zip
*.dSYM

## Playgrounds
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
# *.xcodeproj
#
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
# .swiftpm

.build/

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/
#
# Add this line if you want to avoid checking in source code from the Xcode workspace
# *.xcworkspace

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build/

# Accio dependency management
Dependencies/
.accio/

# fastlane
#
# It is recommended to not store the screenshots in the git repo.
# Instead, use fastlane to re-generate the screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control

fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
fastlane/test_output

# Code Injection
#
# After new code Injection tools there's a generated folder /iOSInjectionProject
# https://github.com/johnno1962/injectionforxcode

iOSInjectionProject/

# macOS
.DS_Store

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions apollo-ios-pagination/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Apollo GraphQL

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
50 changes: 50 additions & 0 deletions apollo-ios-pagination/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"pins" : [
{
"identity" : "apollo-ios",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apollographql/apollo-ios.git",
"state" : {
"revision" : "c3f6951be0e44ce75c820899fa272af66e77f5f6",
"version" : "1.3.3"
}
},
{
"identity" : "inflectorkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/mattt/InflectorKit",
"state" : {
"revision" : "d8cbcc04972690aaa5fc760a2b9ddb3e9f0decd7",
"version" : "1.0.0"
}
},
{
"identity" : "sqlite.swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/stephencelis/SQLite.swift.git",
"state" : {
"revision" : "7a2e3cd27de56f6d396e84f63beefd0267b55ccb",
"version" : "0.14.1"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser.git",
"state" : {
"revision" : "fee6933f37fde9a5e12a1e4aeaa93fe60116ff2a",
"version" : "1.2.2"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections",
"state" : {
"revision" : "937e904258d22af6e447a0b72c0bc67583ef64a2",
"version" : "1.0.4"
}
}
],
"version" : 2
}
31 changes: 31 additions & 0 deletions apollo-ios-pagination/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// swift-tools-version: 5.8

import PackageDescription

let package = Package(
name: "ApolloPagination",
platforms: [
.iOS(.v13),
.macOS(.v10_15),
.tvOS(.v13),
.watchOS(.v6)
],
products: [
.library(name: "ApolloPagination", targets: ["ApolloPagination"]),
],
dependencies: [
.package(
url: "https://github.com/apollographql/apollo-ios.git",
.upToNextMajor(from: "1.2.0")
),
],
targets: [
.target(
name: "ApolloPagination",
dependencies: [
.product(name: "Apollo", package: "apollo-ios"),
.product(name: "ApolloAPI", package: "apollo-ios"),
]
),
]
)
1 change: 1 addition & 0 deletions apollo-ios-pagination/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# apollo-ios-pagination
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import Apollo
import ApolloAPI
import Combine

public class AnyGraphQLQueryPager<Model> {
public typealias Output = Result<([Model], UpdateSource), Error>
private let _fetch: (CachePolicy) -> Void
private let _loadMore: (CachePolicy) async throws -> Void
private let _refetch: () -> Void
private let _cancel: () -> Void
private let _subject: AnyPublisher<Output, Never>
private var cancellables = [AnyCancellable]()

public init<Pager: GraphQLQueryPager<InitialQuery, NextQuery>, InitialQuery, NextQuery>(
pager: Pager,
initialTransform: @escaping (InitialQuery.Data) -> [Model],
nextPageTransform: @escaping (NextQuery.Data) -> [Model]
) {
_fetch = pager.fetch
_loadMore = pager.loadMore
_refetch = pager.refetch
_cancel = pager.cancel

_subject = pager.subject.map { result in
let returnValue: Output

switch result {
case let .success(value):
let (initial, next, updateSource) = value
let firstPage = initialTransform(initial)
let nextPages = next.flatMap { nextPageTransform($0) }
returnValue = .success((firstPage + nextPages, updateSource))
case let .failure(error):
returnValue = .failure(error)
}

return returnValue
}.eraseToAnyPublisher()
}

public func subscribe(completion: @escaping (Output) -> Void) {
_subject.sink { result in
completion(result)
}.store(in: &cancellables)
}

public func fetch(cachePolicy: CachePolicy = .returnCacheDataAndFetch) {
_fetch(cachePolicy)
}

public func loadMore(
cachePolicy: CachePolicy = .returnCacheDataAndFetch,
completion: (@MainActor () -> Void)? = nil
) async throws {
try await _loadMore(cachePolicy)
await completion?()
}

public func refetch() {
_refetch()
}

public func cancel() {
_cancel()
}
}

public extension GraphQLQueryPager {
func eraseToAnyPager<T>(
initialTransform: @escaping (InitialQuery.Data) -> [T],
nextPageTransform: @escaping (PaginatedQuery.Data) -> [T]
) -> AnyGraphQLQueryPager<T> {
.init(pager: self, initialTransform: initialTransform, nextPageTransform: nextPageTransform)
}
}

extension AsyncStream {
public func map<Transformed>(_ transform: @escaping (Self.Element) -> Transformed) -> AsyncStream<Transformed> {
return AsyncStream<Transformed> { continuation in
Task {
for await element in self {
continuation.yield(transform(element))
}
continuation.finish()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public enum CursorBasedPagination { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
extension CursorBasedPagination {
public struct ForwardPagination: PaginationInfo, Hashable {
public let hasNext: Bool
public let endCursor: String?

public var canLoadMore: Bool { hasNext }

public init(hasNext: Bool, endCursor: String?) {
self.hasNext = hasNext
self.endCursor = endCursor
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
extension CursorBasedPagination {
public struct ReversePagination: PaginationInfo, Hashable {
public let hasPrevious: Bool
public let startCursor: String?

public var canLoadMore: Bool { hasPrevious }

public init(hasPrevious: Bool, startCursor: String?) {
self.hasPrevious = hasPrevious
self.startCursor = startCursor
}
}

}
Loading

0 comments on commit 27486ea

Please sign in to comment.