on-demand: iOS: SSIDs view: Always show the selected SSIDs section

This commit is contained in:
Roopesh Chander 2019-03-09 09:47:35 +05:30 committed by Jason A. Donenfeld
parent 8c3fcc6aee
commit d85717785c
3 changed files with 38 additions and 25 deletions

View File

@ -90,8 +90,9 @@
"tunnelOnDemandOnlySelectedSSIDs" = "Only selected SSIDs"; "tunnelOnDemandOnlySelectedSSIDs" = "Only selected SSIDs";
"tunnelOnDemandExceptSelectedSSIDs" = "Except selected SSIDs"; "tunnelOnDemandExceptSelectedSSIDs" = "Except selected SSIDs";
"tunnelOnDemandSelectionViewTitle" = "Select SSIDs"; "tunnelOnDemandSSIDViewTitle" = "SSIDs";
"tunnelOnDemandSectionTitleSelectedSSIDs" = "Selected SSIDs"; "tunnelOnDemandSectionTitleSelectedSSIDs" = "SSIDs";
"tunnelOnDemandNoSSIDs" = "No SSIDs";
"tunnelOnDemandSectionTitleAddSSIDs" = "Add SSIDs"; "tunnelOnDemandSectionTitleAddSSIDs" = "Add SSIDs";
"tunnelOnDemandAddMessageAddNewSSID" = "Add manually"; "tunnelOnDemandAddMessageAddNewSSID" = "Add manually";
"tunnelOnDemandAddMessageAddConnectedSSID (%@)" = "Connected: %@"; "tunnelOnDemandAddMessageAddConnectedSSID (%@)" = "Connected: %@";

View File

@ -17,8 +17,18 @@ class TextCell: UITableViewCell {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
func setTextColor(_ color: UIColor) {
textLabel?.textColor = color
}
func setTextAlignment(_ alignment: NSTextAlignment) {
textLabel?.textAlignment = alignment
}
override func prepareForReuse() { override func prepareForReuse() {
super.prepareForReuse() super.prepareForReuse()
message = "" message = ""
setTextColor(.black)
setTextAlignment(.left)
} }
} }

View File

@ -50,7 +50,7 @@ class SSIDOptionEditTableViewController: UITableViewController {
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
title = tr("tunnelOnDemandSelectionViewTitle") title = tr("tunnelOnDemandSSIDViewTitle")
tableView.estimatedRowHeight = 44 tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableView.automaticDimension tableView.rowHeight = UITableView.automaticDimension
@ -66,9 +66,7 @@ class SSIDOptionEditTableViewController: UITableViewController {
sections.removeAll() sections.removeAll()
sections.append(.ssidOption) sections.append(.ssidOption)
if selectedOption != .anySSID { if selectedOption != .anySSID {
if !selectedSSIDs.isEmpty { sections.append(.selectedSSIDs)
sections.append(.selectedSSIDs)
}
sections.append(.addSSIDs) sections.append(.addSSIDs)
} }
} }
@ -112,7 +110,7 @@ extension SSIDOptionEditTableViewController {
case .ssidOption: case .ssidOption:
return ssidOptionFields.count return ssidOptionFields.count
case .selectedSSIDs: case .selectedSSIDs:
return selectedSSIDs.count return selectedSSIDs.isEmpty ? 1 : selectedSSIDs.count
case .addSSIDs: case .addSSIDs:
return addSSIDRows.count return addSSIDRows.count
} }
@ -123,7 +121,11 @@ extension SSIDOptionEditTableViewController {
case .ssidOption: case .ssidOption:
return ssidOptionCell(for: tableView, at: indexPath) return ssidOptionCell(for: tableView, at: indexPath)
case .selectedSSIDs: case .selectedSSIDs:
return selectedSSIDCell(for: tableView, at: indexPath) if !selectedSSIDs.isEmpty {
return selectedSSIDCell(for: tableView, at: indexPath)
} else {
return noSSIDsCell(for: tableView, at: indexPath)
}
case .addSSIDs: case .addSSIDs:
return addSSIDCell(for: tableView, at: indexPath) return addSSIDCell(for: tableView, at: indexPath)
} }
@ -133,7 +135,9 @@ extension SSIDOptionEditTableViewController {
switch sections[indexPath.section] { switch sections[indexPath.section] {
case .ssidOption: case .ssidOption:
return false return false
case .selectedSSIDs, .addSSIDs: case .selectedSSIDs:
return !selectedSSIDs.isEmpty
case .addSSIDs:
return true return true
} }
} }
@ -169,6 +173,14 @@ extension SSIDOptionEditTableViewController {
return cell return cell
} }
private func noSSIDsCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
let cell: TextCell = tableView.dequeueReusableCell(for: indexPath)
cell.message = tr("tunnelOnDemandNoSSIDs")
cell.setTextColor(.gray)
cell.setTextAlignment(.center)
return cell
}
private func selectedSSIDCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell { private func selectedSSIDCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
let cell: EditableTextCell = tableView.dequeueReusableCell(for: indexPath) let cell: EditableTextCell = tableView.dequeueReusableCell(for: indexPath)
cell.message = selectedSSIDs[indexPath.row] cell.message = selectedSSIDs[indexPath.row]
@ -203,18 +215,15 @@ extension SSIDOptionEditTableViewController {
case .selectedSSIDs: case .selectedSSIDs:
assert(editingStyle == .delete) assert(editingStyle == .delete)
selectedSSIDs.remove(at: indexPath.row) selectedSSIDs.remove(at: indexPath.row)
loadSections() if !selectedSSIDs.isEmpty {
let hasSelectedSSIDsSection = sections.contains(.selectedSSIDs)
if hasSelectedSSIDsSection {
tableView.deleteRows(at: [indexPath], with: .automatic) tableView.deleteRows(at: [indexPath], with: .automatic)
} else { } else {
tableView.deleteSections(IndexSet(integer: indexPath.section), with: .automatic) tableView.reloadRows(at: [indexPath], with: .automatic)
} }
loadAddSSIDRows() loadAddSSIDRows()
updateTableViewAddSSIDRows() updateTableViewAddSSIDRows()
case .addSSIDs: case .addSSIDs:
assert(editingStyle == .insert) assert(editingStyle == .insert)
let hasSelectedSSIDsSection = sections.contains(.selectedSSIDs)
let newSSID: String let newSSID: String
switch addSSIDRows[indexPath.row] { switch addSSIDRows[indexPath.row] {
case .addConnectedSSID(let connectedSSID): case .addConnectedSSID(let connectedSSID):
@ -226,8 +235,8 @@ extension SSIDOptionEditTableViewController {
loadSections() loadSections()
let selectedSSIDsSection = sections.firstIndex(of: .selectedSSIDs)! let selectedSSIDsSection = sections.firstIndex(of: .selectedSSIDs)!
let indexPath = IndexPath(row: selectedSSIDs.count - 1, section: selectedSSIDsSection) let indexPath = IndexPath(row: selectedSSIDs.count - 1, section: selectedSSIDsSection)
if !hasSelectedSSIDsSection { if selectedSSIDs.count == 1 {
tableView.insertSections(IndexSet(integer: selectedSSIDsSection), with: .automatic) tableView.reloadRows(at: [indexPath], with: .automatic)
} else { } else {
tableView.insertRows(at: [indexPath], with: .automatic) tableView.insertRows(at: [indexPath], with: .automatic)
} }
@ -240,12 +249,6 @@ extension SSIDOptionEditTableViewController {
} }
} }
} }
func lastSelectedSSIDItemIndexPath() -> IndexPath? {
guard !selectedSSIDs.isEmpty else { return nil }
guard let section = sections.firstIndex(of: .selectedSSIDs) else { return nil }
return IndexPath(row: selectedSSIDs.count - 1, section: section)
}
} }
extension SSIDOptionEditTableViewController { extension SSIDOptionEditTableViewController {
@ -262,15 +265,14 @@ extension SSIDOptionEditTableViewController {
switch sections[indexPath.section] { switch sections[indexPath.section] {
case .ssidOption: case .ssidOption:
let previousOption = selectedOption let previousOption = selectedOption
let previousSectionCount = sections.count
selectedOption = ssidOptionFields[indexPath.row] selectedOption = ssidOptionFields[indexPath.row]
loadSections() loadSections()
if previousOption == .anySSID { if previousOption == .anySSID {
let indexSet = selectedSSIDs.isEmpty ? IndexSet(integer: 1) : IndexSet(1 ... 2) let indexSet = IndexSet(1 ... 2)
tableView.insertSections(indexSet, with: .fade) tableView.insertSections(indexSet, with: .fade)
} }
if selectedOption == .anySSID { if selectedOption == .anySSID {
let indexSet = previousSectionCount == 2 ? IndexSet(integer: 1) : IndexSet(1 ... 2) let indexSet = IndexSet(1 ... 2)
tableView.deleteSections(indexSet, with: .fade) tableView.deleteSections(indexSet, with: .fade)
} }
tableView.reloadSections(IndexSet(integer: indexPath.section), with: .none) tableView.reloadSections(IndexSet(integer: indexPath.section), with: .none)