[SOLVED] How to add another function after changing the app mode in RxSwift

Issue

I have a switcher that changes the app theme.

        switchButton.rx
            .controlEvent(.valueChanged)
            .withLatestFrom(themeService.typeStream)
            .map { $0 == .dark ? .light : .dark }
            .bind(to: themeService.switcher)
            .disposed(by: disposeBag)

I want to add another custom function when an app theme changes. Thanks

Solution

You’re allowed to subscribe to an Observable more than once.

let theme = switchButton.rx
    .controlEvent(.valueChanged)
    .withLatestFrom(themeService.typeStream)
    .map { $0 == .dark ? .light : .dark }
    .share()

theme
    .bind(to: themeService.switcher)
    .disposed(by: disposeBag)

theme
    .bind(to: anotherCustomFunction)
    .disposed(by: disposeBag)

Answered By – Daniel T.

Answer Checked By – Katrina (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.