Skip to main content

PrismGamification

PrismGamification brings Duolingo-style engagement mechanics to your app — challenges, streaks, badges, leaderboards, analytics, and AI-powered personalized messages. Built on SwiftData with CloudKit sync.

Challenges

Define challenges as enums. Track progress with counters or milestones. Earn points on completion.

Streaks & Badges

Daily streaks with break detection. Badges with 5 tiers and auto-unlock conditions.

AI Messages

Apple Intelligence generates personalized celebrations, motivation, and recommendations — on-device.

Architecture

┌─────────────────────────────────────────────────┐
│            PrismChallengeManager                │  ← single actor
├──────────┬──────────┬───────────┬───────────────┤
│Challenges│ Streaks  │  Badges   │ Leaderboards  │
├──────────┴──────────┴───────────┴───────────────┤
│              Analytics + Events                  │
├─────────────────────────────────────────────────┤
│  PrismGamificationIntelligence (Apple AI)       │
├─────────────────────────────────────────────────┤
│          SwiftData + CloudKit Sync              │
└─────────────────────────────────────────────────┘

Quick Start

Define Challenges
import PrismGamification

enum AppChallenge: String, PrismChallenge, CaseIterable {
    case firstLogin
    case tenWorkouts

    var title: String {
        switch self {
        case .firstLogin: "First Login"
        case .tenWorkouts: "Ten Workouts"
        }
    }

    var type: PrismChallengeType {
        switch self {
        case .firstLogin: .milestone
        case .tenWorkouts: .counter
        }
    }

    var goal: Int {
        switch self {
        case .firstLogin: 1
        case .tenWorkouts: 10
        }
    }

    var points: Int {
        switch self {
        case .firstLogin: 10
        case .tenWorkouts: 50
        }
    }

    var challengeDescription: String { title }
}
Use the Manager
let container = try PrismChallengeContainerProvider.makeContainer()
let manager = PrismChallengeManager(container: container)

// Register all challenge cases
try await manager.register(AppChallenge.self)

// Track progress
try await manager.increment(AppChallenge.tenWorkouts, by: 3)
try await manager.complete(AppChallenge.firstLogin)

// Query
let progress = try await manager.progress(for: AppChallenge.tenWorkouts)
let total = try await manager.totalPoints(AppChallenge.self)

Key Concepts

ConceptTypeDescription
ChallengePrismChallenge protocolEnum-based challenge definitions with metadata
ManagerPrismChallengeManagerCentral actor for all gamification operations
SnapshotPrismChallengeSnapshotSendable value type for cross-actor data
EventPrismChallengeEventStream of state changes (completed, progressed, etc.)
ContainerPrismChallengeContainerProviderSwiftData container with CloudKit sync
All public APIs return Sendable snapshot types instead of @Model classes. This ensures safe cross-actor data transfer in Swift 6 strict concurrency.

CloudKit Sync

Enable CloudKit sync by passing a container ID:
CloudKit Configuration
let container = try PrismChallengeContainerProvider.makeContainer(
    containerID: "iCloud.com.myapp.gamification"
)
For testing, use an in-memory container:
In-Memory for Tests
let container = try PrismChallengeContainerProvider.makeContainer(inMemory: true)

Next Steps

Challenges

Define challenges, track progress, earn points.

Streaks & Badges

Daily streaks and tiered badge system.

Leaderboards

Multi-period scored leaderboards.

AI Messages

Apple Intelligence personalized messages.