add progress callback to "add password"
This commit is contained in:
parent
3cec9643e5
commit
1a7451a326
2 changed files with 27 additions and 9 deletions
|
|
@ -32,8 +32,21 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
|
||||||
}
|
}
|
||||||
@IBAction func saveAddPassword(segue: UIStoryboardSegue) {
|
@IBAction func saveAddPassword(segue: UIStoryboardSegue) {
|
||||||
if let controller = segue.source as? AddPasswordTableViewController {
|
if let controller = segue.source as? AddPasswordTableViewController {
|
||||||
PasswordStore.shared.add(password: controller.password!)
|
SVProgressHUD.setDefaultMaskType(.black)
|
||||||
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated")))
|
SVProgressHUD.setDefaultStyle(.light)
|
||||||
|
SVProgressHUD.show(withStatus: "Saving")
|
||||||
|
DispatchQueue.global(qos: .userInitiated).async {
|
||||||
|
PasswordStore.shared.add(password: controller.password!, progressBlock: { progress in
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
SVProgressHUD.showProgress(progress, status: "Encrypting")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
SVProgressHUD.showSuccess(withStatus: "Done")
|
||||||
|
SVProgressHUD.dismiss(withDelay: 1)
|
||||||
|
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated")))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func syncPasswords() {
|
func syncPasswords() {
|
||||||
|
|
@ -47,9 +60,9 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
|
||||||
SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Pull Remote Repository")
|
SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Pull Remote Repository")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
try PasswordStore.shared.pushRepository(transferProgressBlock: {(git_transfer_progress, stop) in
|
try PasswordStore.shared.pushRepository(transferProgressBlock: {(current, total, bytes, stop) in
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Push Remote Repository")
|
SVProgressHUD.showProgress(Float(current)/Float(total), status: "Push Remote Repository")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
|
|
|
||||||
|
|
@ -181,7 +181,7 @@ class PasswordStore {
|
||||||
func updateRemoteRepo() {
|
func updateRemoteRepo() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createCommitInRepository(message: String, fileData: Data, filename: String) -> GTCommit? {
|
func createCommitInRepository(message: String, fileData: Data, filename: String, progressBlock: (_ progress: Float) -> Void) -> GTCommit? {
|
||||||
do {
|
do {
|
||||||
let head = try storeRepository!.headReference()
|
let head = try storeRepository!.headReference()
|
||||||
let branch = GTBranch(reference: head, repository: storeRepository!)
|
let branch = GTBranch(reference: head, repository: storeRepository!)
|
||||||
|
|
@ -195,7 +195,9 @@ class PasswordStore {
|
||||||
let commitEnum = try GTEnumerator(repository: storeRepository!)
|
let commitEnum = try GTEnumerator(repository: storeRepository!)
|
||||||
try commitEnum.pushSHA(headReference.targetOID.sha)
|
try commitEnum.pushSHA(headReference.targetOID.sha)
|
||||||
let parent = commitEnum.nextObject() as! GTCommit
|
let parent = commitEnum.nextObject() as! GTCommit
|
||||||
|
progressBlock(0.5)
|
||||||
let commit = try storeRepository!.createCommit(with: newTree, message: message, parents: [parent], updatingReferenceNamed: headReference.name)
|
let commit = try storeRepository!.createCommit(with: newTree, message: message, parents: [parent], updatingReferenceNamed: headReference.name)
|
||||||
|
progressBlock(0.7)
|
||||||
return commit
|
return commit
|
||||||
} catch {
|
} catch {
|
||||||
print(error)
|
print(error)
|
||||||
|
|
@ -214,26 +216,29 @@ class PasswordStore {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func pushRepository(transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void) throws {
|
func pushRepository(transferProgressBlock: @escaping (UInt32, UInt32, Int, UnsafeMutablePointer<ObjCBool>) -> Void) throws {
|
||||||
let credentialProvider = try gitCredential!.credentialProvider()
|
let credentialProvider = try gitCredential!.credentialProvider()
|
||||||
let options: [String: Any] = [
|
let options: [String: Any] = [
|
||||||
GTRepositoryRemoteOptionsCredentialProvider: credentialProvider,
|
GTRepositoryRemoteOptionsCredentialProvider: credentialProvider,
|
||||||
]
|
]
|
||||||
let masterBranch = getLocalBranch(withName: "master")!
|
let masterBranch = getLocalBranch(withName: "master")!
|
||||||
let remote = try GTRemote(name: "origin", in: storeRepository!)
|
let remote = try GTRemote(name: "origin", in: storeRepository!)
|
||||||
try storeRepository?.push(masterBranch, to: remote, withOptions: options, progress: nil)
|
try storeRepository?.push(masterBranch, to: remote, withOptions: options, progress: transferProgressBlock)
|
||||||
}
|
}
|
||||||
|
|
||||||
func add(password: Password) {
|
func add(password: Password, progressBlock: (_ progress: Float) -> Void) {
|
||||||
|
progressBlock(0.0)
|
||||||
let passwordEntity = NSEntityDescription.insertNewObject(forEntityName: "PasswordEntity", into: context) as! PasswordEntity
|
let passwordEntity = NSEntityDescription.insertNewObject(forEntityName: "PasswordEntity", into: context) as! PasswordEntity
|
||||||
do {
|
do {
|
||||||
let encryptedData = try passwordEntity.encrypt(password: password)
|
let encryptedData = try passwordEntity.encrypt(password: password)
|
||||||
|
progressBlock(0.3)
|
||||||
let saveURL = storeURL.appendingPathComponent("\(password.name).gpg")
|
let saveURL = storeURL.appendingPathComponent("\(password.name).gpg")
|
||||||
try encryptedData.write(to: saveURL)
|
try encryptedData.write(to: saveURL)
|
||||||
passwordEntity.rawPath = "\(password.name).gpg"
|
passwordEntity.rawPath = "\(password.name).gpg"
|
||||||
try context.save()
|
try context.save()
|
||||||
print(saveURL.path)
|
print(saveURL.path)
|
||||||
let _ = createCommitInRepository(message: "Add new password by pass for iOS", fileData: encryptedData, filename: saveURL.lastPathComponent)
|
let _ = createCommitInRepository(message: "Add new password by pass for iOS", fileData: encryptedData, filename: saveURL.lastPathComponent, progressBlock: progressBlock)
|
||||||
|
progressBlock(1.0)
|
||||||
} catch {
|
} catch {
|
||||||
print(error)
|
print(error)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue