MCPcopy Create free account
hub / github.com/Effect-TS/effect / AppBadge

Class AppBadge

examples/ui/native/plugins/AppBadge.swift:7–51  ·  view source on GitHub ↗

The app's own view, which the shared app renders with `Native.hostedView("AppBadge")`: a count on a capsule. A tap sends `clear`. `withAppBadge.cjs` copies it into the iOS project.

Source from the content-addressed store, hash-verified

5/// `Native.hostedView("AppBadge")`: a count on a capsule. A tap sends
6/// `clear`. `withAppBadge.cjs` copies it into the iOS project.
7final class AppBadge: UIView, EffectUIHostedView {
8 private let context: EffectUIContext
9 private let label = UILabel()
10
11 init(_ context: EffectUIContext) {
12 self.context = context
13 super.init(frame: .zero)
14 label.font = .monospacedDigitSystemFont(ofSize: 13, weight: .semibold)
15 label.textColor = .white
16 label.textAlignment = .center
17 addSubview(label)
18 isAccessibilityElement = true
19 accessibilityTraits = .button
20 addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapped)))
21 }
22
23 @available(*, unavailable)
24 required init?(coder: NSCoder) {
25 nil
26 }
27
28 func update(_ changed: [String: Any]) {
29 if changed.keys.contains("count") {
30 let count = (changed["count"] as? NSNumber)?.intValue ?? 0
31 label.text = String(count)
32 accessibilityLabel = "\(count) tasks, clear"
33 }
34 if changed.keys.contains("badgeColor") { backgroundColor = changed["badgeColor"] as? UIColor }
35 }
36
37 override var intrinsicContentSize: CGSize {
38 let text = label.intrinsicContentSize
39 return CGSize(width: max(text.width + 16, text.height + 8), height: text.height + 8)
40 }
41
42 override func layoutSubviews() {
43 super.layoutSubviews()
44 layer.cornerRadius = bounds.height / 2
45 label.frame = bounds
46 }
47
48 @objc private func tapped() {
49 context.emit("clear")
50 }
51}

Callers

nothing calls this directly

Calls 1

maxFunction · 0.50

Tested by

no test coverage detected