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)
// 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)
All streams generated by ThemeService
has share(1)
themeService.switch(.dark)
// When this is triggered by some signal, you can use:
someSignal.bind(to: themeService.switcher)
// Current theme type
themeService.type
// Current theme attributes
themeService.attrs
// Theme type stream
themeService.typeStream
// Theme attributes stream
themeService.attrsStream
- backgroundColor
- borderWidth
- borderColor
- shadowColor
- strokeColor
- fillColor
- style
- tintColor
- titleColor
- font
- textColor
- highlightedTextColor
- shadowColor
- barStyle
- barTintColor
- titleTextAttributes
- pageIndicatorTintColor
- currentPageIndicatorTintColor
- progressTintColor
- trackTintColor
- barStyle
- barTintColor
- keyboardAppearance
- selectedSegmentTintColor
- thumbTintColor
- minimumTrackTintColor
- maximumTrackTintColor
- onTintColor
- thumbTintColor
- barStyle
- barTintColor
- separatorColor
- selectionStyle
- font
- textColor
- keyboardAppearance
- font
- textColor
- keyboardAppearance
- barStyle
- barTintColor
- tintColor
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")
}
}
}
Open codegen/rx_exts.yml
and codegen/theme_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.
You can run the example project, clone the repo, run pod install
from the Example directory first, and open up the workspace file.
If you prefer, there is also a nice video tutorial by @rebeloper.
pod 'RxTheme', '~> 4.0'
github "RxSwiftCommunity/RxTheme" ~> 4.0.0
duan, wddwyss@gmail.com
RxTheme is available under the MIT license. See the LICENSE file for more info.