Skip to content

Commit

Permalink
✨ Add a feature showing Streaks (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
5anniversary committed Feb 25, 2021
1 parent 6535162 commit 5ad1711
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 6 deletions.
40 changes: 36 additions & 4 deletions Sources/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

private var myContributes: [ContributeData] = []
private var friendContributes: [ContributeData] = []

private var mystreaks: ContributeData = ContributeData(count: 0, weekend: "", date: "")

private let userMenuItem = NSMenuItem().then {
$0.title = Localized.hello
Expand Down Expand Up @@ -329,10 +329,22 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
group.notify(queue: .main){
self.updateContributions()
self.fetchStreaks(self.mystreaks)
}

}

private func fetchStreaks(_ date: ContributeData) {
let menuItem = NSMenuItem().then {
$0.isEnabled = true
$0.tag = Consts.contributionTag
$0.attributedTitle = date.getStreaks()
}

self.menu.insertItem(.separator(), at: .zero)
self.menu.insertItem(menuItem, at: .zero)
}

private func fetchContributionsByUserame(username: String, isFriend: Bool = false, group: DispatchGroup? = nil ) {
guard let targetURL = URL(string: "https://github.com/users/\(username)/contributions") else { return }
URLSession.shared.dataTask(with: targetURL) { [weak self] data, response, error in
Expand Down Expand Up @@ -363,6 +375,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
self.friendContributes = contributeDataList
} else{
self.myContributes = contributeDataList
self.mystreaks = self.parseHtmltoDataForCount(html: html)
}

if group != nil {
Expand Down Expand Up @@ -395,15 +408,34 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let doc: Document = try SwiftSoup.parse(html)
let rects: Elements = try doc.getElementsByTag(ParseKeys.rect)
let days: [Element] = rects.array().filter { $0.hasAttr(ParseKeys.date) }

let weekend = days.suffix(Consts.fetchCount)

let contributeDataList = weekend.map(mapFunction)
return contributeDataList

} catch {
} catch {
return []
}
}


private func parseHtmltoDataForCount(html: String) -> ContributeData {
do {
let doc: Document = try SwiftSoup.parse(html)
let rects: Elements = try doc.getElementsByTag(ParseKeys.rect)
let days: [Element] = rects.array().filter { $0.hasAttr(ParseKeys.date) }
let count = days.suffix(Consts.fetchStreak)
var contributeLastDate = count.map(mapFunction)
contributeLastDate.sort{ $0.date > $1.date }
for date in contributeLastDate {
if date.count == .zero {
return date
}
}

return ContributeData(count: 0, weekend: "", date: "")
} catch {
return ContributeData(count: 0, weekend: "", date: "")
}
}

}
1 change: 1 addition & 0 deletions Sources/Consts/Consts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation

enum Consts {
static let fetchCount = 10
static let fetchStreak = 36500
static let refreshInterval = 10
static let contributionTag = 1
static let usernameDefaultKey = "username"
Expand Down
5 changes: 4 additions & 1 deletion Sources/Consts/Localized.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ enum Localized {
static let changeUsernameDescription = NSLocalizedString("change_username_description", comment: "Change username")
static let information = NSLocalizedString("information", comment: "Enter your GitHub username. We’ll fetch the number of contributions.")
static let friend_information = NSLocalizedString("friend_information", comment: "Enter your friend GitHub username. We'll fetch the number of contributions.")

static let streak_first_stage = NSLocalizedString("streak_first_stage", comment: "streak stage 1")
static let streak_second_stage = NSLocalizedString("streak_second_stage", comment: "streak stage 2")
static let streak_third_stage = NSLocalizedString("streak_third_stage", comment: "streak stage 3")
static let streak_fourth_stage = NSLocalizedString("streak_fourth_stage", comment: "streak stage 4")
static let ok = NSLocalizedString("ok", comment: "Okay")
static let cancel = NSLocalizedString("cancel", comment: "cancel")
static let error = NSLocalizedString("error", comment: "error")
Expand Down
13 changes: 13 additions & 0 deletions Sources/Extensions/Date+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,17 @@ extension Date {
dateFormatter.dateFormat = "E"
return dateFormatter.string(from: self).capitalized
}

func timeAgoSince() -> String {
let calendar = Calendar.current
let now = Date()

let unitFlags: NSCalendar.Unit = [.day]
let components = (calendar as NSCalendar).components(unitFlags, from: self, to: now, options: [])

guard let day = components.day else { return "0" }

return "\(day)"
}

}
13 changes: 13 additions & 0 deletions Sources/Extensions/Int+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,17 @@ extension Int {
return "πŸ”₯"
}
}

func getStreaks() -> String {
switch self {
case 0:
return Localized.streak_first_stage
case 1 ..< 4:
return Localized.streak_second_stage
case 4 ..< 10:
return Localized.streak_third_stage.replacingOccurrences(of: "${day}", with: self.description)
default:
return Localized.streak_fourth_stage.replacingOccurrences(of: "${day}", with: self.description)
}
}
}
21 changes: 21 additions & 0 deletions Sources/Extensions/String+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// String+Extensions.swift
// jandi
//
// Created by μ˜€μ€€ν˜„ on 2021/02/24.
//

import Foundation

extension String {

public func getDateFormat() -> Date {
let dateFormat = DateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd"
dateFormat.timeZone = TimeZone(secondsFromGMT: 0)
guard let timeDateFormat = dateFormat.date(from: self) else { return Date() }

return timeDateFormat
}

}
15 changes: 14 additions & 1 deletion Sources/Models/ContributeData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,25 @@ class ContributeData {
var textString = "\(emoji) \(count)"

if self.friendContributeData != nil {
guard let friendContributeData = self.friendContributeData else {return textString}
guard let friendContributeData = self.friendContributeData else { return textString }
textString += " vs \(friendContributeData.count) \(friendContributeData.count.getEmoji())"
}
return textString
}

public func getStreaks() -> NSAttributedString {
let statusDetailAttributedString = NSMutableAttributedString()
guard let day = Int(date.getDateFormat().timeAgoSince()) else { return statusDetailAttributedString }
var attribute = Attributes.red
let textString = day.getStreaks()
if day > 0 {
attribute = Attributes.green
}

let attributedString = NSAttributedString(string: textString, attributes: attribute)
statusDetailAttributedString.append(attributedString)
return statusDetailAttributedString
}

public func getStatusDetailAttributedString() -> NSAttributedString {
let statusDetailAttributedString = NSMutableAttributedString()
Expand Down
4 changes: 4 additions & 0 deletions Supporting FIles/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
"change_username_description" = "Change username";
"information" = "Enter your GitHub username.\nWe’ll fetch the number of contributions.";
"friend_information" = "Enter your friend GitHub username.\nWe'll fetch the number of contributions.";
"streak_first_stage" = "😒 I haven't planted Jandi yet.";
"streak_second_stage" = "🌱 I planted my first Jandi today.";
"streak_third_stage" = "🌿 It has been ${day} days since the Jandi sprouts.";
"streak_fourth_stage" = "🌳 It's been ${day} days since the Jandi has grown well.";
"ok" = "Okay";
"cancel" = "Cancel";
"error" = "⚠️ Error";
Expand Down
4 changes: 4 additions & 0 deletions Supporting FIles/ko.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
"change_username_description" = "아이디λ₯Ό λ³€κ²½ν•©λ‹ˆλ‹€";
"information" = "Github 아이디λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”.\nμž…λ ₯ν•΄μ£Όμ‹  정보λ₯Ό 기반으둜 컀밋정보λ₯Ό κ°€μ Έμ˜΅λ‹ˆλ‹€";
"friend_information" = "친ꡬ의 Github 아이디λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”. \nμž…λ ₯ν•΄μ£Όμ‹  정보λ₯Ό 기반으둜 친ꡬ의 컀밋정보λ₯Ό κ°€μ Έμ˜΅λ‹ˆλ‹€";
"streak_first_stage" = "😒 아직 μž”λ””λ₯Ό 심지 λͺ»ν–ˆμ–΄μš”.";
"streak_second_stage" = "🌱 였늘 처음으둜 μž”λ””λ₯Ό μ‹¬μ—ˆμ–΄μš”.";
"streak_third_stage" = "🌿 μž”λ””μ— 싹이 튼 지 ${day}일이 λ˜μ—ˆμ–΄μš”.";
"streak_fourth_stage" = "🌳 μž”λ””κ°€ 무럭무럭 μžλž€μ§€ ${day}일이 λ˜μ—ˆμ–΄μš”.";
"ok" = "확인";
"cancel" = "μ·¨μ†Œ";
"error" = "⚠️ 였λ₯˜";
Expand Down
4 changes: 4 additions & 0 deletions jandi.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
C5077EF725E69288008F1BE8 /* String+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5077EF625E69288008F1BE8 /* String+Extensions.swift */; };
ED15D02E25C5600E00C04BEC /* Localized.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED15D02D25C5600E00C04BEC /* Localized.swift */; };
ED15D03225C5604800C04BEC /* ParseKeys.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED15D03125C5604800C04BEC /* ParseKeys.swift */; };
ED15D03525C5605300C04BEC /* Consts.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED15D03425C5605300C04BEC /* Consts.swift */; };
Expand All @@ -24,6 +25,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
C5077EF625E69288008F1BE8 /* String+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Extensions.swift"; sourceTree = "<group>"; };
ED15D02D25C5600E00C04BEC /* Localized.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Localized.swift; sourceTree = "<group>"; };
ED15D03125C5604800C04BEC /* ParseKeys.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParseKeys.swift; sourceTree = "<group>"; };
ED15D03425C5605300C04BEC /* Consts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Consts.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -105,6 +107,7 @@
children = (
ED18259B25C4420A00FCD100 /* Int+Extensions.swift */,
ED18259F25C4422D00FCD100 /* Date+Extensions.swift */,
C5077EF625E69288008F1BE8 /* String+Extensions.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -204,6 +207,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C5077EF725E69288008F1BE8 /* String+Extensions.swift in Sources */,
ED3FF1F225E3EEAA008CBD34 /* ContributeData.swift in Sources */,
ED18259C25C4420A00FCD100 /* Int+Extensions.swift in Sources */,
ED15D03525C5605300C04BEC /* Consts.swift in Sources */,
Expand Down

0 comments on commit 5ad1711

Please sign in to comment.