diff --git a/Tokenary iOS/Screens/Accounts/EditAccountsViewController.swift b/Tokenary iOS/Screens/Accounts/EditAccountsViewController.swift index cd2ca6b6..61464162 100644 --- a/Tokenary iOS/Screens/Accounts/EditAccountsViewController.swift +++ b/Tokenary iOS/Screens/Accounts/EditAccountsViewController.swift @@ -45,10 +45,30 @@ class EditAccountsViewController: UIViewController { } } + private func toggleAccountAtIndex(_ index: Int) { + cellModels[index].isEnabled.toggle() + okButton.isEnabled = cellModels.contains(where: { $0.isEnabled }) + } + @IBAction func okButtonTapped(_ sender: Any) { - // TODO: implement updating accounts - print("yo") - dismissAnimated() + let newDerivations: [CoinDerivation] = cellModels.compactMap { model in + if model.isEnabled { + return model.coinDerivation + } else { + return nil + } + } + + if newDerivations != initialDerivations { + do { + try walletsManager.update(wallet: wallet, coinDerivations: newDerivations) + dismissAnimated() + } catch { + showMessageAlert(text: Strings.somethingWentWrong) + } + } else { + dismissAnimated() + } } } @@ -56,7 +76,9 @@ class EditAccountsViewController: UIViewController { extension EditAccountsViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - + tableView.deselectRow(at: indexPath, animated: true) + (tableView.cellForRow(at: indexPath) as? CoinDerivationTableViewCell)?.toggle() + toggleAccountAtIndex(indexPath.row) } } @@ -79,7 +101,9 @@ extension EditAccountsViewController: UITableViewDataSource { extension EditAccountsViewController: CoinDerivationTableViewCellDelegate { func didToggleSwitch(_ sender: CoinDerivationTableViewCell) { - // TODO: update model + if let index = tableView.indexPath(for: sender)?.row { + toggleAccountAtIndex(index) + } } } diff --git a/Tokenary iOS/Screens/Accounts/Views/CoinDerivationTableViewCell.swift b/Tokenary iOS/Screens/Accounts/Views/CoinDerivationTableViewCell.swift index 4295404d..bf46acc1 100644 --- a/Tokenary iOS/Screens/Accounts/Views/CoinDerivationTableViewCell.swift +++ b/Tokenary iOS/Screens/Accounts/Views/CoinDerivationTableViewCell.swift @@ -22,6 +22,10 @@ class CoinDerivationTableViewCell: UITableViewCell { coinSwitch.isOn = isEnabled } + func toggle() { + coinSwitch.isOn.toggle() + } + @IBAction func didToggleSwitch(_ sender: Any) { cellDelegate?.didToggleSwitch(self) }