weitieda/bottom-sheet

Unable to mimic .sheet usage for project.

haIIux opened this issue · 3 comments

Forgive me please, as this is my first attempt at an issue and I'm new to programming. I am trying to utilize your wonderful SPM project in my own application.

@State var presentedLocation: Location?
           
 .sheet(item: $presentedLocation) { place in
    SelectedBusinessSheet(selectedBusiness: place)
}

Above is how I would call this sheet and display the data. the presentedLocation is an identifiable struct that is used to create an array of data. This method displays the sheet as desired but it looks poorly and would look better in a BottomSheet.

I am unable to mimic this method however, as displayed below, I get the following errors.

.bottomSheet(isPresented: $presentedLocation, height: 700, content: { place in
   SelectedBusinessSheet(selectedBusiness: place)
})

  • Cannot convert value of type 'Binding<Location?>' to expected argument type 'Binding'
  • Contextual closure type '() -> SelectedBusinessSheet' expects 0 arguments, but 1 was used in closure body

I apologize if these are obvious errors that can be easily rectified. Any help is appreciated!

I have the same issue.
This would indeed be a great improvement.

I tried to create an extension to add this functionality, but it didn't work.

extension View {
    func bottomSheet<Item: Identifiable, Content: View>(
        item: Binding<Item?>,
        height: CGFloat,
        topBarHeight: CGFloat = 30,
        topBarCornerRadius: CGFloat? = nil,
        contentBackgroundColor: Color = Color(.systemBackground),
        topBarBackgroundColor: Color = Color(.systemBackground),
        showTopIndicator: Bool = true,
        @ViewBuilder content: @escaping (Item) -> Content
    ) -> some View {
        let isPresented = Binding {
            item.wrappedValue != nil
        } set: { value in
            if !value {
                item.wrappedValue = nil
            }
        }
        
        return self.bottomSheet(
            isPresented: isPresented,
            height: height,
            topBarHeight: topBarHeight,
            topBarCornerRadius: topBarCornerRadius,
            contentBackgroundColor: contentBackgroundColor,
            topBarBackgroundColor: topBarBackgroundColor,
            showTopIndicator: showTopIndicator
        ) {
            if let unwrapedItem = item.wrappedValue {
                content(unwrapedItem)
            } else {
                EmptyView()
            }
        }
    }
}

I'm not sure if I'm missing something.

@weitieda do you have any idea of what I'm doing wrong?

EDIT: I have updated the code snippet and It works now!

I opened #16 that fixes this issue.