|
11 | 11 | import SwiftUI |
12 | 12 |
|
13 | 13 | struct LabelInputField: View { |
| 14 | + let warningThreshold: Int = 80 |
| 15 | + |
14 | 16 | @Binding var label: String |
15 | 17 | @FocusState.Binding var isFocused: Bool |
| 18 | + @State private var showLimitToast = false |
| 19 | + @State private var prevCount = 0 |
16 | 20 |
|
17 | 21 | var body: some View { |
18 | | - HStack { |
19 | | - Text("레이블") |
20 | | - .font(.body) |
21 | | - .foregroundColor(.primary) |
22 | | - |
23 | | - Divider() |
24 | | - .frame(height: 20) |
25 | | - .overlay(Color.gray.opacity(0.4)) |
26 | | - |
27 | | - TextField("레이블을 입력하세요", text: $label) |
28 | | - .focused($isFocused) |
29 | | - .frame(maxWidth: .infinity) |
| 22 | + ZStack(alignment: .bottom) { |
| 23 | + HStack { |
| 24 | + Text("라벨") |
| 25 | + .font(.body) |
| 26 | + .foregroundColor(.primary) |
| 27 | + |
| 28 | + Divider() |
| 29 | + .frame(height: 20) |
| 30 | + .overlay(Color.gray.opacity(0.4)) |
| 31 | + |
| 32 | + TextField("라벨 입력 (비워두면 자동 생성)", text: $label) |
| 33 | + .focused($isFocused) |
| 34 | + .textInputAutocapitalization(.none) |
| 35 | + .frame(maxWidth: .infinity) |
| 36 | + Spacer() |
| 37 | + |
| 38 | + // 카운터: 80 이상일 때부터 표시 |
| 39 | + if label.count >= warningThreshold { |
| 40 | + Text("\(label.count) / \(AppConfig.maxLabelLength)") |
| 41 | + .font(.caption) |
| 42 | + .foregroundColor(colorForCount(label.count)) |
| 43 | + .transition(.opacity.combined(with: .move(edge: .trailing))) |
| 44 | + .animation(.easeInOut(duration: 0.2), value: label.count) |
| 45 | + .layoutPriority(1) |
| 46 | + } |
| 47 | + } |
| 48 | + .padding(.vertical, 16) |
30 | 49 |
|
| 50 | + // 토스트 |
| 51 | + if showLimitToast { |
| 52 | + ToastView(text: "최대 \(AppConfig.maxLabelLength)자까지 입력할 수 있어요") |
| 53 | + .transition(.move(edge: .bottom).combined(with: .opacity)) |
| 54 | + .padding(.bottom, 8) |
| 55 | + } |
31 | 56 | } |
| 57 | + .accessibilityElement(children: .combine) |
| 58 | + .accessibilityLabel("타이머 라벨") |
| 59 | + .accessibilityValue(label.isEmpty ? "입력되지 않음" : label) |
| 60 | + .accessibilityHint("타이머의 라벨을 입력해 주세요. 비워두면 자동으로 라벨이 생성됩니다.") |
32 | 61 | .padding(.vertical, 16) |
33 | 62 | .contentShape(Rectangle()) |
34 | 63 | .onTapGesture { |
35 | 64 | isFocused = true |
36 | 65 | } |
| 66 | + .onChange(of: label) { newValue in |
| 67 | + let count = newValue.count |
| 68 | + if count > AppConfig.maxLabelLength { |
| 69 | + |
| 70 | + label = String(newValue.prefix(AppConfig.maxLabelLength)) |
| 71 | + |
| 72 | + // 초과 진입 시점에만 토스트 + 가벼운 햅틱 |
| 73 | + if prevCount <= AppConfig.maxLabelLength { |
| 74 | + showLimitToastBriefly() |
| 75 | + lightHaptic() |
| 76 | + } |
| 77 | + } |
| 78 | + prevCount = label.count |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private func colorForCount(_ count: Int) -> Color { |
| 83 | + if count >= AppConfig.maxLabelLength { return .red } |
| 84 | + if count >= warningThreshold { return .orange } |
| 85 | + return .secondary |
| 86 | + } |
| 87 | + |
| 88 | + private func showLimitToastBriefly() { |
| 89 | + withAnimation { showLimitToast = true } |
| 90 | + DispatchQueue.main.asyncAfter(deadline: .now() + 1.4) { |
| 91 | + withAnimation { showLimitToast = false } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private func lightHaptic() { |
| 96 | + #if os(iOS) |
| 97 | + UIImpactFeedbackGenerator(style: .light).impactOccurred() |
| 98 | + #endif |
37 | 99 | } |
38 | 100 | } |
39 | 101 |
|
| 102 | +// 심플 토스트 뷰 |
| 103 | +struct ToastView: View { |
| 104 | + let text: String |
| 105 | + var body: some View { |
| 106 | + Text(text) |
| 107 | + .font(.footnote) |
| 108 | + .padding(.horizontal, 12) |
| 109 | + .padding(.vertical, 8) |
| 110 | + .background(.ultraThinMaterial, in: Capsule()) |
| 111 | + } |
| 112 | +} |
0 commit comments