Commit 93c89d2b authored by Jose Blaya's avatar Jose Blaya
Browse files

Add Search Bar in RegionsViewController to filter the server list

parent 5fac29eb
{
"images" : [
{
"idiom" : "universal",
"filename" : "iconRedHeart6Copy2.pdf"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
......@@ -49,6 +49,7 @@ class RegionCell: UITableViewCell, Restylable {
}
accessibilityIdentifier = "uitests.regions.region_name"
self.favoriteImageView.image = self.favoriteImageView.image?.withRenderingMode(.alwaysTemplate)
self.favoriteImageView.alpha = CGFloat(NSNumber(booleanLiteral: server.name != L10n.Global.automatic).floatValue)
self.favoriteButton.alpha = CGFloat(NSNumber(booleanLiteral: server.name != L10n.Global.automatic).floatValue)
......@@ -65,8 +66,13 @@ class RegionCell: UITableViewCell, Restylable {
self.backgroundColor = Theme.current.palette.lightBackground
self.contentView.backgroundColor = Theme.current.palette.lightBackground
Theme.current.applyList(labelRegion, appearance: .dark)
Theme.current.applySettingsCellTitle(labelRegion, appearance: .dark)
Theme.current.applyTag(labelPingTime, appearance: .dark)
Theme.current.applyFavoriteUnselectedImage(self.favoriteImageView)
if Theme.current.palette.appearance! == .dark {
self.favoriteImageView.tintColor = UIColor.piaGrey10
}
}
......@@ -87,8 +93,8 @@ class RegionCell: UITableViewCell, Restylable {
}
private func updateFavoriteImage() {
self.favoriteImageView.image = self.isFavorite ?
Asset.Piax.Global.favoriteSelected.image :
Asset.Piax.Global.favoriteUnselected.image
self.isFavorite ?
self.favoriteImageView.image = Asset.Piax.Global.favoriteSelected.image :
Theme.current.applyFavoriteUnselectedImage(self.favoriteImageView)
}
}
......@@ -17,9 +17,11 @@ class RegionsViewController: AutolayoutViewController {
@IBOutlet private weak var tableView: UITableView!
private var servers: [Server] = []
private var filteredServers = [Server]()
private var selectedServer: Server!
let searchController = UISearchController(searchResultsController: nil)
deinit {
NotificationCenter.default.removeObserver(self)
}
......@@ -37,6 +39,17 @@ class RegionsViewController: AutolayoutViewController {
NotificationCenter.default.addObserver(self, selector: #selector(pingsDidComplete(notification:)), name: .PIADaemonsDidPingServers, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(viewHasRotated), name: .UIDeviceOrientationDidChange, object: nil)
setupSearchBarController()
}
private func setupSearchBarController() {
searchController.searchResultsUpdater = self
if #available(iOS 9.1, *) {
searchController.obscuresBackgroundDuringPresentation = false
}
searchController.searchBar.placeholder = L10n.Region.Search.placeholder
self.tableView.tableHeaderView = self.searchController.searchBar
definesPresentationContext = true
}
override func viewWillAppear(_ animated: Bool) {
......@@ -86,7 +99,14 @@ class RegionsViewController: AutolayoutViewController {
guard let indexPath = tableView.indexPath(for: sender as! UITableViewCell) else {
fatalError("Segue triggered without an input cell?")
}
let newSelectedServer = servers[indexPath.row]
let newSelectedServer: Server
if isFiltering() {
newSelectedServer = filteredServers[indexPath.row]
} else {
newSelectedServer = servers[indexPath.row]
}
selectedServer = newSelectedServer
tableView.reloadData()
......@@ -114,14 +134,26 @@ class RegionsViewController: AutolayoutViewController {
Theme.current.applyLightBackground(view)
Theme.current.applyLightBackground(viewContainer)
}
searchController.view.backgroundColor = .clear
Theme.current.applyLightBackground(tableView)
Theme.current.applyDividerToSeparator(tableView)
Theme.current.applySearchBarStyle(searchController.searchBar)
let bgView = UIView()
bgView.backgroundColor = .clear
self.tableView.backgroundView = bgView
tableView.reloadData()
}
}
extension RegionsViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isFiltering() {
return filteredServers.count
}
return servers.count
}
......@@ -129,10 +161,44 @@ extension RegionsViewController: UITableViewDataSource, UITableViewDelegate {
let cell = tableView.dequeueReusableCell(withIdentifier: Cells.region, for: indexPath) as! RegionCell
cell.selectionStyle = .none
cell.separatorInset = .zero
let server = servers[indexPath.row]
let server: Server
if isFiltering() {
server = filteredServers[indexPath.row]
} else {
server = servers[indexPath.row]
}
let isSelected = (server.identifier == selectedServer.identifier)
cell.fill(withServer: server, isSelected: isSelected)
return cell
}
}
extension RegionsViewController: UISearchResultsUpdating {
// MARK: - UISearchResultsUpdating Delegate
func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchController.searchBar.text!)
}
// MARK: - Private instance methods
func searchBarIsEmpty() -> Bool {
// Returns true if the text is empty or nil
return searchController.searchBar.text?.isEmpty ?? true
}
func filterContentForSearchText(_ searchText: String, scope: String = "All") {
filteredServers = servers.filter({( server : Server) -> Bool in
return server.name.lowercased().contains(searchText.lowercased())
})
tableView.reloadData()
}
func isFiltering() -> Bool {
return searchController.isActive && !searchBarIsEmpty()
}
}
......@@ -50,6 +50,7 @@ enum Asset {
}
enum Global {
static let favoriteSelected = ImageAsset(name: "favorite-selected")
static let favoriteUnselectedDark = ImageAsset(name: "favorite-unselected-dark")
static let favoriteUnselected = ImageAsset(name: "favorite-unselected")
static let iconBack = ImageAsset(name: "icon-back")
static let pagecontrolSelectedDot = ImageAsset(name: "pagecontrol-selected-dot")
......@@ -323,6 +324,7 @@ enum Asset {
static let allImages: [ImageAsset] = [
Piax.Dashboard.vpnButton,
Piax.Global.favoriteSelected,
Piax.Global.favoriteUnselectedDark,
Piax.Global.favoriteUnselected,
Piax.Global.iconBack,
Piax.Global.pagecontrolSelectedDot,
......
......@@ -278,6 +278,13 @@ internal enum L10n {
}
}
internal enum Region {
internal enum Search {
/// Search for a region
internal static let placeholder = L10n.tr("Localizable", "region.search.placeholder")
}
}
internal enum Renewal {
internal enum Failure {
/// Your purchase receipt couldn't be submitted, please retry at a later time.
......
......@@ -47,13 +47,13 @@ extension Theme {
func applyPingTime(_ label: UILabel, time: Int) {
switch AppConfiguration.ServerPing.from(value: time) {
case .low:
label.textColor = palette.emphasis
label.textColor = UIColor.piaGreenDark20
case .medium:
label.textColor = palette.accent1
label.textColor = UIColor.piaOrange
case .high:
label.textColor = palette.accent2
label.textColor = UIColor.piaRed
}
}
......@@ -109,5 +109,49 @@ extension Theme {
label.style(style: palette.appearance == .dark ?
TextStyle.textStyle6 : TextStyle.textStyle7)
}
//MARK: SearchBar
public func applySearchBarStyle(_ searchBar: UISearchBar) {
searchBar.backgroundColor = .clear
let textFieldInsideSearchBar = searchBar.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.backgroundColor = .clear
let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as? UILabel
textFieldInsideSearchBarLabel?.backgroundColor = .clear
let glassIconView = textFieldInsideSearchBar?.leftView as? UIImageView
glassIconView?.tintColor = UIColor.piaGrey4
if palette.appearance == .dark {
//text
textFieldInsideSearchBar?.style(style: TextStyle.textStyle6)
//placeholder
textFieldInsideSearchBarLabel?.style(style: TextStyle.textStyle8)
searchBar.barTintColor = UIColor.piaGrey10
} else {
//text
textFieldInsideSearchBar?.style(style: TextStyle.textStyle7)
//placeholder
textFieldInsideSearchBarLabel?.style(style: TextStyle.textStyle8)
searchBar.barTintColor = UIColor.white
}
//Cancel button
let attributes:[NSAttributedStringKey:Any] = [
NSAttributedStringKey.foregroundColor : TextStyle.textStyle8.color!,
NSAttributedStringKey.font : TextStyle.textStyle8.font!
]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
}
public func applyFavoriteUnselectedImage(_ imageView: UIImageView) {
if palette.appearance == .dark {
imageView.image = Asset.Piax.Global.favoriteUnselectedDark.image
} else {
imageView.image = Asset.Piax.Global.favoriteUnselected.image
}
}
}
......@@ -198,3 +198,7 @@
"settings.dns.validation.primary.mandatory" = "Primary DNS is mandatory.";
"settings.dns.validation.primary.invalid" = "Primary DNS is not valid.";
"settings.dns.validation.secondary.invalid" = "Secondary DNS is not valid.";
// REGION
"region.search.placeholder" = "Search for a region";
......@@ -23,7 +23,7 @@
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" rowHeight="50" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="Kiz-8k-1hw">
<rect key="frame" x="8" y="8" width="359" height="716"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<color key="separatorColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<inset key="separatorInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>
<prototypes>
......@@ -109,6 +109,7 @@
<constraint firstAttribute="trailing" secondItem="rwa-6o-BR8" secondAttribute="trailing" id="mxX-av-P3f"/>
</constraints>
</tableViewCellContentView>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<connections>
<outlet property="favoriteButton" destination="9JG-G1-vW4" id="BvM-Gs-qOP"/>
<outlet property="favoriteImageView" destination="Tt7-8W-weg" id="Twm-Ls-JFQ"/>
......@@ -126,7 +127,7 @@
</connections>
</tableView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="trailingMargin" secondItem="Kiz-8k-1hw" secondAttribute="trailing" id="EIE-xz-cx1"/>
<constraint firstAttribute="bottom" secondItem="Kiz-8k-1hw" secondAttribute="bottom" id="e0d-c6-dIT"/>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment