TheJeff
TheJeff

Reputation: 4121

Tip Parameter macro not working in UIKit - iOS 17

I'm trying to get a popover tip working with the conditional @Parameter macro. Everything works fine if I remove the @Parameter macro, but right now, in the callback, shouldDisplay is always false, and its driving me mad.

More generally, in addition to needing help with this problem, how do we troubleshoot the evaluation of the shouldDisplay bool in TipKit?

Here is my code in AppDelegate to test this:

    try? Tips.resetDatastore()
    Tips.showAllTipsForTesting()
    try? Tips.configure()

Here is my code in the View Controller:

    struct MyTipWithTheParam: Tip {
        var title: Text { Text(NSLocalizedString("title","Title"))) }
        var message: Text? { Text(NSLocalizedString("message","Message")) }
        var image: Image? { Image(systemName: "somesystemicon") }
        
        @Parameter
        static var shouldShow: Bool = false

        var rules: [Rule] {
            [
                // Define a rule based on the app state.
                #Rule(Self.$shouldShow) {
                    // Set the conditions for when the tip displays.
                    $0 == true
                }
            ]
        }
    }

And here is the UIViewController code:

    // member vars for UIViewController
    private var featureTip = MyTipWiththeParam()
    private var tipObservationTask: Task<Void, Never>?
    private weak var tipPopoverController: TipUIPopoverViewController?

    override func viewDidAppear(_ animated: Bool) {

        tipObservationTask = tipObservationTask ?? Task { @MainActor in
            for await shouldDisplay in featureTip.shouldDisplayUpdates {
                if shouldDisplay {
                    log.debug("Should display is true in the TipPopoverCoordinator")
                    let popoverController = TipUIPopoverViewController(featureTip, sourceItem: sourceView)
                    viewController.present(popoverController, animated: true)
                    tipPopoverController = popoverController
                }
                else {
                    log.debug("Should display is false in the TipPopoverCoordinator")
                    if viewController.presentedViewController is TipUIPopoverViewController {
                        viewController.dismiss(animated: true)
                        tipPopoverController = nil
                    }
                }
            }
        }

        MyTipWithTheParam.shouldShow = true
    }

I see the callback fired twice, where shouldShow evaluates to false both times.

Like I said it works fine if I remove the Paramater, but setting its value to true doesn't result in shouldDisplay evaluating to true, its still false.

What the heck could be going on here?

Thanks for any help!

UPDATE

I solved this - the NSLocalizedString was returning blank strings because I didn't have them in my files. As soon as I made sure the string wasn't blank for the message, it displayed.

No error logs or anything in the console when this validation error occurs tho... seems like something a log would be helpful for.

I'm still wondering how a developer would debug the shouldDisplay bool too, and how it decides, if anyone knows the answer to that.

Upvotes: 2

Views: 158

Answers (0)

Related Questions