Skip to content

Commit

Permalink
Use ThemeManager.shared
Browse files Browse the repository at this point in the history
  • Loading branch information
onmyway133 committed Aug 11, 2017
1 parent 8fe326b commit a11dd08
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ let unicornTheme = MyTheme(json)
When app launches, you need to declare 1 theme as the current, it can be loaded from cache

```swift
ThemeManager.currentTheme = dayTheme
ThemeManager.shared.currentTheme = dayTheme
```

### Step 3: React to theme change
Expand Down Expand Up @@ -112,7 +112,7 @@ override func awakeFromNib() {
Change the current theme is as easy as assigning a new theme. All happens in real time and very fast

```swift
ThemeManager.currentTheme = nightTheme
ThemeManager.shared.currentTheme = nightTheme
```

## Installation
Expand Down
4 changes: 2 additions & 2 deletions Sources/Handler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Handler {
func observe() {
observer = NotificationCenter.default.addObserver(
forName: Notification.Name.themeDidChange,
object: ThemeManager.self,
object: ThemeManager.shared,
queue: OperationQueue.main,
using: { [weak self] _ in
self?.handle()
Expand All @@ -26,7 +26,7 @@ class Handler {
return
}

guard let theme = ThemeManager.currentTheme else {
guard let theme = ThemeManager.shared.currentTheme else {
return
}

Expand Down
5 changes: 4 additions & 1 deletion Sources/ThemeManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import Foundation
/// Manager your current theme
public class ThemeManager {

/// The singleton
public static let shared = ThemeManager()

/// This is the current theme. Set it at app launch and whenever you want
/// to change theme
public static var currentTheme: Theme? {
public var currentTheme: Theme? {
didSet {
NotificationCenter.default.post(name: Notification.Name.themeDidChange, object: self)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/ThemeUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public extension ThemeUser {
/// - type: The type of your own theme
/// - apply: the function that gets called with (ThemeUser, Theme)
func use<T: Theme>(_ type: T.Type, apply: @escaping (Self, T) -> Void) {
if let theme = ThemeManager.currentTheme as? T {
if let theme = ThemeManager.shared.currentTheme as? T {
apply(self, theme)
}

Expand Down
6 changes: 3 additions & 3 deletions ThemesTests/ThemeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ThemeTests: XCTestCase {
let theme2 = MyTheme(color: .green, font: .systemFont(ofSize: 10), placeholder: "theme 2")

// theme 1
ThemeManager.currentTheme = theme1
ThemeManager.shared.currentTheme = theme1

let textField = UITextField()

Expand All @@ -39,7 +39,7 @@ class ThemeTests: XCTestCase {
XCTAssertEqual(textField.placeholder, "theme 1")

// theme 2
ThemeManager.currentTheme = theme2
ThemeManager.shared.currentTheme = theme2

// change to theme 2
XCTAssertEqual(textField.textColor, UIColor.green)
Expand All @@ -48,7 +48,7 @@ class ThemeTests: XCTestCase {

// other theme

ThemeManager.currentTheme = OtherTheme()
ThemeManager.shared.currentTheme = OtherTheme()

// textField is unchanged
XCTAssertEqual(textField.textColor, UIColor.green)
Expand Down

0 comments on commit a11dd08

Please sign in to comment.