Giter Club home page Giter Club logo

tweaks's Introduction

Tweaks

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 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.

import SwiftUI
import ConfigTweaks

struct 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, it's easy to use them with ConfigDefinitons, but Tweaks is also usable as a standalone library.

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:

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.

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.

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:

struct StringTextfieldRenderer: ViewRenderer {
    func previewView(value: String) -> some View {
        Text(value)
    }
    func tweakView(value: Binding<String>) -> some View {
        TextField("", text: value)
            .autocapitalization(.none)
            .disableAutocorrection(true)
    }
}

License

See the LICENSE file for license rights and limitations (MIT).

tweaks's People

Contributors

tevelee avatar

Stargazers

Thomas Harr avatar  avatar

Watchers

 avatar  avatar

Forkers

wesprint-io

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.