diff --git a/amplify/backend/api/amplifyDatasource/schema.graphql b/amplify/backend/api/amplifyDatasource/schema.graphql index 0c70f3d..c78ec01 100644 --- a/amplify/backend/api/amplifyDatasource/schema.graphql +++ b/amplify/backend/api/amplifyDatasource/schema.graphql @@ -2,6 +2,8 @@ type AWSTweet @model { id: ID! tweetID: String! text: String + authorName: String + authorUsername: String category: AWSCategory @connection(name: "CategorizedTweet") } diff --git a/amplify/generated/models/AWSTweet+Schema.swift b/amplify/generated/models/AWSTweet+Schema.swift index 65dd6c8..bf506bf 100644 --- a/amplify/generated/models/AWSTweet+Schema.swift +++ b/amplify/generated/models/AWSTweet+Schema.swift @@ -8,6 +8,8 @@ extension AWSTweet { case id case tweetID case text + case authorName + case authorUsername case category } @@ -23,6 +25,8 @@ extension AWSTweet { .id(), .field(aWSTweet.tweetID, is: .required, ofType: .string), .field(aWSTweet.text, is: .optional, ofType: .string), + .field(aWSTweet.authorName, is: .optional, ofType: .string), + .field(aWSTweet.authorUsername, is: .optional, ofType: .string), .belongsTo(aWSTweet.category, is: .optional, ofType: AWSCategory.self, targetName: "awsTweetCategoryId") ) } diff --git a/amplify/generated/models/AWSTweet.swift b/amplify/generated/models/AWSTweet.swift index 77c1824..596bb39 100644 --- a/amplify/generated/models/AWSTweet.swift +++ b/amplify/generated/models/AWSTweet.swift @@ -6,15 +6,21 @@ public struct AWSTweet: Model { public let id: String public var tweetID: String public var text: String? + public var authorName: String? + public var authorUsername: String? public var category: AWSCategory? public init(id: String = UUID().uuidString, tweetID: String, text: String? = nil, + authorName: String? = nil, + authorUsername: String? = nil, category: AWSCategory? = nil) { self.id = id self.tweetID = tweetID self.text = text + self.authorName = authorName + self.authorUsername = authorUsername self.category = category } } \ No newline at end of file diff --git a/amplify/generated/models/AmplifyModels.swift b/amplify/generated/models/AmplifyModels.swift index 2396d1b..44f6254 100644 --- a/amplify/generated/models/AmplifyModels.swift +++ b/amplify/generated/models/AmplifyModels.swift @@ -5,7 +5,7 @@ import Foundation // Contains the set of classes that conforms to the `Model` protocol. final public class AmplifyModels: AmplifyModelRegistration { - public let version: String = "2e2f9f76f94d0d76a69b2e3cdf98ddea" + public let version: String = "7d41c478cd06ada434ebdb360c6e4382" public func registerModels(registry: ModelRegistry.Type) { ModelRegistry.register(modelType: AWSTweet.self) diff --git a/brain-marks.xcodeproj/project.pbxproj b/brain-marks.xcodeproj/project.pbxproj index 0283bbd..d431e95 100644 --- a/brain-marks.xcodeproj/project.pbxproj +++ b/brain-marks.xcodeproj/project.pbxproj @@ -31,6 +31,7 @@ FF59902F2622C1CE004DF328 /* AWSDataStorePlugin in Frameworks */ = {isa = PBXBuildFile; productRef = FF59902E2622C1CE004DF328 /* AWSDataStorePlugin */; }; FF5990692622DD61004DF328 /* DataStoreManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF5990682622DD61004DF328 /* DataStoreManager.swift */; }; FF6DCBF22623530400EC366F /* CategoryListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF6DCBF12623530400EC366F /* CategoryListViewModel.swift */; }; + FFCB1097263E31D400544309 /* ReturnedTweet.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFCB1096263E31D300544309 /* ReturnedTweet.swift */; }; FFEBBB3A26223F75000F475F /* brain_marksApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEBBB3926223F75000F475F /* brain_marksApp.swift */; }; FFEBBB3C26223F75000F475F /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEBBB3B26223F75000F475F /* ContentView.swift */; }; FFEBBB3E26223F7E000F475F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FFEBBB3D26223F7E000F475F /* Assets.xcassets */; }; @@ -81,6 +82,7 @@ FF39431F262FCF6100A3623B /* Response.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Response.swift; sourceTree = ""; }; FF5990682622DD61004DF328 /* DataStoreManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataStoreManager.swift; sourceTree = ""; }; FF6DCBF12623530400EC366F /* CategoryListViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CategoryListViewModel.swift; sourceTree = ""; }; + FFCB1096263E31D300544309 /* ReturnedTweet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReturnedTweet.swift; sourceTree = ""; }; FFEBBB3626223F75000F475F /* brain-marks.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "brain-marks.app"; sourceTree = BUILT_PRODUCTS_DIR; }; FFEBBB3926223F75000F475F /* brain_marksApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = brain_marksApp.swift; sourceTree = ""; }; FFEBBB3B26223F75000F475F /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; @@ -156,6 +158,7 @@ FF39431F262FCF6100A3623B /* Response.swift */, 91C8958826228D1500689196 /* Tweet.swift */, FF394315262FCF3900A3623B /* User.swift */, + FFCB1096263E31D300544309 /* ReturnedTweet.swift */, ); path = Models; sourceTree = ""; @@ -451,6 +454,7 @@ 91C8958926228D1500689196 /* Tweet.swift in Sources */, FFEBBB3C26223F75000F475F /* ContentView.swift in Sources */, A224A9E62622BCED00AC12AF /* TweetCard.swift in Sources */, + FFCB1097263E31D400544309 /* ReturnedTweet.swift in Sources */, FF36B6712623678D007A6D7F /* AWSTweet+Extension.swift in Sources */, FFEBBB7626226A39000F475F /* Secrets.swift in Sources */, A2F449912622829D00725FEA /* CategoryList.swift in Sources */, diff --git a/brain-marks/Add/AddURLView.swift b/brain-marks/Add/AddURLView.swift index 4598a62..a3a0502 100644 --- a/brain-marks/Add/AddURLView.swift +++ b/brain-marks/Add/AddURLView.swift @@ -62,7 +62,7 @@ struct AddURLView: View { } extension AddURLView { - func get(url:String, completion: @escaping (Result) -> Void) { + func get(url:String, completion: @escaping (Result) -> Void) { let id = url.components(separatedBy: "/").last!.components(separatedBy: "?")[0] var request = URLRequest(url: URL(string: "https://api.twitter.com/2/tweets/\(id)?expansions=author_id")!, @@ -89,7 +89,12 @@ extension AddURLView { if error == nil { let result = try JSONDecoder().decode(Response.self, from: data) - completion(.success(result.data)) + let authorName = result.includes.users.first?.name ?? "" + let authorUsername = result.includes.users.first?.username ?? "" + + let tweetToSave = ReturnedTweet(id: result.data.id, text: result.data.text, authorName: authorName , authorUsername: authorUsername) + + completion(.success(tweetToSave)) } DispatchQueue.main.async { diff --git a/brain-marks/Managers/DataStoreManager.swift b/brain-marks/Managers/DataStoreManager.swift index 8eae78d..c01d375 100644 --- a/brain-marks/Managers/DataStoreManager.swift +++ b/brain-marks/Managers/DataStoreManager.swift @@ -54,10 +54,12 @@ class DataStoreManger { // MARK: - Tweets - func createTweet(tweet: Tweet, category: AWSCategory) { + func createTweet(tweet: ReturnedTweet, category: AWSCategory) { let awsTweet = AWSTweet(id: UUID().uuidString, tweetID: tweet.id, text: tweet.text, + authorName: tweet.authorName, + authorUsername: tweet.authorUsername, category: category) Amplify.DataStore.save(awsTweet) { result in diff --git a/brain-marks/Models/ReturnedTweet.swift b/brain-marks/Models/ReturnedTweet.swift new file mode 100644 index 0000000..595341a --- /dev/null +++ b/brain-marks/Models/ReturnedTweet.swift @@ -0,0 +1,15 @@ +// +// ReturnedTweet.swift +// brain-marks +// +// Created by Mikaela Caron on 5/1/21. +// + +import Foundation + +struct ReturnedTweet: Codable { + let id:String + let text:String + let authorName: String + let authorUsername: String +} diff --git a/brain-marks/Models/Tweet.swift b/brain-marks/Models/Tweet.swift index 4e272d3..507e5a3 100644 --- a/brain-marks/Models/Tweet.swift +++ b/brain-marks/Models/Tweet.swift @@ -10,5 +10,4 @@ import Foundation struct Tweet: Codable { let id:String let text:String - } diff --git a/brain-marks/Tweets/Views/TweetCard.swift b/brain-marks/Tweets/Views/TweetCard.swift index be6f0b6..90fa2b9 100644 --- a/brain-marks/Tweets/Views/TweetCard.swift +++ b/brain-marks/Tweets/Views/TweetCard.swift @@ -13,7 +13,7 @@ struct TweetCard: View { var body: some View { VStack(alignment: .leading) { - TweetHeaderView() + TweetHeaderView(tweet: tweet) TweetBodyView(tweetBody: tweet.text!) // TweetFooterView() } @@ -21,10 +21,14 @@ struct TweetCard: View { } struct TweetHeaderView: View { + + let tweet: AWSTweet + var body: some View { HStack { UserIconView(size: 55) - UserInfoView() + UserInfoView(authorName: tweet.authorName ?? "", + authorUsername: tweet.authorUsername ?? "") Spacer() } .padding(EdgeInsets(top: 18, leading: 18, bottom: 18, trailing: 18)) @@ -72,17 +76,20 @@ struct UserIconView: View { struct UserInfoView: View { + let authorName: String + let authorUsername: String + var body: some View { HStack(alignment: .firstTextBaseline) { VStack(alignment: .leading, spacing: 0) { HStack(spacing: 4) { - Text("Elon Musk") + Text(authorName) .font(.system(size: 16, weight: .semibold, design: .default)) // Image("verified") // .resizable() // .frame(width: 14, height: 14, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/) } - Text("@elonmusk") + Text("@\(authorUsername)") .font(.callout) .foregroundColor(.secondary) }