-
-
Notifications
You must be signed in to change notification settings - Fork 61
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
Open
MiaKoring
wants to merge
14
commits into
stackotter:main
Choose a base branch
from
MiaKoring:fix/foreach-removals
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e9b94a8
Fixed reordering, insertion and removal in ForEach where Element: Ide…
MiaKoring 116b72c
Made it work for non identifiables
MiaKoring 466c259
added fallback to old code where no keypath is specified
MiaKoring 76968ca
added node reusing bypass after discovering first duplicate
MiaKoring 2dba16b
Fixed ForEach [MenuItem] compatibility, documentation improvement & c…
MiaKoring 25c6e66
added tvOS excluding compiler flag for Slider Component in ForEachExa…
MiaKoring d582598
Fixed reordering, insertion and removal in ForEach where Element: Ide…
MiaKoring 743a488
Made it work for non identifiables
MiaKoring 30332e6
should fix remaining rebase problems
MiaKoring 1b22d8b
Changed version of swift-collections to 1.2.1 to match swift 5.10
MiaKoring e49b391
Implemented requested changes, potentially additional ForEach initial…
MiaKoring ff93fc6
Merge branch 'main' into fix/foreach-removals
MiaKoring 3a965fe
fixed my merge messup
MiaKoring a2e1080
replaced upToNextMajor with upToNextMinor because major would lead to…
MiaKoring File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import DefaultBackend | ||
| import Foundation | ||
| import SwiftCrossUI | ||
|
|
||
| #if canImport(SwiftBundlerRuntime) | ||
| import SwiftBundlerRuntime | ||
| #endif | ||
|
|
||
| @main | ||
| @HotReloadable | ||
| struct ForEachApp: App { | ||
| @State var items = (0..<20).map { Item("\($0)") } | ||
| @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) { | ||
| let upperLimit = max(items.count - 1, 0) | ||
| insertionPosition = min(insertionPosition, upperLimit) | ||
| } | ||
| #endif | ||
|
|
||
| ForEach(Array(items.enumerated()), id: \.element.id) { (index, item) in | ||
| ItemRow( | ||
| item: item, | ||
| isFirst: index == 0, | ||
| isLast: index == items.count - 1 | ||
| ) { | ||
| items.remove(at: index) | ||
| } moveUp: { | ||
| guard index != items.startIndex else { return } | ||
| items.swapAt(index, index - 1) | ||
| } moveDown: { | ||
| guard index != items.endIndex else { return } | ||
| items.swapAt(index, index + 1) | ||
| } | ||
| } | ||
| } | ||
| .padding(10) | ||
| } | ||
| } | ||
| } | ||
| .defaultSize(width: 400, height: 800) | ||
| } | ||
| } | ||
|
|
||
| struct ItemRow: View { | ||
| var item: Item | ||
| 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) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| struct Item: Identifiable { | ||
| let id = UUID() | ||
| var value: String | ||
|
|
||
| init(_ value: String) { | ||
| self.value = value | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be best to have the
ForEachiterate overitems.enumeratedso that each ItemRow can know its own index and avoid this linear operation in what could be a constant-timemoveUpimplementation (same formoveDown). I'm pointing this out cause I can see ForEachExample being used to stress test SwiftCrossUI's performance, in which case these move functions should be implemented with best practice.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new ForEach would look like this:
EnumeratedSequence.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 ^^