Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 5 additions & 9 deletions Sources/CollectionView/SelectableForEach.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,13 @@ public struct SelectableForEach<
public var body: some View {
ForEach(IndexedCollection(data)) { element in

let isSelected: Bool = selection.isSelected(for: element.id)
let isSelected: Bool = selection.isSelected(for: element.value.id)
let isDisabled: Bool = !selection.isEnabled(for: element.id)

cell(element.index, element.value)
.disabled(isDisabled)
.environment(\.collectionView_isSelected, isSelected)
.environment(
\.collectionView_updateSelection,
{ [selection] isSelected in
selection.update(isSelected: isSelected, for: element.value)
})
selection.applyEnvironments(
for: cell(element.index, element.value),
item: element.value
)
}
}

Expand Down
21 changes: 21 additions & 0 deletions Sources/CollectionView/SelectionState.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import SwiftUI

public enum SelectAction {
case selected
Expand All @@ -18,6 +19,26 @@ public protocol SelectionState<Item> {
func update(isSelected: Bool, for item: Item)
}

extension SelectionState {

public func applyEnvironments<Body: View>(for body: Body, item: Item) -> some View {

let isSelected: Bool = isSelected(for: item.id)
let isDisabled: Bool = !isEnabled(for: item.id)

return body
.disabled(isDisabled)
.environment(\.collectionView_isSelected, isSelected)
.environment(
\.collectionView_updateSelection,
{ isSelected in
self.update(isSelected: isSelected, for: item)
}
)
}

}

extension SelectionState {

public static func single<Item: Identifiable>(
Expand Down