-
-
Notifications
You must be signed in to change notification settings - Fork 62
Fix ForEach Collection change rendered View correctness #245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 10 commits
e9b94a8
116b72c
466c259
76968ca
2dba16b
25c6e66
d582598
743a488
30332e6
1b22d8b
e49b391
ff93fc6
3a965fe
a2e1080
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import DefaultBackend | ||
| import Foundation | ||
| import SwiftCrossUI | ||
|
|
||
| #if canImport(SwiftBundlerRuntime) | ||
| import SwiftBundlerRuntime | ||
| #endif | ||
|
|
||
| @main | ||
| @HotReloadable | ||
| struct ForEachApp: App { | ||
| @State var items = { | ||
| var items = [Item]() | ||
| for i in 0..<20 { | ||
| items.append(.init("\(i)")) | ||
| } | ||
| return items | ||
| }() | ||
| @State var biggestValue = 19 | ||
| @State var insertionPosition = 10 | ||
|
|
||
| var body: some Scene { | ||
| WindowGroup("ForEach") { | ||
| #hotReloadable { | ||
| ScrollView { | ||
| VStack { | ||
| Button("Append") { | ||
| biggestValue += 1 | ||
| items.append(.init("\(biggestValue)")) | ||
| } | ||
|
|
||
| #if !os(tvOS) | ||
| Button( | ||
| "Insert in front of current item at position \(insertionPosition)" | ||
| ) { | ||
| biggestValue += 1 | ||
| items.insert(.init("\(biggestValue)"), at: insertionPosition) | ||
| } | ||
|
|
||
| Slider($insertionPosition, minimum: 0, maximum: items.count - 1) | ||
| .onChange(of: items.count) { | ||
| guard insertionPosition > items.count - 1 else { | ||
| return | ||
| } | ||
| insertionPosition = max(items.count - 1, 0) | ||
MiaKoring marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| #endif | ||
|
|
||
| ForEach(items) { item in | ||
| ItemRow( | ||
| item: item, isFirst: Optional(item.id) == items.first?.id, | ||
MiaKoring marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| isLast: Optional(item.id) == items.last?.id | ||
| ) { | ||
| items.removeAll(where: { $0.id == item.id }) | ||
MiaKoring marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } moveUp: { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it'd be best to have the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new ForEach would look like this: ForEach(Array(items.enumerated()), id: \.element.id) { (index, item) inEnumeratedSequence.Element is not identifiable, so I have to specify the identifier path Also EnumeratedSequence is only conform to Collection on macOS 26+, so I had to wrapp it in an array. If this is fine with you we can leave it like that. Otherwise additional initializers could do it for you and I could remove the necessity to specify a path on enumerated sequences where the element is identifiable. Just let me know ^^ |
||
| guard | ||
| let ownIndex = items.firstIndex(where: { $0.id == item.id }), | ||
| ownIndex != items.startIndex | ||
| else { return } | ||
| items.swapAt(ownIndex, ownIndex - 1) | ||
| } moveDown: { | ||
| guard | ||
| let ownIndex = items.firstIndex(where: { $0.id == item.id }), | ||
| ownIndex != items.endIndex | ||
| else { return } | ||
| items.swapAt(ownIndex, ownIndex + 1) | ||
| } | ||
| } | ||
| } | ||
| .padding(10) | ||
| } | ||
| } | ||
| } | ||
| .defaultSize(width: 400, height: 800) | ||
| } | ||
| } | ||
|
|
||
| struct ItemRow: View { | ||
| @State var item: Item | ||
MiaKoring marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let isFirst: Bool | ||
| let isLast: Bool | ||
| var remove: () -> Void | ||
| var moveUp: () -> Void | ||
| var moveDown: () -> Void | ||
|
|
||
| var body: some View { | ||
| HStack { | ||
| Text(item.value) | ||
| Button("Delete") { remove() } | ||
| Button("⌃") { moveUp() } | ||
| .disabled(isFirst) | ||
| Button("⌄") { moveDown() } | ||
| .disabled(isLast) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class Item: Identifiable, SwiftCrossUI.ObservableObject { | ||
| let id = UUID() | ||
| @SwiftCrossUI.Published var value: String | ||
|
|
||
| init(_ value: String) { | ||
| self.value = value | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,6 +110,10 @@ let package = Package( | |
| url: "https://github.com/stackotter/swift-winui", | ||
| revision: "1695ee3ea2b7a249f6504c7f1759e7ec7a38eb86" | ||
| ), | ||
| .package( | ||
| url: "https://github.com/apple/swift-collections.git", | ||
| exact: "1.2.1" | ||
|
||
| ), | ||
| // .package( | ||
| // url: "https://github.com/stackotter/TermKit", | ||
| // revision: "163afa64f1257a0c026cc83ed8bc47a5f8fc9704" | ||
|
|
@@ -129,6 +133,7 @@ let package = Package( | |
| dependencies: [ | ||
| "HotReloadingMacrosPlugin", | ||
| .product(name: "ImageFormats", package: "swift-image-formats"), | ||
| .product(name: "OrderedCollections", package: "swift-collections") | ||
| ], | ||
| exclude: [ | ||
| "Builders/ViewBuilder.swift.gyb", | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.