Fix performance issues with lists

This commit is contained in:
Christoph Hagen 2025-02-07 14:09:07 +01:00
parent dc7ab6fb15
commit 1bc40bfb47
5 changed files with 59 additions and 35 deletions

View File

@ -51,14 +51,20 @@ struct FileListView: View {
TextField("", text: $searchString, prompt: Text("Search")) TextField("", text: $searchString, prompt: Text("Search"))
.textFieldStyle(.roundedBorder) .textFieldStyle(.roundedBorder)
.padding(.horizontal, 8) .padding(.horizontal, 8)
List(filteredFiles) { file in ScrollView {
LazyVStack(spacing: 0) {
ForEach(filteredFiles) { file in
SelectableListItem(selected: selectedFile == file) { SelectableListItem(selected: selectedFile == file) {
Text(file.id) Text(file.id)
} }
.id(file.id)
.onTapGesture { .onTapGesture {
selectedFile = file selectedFile = file
} }
} }
}
.padding(.horizontal, 8)
}
.onChange(of: selectedFileType) { oldValue, newValue in .onChange(of: selectedFileType) { oldValue, newValue in
guard oldValue != newValue else { guard oldValue != newValue else {
return return

View File

@ -2,25 +2,28 @@ import SwiftUI
struct SelectableListItem<Content>: View where Content: View { struct SelectableListItem<Content>: View where Content: View {
let content: Content let content: () -> Content
let selected: Bool let selected: Bool
public init(selected: Bool, @ViewBuilder content: () -> Content) { public init(selected: Bool, @ViewBuilder content: @escaping () -> Content) {
self.selected = selected self.selected = selected
self.content = content() self.content = content
} }
var body: some View { var body: some View {
HStack { HStack {
content content()
Spacer() Spacer()
} }
.padding(.horizontal, 4)
.padding(.vertical, 7)
.foregroundStyle(selected ? Color.white : Color.primary) .foregroundStyle(selected ? Color.white : Color.primary)
.listRowBackground(RoundedRectangle(cornerRadius: 5) .background(selected ? Color.blue : Color.clear)
.fill(selected ? Color.blue : Color.clear) .clipShape(RoundedRectangle(cornerRadius: 5))
.padding(.horizontal, 10)
)
.contentShape(Rectangle()) .contentShape(Rectangle())
.listRowInsets(.init(top: 0, leading: -8, bottom: 0, trailing: -8))
.listRowBackground(Color.clear)
.listRowSeparator(.hidden)
} }
} }

View File

@ -63,14 +63,19 @@ struct PageListView: View {
TextField("", text: $searchString, prompt: Text("Search")) TextField("", text: $searchString, prompt: Text("Search"))
.textFieldStyle(.roundedBorder) .textFieldStyle(.roundedBorder)
.padding(.horizontal, 8) .padding(.horizontal, 8)
List(filteredPages) { page in ScrollView {
LazyVStack(spacing: 0) {
ForEach(filteredPages) { page in
SelectableListItem(selected: selection.page == page) { SelectableListItem(selected: selection.page == page) {
PageListItem(page: page) PageListItem(page: page)
} }
.id(page)
.onTapGesture { .onTapGesture {
selection.page = page selection.page = page
} }
} }
}.padding(.horizontal, 8)
}
} }
.onAppear { .onAppear {
if selection.page == nil, let first = content.pages.first { if selection.page == nil, let first = content.pages.first {

View File

@ -72,14 +72,19 @@ struct PostListView: View {
TextField("", text: $searchString, prompt: Text("Search")) TextField("", text: $searchString, prompt: Text("Search"))
.textFieldStyle(.roundedBorder) .textFieldStyle(.roundedBorder)
.padding(.horizontal, 8) .padding(.horizontal, 8)
List(filteredAndSortedPosts) { post in ScrollView {
LazyVStack(spacing: 0) {
ForEach(filteredAndSortedPosts) { post in
SelectableListItem(selected: selection.post == post) { SelectableListItem(selected: selection.post == post) {
PostListItem(post: post) PostListItem(post: post)
} }
.id(post)
.onTapGesture { .onTapGesture {
selection.post = post selection.post = post
} }
} }
}.padding(.horizontal, 8)
}
}.onAppear { }.onAppear {
if selection.post == nil, let first = content.posts.first { if selection.post == nil, let first = content.posts.first {
selection.post = first selection.post = first

View File

@ -32,14 +32,19 @@ struct TagListView: View {
TextField("", text: $searchString, prompt: Text("Search")) TextField("", text: $searchString, prompt: Text("Search"))
.textFieldStyle(.roundedBorder) .textFieldStyle(.roundedBorder)
.padding(.horizontal, 8) .padding(.horizontal, 8)
List(filteredAndSortedTags) { tag in ScrollView {
LazyVStack(spacing: 0) {
ForEach(filteredAndSortedTags) { tag in
SelectableListItem(selected: selection.tag == tag) { SelectableListItem(selected: selection.tag == tag) {
TagListItem(tag: tag.localized(in: language)) TagListItem(tag: tag.localized(in: language))
} }
.id(tag)
.onTapGesture { .onTapGesture {
selection.tag = tag selection.tag = tag
} }
} }
}.padding(.horizontal, 8)
}
}.onAppear { }.onAppear {
if selection.tag == nil, let first = content.tags.first { if selection.tag == nil, let first = content.tags.first {
selection.tag = first selection.tag = first