Skip to content

Commit e4a4634

Browse files
committed
Add test views for reverse chronological timeline
1 parent b8a8c10 commit e4a4634

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

Demo App/Twift_SwiftUI.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
7157CD1527F303D900324A43 /* PaginatedTweetsMethodView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7157CD1427F303D900324A43 /* PaginatedTweetsMethodView.swift */; };
5050
7157CD1727F3127C00324A43 /* PaginatedUsersMethodView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7157CD1627F3127C00324A43 /* PaginatedUsersMethodView.swift */; };
5151
716FADEC27F0B82A002C1BA1 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 716FADEB27F0B82A002C1BA1 /* README.md */; };
52+
71934F702838FBE000F1C8C3 /* ReverseChronologicalTimeline.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71934F6F2838FBE000F1C8C3 /* ReverseChronologicalTimeline.swift */; };
5253
71D1487327F99201001E3F7A /* GetBookmarks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71D1487227F99201001E3F7A /* GetBookmarks.swift */; };
5354
71D1487527F99481001E3F7A /* AddBookmark.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71D1487427F99481001E3F7A /* AddBookmark.swift */; };
5455
71D1487727F99593001E3F7A /* DeleteBookmark.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71D1487627F99593001E3F7A /* DeleteBookmark.swift */; };
@@ -101,6 +102,7 @@
101102
7157CD1427F303D900324A43 /* PaginatedTweetsMethodView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginatedTweetsMethodView.swift; sourceTree = "<group>"; };
102103
7157CD1627F3127C00324A43 /* PaginatedUsersMethodView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginatedUsersMethodView.swift; sourceTree = "<group>"; };
103104
716FADEB27F0B82A002C1BA1 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
105+
71934F6F2838FBE000F1C8C3 /* ReverseChronologicalTimeline.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReverseChronologicalTimeline.swift; sourceTree = "<group>"; };
104106
71D1487227F99201001E3F7A /* GetBookmarks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetBookmarks.swift; sourceTree = "<group>"; };
105107
71D1487427F99481001E3F7A /* AddBookmark.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddBookmark.swift; sourceTree = "<group>"; };
106108
71D1487627F99593001E3F7A /* DeleteBookmark.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeleteBookmark.swift; sourceTree = "<group>"; };
@@ -229,6 +231,7 @@
229231
714749DA27934B1A00CB128B /* GetTweet.swift */,
230232
714749ED27960BBC00CB128B /* LikeTweet.swift */,
231233
71D283EE279DAD5F00640B2A /* PostTweet.swift */,
234+
71934F6F2838FBE000F1C8C3 /* ReverseChronologicalTimeline.swift */,
232235
714749D727934ADE00CB128B /* Tweets.swift */,
233236
714749F52799DA5100CB128B /* UploadMedia.swift */,
234237
714749EF27980EDC00CB128B /* UserLikes.swift */,
@@ -367,6 +370,7 @@
367370
71242AC42791F628000372DE /* GetFollowing.swift in Sources */,
368371
714749EC2796079400CB128B /* UserMentions.swift in Sources */,
369372
71D1487927F99767001E3F7A /* GetLists.swift in Sources */,
373+
71934F702838FBE000F1C8C3 /* ReverseChronologicalTimeline.swift in Sources */,
370374
71242AC22791F30C000372DE /* GetFollowers.swift in Sources */,
371375
714749F62799DA5100CB128B /* UploadMedia.swift in Sources */,
372376
71D1487327F99201001E3F7A /* GetBookmarks.swift in Sources */,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//
2+
// ReverseChronologicalTimeline.swift
3+
// Twift_SwiftUI
4+
//
5+
// Created by Daniel Eden on 21/05/2022.
6+
//
7+
8+
import SwiftUI
9+
import Twift
10+
11+
struct ReverseChronologicalTimeline: PagedView {
12+
@EnvironmentObject var twitterClient: Twift
13+
@State var tweets: [Tweet]?
14+
@State var errors: [TwitterAPIError] = []
15+
@State var meta: Meta?
16+
@State var includes: Tweet.Includes?
17+
18+
@SceneStorage("userId") var userId = ""
19+
20+
var body: some View {
21+
Form {
22+
Section {
23+
TextField("User ID", text: $userId)
24+
.keyboardType(.numberPad)
25+
26+
AsyncButton(action: {
27+
await getPage(nil)
28+
}) {
29+
Text("Get user timeline")
30+
}
31+
}
32+
33+
PaginatedTweetsMethodView(tweets: tweets,
34+
errors: errors,
35+
includes: includes,
36+
meta: meta,
37+
getPage: getPage)
38+
39+
}.navigationTitle("Get User Timeline")
40+
}
41+
42+
func userForTweet(tweet: Tweet) -> User? {
43+
guard let authorId = tweet.authorId else { return nil }
44+
45+
return includes?.users?.first(where: { $0.id == authorId })
46+
}
47+
48+
func getPage(_ token: String?) async {
49+
do {
50+
let result = try await twitterClient.reverseChronologicalTimeline(
51+
userId,
52+
fields: Set(Tweet.publicFields),
53+
expansions: [.mediaKeys(mediaFields: [\.url]),
54+
.authorId(userFields: [\.profileImageUrl]),
55+
.referencedTweetsId],
56+
paginationToken: token
57+
)
58+
59+
withAnimation {
60+
tweets = result.data
61+
includes = result.includes
62+
errors = result.errors ?? []
63+
meta = result.meta
64+
}
65+
} catch {
66+
if let error = error as? TwitterAPIError {
67+
withAnimation { errors = [error] }
68+
} else {
69+
print(error.localizedDescription)
70+
}
71+
}
72+
}
73+
}
74+
75+
struct ReverseChronologicalTimeline_Previews: PreviewProvider {
76+
static var previews: some View {
77+
ReverseChronologicalTimeline()
78+
}
79+
}

Demo App/Twift_SwiftUI/Tweets/Tweets.swift

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct Tweets: View {
2424
Section("Timelines") {
2525
NavigationLink(destination: UserTimeline()) { MethodRow(label: "`userTimeline(_ userId)`", method: .GET) }
2626
NavigationLink(destination: UserMentions()) { MethodRow(label: "`userMentions(_ userId)`", method: .GET) }
27+
NavigationLink(destination: ReverseChronologicalTimeline()) { MethodRow(label: "`reverseChronologicalTimeline(_ userId)`", method: .GET) }
2728
}
2829

2930
Section("Likes") {

0 commit comments

Comments
 (0)