https://github.com/tevelee/tweaks
Lightweight, extensible solution Tweaking values on iOS at runtime with SwiftUI
https://github.com/tevelee/tweaks
configs ios tweaks
Last synced: 6 months ago
JSON representation
Lightweight, extensible solution Tweaking values on iOS at runtime with SwiftUI
- Host: GitHub
- URL: https://github.com/tevelee/tweaks
- Owner: tevelee
- License: mit
- Created: 2020-06-12T14:27:18.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-09-30T21:29:20.000Z (about 5 years ago)
- Last Synced: 2025-02-09T13:33:25.326Z (8 months ago)
- Topics: configs, ios, tweaks
- Language: Swift
- Homepage:
- Size: 689 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Tweaks
* [Tweaks](#tweaks)
+ [Overview](#overview-1)
+ [Usage](#usage-1)
- [Tweaks UI](#tweaks-ui)
- [Registering tweaks](#registering-tweaks)
- [Cusotm actions](#custom-actions)
- [Custom views](#custom-views)
- [Custom types](#custom-types)
* [License](#license)## Overview
Tweaks is the powerful and visible part of configuration management.
With the use of SwiftUI, it's easy to define custom views for each data types.
Please check out the other repository [Configs](https://github.com/tevelee/Configs) to see how they integrate with each other.
## Usage
### Tweaks UI
All you need is to show the `TweaksView` SwiftUI view in any way. Usually shaking the device or having a secret settings menu option opens up these internal config menus.
```swift
import SwiftUI
import ConfigTweaksstruct ContentView: View {
@State var showTweaks = false
var body: some View {
VStack {
Spacer()
Text("Hello!")
Spacer()
Button(action: { showTweaks = true }) {
Text("Open Tweaks")
}
Spacer()
}
.sheet(isPresented: $showTweaks) {
TweaksView()
}
}
}
```
![]()
Orange values represent overrides.
### Registering tweaks
As shown in the repository [Configs](https://github.com/tevelee/Configs), it's easy to use them with `ConfigDefiniton`s, but Tweaks is also usable as a standalone library.
```swift
let tweak = TweakDefinition(name: "Number of items", initialValue: 1)
TweakRepository.shared.add(tweak: tweak, category: "Product Settings", section: "Feature Settings")
```You can also specify how your data should be represented in SwiftUI with renderer objects:
```swift
let tweak = TweakDefinition(name: "Number of items", initialValue: 1, renderer: InputAndStepperRenderer())
let tweak = TweakDefinition(name: "Number of items", initialValue: 1, renderer: SliderRenderer())
let tweak = TweakDefinition(name: "Number of items", initialValue: 1, renderer: CustomRenderer(previewView: { Text(String($0)) }, tweakView: { Stepper("", value: $0) }))
```Of course, TweakRepository is an `ObservableObject` so you can use it easily in SwiftUI as well. See the example app for more details.
### Custom actions
The system allows you to add actions, which are only tappable buttons in the Tweaks UI that perform any custom logic.
```swift
let resetAction = TweakAction(category: "Product Settings", section: "Actions", name: "Reset onboarding") {
print("reset")
}TweakRepository.shared.add(resetAction)
```### Custom views
You can compose renderers to show different UIs for the same type of data, for example when you need users to pick from a predefined list of values.
```swift
let backendBaseUrl = ConfigDefinition(defaultValue: "https://production.url")
.firebaseRemoteConfig(key: "server_url")
.tweak(category: "Product Settings", section: "Essentials", name: "Backend Url", renderer: PickerRendererWithCustomValue(options: ["Debug": "https://debug.url", "Production": "https://production.url", "Test": "https://preprod.url"], renderer: StringTextfieldRenderer()))
```The possibilities are endless, you can write different custom renderers upon the built-in ones provided by the library.
There are a few ones provided for common use-cases:
* `ToggleBoolRenderer` and `SegmentedBoolRenderer` for Bools
* `InputAndStepperRenderer` and `SliderRenderer` for numbers
* `StringTextfieldRenderer` for String
* `ColorPickerRenderer` for SwiftUI Colors
* `PickerRenderer` and `PickerRendererWithCustomValue` are generic composite renderers if you have a predefined set of values (of any type) to choose from
* `OptionPickerRenderer` for enums
* `ArrayRenderer` for generic array of types
* `OptionalToggleRenderer` for optional values of generic types
* `CustomRenderer` for dynamic values### Custom types
For any custom representation you need, all you need to write is a struct that conforms to `ViewRenderer` protocol, as follows:
```swift
struct StringTextfieldRenderer: ViewRenderer {
func previewView(value: String) -> some View {
Text(value)
}
func tweakView(value: Binding) -> some View {
TextField("", text: value)
.autocapitalization(.none)
.disableAutocorrection(true)
}
}
```# License
See the [LICENSE](LICENSE.md) file for license rights and limitations (MIT).