Update the logic of showing latest commit time

This commit is contained in:
yishilin14 2017-02-27 15:21:25 +08:00
parent c2db4d4641
commit 55b7e4303b
3 changed files with 18 additions and 20 deletions

View file

@ -253,12 +253,12 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
if section == tableData.count - 1 { if section == tableData.count - 1 {
let view = UIView() let view = UIView()
let footerLabel = UILabel(frame: CGRect(x: 8, y: 15, width: tableView.frame.width, height: 60)) let footerLabel = UILabel(frame: CGRect(x: 15, y: 15, width: tableView.frame.width, height: 60))
footerLabel.numberOfLines = 0 footerLabel.numberOfLines = 0
footerLabel.font = UIFont.preferredFont(forTextStyle: .footnote) footerLabel.font = UIFont.preferredFont(forTextStyle: .footnote)
footerLabel.textColor = UIColor.lightGray footerLabel.textColor = UIColor.lightGray
let dateString = DateFormatter.localizedString(from: passwordEntity?.commitDate as! Date, dateStyle: DateFormatter.Style.long, timeStyle: DateFormatter.Style.long) let dateString = PasswordStore.shared.getLatestCommitDate(filename: (passwordEntity?.rawPath)!)
footerLabel.text = "Latest commit: \(dateString)" footerLabel.text = "Latest commit: \(dateString ?? "Unknown")"
view.addSubview(footerLabel) view.addSubview(footerLabel)
return view return view
} }

View file

@ -243,17 +243,6 @@ class PasswordStore {
let endIndex = url.lastPathComponent.index(url.lastPathComponent.endIndex, offsetBy: -4) let endIndex = url.lastPathComponent.index(url.lastPathComponent.endIndex, offsetBy: -4)
passwordEntity.name = url.lastPathComponent.substring(to: endIndex) passwordEntity.name = url.lastPathComponent.substring(to: endIndex)
passwordEntity.rawPath = "\(url.path)" passwordEntity.rawPath = "\(url.path)"
if let blameHunks = try? storeRepository?.blame(withFile: e, options: nil).hunks {
func GetHunkDate(hunk: GTBlameHunk) -> TimeInterval {
guard let date = hunk.finalSignature?.time?.timeIntervalSince1970 else {
print("Time is missing from GTSignature.")
return 0
}
return date
}
let dates = blameHunks?.map(GetHunkDate).max()
passwordEntity.commitDate = NSDate(timeIntervalSince1970: dates!)
}
let items = url.path.characters.split(separator: "/").map(String.init) let items = url.path.characters.split(separator: "/").map(String.init)
for i in 0 ..< items.count - 1 { for i in 0 ..< items.count - 1 {
let passwordCategoryEntity = PasswordCategoryEntity(context: context) let passwordCategoryEntity = PasswordCategoryEntity(context: context)
@ -344,6 +333,18 @@ class PasswordStore {
} }
} }
func getLatestCommitDate(filename: String) -> String? {
guard let blameHunks = try? storeRepository?.blame(withFile: filename, options: nil).hunks,
let latestCommitTime = blameHunks?.map({
$0.finalSignature?.time?.timeIntervalSince1970 ?? 0
}).max() else {
return nil
}
let date = Date(timeIntervalSince1970: latestCommitTime)
let dateString = DateFormatter.localizedString(from: date, dateStyle: DateFormatter.Style.medium, timeStyle: DateFormatter.Style.medium)
return dateString
}
func updateRemoteRepo() { func updateRemoteRepo() {
} }
@ -450,8 +451,7 @@ class PasswordStore {
passwordEntity.synced = false passwordEntity.synced = false
try context.save() try context.save()
print(saveURL.path) print(saveURL.path)
let commit = createAddCommitInRepository(message: "Add new password by pass for iOS", fileData: encryptedData, filename: saveURL.lastPathComponent, progressBlock: progressBlock) let _ = createAddCommitInRepository(message: "Add new password by pass for iOS", fileData: encryptedData, filename: saveURL.lastPathComponent, progressBlock: progressBlock)
passwordEntity.commitDate = commit?.commitDate as NSDate?
progressBlock(1.0) progressBlock(1.0)
} catch { } catch {
print(error) print(error)
@ -464,8 +464,7 @@ class PasswordStore {
let saveURL = storeURL.appendingPathComponent(passwordEntity.rawPath!) let saveURL = storeURL.appendingPathComponent(passwordEntity.rawPath!)
try encryptedData.write(to: saveURL) try encryptedData.write(to: saveURL)
progressBlock(0.3) progressBlock(0.3)
let commit = createAddCommitInRepository(message: "Update password by pass for iOS", fileData: encryptedData, filename: saveURL.lastPathComponent, progressBlock: progressBlock) let _ = createAddCommitInRepository(message: "Update password by pass for iOS", fileData: encryptedData, filename: saveURL.lastPathComponent, progressBlock: progressBlock)
passwordEntity.commitDate = commit?.commitDate as NSDate?
} catch { } catch {
print(error) print(error)
} }

View file

@ -6,7 +6,6 @@
<relationship name="password" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="PasswordEntity" inverseName="categories" inverseEntity="PasswordEntity" syncable="YES"/> <relationship name="password" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="PasswordEntity" inverseName="categories" inverseEntity="PasswordEntity" syncable="YES"/>
</entity> </entity>
<entity name="PasswordEntity" representedClassName="PasswordEntity" syncable="YES" codeGenerationType="class"> <entity name="PasswordEntity" representedClassName="PasswordEntity" syncable="YES" codeGenerationType="class">
<attribute name="commitDate" optional="YES" attributeType="Date" usesScalarValueType="NO" syncable="YES"/>
<attribute name="image" optional="YES" attributeType="Binary" allowsExternalBinaryDataStorage="YES" syncable="YES"/> <attribute name="image" optional="YES" attributeType="Binary" allowsExternalBinaryDataStorage="YES" syncable="YES"/>
<attribute name="name" attributeType="String" syncable="YES"/> <attribute name="name" attributeType="String" syncable="YES"/>
<attribute name="raw" optional="YES" attributeType="Binary" allowsExternalBinaryDataStorage="YES" syncable="YES"/> <attribute name="raw" optional="YES" attributeType="Binary" allowsExternalBinaryDataStorage="YES" syncable="YES"/>
@ -16,6 +15,6 @@
</entity> </entity>
<elements> <elements>
<element name="PasswordCategoryEntity" positionX="115" positionY="-9" width="128" height="90"/> <element name="PasswordCategoryEntity" positionX="115" positionY="-9" width="128" height="90"/>
<element name="PasswordEntity" positionX="-63" positionY="-18" width="128" height="150"/> <element name="PasswordEntity" positionX="-63" positionY="-18" width="128" height="135"/>
</elements> </elements>
</model> </model>