Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix view flashing #22

Merged
merged 4 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 36 additions & 30 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Example/Tests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Tests: XCTestCase {

func testSingleBind() {
let label = UILabel()
label.theme.textColor = themeService.attrStream { $0.textColor }
label.theme.textColor = themeService.attribute { $0.textColor }
XCTAssertEqual(label.textColor, LightTheme().textColor)
themeService.switch(.dark)
XCTAssertEqual(label.textColor, DarkTheme().textColor)
Expand All @@ -80,9 +80,9 @@ class Tests: XCTestCase {

func testOverrideBind() {
let label = UILabel()
label.theme.textColor = themeService.attrStream { $0.backgroundColor }
label.theme.textColor = themeService.attribute { $0.backgroundColor }
XCTAssertEqual(label.textColor, LightTheme().backgroundColor)
label.theme.textColor = themeService.attrStream { $0.textColor }
label.theme.textColor = themeService.attribute { $0.textColor }
XCTAssertEqual(label.textColor, LightTheme().textColor)
themeService.switch(.dark)
XCTAssertEqual(label.textColor, DarkTheme().textColor)
Expand Down
4 changes: 2 additions & 2 deletions Example/Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ enum ThemeType: ThemeProvider {
}

let themeService = ThemeType.service(initial: .light)
func themed<T>(_ mapper: @escaping ((Theme) -> T)) -> Observable<T> {
return themeService.attrStream(mapper)
func themed<T>(_ mapper: @escaping ((Theme) -> T)) -> ThemeAttribute<T> {
return themeService.attribute(mapper)
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ 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 }
view.theme.backgroundColor = themeService.attribute { $0.backgroundColor }

// Or bind a bunch of attributes, add them to a disposeBag
themeService.rx
Expand Down
30 changes: 21 additions & 9 deletions Sources/Extensions/CALayer+Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ import RxCocoa
public extension ThemeProxy where Base: CALayer {

/// (set only) bind a stream to backgroundColor
var backgroundColor: Observable<CGColor?> {
var backgroundColor: ThemeAttribute<CGColor?> {
get { return .empty() }
set {
let disposable = newValue
if let value = newValue.value {
base.backgroundColor = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.backgroundColor)
Expand All @@ -26,10 +29,13 @@ public extension ThemeProxy where Base: CALayer {
}

/// (set only) bind a stream to borderWidth
var borderWidth: Observable<CGFloat> {
var borderWidth: ThemeAttribute<CGFloat> {
get { return .empty() }
set {
let disposable = newValue
if let value = newValue.value {
base.borderWidth = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.borderWidth)
Expand All @@ -38,10 +44,13 @@ public extension ThemeProxy where Base: CALayer {
}

/// (set only) bind a stream to borderColor
var borderColor: Observable<CGColor?> {
get { return .empty() }
var borderColor: ThemeAttribute<CGColor?> {
get { return ThemeAttribute() }
set {
let disposable = newValue
if let value = newValue.value {
base.borderColor = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.borderColor)
Expand All @@ -50,10 +59,13 @@ public extension ThemeProxy where Base: CALayer {
}

/// (set only) bind a stream to shadowColor
var shadowColor: Observable<CGColor?> {
var shadowColor: ThemeAttribute<CGColor?> {
get { return .empty() }
set {
let disposable = newValue
if let value = newValue.value {
base.shadowColor = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.shadowColor)
Expand Down
14 changes: 10 additions & 4 deletions Sources/Extensions/CAShapeLayer+Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ import RxCocoa
public extension ThemeProxy where Base: CAShapeLayer {

/// (set only) bind a stream to strokeColor
var strokeColor: Observable<CGColor?> {
var strokeColor: ThemeAttribute<CGColor?> {
get { return .empty() }
set {
let disposable = newValue
if let value = newValue.value {
base.strokeColor = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.strokeColor)
Expand All @@ -26,10 +29,13 @@ public extension ThemeProxy where Base: CAShapeLayer {
}

/// (set only) bind a stream to fillColor
var fillColor: Observable<CGColor?> {
var fillColor: ThemeAttribute<CGColor?> {
get { return .empty() }
set {
let disposable = newValue
if let value = newValue.value {
base.fillColor = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.fillColor)
Expand Down
7 changes: 5 additions & 2 deletions Sources/Extensions/UIActivityIndicatorView+Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import RxCocoa
public extension ThemeProxy where Base: UIActivityIndicatorView {

/// (set only) bind a stream to style
var style: Observable<UIActivityIndicatorView.Style> {
var style: ThemeAttribute<UIActivityIndicatorView.Style> {
get { return .empty() }
set {
let disposable = newValue
if let value = newValue.value {
base.style = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.style)
Expand Down
7 changes: 5 additions & 2 deletions Sources/Extensions/UIBarButtonItem+Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import RxCocoa
public extension ThemeProxy where Base: UIBarButtonItem {

/// (set only) bind a stream to tintColor
var tintColor: Observable<UIColor?> {
var tintColor: ThemeAttribute<UIColor?> {
get { return .empty() }
set {
let disposable = newValue
if let value = newValue.value {
base.tintColor = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.tintColor)
Expand Down
7 changes: 5 additions & 2 deletions Sources/Extensions/UIButton+Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ public extension Reactive where Base: UIButton {

public extension ThemeProxy where Base: UIButton {

func titleColor(from stream: Observable<UIColor?>, for state: UIControl.State) {
let disposable = stream
func titleColor(from newValue: ThemeAttribute<UIColor?>, for state: UIControl.State) {
if let value = newValue.value {
base.setTitleColor(value, for: state)
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.titleColor(for: state))
Expand Down
28 changes: 20 additions & 8 deletions Sources/Extensions/UILabel+Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import RxCocoa
public extension ThemeProxy where Base: UILabel {

/// (set only) bind a stream to font
var font: Observable<UIFont> {
var font: ThemeAttribute<UIFont> {
get { return .empty() }
set {
let disposable = newValue
if let value = newValue.value {
base.font = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.font)
Expand All @@ -25,10 +28,13 @@ public extension ThemeProxy where Base: UILabel {
}

/// (set only) bind a stream to textColor
var textColor: Observable<UIColor?> {
var textColor: ThemeAttribute<UIColor?> {
get { return .empty() }
set {
let disposable = newValue
if let value = newValue.value {
base.textColor = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.textColor)
Expand All @@ -37,10 +43,13 @@ public extension ThemeProxy where Base: UILabel {
}

/// (set only) bind a stream to highlightedTextColor
var highlightedTextColor: Observable<UIColor?> {
var highlightedTextColor: ThemeAttribute<UIColor?> {
get { return .empty() }
set {
let disposable = newValue
if let value = newValue.value {
base.highlightedTextColor = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.highlightedTextColor)
Expand All @@ -49,10 +58,13 @@ public extension ThemeProxy where Base: UILabel {
}

/// (set only) bind a stream to shadowColor
var shadowColor: Observable<UIColor?> {
var shadowColor: ThemeAttribute<UIColor?> {
get { return .empty() }
set {
let disposable = newValue
if let value = newValue.value {
base.shadowColor = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.shadowColor)
Expand Down
28 changes: 20 additions & 8 deletions Sources/Extensions/UINavigationBar+Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import RxCocoa
public extension ThemeProxy where Base: UINavigationBar {

/// (set only) bind a stream to barStyle
var barStyle: Observable<UIBarStyle> {
var barStyle: ThemeAttribute<UIBarStyle> {
get { return .empty() }
set {
let disposable = newValue
if let value = newValue.value {
base.barStyle = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.barStyle)
Expand All @@ -25,10 +28,13 @@ public extension ThemeProxy where Base: UINavigationBar {
}

/// (set only) bind a stream to barTintColor
var barTintColor: Observable<UIColor?> {
var barTintColor: ThemeAttribute<UIColor?> {
get { return .empty() }
set {
let disposable = newValue
if let value = newValue.value {
base.barTintColor = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.barTintColor)
Expand All @@ -37,10 +43,13 @@ public extension ThemeProxy where Base: UINavigationBar {
}

/// (set only) bind a stream to titleTextAttributes
var titleTextAttributes: Observable<[NSAttributedString.Key: Any]?> {
var titleTextAttributes: ThemeAttribute<[NSAttributedString.Key: Any]?> {
get { return .empty() }
set {
let disposable = newValue
if let value = newValue.value {
base.titleTextAttributes = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.titleTextAttributes)
Expand All @@ -50,10 +59,13 @@ public extension ThemeProxy where Base: UINavigationBar {

/// (set only) bind a stream to largeTitleTextAttributes
@available(iOS 11.0, *)
var largeTitleTextAttributes: Observable<[NSAttributedString.Key: Any]?> {
var largeTitleTextAttributes: ThemeAttribute<[NSAttributedString.Key: Any]?> {
get { return .empty() }
set {
let disposable = newValue
if let value = newValue.value {
base.largeTitleTextAttributes = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.largeTitleTextAttributes)
Expand Down
14 changes: 10 additions & 4 deletions Sources/Extensions/UIPageControl+Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import RxCocoa
public extension ThemeProxy where Base: UIPageControl {

/// (set only) bind a stream to pageIndicatorTintColor
var pageIndicatorTintColor: Observable<UIColor?> {
var pageIndicatorTintColor: ThemeAttribute<UIColor?> {
get { return .empty() }
set {
let disposable = newValue
if let value = newValue.value {
base.pageIndicatorTintColor = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.pageIndicatorTintColor)
Expand All @@ -25,10 +28,13 @@ public extension ThemeProxy where Base: UIPageControl {
}

/// (set only) bind a stream to currentPageIndicatorTintColor
var currentPageIndicatorTintColor: Observable<UIColor?> {
var currentPageIndicatorTintColor: ThemeAttribute<UIColor?> {
get { return .empty() }
set {
let disposable = newValue
if let value = newValue.value {
base.currentPageIndicatorTintColor = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.currentPageIndicatorTintColor)
Expand Down
14 changes: 10 additions & 4 deletions Sources/Extensions/UIProgressView+Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import RxCocoa
public extension ThemeProxy where Base: UIProgressView {

/// (set only) bind a stream to progressTintColor
var progressTintColor: Observable<UIColor?> {
var progressTintColor: ThemeAttribute<UIColor?> {
get { return .empty() }
set {
let disposable = newValue
if let value = newValue.value {
base.progressTintColor = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.progressTintColor)
Expand All @@ -25,10 +28,13 @@ public extension ThemeProxy where Base: UIProgressView {
}

/// (set only) bind a stream to trackTintColor
var trackTintColor: Observable<UIColor?> {
var trackTintColor: ThemeAttribute<UIColor?> {
get { return .empty() }
set {
let disposable = newValue
if let value = newValue.value {
base.trackTintColor = value
}
let disposable = newValue.stream
.take(until: base.rx.deallocating)
.observe(on: MainScheduler.instance)
.bind(to: base.rx.trackTintColor)
Expand Down
Loading