Skip to content

Commit

Permalink
Add Favourite Page (#13)
Browse files Browse the repository at this point in the history
* set fav ability

* add favourite page

* add changelog

* update
  • Loading branch information
wendyliga authored Aug 29, 2023
1 parent 5707c6c commit 2a62fe6
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 6 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +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
- Add Search functionality ([#11](https://github.com/tokopedia/ios-tptweak/pull/11))
- Fix wrong detailText on cell ([#11](https://github.com/tokopedia/ios-tptweak/pull/11))
- disable switch reloadRows animation ([#11](https://github.com/tokopedia/ios-tptweak/pull/11))
- Add Favourite functionality ([#13](https://github.com/tokopedia/ios-tptweak/pull/13))

# 1.1.0
- Passing closure for switch type ([#9](https://github.com/tokopedia/ios-tptweak/pull/9))
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ inspired by facebook's [Tweak](https://github.com/facebookarchive/Tweaks), TPTwe
|----|----|----|----|
|![](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)|


|Set Favourite by swipe|Set Favourite by long press|Favourite Page|
|----|----|----|
|![Simulator Screenshot - iPhone 14 Pro - 2023-08-29 at 19 07 10](https://github.com/tokopedia/ios-tptweak/assets/16457495/1580305b-1667-4005-ab61-ff03ff419bd8)|![Simulator Screenshot - iPhone 14 Pro - 2023-08-29 at 19 16 06](https://github.com/tokopedia/ios-tptweak/assets/16457495/a8be171b-fcf7-4923-9c2f-bca646f39919)|![Simulator Screenshot - iPhone 14 Pro - 2023-08-29 at 19 07 04](https://github.com/tokopedia/ios-tptweak/assets/16457495/5562f00e-22e7-4ab3-8a6d-237dbcfa596c)|

# Installation
## Swift Package Manager
With xcode, add this URL
Expand Down
6 changes: 6 additions & 0 deletions Sources/TPTweak/TPTweakEntry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,9 @@ public struct TPTweakEntry {
TPTweakStore.add(self)
}
}

extension TPTweakEntry {
internal static var favourite: TPTweakEntry {
TPTweakEntry(category: "tptweak", section: "internal", cell: "favourite", footer: nil, type: .action({}))
}
}
97 changes: 97 additions & 0 deletions Sources/TPTweak/TPTweakPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,77 @@ internal final class TPTweakPickerViewController: UIViewController {
navigationController?.popToViewController(self, animated: true)
}
}

internal static func isFavourite(identifier: String) -> Bool {
let favourites = TPTweakEntry.favourite.getValue(Set<String>.self) ?? []
return favourites.contains(where: { $0 == identifier })
}

private func setFavourite(identifier: String) {
var favourites = TPTweakEntry.favourite.getValue(Set<String>.self) ?? []
favourites.insert(identifier)

TPTweakEntry.favourite.setValue(favourites)
}

private func removeFavourite(identifier: String) {
var favourites = TPTweakEntry.favourite.getValue(Set<String>.self) ?? []
favourites.remove(identifier)

TPTweakEntry.favourite.setValue(favourites)
}

private func createFavouriteSwipeButton(identifier: String) -> UIContextualAction {
if Self.isFavourite(identifier: identifier) {
let action = UIContextualAction(style: .normal, title: "Remove Favourite") { [weak self] _, _, success in
self?.removeFavourite(identifier: identifier)
success(true)
}

if #available(iOS 13.0, *) {
action.image = UIImage(systemName: "star.slash")
}

action.backgroundColor = .systemRed

return action
} else {
let action = UIContextualAction(style: .normal, title: "Favourite") { [weak self] _, _, success in
self?.setFavourite(identifier: identifier)
success(true)
}

if #available(iOS 13.0, *) {
action.image = UIImage(systemName: "star")
}

action.backgroundColor = .systemBlue

return action
}
}

@available(iOS 13.0, *)
private func createContextualMenu(identifier: String) -> UIAction {
if Self.isFavourite(identifier: identifier) {
return UIAction(
title: "Unfavourite",
image: UIImage(systemName: "star.slash"),
identifier: nil,
attributes: .destructive
) { [weak self] _ in
self?.removeFavourite(identifier: identifier)
}
} else {
return UIAction(
title: "Favourite",
image: UIImage(systemName: "star"),
identifier: nil
) { [weak self] _ in
self?.setFavourite(identifier: identifier)
}
}
}
}

extension TPTweakPickerViewController: UITableViewDataSource, UITableViewDelegate {
Expand Down Expand Up @@ -250,6 +321,32 @@ extension TPTweakPickerViewController: UITableViewDataSource, UITableViewDelegat
openDetail(viewController: viewController)
}
}

internal func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

let cellData = data[indexPath.section].cells[indexPath.row]
let action = createFavouriteSwipeButton(identifier: cellData.identifer)
return UISwipeActionsConfiguration(actions: [action])
}

internal func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let cellData = data[indexPath.section].cells[indexPath.row]
let action = createFavouriteSwipeButton(identifier: cellData.identifer)
return UISwipeActionsConfiguration(actions: [action])
}

@available(iOS 13.0, *)
func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
let cellData = data[indexPath.section].cells[indexPath.row]

return UIContextMenuConfiguration(
identifier: nil,
previewProvider: nil,
actionProvider: { _ in
return UIMenu(title: "", children: [self.createContextualMenu(identifier: cellData.identifer)])
}
)
}
}

extension TPTweakPickerViewController: UISearchResultsUpdating {
Expand Down
28 changes: 26 additions & 2 deletions Sources/TPTweak/TPTweakViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ public final class TPTweakViewController: UIViewController {

private lazy var doneBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismissSelf))
private lazy var resetBarButtonItem = UIBarButtonItem(title: "Reset", style: .plain, target: self, action: #selector(resetAll))
private lazy var favouriteBarButtonItem: UIBarButtonItem = {
if #available(iOS 13.0, *) {
return UIBarButtonItem(image: UIImage(systemName: "star.fill"), style: .plain, target: self, action: #selector(openFavourite))
} else {
return UIBarButtonItem(title: "Favourite", style: .plain , target: self, action: #selector(openFavourite))
}
}()

// MARK: - Life Cycle

Expand All @@ -118,7 +125,7 @@ public final class TPTweakViewController: UIViewController {
table.reloadData()

navigationItem.leftBarButtonItem = doneBarButtonItem
navigationItem.rightBarButtonItem = resetBarButtonItem
navigationItem.rightBarButtonItems = [resetBarButtonItem, favouriteBarButtonItem]

navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
Expand Down Expand Up @@ -220,7 +227,7 @@ public final class TPTweakViewController: UIViewController {
return alert
}

let confirmationDialog = UIAlertController(title: "Are you Sure", message: nil, preferredStyle: .alert)
let confirmationDialog = UIAlertController(title: "Are you Sure", message: "This action will clear all custom tweaks value, and revert it all back to default value.", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
let confirmAction = UIAlertAction(title: "Confirm", style: .destructive) { _ in
let loading = showLoading()
Expand All @@ -236,6 +243,19 @@ public final class TPTweakViewController: UIViewController {
present(confirmationDialog, animated: true)
}

@objc
private func openFavourite() {
var favouriteEntries = [TPTweakEntry]()
for row in data {
favouriteEntries += row.entries.filter { TPTweakPickerViewController.isFavourite(identifier: $0.getIdentifier()) }
}

let data = convertRowToSection(row: Row(name: "", entries: favouriteEntries))
let favouriteViewController = TPTweakPickerViewController(data: data)
favouriteViewController.title = "Favourites"
self.navigationController?.pushViewController(favouriteViewController, animated: true)
}

@objc
private func dismissKeyboard() {
searchController.searchBar.endEditing(true)
Expand Down Expand Up @@ -268,6 +288,10 @@ extension TPTweakViewController: UITableViewDataSource, UITableViewDelegate {
viewController.title = cell.name
navigationController?.pushViewController(viewController, animated: true)
}

public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Tweaks"
}
}


Expand Down
2 changes: 1 addition & 1 deletion TPTweak.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = "TPTweak"
spec.version = "1.1.0"
spec.version = "1.2.0"
spec.summary = "TPTweak is a debugging tool to help adjust your iOS app on the fly without recompile"

spec.license = { :type => "Apache 2.0", :file => "LICENSE.md" }
Expand Down

0 comments on commit 2a62fe6

Please sign in to comment.