Git Product home page Git Product logo

rxtheme's Introduction

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
// RxTheme would automatically manage the lifecycle of the binded stream
view.theme.backgroundColor = themeService.attribute { $0.backgroundColor }

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
// Theme type stream
themeService.typeStream

Examples

You can run the example project, clone the repo, run pod install from the Example directory first, and open up the workspace file.

Installation

SPM

  1. File > Swift Packages > Add Package Dependency
  2. Add https://github.com/RxSwiftCommunity/RxTheme

Cocoapods

pod 'RxTheme', '~> 6.0'

Carthage

github "RxSwiftCommunity/RxTheme" ~> 6.0.0

Author

duan, [email protected]

License

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

rxtheme's People

Contributors

barisatamer avatar cozzin avatar kaich avatar wddwycc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rxtheme's Issues

Manual install

It is possible to manually install rxtheme.

I use carthage, but is not available.

tnx

Cell flashing

When I use the contentView.theme.backgroundColor = themeService.attrStream { $0.backgroundColor } to the custom UITableViewCell. It will show the white at first sometimes, then show the theme color.

When I set contentView.backgroundColor = themeService.type.theme.backgroundColor, it works fine.

How to resolve it ?

Wish your reply. Thanks.

Version 1.0.0

  • Support watchOS, macOS and tvOS
  • More syntax support
  • CI config
  • Support animation on theme switch
  • Auto generate Rx Extension code

2.0

  • better syntax support

Adding SPM Support

Have you thought about adding SPM Support to the library rather than only cocoapods?

Why we can't bind more than 2 properties?

Hi,
If I do something like this

        themeService.bind([
            ({ $0.backgroundColor }, [self.rx.backgroundColor]),
            ({ $0.textLabelColor }, [self.textLabel!.rx.textColor]),
            ({ $0.detailTextLabelColor }, [self.detailTextLabel!.rx.textColor])
            ]).disposed(by: disposeBag)

Compiler gives me Type of expression is ambiguous without more context error. If I delete anyone of them, it compiles. Is it Swift bug or by design? What is the proper way to bind 3,4 properties all once instead of binding 2 or 1 at a time. Thanks.

memory leak?

When profiling the app with instruments' Leaks, it shows that rxtheme leaked somewhere. I couldn't find the place exact, so I went and started generating memory graph using Xcode, and it shows this leak:

runtime: Memory Issues - (1 leaked type): 13 instances of Swift closure context leaked

image
instrument output

checking some of those issues revealed that are linked to RxTheme. like this one below:
taken from a Button class:

func makeUI() {
       
        themeService.rx
            .bind({ UIImage(color: $0.primary, size: CGSize(width: 1, height: 1)) }, to: rx.backgroundImage(for: .normal))
            .bind({ UIImage(color: $0.primary.withAlphaComponent(0.9), size: CGSize(width: 1, height: 1)) }, to: rx.backgroundImage(for: .selected))
            .bind({ UIImage(color: $0.primary.withAlphaComponent(0.6), size: CGSize(width: 1, height: 1)) }, to: rx.backgroundImage(for: .disabled))
            .disposed(by: rx.disposeBag)

As I am navigating through the other screens, the number pop's up to 100 Swift closure context leaked. Anyway to solve this? Thanks.

Pass size to a theme font

ThemeService

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

protocol Theme {
    var fontFamilyName: String { get }
    var fontBold: (CGFloat) -> UIFont { get }
}


struct DefaultTheme: Theme {
    let fontFamilyName = "Lato"
    var fontBold: (CGFloat) -> UIFont { return UIFont.appFontBold }
}

enum ThemeType: ThemeProvider {
    case `default`
    var associatedObject: Theme {
        switch self {
        case .`default`:
            return DefaultTheme()
        }
    }
}

UIFont extension

extension UIFont {
    class var fontFamilyName: String {
        return themeService.type.associatedObject.fontFamilyName
    }
    
    class func appFontBold(ofSize size: CGFloat) -> UIFont {
        return UIFont(name: "\(fontFamilyName)-Bold", size: size) ?? UIFont.boldSystemFont(ofSize: size)
    }
}

How to use if I want to pass the size of the font??

class LoginViewController {

    @IBOutlet weak var usernameTxt: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
   
        themeService.rx
            .bind({ $0.fontBold(ofSize: 12.0) }, to: usernameTxt.rx.font)
            .disposed(by: disposeBag)
}

Right now got a compile error
Value of type 'Reactive<ThemeService<ThemeType>>' has no member 'bind'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.