faberNovel / DynamicOverlay

A SwiftUI library that makes easier to develop overlay based interfaces, such as the one presented in the Apple Maps app.
MIT License
212 stars 21 forks source link

Crash with drivingScrollView a List and a condition #21

Closed dynamiccast closed 3 years ago

dynamiccast commented 3 years ago

Hi,

I think #20 introduced an issue when combining dynamic view composition with drivingScrollView. Considering the following code, heavily inspired by https://github.com/faberNovel/DynamicOverlay/issues/18

import SwiftUI
import DynamicOverlay

struct ContentView: View {
    @State var notch: Notch = .min
    @State var showContent2 = false

    enum Notch: CaseIterable, Equatable { case min }

    var body: some View {
        Color.white
            .dynamicOverlay(overlayView)
            .dynamicOverlayBehavior(behavior)
    }

    private var behavior: some DynamicOverlayBehavior {
        MagneticNotchOverlayBehavior<Notch> { _ in .absolute(showContent2 ? 400 : 200) }
    }

    private var overlayView: some View {
        VStack(spacing: 0) {
            Color.blue.frame(height: 50).draggable()
            if showContent2 {
                OverlayContent(title: "Content 1", color: Color.red, action: { withAnimation { showContent2.toggle() }})
            } else {
                OverlayContent(title: "Content 1", color: Color.red, action: { withAnimation { showContent2.toggle() }})
            }
        }
    }
}

struct OverlayContent: View {
    let title: String
    let color: Color
    let action: () -> Void

    var body: some View {
        ZStack(alignment: .top) {
            color
            VStack {
                Text(title).font(.title)
                Button(action: action, label: { Text("Action crash") }).padding()
                List {
                    Button(action: action, label: { Text("Action fine") }).padding()
                }.drivingScrollView() // remove me fix everything
            }
        }
    }
}

@main
struct MapApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Obviously, the crash being in ScrollViewdelegateProxy, not applying a drivingScrollView to the list fixes the issue as well.

Rolling back to 1.0.0-beta.8 fixes the crash but obviously does not include the fix in #20.

Being new to DynamicOverlay and Swift in general I am having a hard time understanding what is going on.