Files
element-x-ios/ElementX/Sources/Other/SwiftUI/Views/NotificationCountBadge.swift
T
Stefan Ceriu 61fe2a8158 Add a shared NotificationCountBadge view
Extract the unread notification count capsule from the room list cell into
its own view so it can be reused elsewhere. It caps the displayed value at
99+ and comes in a medium size (used by the room list) and a small size.
2026-10-09 11:44:39 +03:00

49 lines
1.5 KiB
Swift

//
// Copyright 2026 Element Creations Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
// Please see LICENSE files in the repository root for full details.
//
import Compound
import SwiftUI
/// A capsule showing an unread notification count, capped at 99+.
struct NotificationCountBadge: View {
enum Size {
case small
case medium
}
let count: Int
var size: Size = .medium
var body: some View {
Text(count > 99 ? "99+" : "\(count)")
.font(size == .small ? .compound.bodyXSSemibold : .compound.bodySMSemibold)
.foregroundStyle(.compound.textOnSolidPrimary)
.lineLimit(1)
.padding(.horizontal, size == .small ? 4 : 6)
.frame(minWidth: size == .small ? 16 : 20, minHeight: size == .small ? 16 : 20)
.background(.compound.iconAccentPrimary, in: .capsule)
}
}
// MARK: - Previews
struct NotificationCountBadge_Previews: PreviewProvider, TestablePreview {
static var previews: some View {
VStack(spacing: 8) {
ForEach([Size.small, .medium], id: \.self) { size in
HStack(spacing: 8) {
NotificationCountBadge(count: 1, size: size)
NotificationCountBadge(count: 42, size: size)
NotificationCountBadge(count: 100, size: size)
}
}
}
}
private typealias Size = NotificationCountBadge.Size
}