Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Łukasz Śliwiński committed Jun 25, 2020
1 parent d3e3155 commit 2555b4b
Show file tree
Hide file tree
Showing 25 changed files with 926 additions and 96 deletions.
10 changes: 8 additions & 2 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ excluded: # paths to ignore during linting. Takes precedence over `included`.
- Tests
- Example

disabled_rules: # rule identifiers to exclude from running
- trailing_whitespace
identifier_name:
min_length: # only min_length
error: 4 # only error
excluded: # excluded via string array
- id

nesting:
type_level: 2
14 changes: 5 additions & 9 deletions Example/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
//
// AppDelegate.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

Expand All @@ -28,8 +22,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
/// - application: The UIApplication
/// - launchOptions: The LaunchOptions
/// - Returns: The launch result
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// Initialize UIWindow
self.window = .init(frame: UIScreen.main.bounds)
// Set RootViewController
Expand Down
86 changes: 86 additions & 0 deletions Example/Stylesheet.swift
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
}
}
86 changes: 62 additions & 24 deletions Example/ViewController.swift
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)
}
}
}
Loading

0 comments on commit 2555b4b

Please sign in to comment.