Files
purchases-ios/Sources/Events/FeatureEvents/StoredFeatureEvent.swift
T
Pol Miro 2658f2b8fa Refactor: Rename PaywallEventsManager to EventsManager and separate ad events (#5718)
* Rename paywallsEventsManager to eventsManager

* Rename PaywallEventsManagerType to EventsManagerType

* Rename PaywallEventsManager to EventsManager

* Rename PaywallEventStore to FeatureEventStore

* Rename InternalAPI.postPaywallEvents to postFeatureEvents

* Rename StoredEventSerializer to StoredFeatureEventSerializer

* Rename createStoredEvent to createFeatureStoredEvent

* Rename StoredEvent to StoredFeatureEvent

* Rename HTTPRequest.PaywallPath to HTTPRequest.FeatureEventsPath

* Rearrange events files

* Rename event related files

* Extract shared events endpoint configuration to EventsHTTPRequestPath

Both FeatureEventsPath and AdPath share the same /v1/events endpoint
configuration, differing only in domain (api-paywalls.revenuecat.com vs
a.revenue.cat) and name. Extract common logic to EventsHTTPRequestPath
protocol in Sources/Events/Networking.

* Move Sources/FeatureEvents to Sources/Events/FeatureEvents

* Rename EventsRequest to FeatureEventsRequest

- Rename EventsRequest.swift to FeatureEventsRequest.swift
- Move file to Sources/Events/FeatureEvents/Networking/
- Update all references in source and test files
- Update Xcode project file references

This makes it clear that this request type is specifically for the
feature events endpoint, not a generic events request.

* Remove ad events from feature events system

Ad events should not be sent through the feature events system as they
require their own separate infrastructure with a different API endpoint.

Changes:
- Remove FeatureEvent conformance from AdEvent
- Remove .ads case from Feature enum
- Remove ad events handling from FeatureEventsRequest
- Add no-op track(adEvent:) method to EventsManager with comment
- Update AdTracker to call the new track(adEvent:) method

Ad events tracking now becomes a no-op until proper implementation with:
- StoredAdEvent structure
- AdEventStore for persistence
- InternalAPI.postAdEvents() using HTTPRequest.AdPath.postEvents

* Rename PostEventsOperation to PostFeatureEventsOperation

- Rename PostEventsOperation.swift to PostFeatureEventsOperation.swift
- Move file to Sources/Events/FeatureEvents/Networking/
- Update class name and remove misleading "generic" comment
- Update references in InternalAPI.swift
- Update Xcode project file references

This makes it clear that this operation is specifically for posting
feature events, not a generic operation for various event types.
2025-10-30 14:52:35 +01:00

124 lines
3.9 KiB
Swift

//
// 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
//
// StoredFeatureEvent.swift
//
// Created by Nacho Soto on 9/6/23.
import Foundation
/// Contains the necessary information for storing and sending events.
struct StoredFeatureEvent {
private(set) var encodedEvent: String
private(set) var userID: String
private(set) var feature: Feature
private(set) var appSessionID: UUID?
private(set) var eventDiscriminator: String?
init?<T: Encodable>(event: T, userID: String, feature: Feature, appSessionID: UUID?, eventDiscriminator: String?) {
guard let encodedJSON = try? event.encodedJSON else {
return nil
}
self.encodedEvent = encodedJSON
self.userID = userID
self.feature = feature
self.appSessionID = appSessionID
self.eventDiscriminator = eventDiscriminator
}
}
enum Feature: String, Codable {
case paywalls
case customerCenter
}
// MARK: - Extensions
extension StoredFeatureEvent: Sendable {}
extension StoredFeatureEvent: Codable {
private enum CodingKeys: String, CodingKey {
case encodedEvent = "event"
case userID = "userId"
case feature
case appSessionID = "appSessionId"
case eventDiscriminator = "eventDiscriminator"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
// Try to decode as string first (new format)
if let jsonString = try? container.decode(String.self, forKey: .encodedEvent) {
self.encodedEvent = jsonString
} else {
// Fall back to old format (direct dictionary)
if let oldEvent = try? container.decode(AnyEncodable.self, forKey: .encodedEvent),
let jsonString = try? oldEvent.encodedJSON {
self.encodedEvent = jsonString
} else {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: [CodingKeys.encodedEvent],
debugDescription: "Could not convert old format to JSON string"
)
)
}
}
self.userID = try container.decode(String.self, forKey: .userID)
if let featureString = try container.decodeIfPresent(String.self, forKey: .feature),
let feature = Feature(rawValue: featureString) {
self.feature = feature
} else {
self.feature = .paywalls
}
if let appSessionID = try container.decodeIfPresent(UUID.self, forKey: .appSessionID) {
self.appSessionID = appSessionID
}
if let eventDiscriminator = try container.decodeIfPresent(String.self, forKey: .eventDiscriminator) {
self.eventDiscriminator = eventDiscriminator
}
}
}
extension StoredFeatureEvent: Equatable {
static func == (lhs: StoredFeatureEvent, rhs: StoredFeatureEvent) -> Bool {
guard lhs.userID == rhs.userID,
lhs.feature == rhs.feature,
lhs.appSessionID == rhs.appSessionID,
lhs.eventDiscriminator == rhs.eventDiscriminator else {
return false
}
// Compare decoded events instead of raw JSON strings
guard let lhsData = lhs.encodedEvent.data(using: .utf8),
let rhsData = rhs.encodedEvent.data(using: .utf8),
let lhsDict = try? JSONSerialization.jsonObject(with: lhsData) as? [String: Any],
let rhsDict = try? JSONSerialization.jsonObject(with: rhsData) as? [String: Any] else {
return false
}
return NSDictionary(dictionary: lhsDict).isEqual(rhsDict)
}
}