mycode/myApp/HaruDanim/Widgets/ActionRunWidget.swift
songyc macbook 9bf617f3e5 feat(widgets): 위젯 테마 옵션 추가 — 앱 테마 일치/라이트·다크 고정/리퀴드 글라스 (CLAUDE.md §8.1)
- 홈 위젯 5종(행동 실행/목표 진행률/다짐 진행률/다짐 현황/통계) 설정에
  '위젯 테마' 옵션 추가
- 앱 테마와 일치: 앱의 라이트/다크 설정을 따름 (테마 키를 App Group으로 이관해
  위젯이 읽을 수 있게 함)
- 라이트/다크 고정: 컬러 스킴 강제 + 해당 모드 팔레트
- 리퀴드 글라스: 반투명 머티리얼 배경 + 표면 셀도 머티리얼로 변형해
  유리 질감 위에서 가독성 유지
- DEBUG 미리보기가 테마를 재현하도록 개선 (-widgetTheme light|dark|glass,
  글라스는 그라데이션 배경 위에 렌더링)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013yKDMhuF39GVYy3FMcGHh7
2026-07-10 09:14:31 +09:00

208 lines
7.6 KiB
Swift

//
// ActionRunWidget.swift
// Haru_DanimWidgets
//
// (CLAUDE.md §8.2 A / A / A)
// Interactive Widgets: ( / +1)
//
import SwiftUI
import WidgetKit
import AppIntents
// MARK: -
struct ActionRunConfigIntent: WidgetConfigurationIntent {
static let title: LocalizedStringResource = "행동 실행 위젯"
static let description = IntentDescription("표시할 행동과 누적 기간을 선택하세요.")
@Parameter(title: "표시 기간", default: .day)
var period: SpanOption
@Parameter(title: "위젯 테마", default: .matchApp)
var theme: WidgetThemeOption
@Parameter(title: "행동 1")
var action1: ActionEntity?
@Parameter(title: "행동 2")
var action2: ActionEntity?
@Parameter(title: "행동 3")
var action3: ActionEntity?
@Parameter(title: "행동 4")
var action4: ActionEntity?
}
// MARK: -
struct ActionRunEntry: TimelineEntry {
let date: Date
let locked: Bool
let theme: WidgetThemeOption
let periodLabel: String
let cells: [ActionCellSnapshot]
}
struct ActionRunProvider: AppIntentTimelineProvider {
@MainActor
static func makeEntry(_ configuration: ActionRunConfigIntent) -> ActionRunEntry {
guard WidgetStore.isUnlocked else {
return ActionRunEntry(date: .now, locked: true, theme: configuration.theme, periodLabel: "", cells: [])
}
let span = configuration.period.statSpan
let chosen = [configuration.action1, configuration.action2, configuration.action3, configuration.action4]
.compactMap { $0 }
.compactMap { WidgetStore.action($0.id) }
let actions = chosen.isEmpty ? WidgetStore.defaultActions(4) : chosen
return ActionRunEntry(
date: .now,
locked: false,
theme: configuration.theme,
periodLabel: configuration.period.label,
cells: actions.map { ActionCellSnapshot.make(action: $0, span: span) }
)
}
func placeholder(in context: Context) -> ActionRunEntry {
ActionRunEntry(date: .now, locked: false, theme: .matchApp, periodLabel: "오늘", cells: [])
}
@MainActor
func snapshot(for configuration: ActionRunConfigIntent, in context: Context) async -> ActionRunEntry {
Self.makeEntry(configuration)
}
@MainActor
func timeline(for configuration: ActionRunConfigIntent, in context: Context) async -> Timeline<ActionRunEntry> {
// / reloadAllTimelines ,
// 15
Timeline(entries: [Self.makeEntry(configuration)], policy: .after(.now.addingTimeInterval(15 * 60)))
}
}
// MARK: -
struct ActionRunCellView: View {
let cell: ActionCellSnapshot
let periodLabel: String
var compact = false
var body: some View {
Button(intent: RunActionIntent(action: cell.entity)) {
VStack(alignment: .leading, spacing: compact ? 2 : 4) {
HStack {
Image(systemName: cell.symbolName)
.font(.system(size: compact ? 14 : 18, weight: .semibold))
Spacer()
if cell.isRunning {
Image(systemName: "record.circle")
.font(.system(size: compact ? 10 : 12, weight: .bold))
.foregroundStyle(AppTheme.yellow)
}
}
Spacer(minLength: 0)
Text(cell.name)
.font(compact ? .caption2.weight(.semibold) : .caption.weight(.semibold))
.lineLimit(1)
Group {
if cell.isCount {
Text("\(periodLabel) \(Int(cell.value))")
} else if let base = cell.tickingBase {
// :
Text(base, style: .timer)
} else {
Text("\(periodLabel) \(Format.durationShort(cell.value))")
}
}
.font(compact ? .caption2.monospacedDigit() : .caption.monospacedDigit())
.opacity(0.85)
.lineLimit(1)
}
.foregroundStyle(.white)
.padding(compact ? 8 : 12)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
.background(
RoundedRectangle(cornerRadius: 14, style: .continuous)
.fill(cell.color)
)
.overlay {
if cell.isRunning {
RoundedRectangle(cornerRadius: 14, style: .continuous)
.strokeBorder(AppTheme.yellow, lineWidth: 2)
}
}
}
.buttonStyle(.plain)
}
}
struct ActionRunWidgetView: View {
@Environment(\.widgetFamily) private var envFamily
/// DEBUG
var previewFamily: WidgetFamily? = nil
private var family: WidgetFamily { previewFamily ?? envFamily }
let entry: ActionRunEntry
var body: some View {
Group {
if entry.locked {
WidgetLockedView()
} else if entry.cells.isEmpty {
Text("앱에서 행동을 만들면 여기서 실행할 수 있어요.")
.font(.caption2)
.foregroundStyle(.secondary)
.multilineTextAlignment(.center)
} else {
switch family {
case .systemMedium:
HStack(spacing: 8) {
ForEach(entry.cells.prefix(3)) { cell in
ActionRunCellView(cell: cell, periodLabel: entry.periodLabel, compact: true)
}
}
case .systemLarge:
let cells = Array(entry.cells.prefix(4))
VStack(spacing: 8) {
HStack(spacing: 8) {
ForEach(cells.prefix(2)) { cell in
ActionRunCellView(cell: cell, periodLabel: entry.periodLabel)
}
}
if cells.count > 2 {
HStack(spacing: 8) {
ForEach(cells.dropFirst(2)) { cell in
ActionRunCellView(cell: cell, periodLabel: entry.periodLabel)
}
}
}
}
default:
if let cell = entry.cells.first {
ActionRunCellView(cell: cell, periodLabel: entry.periodLabel)
}
}
}
}
.widgetTheme(entry.theme)
}
}
// MARK: -
struct ActionRunWidget: Widget {
var body: some WidgetConfiguration {
AppIntentConfiguration(
kind: "HaruActionRunWidget",
intent: ActionRunConfigIntent.self,
provider: ActionRunProvider()
) { entry in
ActionRunWidgetView(entry: entry)
}
.configurationDisplayName("행동 실행")
.description("행동을 위젯에서 바로 실행하고 누적값을 확인해요. (프리미엄)")
.supportedFamilies([.systemSmall, .systemMedium, .systemLarge])
}
}