Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paywalls: add PaywallEvent model #3156

Merged
merged 3 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
106 changes: 106 additions & 0 deletions Sources/Paywalls/Events/PaywallEvent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//
// 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
}

}

}

extension PaywallEvent {

/// - Returns: the underlying ``PaywallEvent/Data-swift.struct`` for this event.
public var data: Data {
switch self {
case let .view(data): return data
case let .cancel(data): return data
case let .close(data): return data
}
}

}

// MARK: -

extension PaywallEvent.Data: Equatable, Codable, Sendable {}
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
42 changes: 42 additions & 0 deletions Tests/APITesters/SwiftAPITester/SwiftAPITester/PaywallAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,45 @@ 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
}

let _: PaywallEvent.Data = event.data
}

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
)
}
}