Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #20 from justeat/feature/adb/search-bar
Browse files Browse the repository at this point in the history
Add support to filter tweaks in TweaksConfigurationViewController
  • Loading branch information
albertodebortoli authored Apr 8, 2019
2 parents d782348 + 5a4ac9d commit 30197e1
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 38 deletions.
4 changes: 2 additions & 2 deletions Example/JustTweak.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -609,7 +609,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BuildSystemType</key>
<string>Original</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion Example/Podfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://github.com/CocoaPods/Specs.git'

platform :ios, '9.0'
platform :ios, '10.0'
use_frameworks!

target 'JustTweak_Example' do
Expand Down
14 changes: 7 additions & 7 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ PODS:
- GoogleUtilities/Logger
- GoogleUtilities/UserDefaults (5.3.6):
- GoogleUtilities/Logger
- JustTweak (3.2.0):
- JustTweak/Core (= 3.2.0)
- JustTweak/UI (= 3.2.0)
- JustTweak/Core (3.2.0)
- JustTweak/UI (3.2.0):
- JustTweak (3.3.0):
- JustTweak/Core (= 3.3.0)
- JustTweak/UI (= 3.3.0)
- JustTweak/Core (3.3.0)
- JustTweak/UI (3.3.0):
- JustTweak/Core
- nanopb (0.3.901):
- nanopb/decode (= 0.3.901)
Expand Down Expand Up @@ -120,7 +120,7 @@ SPEC CHECKSUMS:
FirebaseRemoteConfig: 7e11c65f0769c09bff6947997c209515058c5318
GoogleAppMeasurement: 98b71f5e04142793729a5ef23e5b96651ff4b70f
GoogleUtilities: 95996bea7c7d9b8fb811b7507669a4a8762f80c7
JustTweak: 2f6a9f5cf084cb7027f6a32b5f59751140fc52e8
JustTweak: c5ee966f54c0c00b14d6225c3f5714ad6a75a067
nanopb: 2901f78ea1b7b4015c860c2fdd1ea2fee1a18d48
OptimizelySDKCore: cff8a662f868372ecedcd6be9ce823b546535a16
OptimizelySDKDatafileManager: 805457da07957741c9199ae4e15e671bf9f6edb0
Expand All @@ -130,6 +130,6 @@ SPEC CHECKSUMS:
OptimizelySDKUserProfileService: 7cffcfbcdb104a864f7961b7a08e0a13de4540a4
Protobuf: 1eb9700044745f00181c136ef21b8ff3ad5a0fd5

PODFILE CHECKSUM: 699b095b8b7f3b37cd7fb3b4f31ba8cb862688e1
PODFILE CHECKSUM: bc8e9826fb88612e28170bb505bed61edc07bc72

COCOAPODS: 1.5.3
4 changes: 2 additions & 2 deletions JustTweak.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Pod::Spec.new do |s|
s.name = 'JustTweak'
s.version = '3.2.0'
s.version = '3.3.0'
s.summary = 'A framework for feature flagging, locally and remotely configure and A/B test iOS apps.'
s.description = <<-DESC
JustTweak is a framework for feature flagging, locally and remotely configure and A/B test iOS apps.
Expand All @@ -12,7 +12,7 @@ JustTweak is a framework for feature flagging, locally and remotely configure an
s.authors = { 'Just Eat iOS team' => '[email protected]', 'Gianluca Tranchedone' => '[email protected]' }
s.source = { :git => 'https://github.com/justeat/JustTweak.git', :tag => s.version.to_s }

s.ios.deployment_target = '9.0'
s.ios.deployment_target = '10.0'
s.swift_version = '4.2'

s.default_subspecs = 'Core', 'UI'
Expand Down
1 change: 1 addition & 0 deletions JustTweak/Assets/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
"just_tweak_unnamed_tweaks_group_title" = "Other";
"just_tweak_configurations_vc_no_configurations_message" = "No Mutable Configurations Found";
"just_tweak_search_bar_placeholder_text" = "Search Tweaks";
102 changes: 76 additions & 26 deletions JustTweak/Classes/UI/TweaksConfigurationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ internal protocol TweaksConfigurationViewControllerCellDelegate: class {

public class TweaksConfigurationViewController: UITableViewController {

fileprivate struct Section {
let title: String
let tweaks: [Tweak]
}

fileprivate class Tweak {
var feature: String
var variable: String
Expand All @@ -38,11 +43,8 @@ public class TweaksConfigurationViewController: UITableViewController {
case ToogleCell, TextCell, NumberCell
}

private enum SectionGroupKeys: String {
case Title, Items
}

private var sections: [[String : Any]]?
private var sections = [Section]()
private var filteredSections = [Section]()
public var configurationsCoordinator: TweaksConfigurationsCoordinator? {
didSet {
rebuildSections()
Expand All @@ -63,15 +65,13 @@ public class TweaksConfigurationViewController: UITableViewController {
comment: "")
}()

// MARK: Convenience Initializers
private let searchController = UISearchController(searchResultsController: nil)

public convenience init(style: UITableView.Style, configurationsCoordinator: TweaksConfigurationsCoordinator) {
self.init(style: style)
self.configurationsCoordinator = configurationsCoordinator
}

// MARK: View Lifecycle

public override func viewDidLoad() {
super.viewDidLoad()
let backgroundView = TweaksErrorView()
Expand All @@ -82,15 +82,17 @@ public class TweaksConfigurationViewController: UITableViewController {
tableView.backgroundView = backgroundView
tableView.keyboardDismissMode = .onDrag
updateBackgroundView()
setUpBarButtonItems()
setupBarButtonItems()
setupSearchController()
registerCellClasses()
rebuildSections()
}

// MARK: UITableViewDataSource

}

extension TweaksConfigurationViewController {

public override func numberOfSections(in tableView: UITableView) -> Int {
return sections?.count ?? 0
return isFiltering() ? filteredSections.count : sections.count
}

public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
Expand All @@ -114,8 +116,6 @@ public class TweaksConfigurationViewController: UITableViewController {
return titleForHeaderForSection(section)
}

// MARK: Convenience

public func indexPathForTweak(with feature: String, variable: String) -> IndexPath? {
for section in 0 ..< numberOfSections(in: tableView) {
for (row, tweak) in tweaksIn(section: section).enumerated() {
Expand All @@ -127,8 +127,6 @@ public class TweaksConfigurationViewController: UITableViewController {
return nil
}

// MARK: Helpers

private func cellIdentifierForTweak(_ tweak: Tweak) -> String {
if let _ = tweak.value as? Bool {
return CellIdentifiers.ToogleCell.rawValue
Expand All @@ -140,26 +138,44 @@ public class TweaksConfigurationViewController: UITableViewController {
}

private func titleForHeaderForSection(_ section: Int) -> String? {
let thisSection = sections?[section]
return thisSection?[SectionGroupKeys.Title.rawValue] as? String
let thisSection: Section = isFiltering() ? filteredSections[section] : sections[section]
return thisSection.title
}

private func tweaksIn(section: Int) -> [Tweak] {
let thisSection = sections?[section]
return thisSection?[SectionGroupKeys.Items.rawValue] as! [Tweak]
let thisSection: Section = isFiltering() ? filteredSections[section] : sections[section]
return thisSection.tweaks
}

fileprivate func tweakAt(indexPath: IndexPath) -> Tweak? {
let tweaks = tweaksIn(section: (indexPath as NSIndexPath).section)
return tweaks[(indexPath as NSIndexPath).row]
}
}

extension TweaksConfigurationViewController {

private func setUpBarButtonItems() {
private func setupBarButtonItems() {
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done,
target: self,
action: #selector(dismissViewController))
}

private func setupSearchController() {
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = NSLocalizedString("just_tweak_search_bar_placeholder_text",
bundle: TweaksConfigurationViewController.justTweakResourcesBundle(),
comment: "")
searchController.searchBar.sizeToFit()

tableView.tableHeaderView = searchController.searchBar
// on iOS 11 it would be possible to add the searchController to the navigationItem instead of adding it as tableHeaderView
// navigationItem.searchController = searchController
definesPresentationContext = true
}

private func registerCellClasses() {
tableView.register(BooleanTweakTableViewCell.self,
forCellReuseIdentifier: CellIdentifiers.ToogleCell.rawValue)
Expand All @@ -170,7 +186,7 @@ public class TweaksConfigurationViewController: UITableViewController {
}

private func rebuildSections() {
var allSections = [[String : Any]]()
var allSections = [Section]()
if let configurationsCoordinator = configurationsCoordinator {
let allTweaks = configurationsCoordinator.displayableTweaks().sorted(by: { (lhs, rhs) -> Bool in
return lhs.displayTitle < rhs.displayTitle
Expand All @@ -193,14 +209,13 @@ public class TweaksConfigurationViewController: UITableViewController {
}
}
if items.count > 0 {
let section: [String : Any] = [SectionGroupKeys.Items.rawValue: items,
SectionGroupKeys.Title.rawValue: group]
let section = Section(title: group, tweaks: items)
allSections.append(section)
}
}
}
sections = allSections.sorted { (lhs, rhs) -> Bool in
return (lhs[SectionGroupKeys.Title.rawValue] as! String) < (rhs[SectionGroupKeys.Title.rawValue] as! String)
return lhs.title < rhs.title
}

tableView.reloadData()
Expand Down Expand Up @@ -230,3 +245,38 @@ extension TweaksConfigurationViewController: TweaksConfigurationViewControllerCe
}
}
}

extension TweaksConfigurationViewController: UISearchResultsUpdating {

public func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchController.searchBar.text!)
}
}

extension TweaksConfigurationViewController {

func searchBarIsEmpty() -> Bool {
return searchController.searchBar.text?.isEmpty ?? true
}

func filterContentForSearchText(_ searchText: String) {
filteredSections = [Section]()
for section in sections {
var filteredTweaks = [Tweak]()
for tweak in section.tweaks {
if let title = tweak.title, title.lowercased().contains(searchText.lowercased()) {
filteredTweaks.append(tweak)
}
}
if filteredTweaks.count > 0 {
let filteredSection = Section(title: section.title, tweaks: filteredTweaks)
filteredSections.append(filteredSection)
}
}
tableView.reloadData()
}

func isFiltering() -> Bool {
return searchController.isActive && !searchBarIsEmpty()
}
}

0 comments on commit 30197e1

Please sign in to comment.