damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit ae2e70ba7d8ebaa7bf46262e974485ab91afa4fb
parent 1b4e54582fa01095066cf75f8776a429eb285ed5
Author: Joel Klabo <joelklabo@gmail.com>
Date:   Wed, 15 Feb 2023 22:30:15 -0800

Format Large Numbers of Action Bar Actions

Changelog-Changed: Format large numbers of action bar actions
Closes: #626

Diffstat:
Mdamus/Models/Mentions.swift | 26++++++++++++++++++++++++++
Mdamus/Views/ActionBar/EventActionBar.swift | 10++++++++--
2 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/damus/Models/Mentions.swift b/damus/Models/Mentions.swift @@ -211,6 +211,32 @@ enum Amount: Equatable { } } +func format_actions_abbrev(_ actions: Int) -> String { + let formatter = NumberFormatter() + formatter.numberStyle = .decimal + formatter.positiveSuffix = "m" + formatter.positivePrefix = "" + formatter.minimumFractionDigits = 0 + formatter.maximumFractionDigits = 3 + formatter.roundingMode = .down + formatter.roundingIncrement = 0.1 + formatter.multiplier = 1 + + if actions >= 1_000_000 { + formatter.positiveSuffix = "m" + formatter.multiplier = 0.000001 + } else if actions >= 1000 { + formatter.positiveSuffix = "k" + formatter.multiplier = 0.001 + } else { + return "\(actions)" + } + + let actions = NSNumber(value: actions) + + return formatter.string(from: actions) ?? "\(actions)" +} + func format_msats_abbrev(_ msats: Int64) -> String { let formatter = NumberFormatter() formatter.numberStyle = .decimal diff --git a/damus/Views/ActionBar/EventActionBar.swift b/damus/Views/ActionBar/EventActionBar.swift @@ -61,7 +61,7 @@ struct EventActionBar: View { } } .accessibilityLabel(NSLocalizedString("Boosts", comment: "Accessibility label for boosts button")) - Text(String("\(bar.boosts > 0 ? "\(bar.boosts)" : "")")) + Text(String("\(bar.boosts > 0 ? "\(format_actions_abbrev(bar.boosts))" : "")")) .offset(x: 18) .font(.footnote.weight(.medium)) .foregroundColor(bar.boosted ? Color.green : Color.gray) @@ -76,7 +76,7 @@ struct EventActionBar: View { send_like() } } - Text(String("\(bar.likes > 0 ? "\(bar.likes)" : "")")) + Text(String("\(bar.likes > 0 ? "\(format_actions_abbrev(bar.likes))" : "")")) .offset(x: 22) .font(.footnote.weight(.medium)) .foregroundColor(bar.liked ? Color.accentColor : Color.gray) @@ -190,6 +190,8 @@ struct EventActionBar_Previews: PreviewProvider { let likedbar = ActionBarModel(likes: 10, boosts: 0, zaps: 0, zap_total: 0, our_like: nil, our_boost: nil, our_zap: nil) let likedbar_ours = ActionBarModel(likes: 10, boosts: 0, zaps: 0, zap_total: 0, our_like: NostrEvent(id: "", content: "", pubkey: ""), our_boost: nil, our_zap: nil) let maxed_bar = ActionBarModel(likes: 999, boosts: 999, zaps: 999, zap_total: 99999999, our_like: NostrEvent(id: "", content: "", pubkey: ""), our_boost: NostrEvent(id: "", content: "", pubkey: ""), our_zap: nil) + let extra_max_bar = ActionBarModel(likes: 9999, boosts: 9999, zaps: 9999, zap_total: 99999999, our_like: NostrEvent(id: "", content: "", pubkey: ""), our_boost: NostrEvent(id: "", content: "", pubkey: ""), our_zap: nil) + let mega_max_bar = ActionBarModel(likes: 9999999, boosts: 99999, zaps: 9999, zap_total: 99999999, our_like: NostrEvent(id: "", content: "", pubkey: ""), our_boost: NostrEvent(id: "", content: "", pubkey: ""), our_zap: nil) let zapbar = ActionBarModel(likes: 0, boosts: 0, zaps: 5, zap_total: 10000000, our_like: nil, our_boost: nil, our_zap: nil) VStack(spacing: 50) { @@ -200,7 +202,11 @@ struct EventActionBar_Previews: PreviewProvider { EventActionBar(damus_state: ds, event: ev, bar: likedbar_ours) EventActionBar(damus_state: ds, event: ev, bar: maxed_bar) + + EventActionBar(damus_state: ds, event: ev, bar: extra_max_bar) + EventActionBar(damus_state: ds, event: ev, bar: mega_max_bar) + EventActionBar(damus_state: ds, event: ev, bar: zapbar, test_lnurl: "lnurl") } .padding(20)