Files
Pulse/Sources/PulseUI/Features/Console/ConsoleMessageView.swift
2022-08-07 20:52:47 -04:00

87 lines
2.0 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
import Pulse
import CoreData
import Combine
struct ConsoleMessageView: View {
let viewModel: ConsoleMessageViewModel
var body: some View {
VStack(alignment: .leading, spacing: 4) {
HStack(alignment: .firstTextBaseline) {
title
#if os(macOS)
PinView(viewModel: viewModel.pinViewModel, font: fonts.title)
Spacer()
#endif
}
text
}
.padding(.vertical, 4)
}
private var title: some View {
badge + (Text(viewModel.title)
.foregroundColor(.secondary)
.font(fonts.title))
}
private var badge: Text {
guard let badge = viewModel.badge else {
return Text("")
}
var separator: Text {
#if os(watchOS)
return Text("\n")
#else
return Text(" · ")
.font(fonts.title)
.foregroundColor(.secondary)
#endif
}
return Text(badge.title)
.font(fonts.title)
.foregroundColor(badge.color)
+ separator
}
private var pin: some View {
Image(systemName: "pin")
.font(fonts.title)
.foregroundColor(.secondary)
}
private var text: some View {
Text(viewModel.text)
.font(fonts.body)
.foregroundColor(viewModel.textColor)
.lineLimit(4)
}
private struct Fonts {
let title: Font
let body: Font
}
#if os(watchOS)
private let fonts = Fonts(title: .system(size: 12), body: .system(size: 15))
#else
private let fonts = Fonts(title: .body, body: .body)
#endif
}
#if DEBUG
struct ConsoleMessageView_Previews: PreviewProvider {
static var previews: some View {
ConsoleMessageView(viewModel: .init(message: (try! LoggerStore.mock.allMessages())[0]))
.padding()
.previewLayout(.sizeThatFits)
}
}
#endif