Section 4/181 menit

4. Sintaks Dasar: AppIntent Protocol

4. Sintaks Dasar: AppIntent Protocol

Intent Paling Sederhana

swift
import AppIntents

// Intent tanpa parameter — cukup conform AppIntent dan implement perform()
struct ToggleFavoriteIntent: AppIntent {
    // Wajib: judul ditampilkan di Shortcuts app
    static var title: LocalizedStringResource = "Toggle Favorit"
    
    // Opsional: deskripsi muncul di Shortcuts app
    static var description = IntentDescription(
        "Aktifkan atau nonaktifkan mode favorit",
        categoryName: "Preferensi"
    )
    
    // Opsional: buka app setelah intent selesai
    static var openAppWhenRun: Bool = false
    
    // Wajib: eksekusi intent, harus async
    func perform() async throws -> some IntentResult {
        UserDefaults.standard.toggle(forKey: "favoriteMode")
        return .result()
    }
}

Intent dengan Parameter Sederhana

swift
struct SetTimerIntent: AppIntent {
    static var title: LocalizedStringResource = "Set Timer"
    
    // @Parameter mendefinisikan input dari user/Siri
    @Parameter(
        title: "Durasi (menit)",
        description: "Berapa menit timer akan berjalan",
        default: 25,
        inclusiveRange: (1, 60)
    )
    var minutes: Int
    
    @Parameter(
        title: "Label Timer",
        default: "Fokus"
    )
    var label: String
    
    func perform() async throws -> some IntentResult & ProvidesDialog {
        let timer = TimerService.shared.start(minutes: minutes, label: label)
        return .result(dialog: "Timer '\(label)' \(minutes) menit dimulai.")
    }
}

Supported Parameter Types

swift
struct ParameterShowcaseIntent: AppIntent {
    static var title: LocalizedStringResource = "Parameter Showcase"
    
    // Primitive types
    @Parameter(title: "Teks") var text: String
    @Parameter(title: "Angka") var number: Int
    @Parameter(title: "Desimal") var decimal: Double
    @Parameter(title: "Toggle") var isEnabled: Bool
    
    // Enums (conform ke AppEnum)
    @Parameter(title: "Prioritas") var priority: PriorityLevel
    
    // Dates
    @Parameter(title: "Tanggal") var date: Date
    @Parameter(title: "Durasi") var duration: Measurement<UnitDuration>
    
    // Files
    @Parameter(title: "File") var file: IntentFile
    
    // Custom entities (conform ke AppEntity)
    @Parameter(title: "Kontak") var contact: ContactEntity
    
    // Optional parameters
    @Parameter(title: "Catatan") var note: String?
    
    // Arrays
    @Parameter(title: "Tags") var tags: [String]
    
    func perform() async throws -> some IntentResult { .result() }
}

// AppEnum — enum yang bisa digunakan sebagai parameter
enum PriorityLevel: String, AppEnum {
    case low, medium, high, urgent
    
    static var typeDisplayRepresentation: TypeDisplayRepresentation = "Prioritas"
    static var caseDisplayRepresentations: [PriorityLevel: DisplayRepresentation] = [
        .low:    .init(title: "Rendah"),
        .medium: .init(title: "Sedang"),
        .high:   .init(title: "Tinggi"),
        .urgent: .init(title: "Mendesak", image: .init(systemName: "exclamationmark.triangle"))
    ]
}