Skip to content

Commit

Permalink
Fixes an issue that physical weaknesses doesn't follow changes to Dyn…
Browse files Browse the repository at this point in the history
…amic Type.
  • Loading branch information
mntone committed Feb 6, 2024
1 parent 68a88ae commit 8ff8835
Showing 1 changed file with 28 additions and 49 deletions.
77 changes: 28 additions & 49 deletions src/App/Views/Weaknesses/PhysicalWeaknessItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,39 @@ import SwiftUI

@available(watchOS, unavailable)
struct PhysicalWeaknessItemView: View {
private static let separator: String = {
String(localized: ", ")
private static let placeholder: Text = {
Text(verbatim: "???")
}()

private static let none: String = {
String(localized: "None")
private static let separator: Text = {
Text(", ")
}()

private static let none: Text = {
let baseText = Text("None").fontWeight(.light)
if #available(iOS 17.0, macOS 14.0, *) {
return baseText.foregroundStyle(.secondary)
} else {
return baseText.foregroundColor(.secondary)
}
}()

#if os(macOS)
private static var font: Font {
.body
}

private static var secondaryColor: NSColor {
NSColor.secondaryLabelColor
}
#else
private static var font: Font {
.subheadline
}

private static var secondaryColor: UIColor {
UIColor.secondaryLabel
}
#endif

private static let attributes: AttributeContainer = {
#if os(macOS)
let attributes: [NSAttributedString.Key: Any] = [
.font: NSFont.systemFont(ofSize: 13),
.foregroundColor: NSColor.labelColor,
]
#else
let attributes: [NSAttributedString.Key: Any] = [
.font: UIFont.preferredFont(forTextStyle: .subheadline),
.foregroundColor: UIColor.label,
]
#endif
return AttributeContainer(attributes)
}()

let viewModel: PhysicalWeaknessItemViewModel?
let physical: Physical

#if !os(macOS)
@ScaledMetric(relativeTo: .subheadline)
private var offsetX: CGFloat = 30.0
private var offsetX: CGFloat = 22.5
#endif

var body: some View {
Expand All @@ -61,10 +47,11 @@ struct PhysicalWeaknessItemView: View {
0.75 * d[.bottom]
}

Text(content.attributedString)
content
.font(Self.font)
.redacted(reason: viewModel == nil ? .placeholder : [])
#if os(macOS)
.offset(x: viewModel == nil ? 28.0 : 0.0)
.offset(x: viewModel == nil ? 21.0 : 0.0)
#else
.offset(x: viewModel == nil ? offsetX : 0.0)
#endif
Expand All @@ -73,33 +60,25 @@ struct PhysicalWeaknessItemView: View {
.id(viewModel?.id ?? physical.prefix)
}

private var content: (text: String, attributedString: AttributedString) {
private var content: Text {
guard let viewModel else {
let placeholderText = "???"
let placeholder = AttributedString(placeholderText, attributes: Self.attributes)
return (placeholderText, placeholder)
return Self.placeholder
}

if !viewModel.firstPartNames.isEmpty {
let first = AttributedString(" " + viewModel.firstPartNames, attributes: Self.attributes)
let first = Text(verbatim: " ") + Text(viewModel.firstPartNames)
if !viewModel.secondPartNames.isEmpty {
var attributes = Self.attributes
attributes.foregroundColor = Self.secondaryColor

let secondText = PhysicalWeaknessItemView.separator + viewModel.secondPartNames
let second = AttributedString(secondText, attributes: attributes)
return (viewModel.firstPartNames + secondText, first + second)
let second = PhysicalWeaknessItemView.separator + Text(viewModel.secondPartNames)
if #available(iOS 17.0, macOS 14.0, *) {
return first + second.foregroundStyle(.secondary)
} else {
return first + second.foregroundColor(.secondary)
}
} else {
return (viewModel.firstPartNames, first)
return first
}
} else {
var attributes = Self.attributes
attributes.foregroundColor = Self.secondaryColor
attributes.font = Self.font.weight(.light)

let noneText = Self.none
let none = AttributedString(" " + noneText, attributes: attributes)
return (noneText, none)
return Text(verbatim: " ") + Self.none
}
}
}

0 comments on commit 8ff8835

Please sign in to comment.