//
//  DisableScroll.swift
//
//
//  Created by Victor on 02/08/2024.
//

import Foundation
import SwiftUI

struct DisableScroll: ViewModifier {
    @Binding var isEnabled: Bool
    
    func body(content: Content) -> some View {
        if !isEnabled {
            return AnyView(content.gesture(DragGesture().onChanged { _ in }))
        } else {
            return AnyView(content)
        }
    }
}

extension ScrollView {
    func scrollEnabled(_ isEnabled: Binding<Bool>) -> some View {
        self.modifier(DisableScroll(isEnabled: isEnabled))
    }
}

struct TAdaView: View {
    @State private var isScrollEnabled = true
    
    var body: some View {
        VStack {
            ScrollViewReader { proxy in
                ScrollView {
                    VStack {
                        ForEach(0..<50) { index in
                            Text("Item \(index)")
                                .padding()
                        }
                    }
                }
                .scrollEnabled($isScrollEnabled)
            }
            .overlay(
                Button("Toggle Scroll") {
                    isScrollEnabled.toggle()
                }
                .padding()
                .background(Color.blue)
                .cornerRadius(8)
                .foregroundColor(.white),
                alignment: .bottom
            )
            .padding()
        }
    }
}

#Preview {
    TAdaView()
}
