Merge pull request #1455 from Austinate/bugfix/add_folder_account_name_sync_issue

iOS, Add Folder: Make sure Account Name field is in sync with selecte…
This commit is contained in:
Maurice Parker 2019-12-24 16:02:28 -07:00 committed by GitHub
commit 5eac0dc0b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 32 additions and 15 deletions

View File

@ -20,7 +20,22 @@ class AddFolderViewController: UITableViewController, AddContainerViewController
return accounts.count > 1 return accounts.count > 1
} }
private var accounts: [Account]! private var accounts: [Account]! {
didSet {
if let predefinedAccount = accounts.first(where: { $0.accountID == AppDefaults.addFolderAccountID }) {
selectedAccount = predefinedAccount
} else {
selectedAccount = accounts[0]
}
}
}
private var selectedAccount: Account! {
didSet {
guard selectedAccount != oldValue else { return }
accountLabel.text = selectedAccount.flatMap { ($0 as DisplayNameProvider).nameForDisplay }
}
}
weak var delegate: AddContainerViewControllerChildDelegate? weak var delegate: AddContainerViewControllerChildDelegate?
@ -32,13 +47,11 @@ class AddFolderViewController: UITableViewController, AddContainerViewController
nameTextField.delegate = self nameTextField.delegate = self
accountLabel.text = (accounts[0] as DisplayNameProvider).nameForDisplay
if shouldDisplayPicker { if shouldDisplayPicker {
accountPickerView.dataSource = self accountPickerView.dataSource = self
accountPickerView.delegate = self accountPickerView.delegate = self
if let index = accounts.firstIndex(where: { $0.accountID == AppDefaults.addFolderAccountID }) { if let index = accounts.firstIndex(of: selectedAccount) {
accountPickerView.selectRow(index, inComponent: 0, animated: false) accountPickerView.selectRow(index, inComponent: 0, animated: false)
} }
@ -53,20 +66,25 @@ class AddFolderViewController: UITableViewController, AddContainerViewController
} }
private func didSelect(_ account: Account) {
AppDefaults.addFolderAccountID = account.accountID
selectedAccount = account
}
func cancel() { func cancel() {
delegate?.processingDidEnd() delegate?.processingDidEnd()
} }
func add() { func add() {
let account = accounts[accountPickerView.selectedRow(inComponent: 0)] guard let folderName = nameTextField.text else {
if let folderName = nameTextField.text { return
account.addFolder(folderName) { result in }
switch result { selectedAccount.addFolder(folderName) { result in
case .success: switch result {
self.delegate?.processingDidEnd() case .success:
case .failure(let error): self.delegate?.processingDidEnd()
self.presentError(error) case .failure(let error):
} self.presentError(error)
} }
} }
} }
@ -100,8 +118,7 @@ extension AddFolderViewController: UIPickerViewDataSource, UIPickerViewDelegate
} }
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
accountLabel.text = (accounts[row] as DisplayNameProvider).nameForDisplay didSelect(accounts[row])
AppDefaults.addFolderAccountID = accounts[row].accountID
} }
} }