RxSwiftCommunity / RxTheme

Theme management based on Rx
MIT License
382 stars 29 forks source link

Cell flashing #21

Closed kaich closed 3 years ago

kaich commented 3 years ago

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.

wddwycc commented 3 years ago

@kaich try setup the binding in cell's init function ?

kaich commented 3 years ago

@wddwycc Yes, I'm binding the cell in the init function init(style: UITableViewCell.CellStyle, reuseIdentifier: String?). I think the observer receive the initial value after it displayed.

wddwycc commented 3 years ago

@kaich Since the signal is emitted asyncronously after the binding, what you mentioned is possbile to happen. I'm thinking about apply a setter before the binding.

kaich commented 3 years ago

@wddwycc Looking forward to your good news.

wddwycc commented 3 years ago

@kaich I'll take a look this week

wddwycc commented 3 years ago

Just realized I did some interface design attempts on dev branch before to improve this, but cannot achieve a flexible API at that time.

kaich commented 3 years ago

@wddwycc I spend time to do it like below:

public class ThemeAttribute<U> {
    var value: U?

    var steam: Observable<U>?

    init<Provider: ThemeProvider>(service: ThemeService<Provider>, mapper: @escaping ((Provider.T) -> U)) {
        value = mapper(service.attrs)
        steam = service.attrStream(mapper)
    }
}

extension ThemeService {

    func attribute<U>(mapper: @escaping ((Provider.T) -> U)) -> ThemeAttribute<U> {
        return ThemeAttribute(service: self, mapper: mapper)
    }

}

Then I change all extension like this:

var backgroundColor: Observable<CGColor?> to var backgroundColor: ThemeAttribute<CGColor?>.

So I can get the initialized value. If there are any changes in future, I can modify the ThemeAttribute very easily.

Finally , We can use it like this:

view.theme.backgroundColor = themeService.attribute { $0.backgroundColor }

The codes are not big change. Do you think It's a good idea ? If so, I will change it and create a pull request.

Hope your reply. Thanks.

wddwycc commented 3 years ago

@kaich looks good to me, please send the PR (๑•̀ㅂ•́)و✧

wddwycc commented 3 years ago

@kaich

thank you for the PR. I've merged and did some interface adjustment. It's been released in 6.0.0.