Skip to content

Commit f2a0c4c

Browse files
djohsmssun
andauthored
do not dismiss views when application is resumed (mssun#605)
* do not dismiss views when application is resumed * prevents the PasswordNavigationViewController and PasswordDetailTableViewController from being dismissed when the app is put to the background and then brought back to the foreground * Instead, the PasswordEntities are re-fetched from the context by their path to handle the re-creation of the entities during an update process that could have run in the background * update SwiftLint to version 0.50.* * update SwiftFormat to 0.51.* --------- Co-authored-by: Mingshen Sun <bob@mssun.me>
1 parent 83c6ae3 commit f2a0c4c

File tree

10 files changed

+77
-26
lines changed

10 files changed

+77
-26
lines changed

.swiftformat

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
numberFormatting, \
4545
# opaqueGenericParameters, \
4646
# organizeDeclarations, \
47-
preferDouble, \
4847
preferKeyPath, \
4948
redundantBackticks, \
5049
redundantBreak, \

pass/Controllers/AdvancedSettingsTableViewController.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ class AdvancedSettingsTableViewController: UITableViewController {
5353
alert.addAction(
5454
UIAlertAction(title: "ErasePasswordStoreData".localize(), style: UIAlertAction.Style.destructive) { [unowned self] _ in
5555
SVProgressHUD.show(withStatus: "Erasing...".localize())
56-
self.passwordStore.erase()
57-
self.navigationController!.popViewController(animated: true)
56+
passwordStore.erase()
57+
navigationController!.popViewController(animated: true)
5858
SVProgressHUD.showSuccess(withStatus: "Done".localize())
5959
SVProgressHUD.dismiss(withDelay: 1)
6060
}
@@ -67,8 +67,8 @@ class AdvancedSettingsTableViewController: UITableViewController {
6767
UIAlertAction(title: "DiscardAllLocalChanges".localize(), style: UIAlertAction.Style.destructive) { [unowned self] _ in
6868
SVProgressHUD.show(withStatus: "Resetting...".localize())
6969
do {
70-
let numberDiscarded = try self.passwordStore.reset()
71-
self.navigationController!.popViewController(animated: true)
70+
let numberDiscarded = try passwordStore.reset()
71+
navigationController!.popViewController(animated: true)
7272
SVProgressHUD.showSuccess(withStatus: "DiscardedCommits(%d)".localize(numberDiscarded))
7373
SVProgressHUD.dismiss(withDelay: 1)
7474
} catch {

pass/Controllers/PasswordDetailTableViewController.swift

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@ import UIKit
1515
import YubiKit
1616

1717
class PasswordDetailTableViewController: UITableViewController, UIGestureRecognizerDelegate, AlertPresenting {
18-
var passwordEntity: PasswordEntity?
18+
var passwordEntity: PasswordEntity? {
19+
didSet {
20+
passwordPath = passwordEntity?.path
21+
}
22+
}
23+
1924
private var password: Password?
2025
private var passwordImage: UIImage?
2126
private var oneTimePasswordIndexPath: IndexPath?
2227
private var shouldPopCurrentView = false
2328
private let passwordStore = PasswordStore.shared
2429

30+
// preserve path so it can be reloaded even if the passwordEntity is deleted during the update process
31+
private var passwordPath: String?
32+
2533
private lazy var editUIBarButtonItem: UIBarButtonItem = {
2634
let uiBarButtonItem = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(pressEdit))
2735
return uiBarButtonItem
@@ -74,6 +82,9 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
7482

7583
// reset the data table if the disaply settings have been changed
7684
NotificationCenter.default.addObserver(self, selector: #selector(decryptThenShowPasswordSelector), name: .passwordDetailDisplaySettingChanged, object: nil)
85+
86+
// A Siri shortcut can change the state of the app in the background. Hence, reload when opening the app.
87+
NotificationCenter.default.addObserver(self, selector: #selector(actOnPossiblePasswordStoreUpdate), name: UIApplication.willEnterForegroundNotification, object: nil)
7788
}
7889

7990
override func viewDidAppear(_ animated: Bool) {
@@ -526,6 +537,23 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
526537
}
527538

528539
extension PasswordDetailTableViewController {
540+
@objc
541+
func actOnPossiblePasswordStoreUpdate() {
542+
DispatchQueue.main.async {
543+
if let path = self.passwordPath {
544+
// reload PasswordEntity because all PasswordEntities are re-created on PasswordStore update
545+
self.passwordEntity = PasswordStore.shared.fetchPasswordEntity(with: path)
546+
547+
// dismiss if the PasswordEntity does not exist anymore
548+
if self.passwordEntity == nil {
549+
self.navigationController?.popToRootViewController(animated: true)
550+
} else {
551+
self.decryptThenShowPassword()
552+
}
553+
}
554+
}
555+
}
556+
529557
private func requestYubiKeyPIN(completion: @escaping (String) -> Void, cancellation: @escaping () -> Void) {
530558
let alert = UIAlertController(title: "YubiKey PIN", message: "Verify YubiKey OpenPGP PIN.", preferredStyle: .alert)
531559
alert.addAction(

pass/Controllers/PasswordEditorTableViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ class PasswordEditorTableViewController: UITableViewController {
248248
let alert = UIAlertController(title: "DeletePassword?".localize(), message: nil, preferredStyle: UIAlertController.Style.alert)
249249
alert.addAction(
250250
UIAlertAction(title: "Delete".localize(), style: UIAlertAction.Style.destructive) { [unowned self] _ in
251-
self.performSegue(withIdentifier: "deletePasswordSegue", sender: self)
251+
performSegue(withIdentifier: "deletePasswordSegue", sender: self)
252252
}
253253
)
254254
alert.addAction(UIAlertAction.cancel())
@@ -442,9 +442,9 @@ extension PasswordEditorTableViewController: SFSafariViewControllerDelegate {
442442
alert.addAction(
443443
UIAlertAction(title: "Yes", style: UIAlertAction.Style.default) { [unowned self] _ in
444444
// update tableData so to make sure reloadData() works correctly
445-
self.tableData[self.passwordSection][0][PasswordEditorCellKey.content] = generatedPassword
445+
tableData[passwordSection][0][PasswordEditorCellKey.content] = generatedPassword
446446
// update cell manually, no need to call reloadData()
447-
self.fillPasswordCell?.setContent(content: generatedPassword)
447+
fillPasswordCell?.setContent(content: generatedPassword)
448448
}
449449
)
450450
alert.addAction(UIAlertAction.cancel())

pass/Controllers/PasswordNavigationViewController.swift

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ class PasswordNavigationViewController: UIViewController {
2121
@IBOutlet var tableView: UITableView!
2222

2323
var dataSource: PasswordNavigationDataSource?
24-
var parentPasswordEntity: PasswordEntity?
24+
var parentPasswordEntity: PasswordEntity? {
25+
didSet {
26+
parentPath = parentPasswordEntity?.path
27+
}
28+
}
29+
30+
// preserve parent path so it can be reloaded even if the parentPasswordEntity is deleted during the update process
31+
private var parentPath: String?
2532

2633
var viewingUnsyncedPasswords = false
2734
var tapTabBarTime: TimeInterval = 0
@@ -181,13 +188,13 @@ class PasswordNavigationViewController: UIViewController {
181188
private func configureNotification() {
182189
let notificationCenter = NotificationCenter.default
183190
// Reset the data table if some password (maybe another one) has been updated.
184-
notificationCenter.addObserver(self, selector: #selector(actOnReloadTableViewRelatedNotification), name: .passwordStoreUpdated, object: nil)
191+
notificationCenter.addObserver(self, selector: #selector(actOnPossiblePasswordStoreUpdate), name: .passwordStoreUpdated, object: nil)
185192
// Reset the data table if the disaply settings have been changed.
186193
notificationCenter.addObserver(self, selector: #selector(actOnReloadTableViewRelatedNotification), name: .passwordDisplaySettingChanged, object: nil)
187194
// Search entrypoint for home screen quick action.
188195
notificationCenter.addObserver(self, selector: #selector(actOnSearchNotification), name: .passwordSearch, object: nil)
189196
// A Siri shortcut can change the state of the app in the background. Hence, reload when opening the app.
190-
notificationCenter.addObserver(self, selector: #selector(actOnReloadTableViewRelatedNotification), name: UIApplication.willEnterForegroundNotification, object: nil)
197+
notificationCenter.addObserver(self, selector: #selector(actOnPossiblePasswordStoreUpdate), name: UIApplication.willEnterForegroundNotification, object: nil)
191198
}
192199

193200
@objc
@@ -352,6 +359,23 @@ extension PasswordNavigationViewController {
352359
}
353360
}
354361

362+
@objc
363+
func actOnPossiblePasswordStoreUpdate() {
364+
DispatchQueue.main.async {
365+
if let path = self.parentPath {
366+
// reload parent because all PasswordEntities are re-created on PasswordStore update
367+
self.parentPasswordEntity = PasswordStore.shared.fetchPasswordEntity(with: path)
368+
369+
// pop to the root controller if the parent does not exist anymore
370+
if self.parentPasswordEntity == nil {
371+
self.navigationController?.popToRootViewController(animated: true)
372+
}
373+
}
374+
375+
self.resetViews()
376+
}
377+
}
378+
355379
func resetViews() {
356380
configureTableView(in: parentPasswordEntity)
357381
tableView.reloadData()
@@ -447,14 +471,14 @@ extension PasswordNavigationViewController: PasswordAlertPresenter {
447471
}
448472
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
449473
do {
450-
let pullOptions = gitCredential.getCredentialOptions(passwordProvider: self.present)
474+
let pullOptions = gitCredential.getCredentialOptions(passwordProvider: present)
451475
try PasswordStore.shared.pullRepository(options: pullOptions) { git_transfer_progress, _ in
452476
DispatchQueue.main.async {
453477
SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects) / Float(git_transfer_progress.pointee.total_objects), status: "PullingFromRemoteRepository".localize())
454478
}
455479
}
456480
if PasswordStore.shared.numberOfLocalCommits > 0 {
457-
let pushOptions = gitCredential.getCredentialOptions(passwordProvider: self.present)
481+
let pushOptions = gitCredential.getCredentialOptions(passwordProvider: present)
458482
try PasswordStore.shared.pushRepository(options: pushOptions) { current, total, _, _ in
459483
DispatchQueue.main.async {
460484
SVProgressHUD.showProgress(Float(current) / Float(total), status: "PushingToRemoteRepository".localize())

passAutoFillExtension/Controllers/CredentialProviderViewController.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ class CredentialProviderViewController: ASCredentialProviderViewController {
3838
passcodelock.presentPasscodeLockIfNeeded(self) {
3939
self.view.isHidden = true
4040
} after: { [unowned self] in
41-
self.view.isHidden = false
42-
self.credentialProvider.identifier = serviceIdentifiers.first
41+
view.isHidden = false
42+
credentialProvider.identifier = serviceIdentifiers.first
4343
let url = serviceIdentifiers.first
4444
.map(\.identifier)
4545
.flatMap(URL.init)
46-
self.passwordsViewController.navigationItem.prompt = url?.host
47-
self.passwordsViewController.showPasswordsWithSuggestion(matching: url?.host ?? "")
46+
passwordsViewController.navigationItem.prompt = url?.host
47+
passwordsViewController.showPasswordsWithSuggestion(matching: url?.host ?? "")
4848
}
4949
}
5050

@@ -61,7 +61,7 @@ class CredentialProviderViewController: ASCredentialProviderViewController {
6161
passcodelock.presentPasscodeLockIfNeeded(self) {
6262
self.view.isHidden = true
6363
} after: { [unowned self] in
64-
self.credentialProvider.credentials(for: credentialIdentity)
64+
credentialProvider.credentials(for: credentialIdentity)
6565
}
6666
}
6767

passExtension/Controllers/ExtensionViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class ExtensionViewController: UIViewController {
4646
super.viewWillAppear(animated)
4747
prepareCredentialList()
4848
passcodelock.presentPasscodeLockIfNeeded(self, after: { [unowned self] in
49-
self.view.isHidden = false
49+
view.isHidden = false
5050
})
5151
}
5252

passKit/Controllers/PasscodeLockViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ open class PasscodeLockViewController: UIViewController, UITextFieldDelegate {
191191
let myContext = LAContext()
192192
// If the device passcode is not set, reset the app.
193193
guard myContext.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil) else {
194-
self.passwordStore.erase()
195-
self.passcodeLockDidSucceed()
194+
passwordStore.erase()
195+
passcodeLockDidSucceed()
196196
return
197197
}
198198
// If the device passcode is set, authentication is required.

passKit/Models/GitCredential.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ public struct GitCredential {
7777
private func createCredentialProvider(_ passwordProvider: @escaping PasswordProvider) -> GTCredentialProvider {
7878
var attempts = 1
7979
return GTCredentialProvider { _, _, _ -> GTCredential? in
80-
if attempts > self.credentialType.allowedAttempts {
80+
if attempts > credentialType.allowedAttempts {
8181
return nil
8282
}
83-
guard let password = self.getPassword(attempts: attempts, passwordProvider: passwordProvider) else {
83+
guard let password = getPassword(attempts: attempts, passwordProvider: passwordProvider) else {
8484
return nil
8585
}
8686
attempts += 1
87-
return try? self.credentialType.createGTCredential(password: password)
87+
return try? credentialType.createGTCredential(password: password)
8888
}
8989
}
9090

scripts/swiftformat.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export PATH="/opt/homebrew/bin:/opt/homebrew/sbin${PATH+:$PATH}"
2-
SWIFTFORMAT_VERSION="0.50.*"
2+
SWIFTFORMAT_VERSION="0.51.*"
33

44
if [[ "${CI}" == "true" ]]; then
55
echo "Running in a Continuous Integration environment. Formatting is skipped."

0 commit comments

Comments
 (0)