Skip to content

Commit

Permalink
Add "Older Posts" pagination for timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
sbertix committed Jan 7, 2021
1 parent 076cc28 commit 8c63ba9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
29 changes: 21 additions & 8 deletions Sources/Swiftagram/Endpoints/EndpointMedia.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,25 +179,38 @@ public extension Endpoint.Media {
/// Timeline.
///
/// - parameter page: An optional `String` holding reference to a valid cursor. Defaults to `nil`.
@available(*, deprecated, message: "visit https://github.com/sbertix/Swiftagram/discussions/128")
public static func timeline(startingAt page: String? = nil) -> Endpoint.Paginated<Wrapper> {
Endpoint.version1.feed.appendingDefaultHeader()
.appending(path: "timeline/")
.appending(query: ["max_id": page])
.replacing(body: ["max_id": page])
.prepare {
switch $1 {
case .none:
switch $0.query["max_id"] {
case let value? where !value.isEmpty:
return $0.appending(query: ["reason": "pagination"])
return try? $0.appending(body: ["reason": "pagination"])
default:
return (try? $0.appending(body: ["reason": "cold_start_fetch",
"is_pull_to_refresh": "0"]))
?? $0
return try? $0.appending(body: ["reason": "cold_start_fetch", "is_pull_to_refresh": "0"])
}
case let response?:
guard let nextMaxId = try? response.get().nextMaxId.string() else { return nil }
return try? $0.appending(query: ["max_id": nextMaxId]).appending(body: ["reason": "pagination"])
guard let success = try? response.get(), let nextMaxId = success.nextMaxId.string() else { return nil }
// Check if you reached the end for unwatched posts.
// If you have, load older posts.
switch nextMaxId {
case "feed_recs_head_load":
guard let actualNextMaxId = success.feedItems.array()?
.last?
.endOfFeedDemarcator
.groupSet
.groups
.array()?
.first(where: { $0.id.string() == "past_posts" })?
.nextMaxId
.string() else { return nil }
return try? $0.appending(body: ["max_id": actualNextMaxId, "reason": "pagination"])
default:
return try? $0.appending(body: ["max_id": nextMaxId, "reason": "pagination"])
}
}
}
.locking(Secret.self) {
Expand Down
1 change: 1 addition & 0 deletions Tests/SwiftagramTests/EndpointTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ final class EndpointTests: XCTestCase {

/// Test `Endpoint.Media.Posts`.
func testEndpointPosts() {
performTest(on: Endpoint.Media.Posts.timeline())
performTest(on: Endpoint.Media.Posts.liked())
performTest(on: Endpoint.Media.Posts.saved())
performTest(on: Endpoint.Media.Posts.owned(by: "183250726"))
Expand Down

0 comments on commit 8c63ba9

Please sign in to comment.