Fix crash when extra_usage fields are null
This commit is contained in:
@@ -491,7 +491,7 @@ struct FooterBar: View {
|
||||
struct ExtraUsageRow: View {
|
||||
let extra: ExtraUsage
|
||||
|
||||
var pct: Double { min(100, max(0, extra.utilization)) }
|
||||
var pct: Double { min(100, max(0, extra.utilization ?? 0)) }
|
||||
var color: Color { pct > 80 ? .red : pct > 50 ? .orange : .green }
|
||||
|
||||
var body: some View {
|
||||
@@ -504,7 +504,7 @@ struct ExtraUsageRow: View {
|
||||
.font(.system(size: 12, weight: .semibold))
|
||||
.foregroundColor(.secondary)
|
||||
Spacer()
|
||||
Text("\(extra.currency) \(String(format: "%.2f", extra.usedCredits / 100)) / \(String(format: "%.2f", extra.monthlyLimit / 100))")
|
||||
Text("\(extra.currency ?? "") \(String(format: "%.2f", (extra.usedCredits ?? 0) / 100)) / \(String(format: "%.2f", (extra.monthlyLimit ?? 0) / 100))")
|
||||
.font(.system(size: 11.5, weight: .medium).monospacedDigit())
|
||||
.foregroundColor(color)
|
||||
}
|
||||
|
||||
@@ -83,10 +83,10 @@ struct UsageWindow: Codable {
|
||||
|
||||
struct ExtraUsage: Codable {
|
||||
let isEnabled: Bool
|
||||
let monthlyLimit: Double
|
||||
let usedCredits: Double
|
||||
let utilization: Double
|
||||
let currency: String
|
||||
let monthlyLimit: Double?
|
||||
let usedCredits: Double?
|
||||
let utilization: Double?
|
||||
let currency: String?
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case isEnabled = "is_enabled"
|
||||
case monthlyLimit = "monthly_limit"
|
||||
@@ -94,6 +94,14 @@ struct ExtraUsage: Codable {
|
||||
case utilization
|
||||
case currency
|
||||
}
|
||||
init(from decoder: Decoder) throws {
|
||||
let c = try decoder.container(keyedBy: CodingKeys.self)
|
||||
isEnabled = (try? c.decodeIfPresent(Bool.self, forKey: .isEnabled)) ?? false
|
||||
monthlyLimit = try? c.decodeIfPresent(Double.self, forKey: .monthlyLimit)
|
||||
usedCredits = try? c.decodeIfPresent(Double.self, forKey: .usedCredits)
|
||||
utilization = try? c.decodeIfPresent(Double.self, forKey: .utilization)
|
||||
currency = try? c.decodeIfPresent(String.self, forKey: .currency)
|
||||
}
|
||||
}
|
||||
|
||||
// (Bootstrap models removed — org ID is configured directly)
|
||||
|
||||
Reference in New Issue
Block a user