Tunnel edit: Update for VPN-on-demand changes

This commit is contained in:
Roopesh Chander 2018-11-12 15:53:18 +05:30
parent 1caa186ebc
commit e708e26a00
1 changed files with 26 additions and 14 deletions

View File

@ -26,8 +26,8 @@ class TunnelEditTableViewController: UITableViewController {
.deletePeer .deletePeer
] ]
let activateOnDemandOptions: [ActivationType] = [ let activateOnDemandOptions: [ActivateOnDemandOption] = [
.useOnDemandOverWifiAndCellular, .useOnDemandOverWifiOrCellular,
.useOnDemandOverWifiOnly, .useOnDemandOverWifiOnly,
.useOnDemandOverCellularOnly .useOnDemandOverCellularOnly
] ]
@ -35,12 +35,14 @@ class TunnelEditTableViewController: UITableViewController {
let tunnelsManager: TunnelsManager let tunnelsManager: TunnelsManager
let tunnel: TunnelContainer? let tunnel: TunnelContainer?
let tunnelViewModel: TunnelViewModel let tunnelViewModel: TunnelViewModel
var activateOnDemandSetting: ActivateOnDemandSetting
init(tunnelsManager tm: TunnelsManager, tunnel t: TunnelContainer) { init(tunnelsManager tm: TunnelsManager, tunnel t: TunnelContainer) {
// Use this initializer to edit an existing tunnel. // Use this initializer to edit an existing tunnel.
tunnelsManager = tm tunnelsManager = tm
tunnel = t tunnel = t
tunnelViewModel = TunnelViewModel(tunnelConfiguration: t.tunnelConfiguration()) tunnelViewModel = TunnelViewModel(tunnelConfiguration: t.tunnelConfiguration())
activateOnDemandSetting = t.activateOnDemandSetting()
super.init(style: .grouped) super.init(style: .grouped)
} }
@ -50,6 +52,7 @@ class TunnelEditTableViewController: UITableViewController {
tunnelsManager = tm tunnelsManager = tm
tunnel = nil tunnel = nil
tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnelConfiguration) tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnelConfiguration)
activateOnDemandSetting = ActivateOnDemandSetting.defaultSetting
super.init(style: .grouped) super.init(style: .grouped)
} }
@ -83,7 +86,9 @@ class TunnelEditTableViewController: UITableViewController {
case .saved(let tunnelConfiguration): case .saved(let tunnelConfiguration):
if let tunnel = tunnel { if let tunnel = tunnel {
// We're modifying an existing tunnel // We're modifying an existing tunnel
tunnelsManager.modify(tunnel: tunnel, with: tunnelConfiguration) { [weak self] (error) in tunnelsManager.modify(tunnel: tunnel,
tunnelConfiguration: tunnelConfiguration,
activateOnDemandSetting: activateOnDemandSetting) { [weak self] (error) in
if let error = error { if let error = error {
ErrorPresenter.showErrorAlert(error: error, from: self) ErrorPresenter.showErrorAlert(error: error, from: self)
} else { } else {
@ -93,7 +98,8 @@ class TunnelEditTableViewController: UITableViewController {
} }
} else { } else {
// We're adding a new tunnel // We're adding a new tunnel
tunnelsManager.add(tunnelConfiguration: tunnelConfiguration) { [weak self] (tunnel, error) in tunnelsManager.add(tunnelConfiguration: tunnelConfiguration,
activateOnDemandSetting: activateOnDemandSetting) { [weak self] (tunnel, error) in
if let error = error { if let error = error {
ErrorPresenter.showErrorAlert(error: error, from: self) ErrorPresenter.showErrorAlert(error: error, from: self)
} else { } else {
@ -149,10 +155,10 @@ extension TunnelEditTableViewController {
return 1 return 1
} else { } else {
// On-Demand Rules // On-Demand Rules
if (tunnelViewModel.activationType == .activateManually) { if (activateOnDemandSetting.isActivateOnDemandEnabled) {
return 1
} else {
return 4 return 4
} else {
return 1
} }
} }
} }
@ -385,15 +391,18 @@ extension TunnelEditTableViewController {
if (row == 0) { if (row == 0) {
let cell = tableView.dequeueReusableCell(withIdentifier: TunnelEditTableViewSwitchCell.id, for: indexPath) as! TunnelEditTableViewSwitchCell let cell = tableView.dequeueReusableCell(withIdentifier: TunnelEditTableViewSwitchCell.id, for: indexPath) as! TunnelEditTableViewSwitchCell
cell.message = "Activate on demand" cell.message = "Activate on demand"
cell.isOn = (tunnelViewModel.activationType != .activateManually) cell.isOn = activateOnDemandSetting.isActivateOnDemandEnabled
cell.onSwitchToggled = { [weak self] (isOn) in cell.onSwitchToggled = { [weak self] (isOn) in
guard let s = self else { return } guard let s = self else { return }
let indexPaths: [IndexPath] = (1 ..< 4).map { IndexPath(row: $0, section: section) } let indexPaths: [IndexPath] = (1 ..< 4).map { IndexPath(row: $0, section: section) }
if (isOn) { if (isOn) {
s.tunnelViewModel.activationType = .useOnDemandOverWifiAndCellular s.activateOnDemandSetting.isActivateOnDemandEnabled = true
if (s.activateOnDemandSetting.activateOnDemandOption == .none) {
s.activateOnDemandSetting.activateOnDemandOption = s.tunnelViewModel.defaultActivateOnDemandOption()
}
s.tableView.insertRows(at: indexPaths, with: .automatic) s.tableView.insertRows(at: indexPaths, with: .automatic)
} else { } else {
s.tunnelViewModel.activationType = .activateManually s.activateOnDemandSetting.isActivateOnDemandEnabled = false
s.tableView.deleteRows(at: indexPaths, with: .automatic) s.tableView.deleteRows(at: indexPaths, with: .automatic)
} }
} }
@ -401,9 +410,11 @@ extension TunnelEditTableViewController {
} else { } else {
assert(row < 4) assert(row < 4)
let cell = tableView.dequeueReusableCell(withIdentifier: TunnelEditTableViewSelectionListCell.id, for: indexPath) as! TunnelEditTableViewSelectionListCell let cell = tableView.dequeueReusableCell(withIdentifier: TunnelEditTableViewSelectionListCell.id, for: indexPath) as! TunnelEditTableViewSelectionListCell
let option = activateOnDemandOptions[row - 1] let rowOption = activateOnDemandOptions[row - 1]
cell.message = tunnelViewModel.activateOnDemandOptionText(for: option) let selectedOption = activateOnDemandSetting.activateOnDemandOption
cell.isChecked = (tunnelViewModel.activationType == option) assert(selectedOption != .none)
cell.message = tunnelViewModel.activateOnDemandOptionText(for: rowOption)
cell.isChecked = (selectedOption == rowOption)
return cell return cell
} }
} }
@ -475,7 +486,8 @@ extension TunnelEditTableViewController {
assert(row > 0) assert(row > 0)
let option = activateOnDemandOptions[row - 1] let option = activateOnDemandOptions[row - 1]
tunnelViewModel.activationType = option assert(option != .none)
activateOnDemandSetting.activateOnDemandOption = option
let indexPaths: [IndexPath] = (1 ..< 4).map { IndexPath(row: $0, section: section) } let indexPaths: [IndexPath] = (1 ..< 4).map { IndexPath(row: $0, section: section) }
tableView.reloadRows(at: indexPaths, with: .automatic) tableView.reloadRows(at: indexPaths, with: .automatic)