2024-12-11 20:44:44 -05:00
|
|
|
//
|
|
|
|
|
// SeriesConfiguration.swift
|
|
|
|
|
// Meshtastic
|
|
|
|
|
//
|
|
|
|
|
// Created by Jake Bordens on 12/7/24.
|
|
|
|
|
//
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
|
|
class MetricsColumnList: ObservableObject, RandomAccessCollection, RangeReplaceableCollection {
|
|
|
|
|
|
|
|
|
|
@Published var columns: [MetricsTableColumn]
|
|
|
|
|
|
|
|
|
|
init(columns: [MetricsTableColumn]) {
|
|
|
|
|
self.columns = columns
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var visible: [MetricsTableColumn] {
|
|
|
|
|
return columns.filter { $0.visible }
|
|
|
|
|
}
|
2024-12-13 08:01:09 -05:00
|
|
|
|
2024-12-11 20:44:44 -05:00
|
|
|
func toggleVisibity(for column: MetricsTableColumn) {
|
|
|
|
|
if columns.contains(column) {
|
|
|
|
|
self.objectWillChange.send()
|
|
|
|
|
column.visible.toggle()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var gridItems: [GridItem] {
|
|
|
|
|
var returnValues: [GridItem] = []
|
|
|
|
|
let columnsInChart = self.visible
|
|
|
|
|
for i in 0..<columnsInChart.count {
|
|
|
|
|
let thisColumn = columnsInChart[i]
|
|
|
|
|
let spacing = (i == columns.count - 1) ? 0 : thisColumn.spacing
|
|
|
|
|
if let min = thisColumn.minWidth, let max = thisColumn.maxWidth {
|
|
|
|
|
returnValues.append(
|
|
|
|
|
GridItem(
|
|
|
|
|
.flexible(minimum: min, maximum: max), spacing: spacing)
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
returnValues.append(GridItem(.flexible(), spacing: spacing))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return returnValues
|
|
|
|
|
}
|
2024-12-13 15:54:25 -05:00
|
|
|
|
2025-02-21 18:23:03 -05:00
|
|
|
func column(withId id: String) -> MetricsTableColumn? {
|
|
|
|
|
return columns.first(where: { $0.id == id})
|
2024-12-13 15:54:25 -05:00
|
|
|
}
|
|
|
|
|
|
2024-12-11 20:44:44 -05:00
|
|
|
// Collection conformance
|
|
|
|
|
typealias Index = Int
|
|
|
|
|
typealias Element = MetricsTableColumn
|
|
|
|
|
typealias SubSequence = ArraySlice<Element>
|
2024-12-13 08:01:09 -05:00
|
|
|
|
2024-12-11 20:44:44 -05:00
|
|
|
required init() { columns = [] }
|
|
|
|
|
required init<S: Sequence>(_ columns: S) where S.Element == Element {
|
|
|
|
|
self.columns = Array(columns)
|
|
|
|
|
}
|
2024-12-13 08:01:09 -05:00
|
|
|
|
2024-12-11 20:44:44 -05:00
|
|
|
var startIndex: Int { columns.startIndex }
|
|
|
|
|
var endIndex: Int { columns.endIndex }
|
2024-12-13 08:01:09 -05:00
|
|
|
|
2024-12-11 20:44:44 -05:00
|
|
|
subscript(position: Int) -> Element {
|
|
|
|
|
get { columns[position] }
|
|
|
|
|
set {
|
|
|
|
|
objectWillChange.send()
|
|
|
|
|
columns[position] = newValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
subscript(bounds: Range<Int>) -> ArraySlice<Element> { columns[bounds] }
|
|
|
|
|
func index(after i: Int) -> Int { columns.index(after: i) }
|
2024-12-13 08:01:09 -05:00
|
|
|
|
2024-12-11 20:44:44 -05:00
|
|
|
func replaceSubrange<C: Collection>(_ subrange: Range<Int>, with newElements: C) where C.Element == Element {
|
|
|
|
|
objectWillChange.send()
|
|
|
|
|
columns.replaceSubrange(subrange, with: newElements)
|
|
|
|
|
}
|
2024-12-13 08:01:09 -05:00
|
|
|
|
2024-12-11 20:44:44 -05:00
|
|
|
func append(_ newElement: Element) {
|
|
|
|
|
columns.append(newElement)
|
|
|
|
|
objectWillChange.send()
|
|
|
|
|
}
|
2025-01-01 10:45:25 -08:00
|
|
|
|
2024-12-11 20:44:44 -05:00
|
|
|
func remove(at index: Int) -> Element {
|
|
|
|
|
objectWillChange.send()
|
|
|
|
|
let removedElement = columns.remove(at: index)
|
|
|
|
|
return removedElement
|
|
|
|
|
}
|
2025-01-01 10:45:25 -08:00
|
|
|
|
2024-12-11 20:44:44 -05:00
|
|
|
func removeAll() {
|
|
|
|
|
objectWillChange.send()
|
|
|
|
|
columns.removeAll()
|
|
|
|
|
}
|
2025-01-01 10:45:25 -08:00
|
|
|
|
2024-12-11 20:44:44 -05:00
|
|
|
func insert(_ newElement: Element, at index: Int) {
|
|
|
|
|
objectWillChange.send()
|
|
|
|
|
columns.insert(newElement, at: index)
|
|
|
|
|
}
|
|
|
|
|
}
|