mana-swift-ui/Sources/ManaCreditsUI/ManaCreditsFormat.swift
Till JS d7bd4d1ba2 feat: ManaCreditsUI — native Gesamt-Mana-Anzeige (Product)
ManaView (Guthaben + EUR + Topf-Split 80/5/5/5/5 + Bewegungen) + ManaBalanceRow
+ ManaFormat + ManaCreditsModel. Pendant zur Web-<ManaPage>. Konsumiert
ManaCreditsClient (ManaCore), System-Styling, app-agnostisch.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-07 19:33:03 +02:00

42 lines
1.6 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.

import Foundation
/// Formatierungs-Helfer für Mana-Werte (1 Mana = 1 Cent).
public enum ManaFormat {
/// Mana als Ganzzahl mit Tausender-Trennung: 1234 → „1.234".
public static func mana(_ value: Int) -> String {
let f = NumberFormatter()
f.locale = Locale(identifier: "de_DE")
f.numberStyle = .decimal
f.maximumFractionDigits = 0
return f.string(from: NSNumber(value: value)) ?? String(value)
}
/// Mana → EUR-Gegenwert: 1234 → „12,34 €".
public static func euro(_ mana: Int) -> String {
let f = NumberFormatter()
f.locale = Locale(identifier: "de_DE")
f.numberStyle = .currency
f.currencyCode = "EUR"
return f.string(from: NSNumber(value: Double(mana) / 100.0)) ?? "\(mana) ct"
}
/// Vorzeichenbehaftete Mana-Menge für eine Bewegung: 12 → „+12", -5 → „−5".
public static func signedMana(_ value: Int) -> String {
let sign = value < 0 ? "−" : "+"
return sign + mana(abs(value))
}
/// ISO-8601-Zeitstempel → kurzes deutsches Datum („7. Juni 2026").
/// Fällt bei Parse-Fehler auf den Rohwert zurück.
public static func shortDate(_ iso: String) -> String {
let parser = ISO8601DateFormatter()
parser.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
let date = parser.date(from: iso) ?? ISO8601DateFormatter().date(from: iso)
guard let date else { return iso }
let out = DateFormatter()
out.locale = Locale(identifier: "de_DE")
out.dateStyle = .medium
out.timeStyle = .none
return out.string(from: date)
}
}