Skip to content

Commit

Permalink
Paywalls: add PaywallEvent model
Browse files Browse the repository at this point in the history
This will represent events created by `RevenueCatUI`.
  • Loading branch information
NachoSoto committed Sep 7, 2023
1 parent 8f2d279 commit 26a3d77
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
12 changes: 12 additions & 0 deletions RevenueCat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@
4FCBA8512A153940004134BD /* SnapshotTesting in Frameworks */ = {isa = PBXBuildFile; productRef = 4FCBA8502A153940004134BD /* SnapshotTesting */; };
4FCEEA5E2A379B80002C2112 /* DebugViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4FCEEA5D2A379B80002C2112 /* DebugViewController.swift */; };
4FD291BE2A1E9A2E0098D1B9 /* StoreKit2TransactionFetcherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4FD291BD2A1E9A2E0098D1B9 /* StoreKit2TransactionFetcherTests.swift */; };
4FD3688B2AA7C12600F63354 /* PaywallEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4FD3688A2AA7C12600F63354 /* PaywallEvent.swift */; };
4FDA13842A33D9BD00C45CFE /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 4FDA13662A33D13700C45CFE /* PrivacyInfo.xcprivacy */; };
4FDF10E72A725EA6004F3680 /* ExternalPurchasesManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4FDF10E62A725EA6004F3680 /* ExternalPurchasesManager.swift */; };
4FDF10E82A725EA6004F3680 /* ExternalPurchasesManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4FDF10E62A725EA6004F3680 /* ExternalPurchasesManager.swift */; };
Expand Down Expand Up @@ -1029,6 +1030,7 @@
4FCEEA602A379CF9002C2112 /* DebugViewSwiftUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DebugViewSwiftUITests.swift; sourceTree = "<group>"; };
4FCEEA622A37A2E9002C2112 /* ImageSnapshot.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageSnapshot.swift; sourceTree = "<group>"; };
4FD291BD2A1E9A2E0098D1B9 /* StoreKit2TransactionFetcherTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoreKit2TransactionFetcherTests.swift; sourceTree = "<group>"; };
4FD3688A2AA7C12600F63354 /* PaywallEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaywallEvent.swift; sourceTree = "<group>"; };
4FDA13662A33D13700C45CFE /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
4FDF10E62A725EA6004F3680 /* ExternalPurchasesManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExternalPurchasesManager.swift; sourceTree = "<group>"; };
4FDF10E92A726269004F3680 /* ObserverModeManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObserverModeManager.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2262,6 +2264,7 @@
4F87610D2A5C9E330006FA14 /* Paywalls */ = {
isa = PBXGroup;
children = (
4FD368B22AA7CFDD00F63354 /* Events */,
4FBBD4E52A620573001CBA21 /* PaywallColor.swift */,
4F87610E2A5C9E490006FA14 /* PaywallData.swift */,
4F062D312A85A11600A8A613 /* PaywallData+Localization.swift */,
Expand Down Expand Up @@ -2313,6 +2316,14 @@
path = StoreKit2;
sourceTree = "<group>";
};
4FD368B22AA7CFDD00F63354 /* Events */ = {
isa = PBXGroup;
children = (
4FD3688A2AA7C12600F63354 /* PaywallEvent.swift */,
);
path = Events;
sourceTree = "<group>";
};
570896B627596E6E00296F1C /* APITesters */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -3278,6 +3289,7 @@
B3083A132699334C007B5503 /* Offering.swift in Sources */,
2DD58DD827F240EB000FDFE3 /* EmptyFile.swift in Sources */,
2CD72942268A823900BFC976 /* Data+Extensions.swift in Sources */,
4FD3688B2AA7C12600F63354 /* PaywallEvent.swift in Sources */,
5766AA3E283C750300FA6091 /* Operators+Extensions.swift in Sources */,
4F6BEDE22A26B69500CD9322 /* DebugContentViews.swift in Sources */,
B3B5FBBC269D121B00104A0C /* Offerings.swift in Sources */,
Expand Down
109 changes: 109 additions & 0 deletions Sources/Paywalls/Events/PaywallEvent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// PaywallEvent.swift
//
// Created by Nacho Soto on 9/5/23.

import Foundation

/// An event to be sent by the `RevenueCatUI` SDK.
public enum PaywallEvent {

/// An identifier that represents a paywall session.
public typealias SessionID = UUID

/// A `PaywallView` was displayed.
case view(Data)

/// A purchase was cancelled.
case cancel(Data)

/// A `PaywallView` was closed.
case close(Data)

}

extension PaywallEvent {

/// The content of a ``PaywallEvent``.
public struct Data {

// swiftlint:disable missing_docs
public var offeringIdentifier: String
public var paywallRevision: Int
public var sessionIdentifier: SessionID
public var displayMode: PaywallViewMode
public var localeIdentifier: String
public var darkMode: Bool
public var date: Date

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
public init(
offering: Offering,
paywall: PaywallData,
sessionID: SessionID,
displayMode: PaywallViewMode,
locale: Locale,
darkMode: Bool
) {
self.init(
offeringIdentifier: offering.identifier,
paywallRevision: paywall.revision,
sessionID: sessionID,
displayMode: displayMode,
localeIdentifier: locale.identifier,
darkMode: darkMode,
date: .now
)
}
// swiftlint:enable missing_docs

init(
offeringIdentifier: String,
paywallRevision: Int,
sessionID: SessionID,
displayMode: PaywallViewMode,
localeIdentifier: String,
darkMode: Bool,
date: Date
) {
self.offeringIdentifier = offeringIdentifier
self.paywallRevision = paywallRevision
self.sessionIdentifier = sessionID
self.displayMode = displayMode
self.localeIdentifier = localeIdentifier
self.darkMode = darkMode
self.date = date
}

}

}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
extension PaywallEvent {

var data: Data {
switch self {
case let .view(data): return data
case let .cancel(data): return data
case let .close(data): return data
}
}

}

// MARK: - Codable

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
extension PaywallEvent.Data: Equatable, Codable, Sendable {}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
extension PaywallEvent: Equatable, Codable, Sendable {}
1 change: 1 addition & 0 deletions Tests/APITesters/.swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ disabled_rules:
- file_length
- type_body_length
- function_body_length
- function_parameter_count
- force_try
40 changes: 40 additions & 0 deletions Tests/APITesters/SwiftAPITester/SwiftAPITester/PaywallAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,43 @@ func checkPaywallViewMode(_ mode: PaywallViewMode) {
break
}
}

func checkPaywallEvent(_ event: PaywallEvent) {
let _: PaywallEvent.SessionID = UUID()

switch event {
case let .view(data): checkPaywallEventData(data)
case let .cancel(data): checkPaywallEventData(data)
case let .close(data): checkPaywallEventData(data)
@unknown default: break
}
}

func checkPaywallEventData(_ data: PaywallEvent.Data) {
let _: String = data.offeringIdentifier
let _: Int = data.paywallRevision
let _: PaywallEvent.SessionID = data.sessionIdentifier
let _: PaywallViewMode = data.displayMode
let _: String = data.localeIdentifier
let _: Bool = data.darkMode
let _: Date = data.date

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
func create(
offering: Offering,
paywall: PaywallData,
sessionID: PaywallEvent.SessionID,
displayMode: PaywallViewMode,
locale: Locale,
darkMode: Bool
) {
_ = PaywallEvent.Data.init(
offering: offering,
paywall: paywall,
sessionID: sessionID,
displayMode: displayMode,
locale: locale,
darkMode: darkMode
)
}
}

0 comments on commit 26a3d77

Please sign in to comment.