divadretlaw / CustomAlert

🔔 Custom Alert for SwiftUI
MIT License
190 stars 18 forks source link

Error with View with dynamic size #22

Closed MiguelDias90 closed 2 months ago

MiguelDias90 commented 2 months ago

Hi,

First of all, thank you for the excellent work!

When I call a CustomAlert dialog and the View has a fixed size, for example, .frame(width: 100, height: 100) no error is triggered.

However, if I use a dynamic size in case the View's height size is unknown, I use something like .frame(width: 100, height: viewHeight). The viewHeight is updated when the View's content is being rendered, when an .overlay() is applied to the View.

@State var viewHeight: CGFloat: .zero

var body: some View {
    ZStack {
        ZStack {
            // code
        }
        .overlay(
            GeometryReader { proxy in
                Color.clear.onAppear {
                    viewHeight = viewHeight + proxy.size.height
                }
            }
        )
    }
    .frame(width: 100, height: viewHeight)
}

As there are multiple calls to the CustomAlert dialog, it checks the frame size.

In my opinion, the error is due to the fact that the first time it tries to render the height is 0. The second time it renders, the size is known. And this triggers several errors in a row because the size is not the same as the initial one: onChange(of: UUID) action tried to update multiple times per frame.

This error does not occur in versions 3.3.0 and earlier, however it's possible to see the View being rendered from top to bottom (and not as a whole).

EDIT: I tested version 3.1.0, it's the most "recent" version that doesn't give the error and renders the View with dynamic size perfectly.

divadretlaw commented 2 months ago

Hi, thank you for opening an issue.

I have noticed the onChange(of: UUID) action tried to update multiple times per frame. in the log and I think they were caused by WindowKit how it propagates changes to the view in the window. I have pushed an update that should fix that issue.

Could you please try if either updating to WindowKit 2.5.0 or using CustomAlert from the main branch if that possibly fixes your issue(s)?

I'm not 100% sure how the dynamic sized view is used in CustomAlert. Is this dynamic sized view used as the content of the custom alert? It would help with investigating the issue if you could provide a working example where the issue(s) occur. Thank you very much in advance.

MiguelDias90 commented 2 months ago

Hi David,

I pulled the latest version from the main branch, and the WindowKit version 2.5.0 fixes this error. 🥳

Thank you for the excellent work you are putting in at both libraries, and for giving me such great support on a Sunday! 😎