Files
Pulse/Sources/PulseUI/Views/KeyValueGridView.swift
2022-07-27 18:49:46 -04:00

50 lines
1.3 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// The MIT License (MIT)
//
// Copyright (c) 20202022 Alexander Grebenyuk (github.com/kean).
import SwiftUI
#if os(iOS) || os(macOS)
struct KeyValueGridView: View {
#if os(iOS)
@Environment(\.horizontalSizeClass) var sizeClass: UserInterfaceSizeClass?
#endif
let items: [KeyValueSectionViewModel]
var body: some View {
#if os(iOS)
let isTwoColumnEnabled = sizeClass == .regular && items.count > 1
#else
let isTwoColumnEnabled = items.count > 1
#endif
if isTwoColumnEnabled {
VStack(spacing: 16) {
let rows = items.chunked(into: 2).enumerated().map {
Row(index: $0, items: $1)
}
ForEach(rows, id: \.index) { row in
HStack(alignment: .top, spacing: 16) {
ForEach(row.items, id: \.title) { item in
KeyValueSectionView(viewModel: item)
}
}
}
}
} else {
VStack(spacing: 16) {
ForEach(items, id: \.title) {
KeyValueSectionView(viewModel: $0)
}
}
}
}
}
private struct Row {
let index: Int
let items: [KeyValueSectionViewModel]
}
#endif