Skip to content

Commit

Permalink
added metrics on pinned tweet to dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Darragh Rogan committed Jul 18, 2020
1 parent 925fe29 commit 5689e0d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,21 @@
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "817ACB76-768F-4DCF-BDEC-734EAA8EACE9"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Per mille/DataLoaderTwitter.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "151"
endingLineNumber = "151"
landmarkName = "loadTwitterData()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<key>LauncherApplication.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>2</integer>
</dict>
<key>Per mille Launcher Application.xcscheme_^#shared#^_</key>
<dict>
Expand All @@ -27,7 +27,7 @@
<key>Per mille iOS.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>2</integer>
<integer>1</integer>
</dict>
<key>Per mille.xcscheme_^#shared#^_</key>
<dict>
Expand Down
20 changes: 19 additions & 1 deletion Per mille/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
return NSMenuItem(title: "Listed: Please Refresh", action: nil, keyEquivalent: "")
}()

lazy var twitterPinnedTweet : NSMenuItem = {
return NSMenuItem(title: "Pinned Tweet: Please Refresh", action: nil, keyEquivalent: "")
}()

lazy var youTubeTitle : NSMenuItem = {
return NSMenuItem(title: "Channel: Please Refresh", action: nil, keyEquivalent: "")
}()
Expand Down Expand Up @@ -152,6 +156,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
menu.addItem(twitterFollowers)

menu.addItem(twitterListed)

menu.addItem(twitterPinnedTweet)

menu.addItem(
NSMenuItem.separator()
Expand Down Expand Up @@ -262,6 +268,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
self.twitterScreenName.title = "Screen Name: Loading, please wait"
self.twitterFollowers.title = "Followers ጰ: Loading, please wait"
self.twitterListed.title = "Listed: Loading, please wait"
self.twitterPinnedTweet.title = "Pinned Tweet: Loading, please wait"
}
else{
}
Expand Down Expand Up @@ -306,9 +313,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
})


DispatchQueue.main.asyncAfter(deadline: .now() + 2.0, execute: {
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0, execute: {

if self.defaults.integer(forKey: "TwitterInUse") == 1{

if twitterData.data?[0].username != nil {
self.twitterScreenName.title = "Screen Name: \(twitterData.data?[0].username as! String)"
self.twitterFollowers.title = "Followers ጰ: \(String(format: "%U", locale: Locale.current, (twitterData.data?[0].publicMetrics.followersCount as! Int)))"
Expand All @@ -318,6 +326,16 @@ class AppDelegate: NSObject, NSApplicationDelegate {
self.twitterScreenName.title = "Error; if internet connectivity & username okay, problem is with Twitter API. Try later"
self.twitterFollowers.title = "Error; if internet connectivity & username okay, problem is with Twitter API. Try later"
}

if twitterData.includes?.tweets[0].publicMetrics.likeCount != nil {
self.twitterPinnedTweet.title = "Pinned Tweet: ♺ \(String(format: "%U", locale: Locale.current, (twitterData.includes?.tweets[0].publicMetrics.retweetCount as! Int))), ❝❞ \(String(format: "%U", locale: Locale.current, (twitterData.includes?.tweets[0].publicMetrics.quoteCount as! Int))), ♥ \(String(format: "%U", locale: Locale.current, (twitterData.includes?.tweets[0].publicMetrics.likeCount as! Int))), 🗨 \(String(format: "%U", locale: Locale.current, (twitterData.includes?.tweets[0].publicMetrics.replyCount as! Int)))"
}
else {
self.twitterPinnedTweet.title = "Pinned Tweet: None"
}



}
else{
}
Expand Down
67 changes: 37 additions & 30 deletions Per mille/DataLoaderTwitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,64 +34,71 @@ import Foundation
// User's Twitter v2 API
struct TwitterDataStructure: Codable {
var data: [Datum]?
var includes: Includes?
}


// MARK: - Datum
struct Datum: Codable {
var publicMetrics: DatumPublicMetrics
var id: String
var entities: Entities
var username, name: String
var publicMetrics: PublicMetrics
var pinnedTweetID: String?
var name: String
var username: String

enum CodingKeys: String, CodingKey {
case id, entities, username, name
case publicMetrics = "public_metrics"
case id
case pinnedTweetID = "pinned_tweet_id"
case name, username
}
}


struct Entities: Codable {
var url, entitiesDescription: Description
// MARK: - DatumPublicMetrics
struct DatumPublicMetrics: Codable {
var followersCount, followingCount, tweetCount, listedCount: Int

enum CodingKeys: String, CodingKey {
case url
case entitiesDescription = "description"
case followersCount = "followers_count"
case followingCount = "following_count"
case tweetCount = "tweet_count"
case listedCount = "listed_count"
}
}

// MARK: - Description
struct Description: Codable {
var urls: [URLElement]?
// MARK: - Includes
struct Includes: Codable {
var tweets: [Tweet]
}

// MARK: - URLElement
struct URLElement: Codable {
var start, end: Int
var url: String
var expandedURL: String
var displayURL: String
// MARK: - Tweet
struct Tweet: Codable {
var id: String
var publicMetrics: TweetPublicMetrics
var text, createdAt: String

enum CodingKeys: String, CodingKey {
case start, end, url
case expandedURL = "expanded_url"
case displayURL = "display_url"
case id
case publicMetrics = "public_metrics"
case text
case createdAt = "created_at"
}
}

// MARK: - PublicMetrics
struct PublicMetrics: Codable {
var followersCount, followingCount, tweetCount, listedCount: Int
// MARK: - TweetPublicMetrics
struct TweetPublicMetrics: Codable {
var retweetCount, replyCount, likeCount, quoteCount: Int

enum CodingKeys: String, CodingKey {
case followersCount = "followers_count"
case followingCount = "following_count"
case tweetCount = "tweet_count"
case listedCount = "listed_count"
case retweetCount = "retweet_count"
case replyCount = "reply_count"
case likeCount = "like_count"
case quoteCount = "quote_count"
}
}




// define an instance of the twwitter data that can be filled by the data loader and read by the menu
var twitterData = TwitterDataStructure()

Expand All @@ -116,7 +123,7 @@ var twitterData = TwitterDataStructure()
"Authorization": "Bearer \(APIKeyTwitter)",
]

let request = NSMutableURLRequest(url: NSURL(string: "https://api.twitter.com/labs/2/users/by?usernames=\(AppDelegate().defaults.object(forKey:"TwitterHandle") as? String ?? String())&user.fields=entities,public_metrics&tweet.fields=created_at,public_metrics")! as URL,
let request = NSMutableURLRequest(url: NSURL(string: "https://api.twitter.com/labs/2/users/by?usernames=\(AppDelegate().defaults.object(forKey:"TwitterHandle") as? String ?? String())&user.fields=public_metrics&expansions=pinned_tweet_id&tweet.fields=created_at,public_metrics")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)

Expand Down

0 comments on commit 5689e0d

Please sign in to comment.