#1098 - Switched to dynamically calculated row heights.

This commit is contained in:
Stefan Ceriu
2021-10-11 16:56:34 +03:00
parent 034fb38f07
commit ce4d41fd0d
2 changed files with 36 additions and 18 deletions
@@ -21,9 +21,10 @@ import SwiftUI
@available(iOS 14.0, *)
struct UserSuggestionList: View {
private struct Constants {
static let rowHeight: CGFloat = 60.0
static let maxHeight: CGFloat = 300.0
static let listItemPadding: CGFloat = 4.0
static let lineSpacing: CGFloat = 10.0
static let maxHeight: CGFloat = 300.0
static let maxVisibleRows = 4
}
// MARK: - Properties
@@ -31,31 +32,48 @@ struct UserSuggestionList: View {
// MARK: Private
@Environment(\.theme) private var theme: ThemeSwiftUI
@State private var prototypeListItemFrame: CGRect = .zero
// MARK: Public
@ObservedObject var viewModel: UserSuggestionViewModel.Context
var body: some View {
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([.top, .bottom], Constants.listItemPadding)
if viewModel.viewState.items.isEmpty {
EmptyView()
} else {
ZStack {
UserSuggestionListItem(avatar: AvatarInput(mxContentUri: "", matrixItemId: "", displayName: "Prototype"),
displayName: "Prototype",
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([.top, .bottom], 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.
}
}
.listStyle(PlainListStyle())
.environment(\.defaultMinListRowHeight, Constants.rowHeight)
.frame(height: min(Constants.maxHeight, Constants.rowHeight * CGFloat(viewModel.viewState.items.count)))
.id(UUID()) // Rebuild the whole list on item changes. Fixes performance issues.
}
}
private func contentHeightForRowCount(_ count: Int) -> CGFloat {
(prototypeListItemFrame.height + (Constants.listItemPadding * 2) + Constants.lineSpacing) * CGFloat(count)
}
}
@available(iOS 14.0, *)