nightshift/bug: drag-reorder has no effect — filtered() always sorts by useCount#2
Open
nightshift/bug: drag-reorder has no effect — filtered() always sorts by useCount#2
Conversation
filtered(by:) always sorted snippets by useCount, making manual drag-reordering via move(from:to:) invisible — the backing array was saved correctly but the view immediately re-rendered in useCount order. Now filtered(by:) returns snippets in their stored order when the query is empty, so drag-reordering is preserved. When a search query is active results are still sorted by useCount so the most-used matches surface first.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Bug
SnippetStore.filtered(by:)inSources/SnippetStore.swiftalways sorts the snippet list byuseCountdescending before returning it, even when the query is empty.SnippetStore.move(from:to:)correctly reorderssnippetsin-place and callssave(), but the very next SwiftUI render callsfiltered(by: "")again and re-sorts the array byuseCount, silently undoing the user's drag.Result: drag-to-reorder appears to do nothing — the list springs back to
useCountorder on every frame.Repro
useCountvalues.useCountorder; the manual position is lost.Reading the code confirms this without running the app:
filtered(by: "")always executeslet sorted = snippets.sorted { $0.useCount > $1.useCount }and returnssorted, discardingmove()'s reorder.Fix
filtered(by:)now returnssnippets(stored order) directly when the query is empty, and only sorts byuseCountwhen a non-empty search query is active so the most-relevant results surface first.