Section 9/181 menit
9. Intent Result, Dialog, dan View
9. Intent Result, Dialog, dan View
IntentResult Variants
swift
// Tidak ada output — intent selesai diam-diam
func perform() async throws -> some IntentResult {
try await SomeService.doWork()
return .result()
}
// Dengan dialog — Siri ucapkan ini setelah selesai
func perform() async throws -> some IntentResult & ProvidesDialog {
return .result(dialog: "Selesai! Kopi kamu sedang disiapkan.")
}
// Dengan value — bisa di-chain ke intent berikutnya di Shortcuts
func perform() async throws -> some IntentResult & ReturnsValue<TaskEntity> {
let task = try await TaskRepository.shared.create(title: title, priority: priority, dueDate: dueDate)
return .result(value: TaskEntity(task: task))
}
// Dengan dialog dan value sekaligus
func perform() async throws -> some IntentResult & ReturnsValue<Int> & ProvidesDialog {
let count = try await TaskRepository.shared.fetchPending().count
return .result(
value: count,
dialog: "Kamu punya \(count) tugas yang belum selesai."
)
}
Custom SwiftUI View sebagai Result
swift
struct ShowSummaryIntent: AppIntent {
static var title: LocalizedStringResource = "Tampilkan Ringkasan"
func perform() async throws -> some IntentResult & ShowsSnippetView {
let stats = try await TaskRepository.shared.fetchStats()
return .result {
// SwiftUI view yang muncul di Siri / Shortcuts result
TaskSummaryView(
total: stats.total,
completed: stats.completed,
pending: stats.pending
)
}
}
}
struct TaskSummaryView: View {
let total: Int
let completed: Int
let pending: Int
var body: some View {
VStack(spacing: 12) {
HStack {
StatCard(title: "Total", value: total, color: .blue)
StatCard(title: "Selesai", value: completed, color: .green)
StatCard(title: "Pending", value: pending, color: .orange)
}
.padding()
}
.background(Color(.systemBackground))
.cornerRadius(12)
}
}