Skip to content

Commit

Permalink
Passing closure for switch type (#9)
Browse files Browse the repository at this point in the history
* add closure for switch type that being triggered after its value is toggled

* add feature to passing closure on  type

* revert example ios target
  • Loading branch information
wowodiergo committed Aug 9, 2023
1 parent 68f35b2 commit 784c0a2
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions Example/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal final class AppDelegate: UIResponder, UIApplicationDelegate {
TPTweakEntry.trackingTimeout.register()
TPTweakEntry.trackingHistory.register()
TPTweakEntry.trackingServerLocation.register()
TPTweakEntry.trackingUsingLocale.register()
TPTweakEntry.changeLanguage.register()

let viewController = ViewController()
Expand Down
14 changes: 14 additions & 0 deletions Example/Example/TPTweakEntry+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ extension TPTweakEntry {
type: .strings(item: ["US", "UK", "SG"], selected: "SG")
)

static let trackingUsingLocale = TPTweakEntry(
category: "Tracking",
section: "Locale",
cell: "Using Locale",
footer: "Enabled this to let the tracker send data about your locale",
type: .switch(defaultValue: false, closure: { isUsingLocale in
if isUsingLocale {
UserDefaults.standard.set(Locale.current.identifier, forKey: "tracker_locale")
} else {
UserDefaults.standard.removeObject(forKey: "tracker_locale")
}
})
)

static let changeLanguage = TPTweakEntry(
category: "Apperance",
section: "Language",
Expand Down
3 changes: 3 additions & 0 deletions Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class ViewController: UIViewController {
Tracking status: \(TPTweakEntry.enableTracking.getValue(Bool.self))
Tracking server location: \(TPTweakEntry.trackingServerLocation.getValue(String.self))
Tracking max timeout: \(TPTweakEntry.trackingTimeout.getValue(Int.self))
Tracking locale is active: \(TPTweakEntry.trackingUsingLocale.getValue(Bool.self))
Tracking locale identifer: \((UserDefaults.standard.value(forKey: "tracker_locale") as? String) ?? "no locale")
"""
}
}
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ Using this type, you can create a cell with a UISwitch to enable/disable an opti
.switch(defaultValue: true)
```


You could also add `closure: ((Bool) -> Void)?` that will run **after** the value is changed.

```swift
.switch(defaultValue: true, closure: { isToggledOn in
UserDefaults.standard.set(isToggledOn, forKey: "myvalue_is_on")
})
```

**Strings**

![](assets/strings.png)
Expand Down
2 changes: 1 addition & 1 deletion Sources/TPTweak/TPTweakEntry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Foundation
Entry type, pick your poison
*/
public enum TPTweakEntryType {
case `switch`(defaultValue: Bool)
case `switch`(defaultValue: Bool, closure: ((Bool) -> Void)? = nil)
case action(() -> Void)
case strings(item: [String], selected: String)
case numbers(item: [Double], selected: Double)
Expand Down
3 changes: 2 additions & 1 deletion Sources/TPTweak/TPTweakPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,13 @@ extension TPTweakPickerViewController: UITableViewDataSource, UITableViewDelegat
switch cellData.type {
case let .action(closure):
closure()
case .switch:
case let .switch(_, closure):
var value = TPTweakStore.read(type: Bool.self, identifier: cellData.identifer) ?? false
value.toggle()

TPTweakStore.set(value, identifier: cellData.identifer)
tableView.reloadRows(at: [indexPath], with: .automatic) // to update cell value after action
closure?(value)
case let .numbers(item, defaultValue):
let viewController = TPTweakOptionsViewController(
title: cellData.name,
Expand Down
2 changes: 1 addition & 1 deletion Sources/TPTweak/TPTweakStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public enum TPTweakStore {
/// indicate no value exist before on provider
if environment.provider().data(forKey: identifier) == nil {
switch entry.type {
case let .switch(defaultValue):
case let .switch(defaultValue, _):
set(defaultValue, identifier: identifier)
case let .numbers(_, defaultValue):
set(defaultValue, identifier: identifier)
Expand Down

0 comments on commit 784c0a2

Please sign in to comment.