How to handle wait until alertIsPresent with java client 5.0.4

Hi there,
in Java client 4.x I used the following code to wait until alert showed up:

WebDriverWait wait = new WebDriverWait(driver.getDriver(), time);
try {

… but in Java client 5.0.4, “ExpectedConditions.alertIsPresent()” is not accepted. How can I achieve the same result now with new client?

I managed to write this code:

Wait wait = new FluentWait<>(driver).withTimeout(30, TimeUnit.SECONDS);
wait.until((AppiumFunction<Appium, Alert>) input → input.getDriver().switchTo().alert()).accept();
}catch(Exception e){…

… but this seems to work only if alert is already displayed by the time this function is being called. If it is called sooner, Exception is thrown… Even though 30 seconds is put as parameter in ‘withTimeout’, exception is throw sooner.

Any better approach to this? It doesn’t need to be Alert, it could go for any WebElement.

Some reference: