Contents

Event

A repeatable user-defined action.

Declaration

typealias Event = Tips.Event

Overview

Use an event when you want to track an action that can occur one or more times in your app (such as a user logging in). Then use donate() to donate to the event when the action occurs, increasing the event count by one.

Creating an event with no associated donation value

The example below creates a landmarksAppDidOpen event with no associated donation value and donates it anytime ContentView appears:

struct LandmarkTips: App {
    static let landmarksAppDidOpen = Tips.Event(id: "landmarksAppDidOpen")

    var body: some Scene {
        WindowGroup {
            ContentView()
                .onAppear { Self.landmarksAppDidOpen.sendDonation() }
        }
    }
}

The example below creates a display rule for LandmarkFeatureTip based on the landmarksAppDidOpen event.

struct LandmarkFeatureTip: Tip {
    var rules: [Rule] {
        // Tip will only display when the landmarksAppDidOpen event has been donated 3 or more times in the last week.
        #Rule(LandmarkTips.landmarksAppDidOpen) {
            $0.donations.donatedWithin(.week).count >= 3
        }
    }
}

Creating an event with an associated donation value

The example below creates a didViewLandmarkDetail event with an associated donation value and donates it anytime the LandmarkDetail appears:

struct LandmarkDetail: View {
    static let didViewLandmarkDetail = Tips.Event<DidViewLandmark>(id: "didViewLandmarkDetail")

    struct DidViewLandmark: Codable, Sendable {
        let landmarkID: Int
        let landmarkName: String
    }

    var landmark: Landmark

    var body: some View {
        ScrollView {
            MapView(coordinate: landmark.locationCoordinate)
        }
        .onAppear {
            Self.didViewLandmarkDetail.sendDonation(.init(landmarkID: landmark.id, landmarkName: landmark.name))
        }
    }
}

The example below creates a display rule for LandmarkDetailTip based on the didViewLandmarkDetail event.

struct LandmarkDetailTip: Tip {
    var rules: [Rule] {
        // Tip will only display when the didViewLandmarkDetail has been donated 3 or more times for landmarks not named "Wilbere Bowl".
        #Rule(LandmarkDetail.didViewLandmarkDetail) {
            $0.donations.filter({ $0.landmarkName != "Wilbere Bowl" }).count > 3
        }
    }
}

Topics

Initializers

Initializers with a donation value

Donations

Add Donations

Delete Donations

See Also

Related Documentation