Search code examples
functionrx-swiftuiswitch

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


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)