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
// RxTheme would automatically manage the lifecycle of the binded stream
view.theme.backgroundColor = themeService.attribute { $0.backgroundColor }
themeService.switch(.dark)
// When this is triggered by some signal, you can use:
someSignal.bind(to: themeService.switcher)
// Current theme type
themeService.type
// Theme type stream
themeService.typeStream
You can run the example project, clone the repo, run pod install
from the Example directory first, and open up the workspace file.
- File > Swift Packages > Add Package Dependency
- Add https://github.com/RxSwiftCommunity/RxTheme
pod 'RxTheme', '~> 6.0'
github "RxSwiftCommunity/RxTheme" ~> 6.0.0
duan, wddwyss@gmail.com
RxTheme is available under the MIT license. See the LICENSE file for more info.