-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Łukasz Śliwiński
committed
Jun 25, 2020
1 parent
d3e3155
commit 2555b4b
Showing
25 changed files
with
926 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright © 2020 plum. All rights reserved. | ||
|
||
import UIKit | ||
import ObservableThemeKit | ||
|
||
// MARK: - Stylesheet | ||
|
||
/// For the sake of the example Stylesheet represents what kind of the Style type, the Theme could hold | ||
struct Stylesheet { | ||
|
||
fileprivate static var shared: Observable<Stylesheet> = .init(Stylesheet(appearance: .light)) | ||
|
||
let appearance: Appearance | ||
let primaryColor: UIColor | ||
let backgroundColor: UIColor | ||
|
||
init(appearance: Appearance) { | ||
self.appearance = appearance | ||
|
||
switch appearance { | ||
case .light: | ||
self.primaryColor = .black | ||
self.backgroundColor = .white | ||
case .dark: | ||
self.primaryColor = .white | ||
self.backgroundColor = .darkGray | ||
} | ||
} | ||
|
||
func font(with type: FontType) -> UIFont { | ||
switch type { | ||
case .header: | ||
return UIFont.systemFont(ofSize: 16, weight: .semibold) | ||
case .normal: | ||
return UIFont.systemFont(ofSize: 14, weight: .regular) | ||
case .small: | ||
return UIFont.systemFont(ofSize: 10, weight: .regular) | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Stylesheet helpers | ||
|
||
extension Stylesheet { | ||
|
||
/// Change shared stylesheet using the new appearance | ||
static func changeAppearance(to appearance: Appearance) { | ||
Self.shared.wrappedValue = Stylesheet(appearance: appearance) | ||
} | ||
|
||
/// Currently used appearance in the stylesheet | ||
static var appearance: Appearance { | ||
return Self.shared.wrappedValue.appearance | ||
} | ||
|
||
/// Currently set stylesheet | ||
static var current: Stylesheet { | ||
return Self.shared.wrappedValue | ||
} | ||
} | ||
|
||
// MARK: - Stylesheet types | ||
|
||
extension Stylesheet { | ||
|
||
enum Appearance { | ||
case light | ||
case dark | ||
} | ||
|
||
enum FontType { | ||
case header | ||
case normal | ||
case small | ||
} | ||
} | ||
|
||
// MARK: - Theme default implementation | ||
|
||
extension Theme { | ||
|
||
/// Default stylesheet for the convenience | ||
static var stylesheet: Observable<Stylesheet> { | ||
return Stylesheet.shared | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,81 @@ | ||
// | ||
// ViewController.swift | ||
// Example | ||
// | ||
// Created by Łukasz Śliwiński on 30 Apr 2020. | ||
// Copyright © 2020 plum. All rights reserved. | ||
// | ||
// Copyright © 2020 plum. All rights reserved. | ||
|
||
import UIKit | ||
import ObservableThemeKit | ||
import UIKit | ||
|
||
// MARK: - ViewController | ||
|
||
/// The ViewController | ||
class ViewController: UIViewController { | ||
|
||
// MARK: Properties | ||
/// The Label | ||
lazy var label: UILabel = { | ||
let label = UILabel() | ||
label.text = "🚀\nObservableThemeKit\nExample" | ||
label.font = .systemFont(ofSize: 25, weight: .semibold) | ||
label.numberOfLines = 0 | ||
label.lineBreakMode = .byWordWrapping | ||
label.textAlignment = .center | ||
return label | ||
|
||
/// Theme | ||
@ObservableTheme var theme: ViewTheme | ||
|
||
/// Button | ||
lazy var button: UIButton = { | ||
let button = UIButton() | ||
button.setTitle("Change appearance", for: .normal) | ||
button.addTarget(self, action: #selector(handleButtonTap), for: .touchUpInside) | ||
return button | ||
}() | ||
|
||
// MARK: View-Lifecycle | ||
|
||
/// View did load | ||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
self.view.backgroundColor = .white | ||
self.setupAppearance() | ||
|
||
self.$theme.observe( | ||
owner: self, | ||
handler: { (owner, _) in | ||
owner.setupAppearance() | ||
} | ||
) | ||
} | ||
/// LoadView | ||
|
||
/// Load view | ||
override func loadView() { | ||
self.view = self.label | ||
self.view = self.button | ||
} | ||
|
||
// MARK: Helpers | ||
|
||
/// Setup appearance | ||
private func setupAppearance() { | ||
self.button.setTitleColor(theme.labelColor, for: .normal) | ||
self.button.titleLabel?.font = theme.buttonFont | ||
self.view.backgroundColor = theme.backgroundColor | ||
} | ||
|
||
/// Handle button tap | ||
@objc private func handleButtonTap() { | ||
if Stylesheet.appearance == .light { | ||
Stylesheet.changeAppearance(to: .dark) | ||
} else { | ||
Stylesheet.changeAppearance(to: .light) | ||
} | ||
} | ||
} | ||
|
||
// MARK: - ViewTheme | ||
|
||
extension ViewController { | ||
|
||
/// Theme for the ViewController | ||
struct ViewTheme: Theme { | ||
static let `default`: ViewController.ViewTheme = .init(stylesheet: Self.stylesheet.wrappedValue) | ||
|
||
let labelColor: UIColor | ||
let backgroundColor: UIColor | ||
let buttonFont: UIFont | ||
|
||
init(stylesheet: Stylesheet) { | ||
self.labelColor = stylesheet.primaryColor | ||
self.backgroundColor = stylesheet.backgroundColor | ||
self.buttonFont = stylesheet.font(with: .header) | ||
} | ||
} | ||
} |
Oops, something went wrong.