/RxTheme

Theme management based on Rx

Primary LanguageSwiftMIT LicenseMIT

RxTheme

Build Status Version Carthage compatible License Platform

Manual

Define theme service

import RxTheme

protocol Theme {
    var backgroundColor: UIColor { get }
    var textColor: UIColor { get }
}

struct LightTheme: Theme {
    let backgroundColor = .white
    let textColor = .black
}

struct DarkTheme: Theme {
    let backgroundColor = .black
    let textColor = .white
}

enum ThemeType: ThemeProvider {
    case light, dark
    var associatedObject: Theme {
        switch self {
        case .light:
            return LightTheme()
        case .dark:
            return DarkTheme()
        }
    }
}

let themeService = ThemeType.service(initial: .light)

Apply theme to UI

// Bind stream to a single attribute
// In the way, RxTheme would automatically manage the lifecycle of the binded stream 
view.theme.backgroundColor = themeService.attrStream { $0.backgroundColor }

// Or bind a bunch of attributes, add them to a disposeBag
themeService.rx
    .bind({ $0.textColor }, to: label1.rx.textColor, label2.rx.textColor)
    .bind({ $0.backgroundColor }, to: view.rx.backgroundColor)
    .disposed(by: disposeBag)

// Or when you don't have a disposeBag
// Add `until` to control the stream's lifecycle
themeService.rx
    .bind({ $0.textColor }, until: label.rx.deallocating, to: label.rx.textColor)
    .bind({ $0.backgroundColor }, until: view.rx.deallocating, to: view.rx.backgroundColor)

All streams generated by ThemeService has share(1)

Switch themes

themeService.switch(.dark)
// When this is triggered by some signal, you can use:
someSignal.bind(to: themeService.switcher)

Other APIs

// Current theme type
themeService.type
// Current theme attributes
themeService.attrs
// Theme type stream
themeService.typeStream
// Theme attributes stream
themeService.attrsStream

Binder presets

CALayer
  • backgroundColor
  • borderWidth
  • borderColor
  • shadowColor
CAShapeLayer
  • strokeColor
  • fillColor
UIActivityIndicatorView
  • style
UIBarButtonItem
  • tintColor
UIButton
  • titleColor
UILabel
  • font
  • textColor
  • highlightedTextColor
  • shadowColor
UINavigationBar
  • barStyle
  • barTintColor
  • titleTextAttributes
UIPageControl
  • pageIndicatorTintColor
  • currentPageIndicatorTintColor
UIProgressView
  • progressTintColor
  • trackTintColor
UISearchBar
  • barStyle
  • barTintColor
  • keyboardAppearance
UISlider
  • thumbTintColor
  • minimumTrackTintColor
  • maximumTrackTintColor
UISwitch
  • onTintColor
  • thumbTintColor
UITabBar
  • barStyle
  • barTintColor
UITableView
  • separatorColor
UITAbleViewCell
  • selectionStyle
UITextField
  • font
  • textColor
  • keyboardAppearance
UITextView
  • font
  • textColor
  • keyboardAppearance
UIToolbar
  • barStyle
  • barTintColor
UIView
  • backgroundColor
  • tintColor

Extend binders in your codebase

Because RxTheme uses Binder<T> from RxCocoa, any Binder defined in RxCocoa could be used here.

This also makes the lib super easy to extend in your codebase, here is an example

extension Reactive where Base: UIView {
    var borderColor: Binder<UIColor?> {
        return Binder(self.base) { view, color in
            view.layer.borderColor = color?.cgColor
        }
    }
}

if you also want to use the sugar view.theme.borderColor, you have to write another extension:

extension ThemeProxy where Base: UIView {
    var borderColor: Observable<UIColor?> {
        get { return .empty() }
        set {
            let disposable = newValue
                .takeUntil(base.rx.deallocating)
                .observeOn(MainScheduler.instance)
                .bind(to: base.rx.borderColor)
            hold(disposable, for: "borderColor")
        }
    }
}

Extend binders in the lib

Open codegen/exts.yml, add class, attributes and supported os.

UILabel:
  attrs:
    font: UIFont
    textColor: UIColor?
    highlightedTextColor: UIColor?
    shadowColor: UIColor?
  os: [iOS, tvOS]

then run codegen script

// make sure you have python3 and pipenv installed
$ pipenv install
$ pipenv run python -m codegen update-exts

If you think it's commonly used, please send us a PR.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

Cocoapods

pod 'RxTheme', '~> 3.0'

Carthage

github "RxSwiftCommunity/RxTheme" ~> 3.0.0

Author

duan, wddwyss@gmail.com

License

RxTheme is available under the MIT license. See the LICENSE file for more info.