Skip to content

Instantly share code, notes, and snippets.

@edwellbrook
Last active January 29, 2017 13:04
Show Gist options
  • Save edwellbrook/acd7be8ad94544fbb960f69c2d48dc21 to your computer and use it in GitHub Desktop.
Save edwellbrook/acd7be8ad94544fbb960f69c2d48dc21 to your computer and use it in GitHub Desktop.
Migrating from v0.4.x -> v0.5.0
Your `Theme` should now have a static `manager` property and this should be used
in place of the global/singleton instance you made and used before.
You no longer need a `themer` property and these should be deleted.
Adding a themeable should now be done via the Theme.manager and looks like this:
```swift
ExampleTheme.manager.register(themeable: self)
```
//
// ExampleTheme.swift
// Example
//
// Created by Edward Wellbrook on 27/01/2017.
// Copyright © 2017 Edward Wellbrook. All rights reserved.
//
import UIKit
import Themeable
public struct ExampleTheme: Theme {
public let identifier: String
public let backgroundColor: UIColor
public let switchTintColor: UIColor
// export all the available themes
public static let variants: [ExampleTheme] = [ .light, .dark ]
// export the theme manager
public static let manager = ThemeManager<ExampleTheme>(default: .light)
// light theme
public static let light = ExampleTheme(
identifier: "co.brushedtype.Themeable.light-theme",
backgroundColor: .lightGray,
switchTintColor: .blue
)
// dark theme
public static let dark = ExampleTheme(
identifier: "co.brushedtype.Themeable.dark-theme",
backgroundColor: .darkGray,
switchTintColor: .red
)
}
//
// ViewController.swift
// Example
//
// Created by Edward Wellbrook on 29/01/2017.
// Copyright © 2017 Brushed Type. All rights reserved.
//
import UIKit
import Themeable
class ViewController: UIViewController {
lazy var themeToggle: UISwitch = {
let toggle = UISwitch()
toggle.addTarget(self, action: #selector(ViewController.toggleTheme), for: .valueChanged)
toggle.center = self.view.center
return toggle
}()
// register themeables after views have been loaded
override func viewDidLoad() {
super.viewDidLoad()
ExampleTheme.manager.register(themeable: self)
ExampleTheme.manager.register(themeable: self.themeToggle)
self.view.addSubview(self.themeToggle)
}
// switch between themes
func toggleTheme() {
if self.themeToggle.isOn {
ExampleTheme.manager.activeTheme = .dark
} else {
ExampleTheme.manager.activeTheme = .light
}
}
}
extension ViewController: Themeable {
func apply(theme: ExampleTheme) {
self.view.backgroundColor = theme.backgroundColor
}
}
extension UISwitch: Themeable {
public func apply(theme: ExampleTheme) {
self.tintColor = theme.switchTintColor
self.onTintColor = theme.switchTintColor
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment