135 lines
5.0 KiB
Swift
135 lines
5.0 KiB
Swift
//
|
|
// PreviewSupport.swift
|
|
// GasTankAR
|
|
//
|
|
// Created by Dennis Nemec on 26.08.26.
|
|
//
|
|
|
|
#if DEBUG
|
|
import Foundation
|
|
import SwiftData
|
|
import UIKit
|
|
|
|
/// Beispieldaten und In-Memory-Container ausschließlich für SwiftUI-Previews.
|
|
/// Wird über `#if DEBUG` aus Release-Builds ausgeschlossen.
|
|
@MainActor
|
|
enum PreviewSupport {
|
|
/// Container mit einer Reihe an Beispielprojekten.
|
|
static let container: ModelContainer = {
|
|
let container = makeEmptyContainer()
|
|
for project in sampleProjects() {
|
|
container.mainContext.insert(project)
|
|
}
|
|
return container
|
|
}()
|
|
|
|
/// Leerer In-Memory-Container (z. B. für den „Leer"-Zustand).
|
|
static func makeEmptyContainer() -> ModelContainer {
|
|
let configuration = ModelConfiguration(isStoredInMemoryOnly: true)
|
|
// swiftlint:disable:next force_try
|
|
return try! ModelContainer(for: Project.self, configurations: configuration)
|
|
}
|
|
|
|
/// Ein einzelnes Beispielprojekt inklusive Container (für Detail-Previews).
|
|
static func singleProjectContainer() -> (container: ModelContainer, project: Project) {
|
|
let container = makeEmptyContainer()
|
|
let project = Project(
|
|
leadID: "LEAD-1001",
|
|
customerName: "Müller Heizungsbau",
|
|
creationDate: .now,
|
|
tank: Tank()
|
|
)
|
|
container.mainContext.insert(project)
|
|
return (container, project)
|
|
}
|
|
|
|
/// Projekt inklusive Beispiel-Fotos (mit generierten Test-Bildern) für die Galerie-Preview.
|
|
/// Deckt mehrere Aufnahmetage sowie Fotos mit und ohne Standort ab.
|
|
static func galleryProjectContainer() -> (container: ModelContainer, project: Project) {
|
|
let container = makeEmptyContainer()
|
|
let project = Project(
|
|
leadID: "LEAD-1042",
|
|
customerName: "Müller Heizungsbau",
|
|
creationDate: .now
|
|
)
|
|
container.mainContext.insert(project)
|
|
|
|
let day: TimeInterval = 86_400
|
|
let samples: [(color: UIColor, label: String, offset: TimeInterval, lat: Double?, lon: Double?)] = [
|
|
(.systemBlue, "1", 0, 52.5200, 13.4050),
|
|
(.systemGreen, "2", -3_600, 48.1372, 11.5756),
|
|
(.systemOrange, "3", -day, nil, nil),
|
|
(.systemPurple, "4", -3 * day, 50.1109, 8.6821),
|
|
]
|
|
for sample in samples {
|
|
let photo = ProjectPhoto(
|
|
creationDate: Date.now.addingTimeInterval(sample.offset),
|
|
imageData: sampleImageData(color: sample.color, label: sample.label),
|
|
latitude: sample.lat,
|
|
longitude: sample.lon,
|
|
project: project
|
|
)
|
|
container.mainContext.insert(photo)
|
|
}
|
|
return (container, project)
|
|
}
|
|
|
|
/// Einzelnes Beispiel-Foto (mit Standort) für die Detail-Preview.
|
|
static func samplePhoto() -> ProjectPhoto {
|
|
ProjectPhoto(
|
|
creationDate: .now,
|
|
imageData: sampleImageData(color: .systemTeal, label: "AR"),
|
|
latitude: 52.5200,
|
|
longitude: 13.4050
|
|
)
|
|
}
|
|
|
|
/// Erzeugt ein einfarbiges Test-Bild mit zentrierter Beschriftung als JPEG-Daten.
|
|
static func sampleImageData(color: UIColor, label: String) -> Data {
|
|
let size = CGSize(width: 1200, height: 900)
|
|
let image = UIGraphicsImageRenderer(size: size).image { context in
|
|
color.setFill()
|
|
context.fill(CGRect(origin: .zero, size: size))
|
|
|
|
let text = label as NSString
|
|
let attributes: [NSAttributedString.Key: Any] = [
|
|
.font: UIFont.boldSystemFont(ofSize: 240),
|
|
.foregroundColor: UIColor.white,
|
|
]
|
|
let textSize = text.size(withAttributes: attributes)
|
|
let origin = CGPoint(
|
|
x: (size.width - textSize.width) / 2,
|
|
y: (size.height - textSize.height) / 2
|
|
)
|
|
text.draw(at: origin, withAttributes: attributes)
|
|
}
|
|
return image.jpegData(compressionQuality: 0.8) ?? Data()
|
|
}
|
|
|
|
/// Erzeugt eine Reihe von Beispielprojekten.
|
|
static func sampleProjects(count: Int = 40) -> [Project] {
|
|
let names: [String] = [
|
|
"Müller Heizungsbau", "Schmidt GmbH", "Weber & Söhne", "Fischer Sanitär",
|
|
"Becker Klimatechnik", "Hoffmann Anlagenbau", "Wagner & Partner", "Schulz Energie",
|
|
"Koch Gasversorgung", "Richter Industrie", "Klein Tankbau", "Wolf Technik",
|
|
"Neumann GmbH", "Schwarz Heizsysteme", "Zimmermann AG", "Braun Installationen",
|
|
]
|
|
|
|
var projects: [Project] = []
|
|
for index in 0..<count {
|
|
let leadNumber: Int = 1001 + index
|
|
let leadID: String = "LEAD-" + String(leadNumber)
|
|
let offset: TimeInterval = TimeInterval(-index) * 86_400
|
|
let project = Project(
|
|
leadID: leadID,
|
|
customerName: names[index % names.count],
|
|
creationDate: Date.now.addingTimeInterval(offset),
|
|
tank: Tank()
|
|
)
|
|
projects.append(project)
|
|
}
|
|
return projects
|
|
}
|
|
}
|
|
#endif
|