Use relative time to present last updated time

This commit is contained in:
yishilin14 2017-03-02 00:06:27 +08:00
parent 98b01d16cf
commit 8798be65b5
2 changed files with 18 additions and 7 deletions

View file

@ -300,8 +300,8 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
footerLabel.numberOfLines = 0 footerLabel.numberOfLines = 0
footerLabel.font = UIFont.preferredFont(forTextStyle: .footnote) footerLabel.font = UIFont.preferredFont(forTextStyle: .footnote)
footerLabel.textColor = UIColor.gray footerLabel.textColor = UIColor.gray
let dateString = PasswordStore.shared.getLatestCommitDate(filename: (passwordEntity?.rawPath)!) let dateString = PasswordStore.shared.getLatestUpdateInfo(filename: (passwordEntity?.rawPath)!)
footerLabel.text = "Last Updated: \(dateString ?? "Unknown")" footerLabel.text = "Last Updated: \(dateString)"
view.addSubview(footerLabel) view.addSubview(footerLabel)
return view return view
} }

View file

@ -338,16 +338,27 @@ class PasswordStore {
} }
} }
func getLatestCommitDate(filename: String) -> String? { func getLatestUpdateInfo(filename: String) -> String {
guard let blameHunks = try? storeRepository?.blame(withFile: filename, options: nil).hunks, guard let blameHunks = try? storeRepository?.blame(withFile: filename, options: nil).hunks,
let latestCommitTime = blameHunks?.map({ let latestCommitTime = blameHunks?.map({
$0.finalSignature?.time?.timeIntervalSince1970 ?? 0 $0.finalSignature?.time?.timeIntervalSince1970 ?? 0
}).max() else { }).max() else {
return nil return "unknown"
} }
let date = Date(timeIntervalSince1970: latestCommitTime) let lastCommitDate = Date(timeIntervalSince1970: latestCommitTime)
let dateString = DateFormatter.localizedString(from: date, dateStyle: DateFormatter.Style.medium, timeStyle: DateFormatter.Style.medium) let currentDate = Date()
return dateString var autoFormattedDifference: String
if currentDate.timeIntervalSince(lastCommitDate) <= 60 {
autoFormattedDifference = "just now"
} else {
let diffDate = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: lastCommitDate, to: currentDate)
let dateComponentsFormatter = DateComponentsFormatter()
dateComponentsFormatter.unitsStyle = .full
dateComponentsFormatter.maximumUnitCount = 2
dateComponentsFormatter.includesApproximationPhrase = true
autoFormattedDifference = (dateComponentsFormatter.string(from: diffDate)?.appending(" ago"))!
}
return autoFormattedDifference
} }
func updateRemoteRepo() { func updateRemoteRepo() {