Allow displaying UserSuggestionList without shadow

This commit is contained in:
aringenbach
2023-03-23 14:51:56 +01:00
parent e29b40fdfc
commit 0c061edf60
2 changed files with 28 additions and 19 deletions
@@ -35,6 +35,7 @@ struct UserSuggestionList: View {
// MARK: Public
@ObservedObject var viewModel: UserSuggestionViewModel.Context
var showBackgroundShadow: Bool = true
var body: some View {
if viewModel.viewState.items.isEmpty {
@@ -46,25 +47,12 @@ struct UserSuggestionList: View {
userId: "Prototype")
.background(ViewFrameReader(frame: $prototypeListItemFrame))
.hidden()
BackgroundView {
List(viewModel.viewState.items) { item in
Button {
viewModel.send(viewAction: .selectedItem(item))
} label: {
UserSuggestionListItem(
avatar: item.avatar,
displayName: item.displayName,
userId: item.id
)
.padding(.bottom, Constants.listItemPadding)
.padding(.top, viewModel.viewState.items.first?.id == item.id ? Constants.listItemPadding + Constants.topPadding : Constants.listItemPadding)
}
if showBackgroundShadow {
BackgroundView {
list()
}
.listStyle(PlainListStyle())
.frame(height: min(Constants.maxHeight,
min(contentHeightForRowCount(Constants.maxVisibleRows),
contentHeightForRowCount(viewModel.viewState.items.count))))
.id(UUID()) // Rebuild the whole list on item changes. Fixes performance issues.
} else {
list()
}
}
}
@@ -73,6 +61,27 @@ struct UserSuggestionList: View {
private func contentHeightForRowCount(_ count: Int) -> CGFloat {
(prototypeListItemFrame.height + (Constants.listItemPadding * 2) + Constants.lineSpacing) * CGFloat(count) + Constants.topPadding
}
private func list() -> some View {
List(viewModel.viewState.items) { item in
Button {
viewModel.send(viewAction: .selectedItem(item))
} label: {
UserSuggestionListItem(
avatar: item.avatar,
displayName: item.displayName,
userId: item.id
)
.padding(.bottom, Constants.listItemPadding)
.padding(.top, viewModel.viewState.items.first?.id == item.id ? Constants.listItemPadding + Constants.topPadding : Constants.listItemPadding)
}
}
.listStyle(PlainListStyle())
.frame(height: min(Constants.maxHeight,
min(contentHeightForRowCount(Constants.maxVisibleRows),
contentHeightForRowCount(viewModel.viewState.items.count))))
.id(UUID()) // Rebuild the whole list on item changes. Fixes performance issues.
}
}
private struct BackgroundView<Content: View>: View {