Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions Sources/BSWInterfaceKit/SwiftUI/Views/SwipeableListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ private struct Item: Identifiable, Equatable {
}

#Preview {

@Previewable
@State
var items: [Item] = [
Expand Down Expand Up @@ -78,7 +79,7 @@ private struct Item: Identifiable, Equatable {
NavigationStack {
ScrollView {
SwipeableListView(
items: items,
items: $items,
isSwipeDisabled: { $0.id == "milan" },
rowContent: { item in
/// This is intentionally a Button to prove the swipe still works.
Expand Down Expand Up @@ -132,9 +133,9 @@ private struct Item: Identifiable, Equatable {

// MARK: - SwipeableListView

public struct SwipeableListView<Item: Identifiable, RowContent: View>: View {
public struct SwipeableListView<Item: Identifiable & Equatable, RowContent: View>: View {

@State
@Binding
var items: [Item]

@State
Expand All @@ -151,38 +152,48 @@ public struct SwipeableListView<Item: Identifiable, RowContent: View>: View {

public init(
spacing: Double = 4,
items: [Item],
items: Binding<[Item]>,
isSwipeDisabled: @escaping (Item) -> Bool = { _ in false },
@ViewBuilder rowContent: @escaping (Item) -> RowContent,
onDelete: @escaping Handler
) {
self.spacing = spacing
self.items = items
self._items = items
self.isSwipeDisabled = isSwipeDisabled
self.rowContent = rowContent
self.onDelete = onDelete
}

public var body: some View {
LazyVStack(spacing: spacing) {
listStack(spacing: spacing) {
ForEach(items) { item in
SwipeableRow(
id: item.id,
isDisabled: isSwipeDisabled(item),
openRowID: $openRowID,
content: { rowContent(item) },
handler: { id in
/// Remove locally to keep UI consistency while
/// the parent-owned source of truth updates.
onDelete(id)
withAnimation(.swipeable) {
items.removeAll { $0.id == id }
}
}
handler: onDelete
)
.transition(.swipeableRow)
}
}
.animation(.swipeable, value: items)
}

@ViewBuilder
private func listStack<Content: View>(
spacing: Double,
@ViewBuilder content: () -> Content
) -> some View {
#if os(Android)
VStack(spacing: spacing) {
content()
}
#else
LazyVStack(spacing: spacing) {
content()
}
#endif
}
}

Expand Down Expand Up @@ -246,7 +257,11 @@ struct SwipeableRow<ID: Hashable, Content: View>: View {
.overlay(alignment: .trailing) {
if !isDisabled {
Color.clear
.frame(width: Constants.swipeActivationWidth)
.frame(
maxWidth: baseOffsetX != 0
? .infinity
: Constants.swipeActivationWidth
)
.contentRectangleShape()
.gesture(dragGesture)
.zIndex(999)
Expand Down