uias / Tabman

™️ A powerful paging view controller with interactive indicator bars
https://uias.github.io/Tabman
MIT License
2.87k stars 238 forks source link

Setting badge for given tab when tab is not currently selected #596

Open bstillitano opened 2 years ago

bstillitano commented 2 years ago

New Issue Checklist

Issue Description

I am attempting to set the badge value of a tab, while that tab is not the one currently visible. For example, I want tab 3 to update its badge value, while tab 5 is the currently selected one. I know my functions and notification methods are working fine, because I am able to console log a test string to ensure that it is being hit. It appears that the only time a badge can be updated, is when the tab the badge belongs to, is the currently selected tab.

Other useful things

N/A

bstillitano commented 2 years ago

For anyone else with this issue, i've managed to work around this by doing the following in my TabmanViewController instance. All subclasses of ViewController, in my project, have a property called badgeValue which contains a didSet listener which posts a notification and subsequently triggers the update in my parent TabmanViewController:

Task { @MainActor [weak self] in
            for (index, viewController) in viewControllers.enumerated() {
                guard self?.bars.first?.items?.indices.contains(index) ?? false else { return }
                self?.bars.first?.items?[index].badgeValue = (viewController as? ViewController)?.badgeValue
            }
        }