From 784c0a211323cc7f999818a4e9be6a3d3694d5fb Mon Sep 17 00:00:00 2001 From: Wowo Diergo <50097952+wowodiergo@users.noreply.github.com> Date: Wed, 9 Aug 2023 13:36:17 +0700 Subject: [PATCH] Passing closure for `switch` type (#9) * add closure for switch type that being triggered after its value is toggled * add feature to passing closure on type * revert example ios target --- Example/Example/AppDelegate.swift | 1 + Example/Example/TPTweakEntry+Extension.swift | 14 ++++++++++++++ Example/Example/ViewController.swift | 3 +++ README.md | 9 +++++++++ Sources/TPTweak/TPTweakEntry.swift | 2 +- Sources/TPTweak/TPTweakPickerViewController.swift | 3 ++- Sources/TPTweak/TPTweakStore.swift | 2 +- 7 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Example/Example/AppDelegate.swift b/Example/Example/AppDelegate.swift index 37780e3..0f62778 100644 --- a/Example/Example/AppDelegate.swift +++ b/Example/Example/AppDelegate.swift @@ -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() diff --git a/Example/Example/TPTweakEntry+Extension.swift b/Example/Example/TPTweakEntry+Extension.swift index 614baab..dcc2c07 100644 --- a/Example/Example/TPTweakEntry+Extension.swift +++ b/Example/Example/TPTweakEntry+Extension.swift @@ -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", diff --git a/Example/Example/ViewController.swift b/Example/Example/ViewController.swift index 8c4b624..019314a 100644 --- a/Example/Example/ViewController.swift +++ b/Example/Example/ViewController.swift @@ -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") """ } } diff --git a/README.md b/README.md index 5d4910c..b31af66 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/Sources/TPTweak/TPTweakEntry.swift b/Sources/TPTweak/TPTweakEntry.swift index 2101498..5564946 100644 --- a/Sources/TPTweak/TPTweakEntry.swift +++ b/Sources/TPTweak/TPTweakEntry.swift @@ -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) diff --git a/Sources/TPTweak/TPTweakPickerViewController.swift b/Sources/TPTweak/TPTweakPickerViewController.swift index e91332f..b08c8ae 100644 --- a/Sources/TPTweak/TPTweakPickerViewController.swift +++ b/Sources/TPTweak/TPTweakPickerViewController.swift @@ -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, diff --git a/Sources/TPTweak/TPTweakStore.swift b/Sources/TPTweak/TPTweakStore.swift index 0f5e1f5..2a88085 100644 --- a/Sources/TPTweak/TPTweakStore.swift +++ b/Sources/TPTweak/TPTweakStore.swift @@ -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)