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

View File

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

View File

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

View File

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

View File

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