Skip to content

Commit

Permalink
Add Search (#11)
Browse files Browse the repository at this point in the history
* initial commit

* reuse logic

* update changelog

* update readme

* improve footer logic
  • Loading branch information
wendyliga committed Aug 29, 2023
1 parent 0b22a27 commit c29c4bc
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 49 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@


# 1.2.0
- Add Search functionality ([#9](https://github.com/tokopedia/ios-tptweak/pull/11))
- Fix wrong detailText on cell
- disable switch reloadRows animation

# 1.1.0
- Passing closure for switch type ([#9](https://github.com/tokopedia/ios-tptweak/pull/9))

Expand Down
4 changes: 2 additions & 2 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@
INFOPLIST_KEY_UISupportedInterfaceOrientations = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UIInterfaceOrientationPortraitUpsideDown";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -317,7 +317,7 @@
INFOPLIST_KEY_UISupportedInterfaceOrientations = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UIInterfaceOrientationPortraitUpsideDown";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
TPTweak is a debugging tool to help adjust your iOS app on the fly without recompile.
inspired by facebook's [Tweak](https://github.com/facebookarchive/Tweaks), TPTweak is fully written in swift, and with simpler API.

|Example of TPTweak|Selecting options of string|Selecting options of number|
|----|----|----|
|![](assets/tptweak_home.png)|![](assets/tptweak_string_selection_example.png)|![](assets/tptweak_number_selection_example.png)|
|Example of TPTweak|Selecting options of string|Selecting options of number|Search|
|----|----|----|----|
|![](assets/tptweak_home.png)|![](assets/tptweak_string_selection_example.png)|![](assets/tptweak_number_selection_example.png)|![Simulator Screenshot - iPhone 14 Pro - 2023-08-29 at 16 57 39](https://github.com/tokopedia/ios-tptweak/assets/16457495/02dbd7d6-0fa3-4a44-85b0-dfc52501b3b6)|

# Installation
## Swift Package Manager
Expand Down
86 changes: 75 additions & 11 deletions Sources/TPTweak/TPTweakPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,39 @@ import UIKit
*/
internal final class TPTweakPickerViewController: UIViewController {
// MARK: - Values

private var data: [Section] = []
private var searchKeyword: String? = nil
private var _data: [Section] = []
private var data: [Section] {
get {
// if keyword does not exist, use full data
guard let searchKeyword = searchKeyword, searchKeyword != "" else {
return _data
}

// filter section based on if cell name contain keyword or not
var filteredData = [Section]()

for section in _data {
let newCells = section.cells.filter { $0.name.lowercased().contains(searchKeyword) }

// skip if this section's cell does not have any matching cell
if newCells.isEmpty { continue }

let newSection = Section(
name: section.name,
footer: newCells.last(where: { $0.footer != nil })?.footer, // use footer from last cell in newCells that contan any footer.
cells: newCells
)
filteredData.append(newSection)
}

return filteredData
}

set {
_data = newValue
}
}

// MARK: - Views

Expand All @@ -46,9 +77,9 @@ internal final class TPTweakPickerViewController: UIViewController {
// MARK: - Life Cycle

internal init(data: [Section]) {
self.data = data
super.init(nibName: nil, bundle: nil)


self.data = data
title = "TPTweaks"
view.backgroundColor = .white

Expand Down Expand Up @@ -85,6 +116,28 @@ internal final class TPTweakPickerViewController: UIViewController {
table.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}

internal func setData(data: [Section]) {
self.data = data
self.table.reloadData()
}

private func openDetail(viewController: UIViewController) {
if searchKeyword != nil, searchKeyword != "" {
let navigationController = UINavigationController(rootViewController: viewController)
self.present(navigationController, animated: true)
} else {
navigationController?.pushViewController(viewController, animated: true)
}
}

private func closeDetail(viewController: UIViewController) {
if searchKeyword != nil, searchKeyword != "" {
viewController.dismiss(animated: true)
} else {
navigationController?.popToViewController(self, animated: true)
}
}
}

extension TPTweakPickerViewController: UITableViewDataSource, UITableViewDelegate {
Expand All @@ -106,13 +159,15 @@ extension TPTweakPickerViewController: UITableViewDataSource, UITableViewDelegat
switch cellData.type {
case .action:
cell.textLabel?.text = cellData.name
cell.detailTextLabel?.text = nil
cell.accessoryType = .disclosureIndicator
case .switch:
let switcher = UISwitch()
switcher.isOn = TPTweakStore.read(type: Bool.self, identifier: cellData.identifer) ?? false
switcher.isUserInteractionEnabled = false

cell.textLabel?.text = cellData.name
cell.detailTextLabel?.text = nil
cell.accessoryView = switcher
case let .strings(_, defaultValue):
let currentValue = TPTweakStore.read(type: String.self, identifier: cellData.identifer) ?? defaultValue
Expand Down Expand Up @@ -157,7 +212,7 @@ extension TPTweakPickerViewController: UITableViewDataSource, UITableViewDelegat
value.toggle()

TPTweakStore.set(value, identifier: cellData.identifer)
tableView.reloadRows(at: [indexPath], with: .automatic) // to update cell value after action
tableView.reloadRows(at: [indexPath], with: .none) // to update cell value after action
closure?(value)
case let .numbers(item, defaultValue):
let viewController = TPTweakOptionsViewController(
Expand All @@ -166,37 +221,45 @@ extension TPTweakPickerViewController: UITableViewDataSource, UITableViewDelegat
defaultSelected: TPTweakStore.read(type: Double.self, identifier: cellData.identifer) ?? defaultValue
)

viewController.didChoose = { [weak tableView, weak navigationController, weak self] newValue in
viewController.didChoose = { [weak tableView, weak self] newValue in
TPTweakStore.set(newValue, identifier: cellData.identifer)
tableView?.reloadRows(at: [indexPath], with: .automatic) // to update cell value after action

if let self = self {
navigationController?.popToViewController(self, animated: true) // back to picker
self.closeDetail(viewController: viewController) // back to picker
}
}

navigationController?.pushViewController(viewController, animated: true)
openDetail(viewController: viewController)
case let .strings(item, defaultValue):
let viewController = TPTweakOptionsViewController(
title: cellData.name,
data: item.map { TPTweakOptionsViewController<String>.Cell(name: $0, value: $0) },
defaultSelected: TPTweakStore.read(type: String.self, identifier: cellData.identifer) ?? defaultValue
)

viewController.didChoose = { [weak tableView, weak navigationController, weak self] newValue in
viewController.didChoose = { [weak tableView, weak self] newValue in
TPTweakStore.set(newValue, identifier: cellData.identifer)
tableView?.reloadRows(at: [indexPath], with: .automatic) // to update cell value after action

if let self = self {
navigationController?.popToViewController(self, animated: true) // back to picker
self.closeDetail(viewController: viewController) // back to picker
}
}

navigationController?.pushViewController(viewController, animated: true)
openDetail(viewController: viewController)
}
}
}

extension TPTweakPickerViewController: UISearchResultsUpdating {
public func updateSearchResults(for searchController: UISearchController) {
searchKeyword = searchController.searchBar.text?.lowercased()
table.reloadData()
}
}


extension TPTweakPickerViewController {
internal struct Section {
internal let name: String
Expand All @@ -208,6 +271,7 @@ extension TPTweakPickerViewController {
internal let name: String
internal let identifer: String
internal let type: TPTweakEntryType
internal let footer: String?
}
}
#endif
Loading

0 comments on commit c29c4bc

Please sign in to comment.