Skip to content
Draft
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
31 changes: 31 additions & 0 deletions Sources/AutoPresentationClient/Condition.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import DuckCore

extension AutoPresentation {
public struct FeatureCondition: Sendable {
public var isEligibleForPresentation: @Sendable (
_ event: AutoPresentation.Event?,
_ placement: Placement?,
_ userInfo: UserInfo?
) -> Bool

public var increment: @Sendable () async -> Void
public var logEvent: @Sendable (Event) async -> Void
public var reset: @Sendable () async -> Void

public init(
isEligibleForPresentation: @escaping @Sendable (
_ event: AutoPresentation.Event?,
_ placement: Placement?,
_ userInfo: AutoPresentation.UserInfo?
) -> Bool,
increment: @escaping @Sendable () async -> Void,
logEvent: @escaping @Sendable (AutoPresentation.Event) async -> Void,
reset: @escaping @Sendable () async -> Void
) {
self.isEligibleForPresentation = isEligibleForPresentation
self.increment = increment
self.logEvent = logEvent
self.reset = reset
}
}
}
7 changes: 4 additions & 3 deletions Sources/AutoPresentationClient/Interface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ public struct AutoPresentationClient: Sendable {

public var isEligibleForPresentation: @Sendable (
_ _: AutoPresentation.Feature,
_ event: AutoPresentation.Event?,
_ placement: Placement?,
_ userInfo: AutoPresentation.UserInfo?
) async -> Bool = { _, _, _ in false }
) async -> Bool = { _, _, _, _ in false }

public var increment: @Sendable (
_ _: AutoPresentation.Feature
Expand All @@ -32,11 +33,12 @@ public struct AutoPresentationClient: Sendable {
public var reset: @Sendable () async -> Void

public func featureToPresent(
_ event: AutoPresentation.Event?,
_ placement: Placement?,
_ userInfo: AutoPresentation.UserInfo?
) async -> AutoPresentation.Feature? {
for feature in availableFeatures() {
guard await isEligibleForPresentation(feature, placement, userInfo) else {
guard await isEligibleForPresentation(feature, event, placement, userInfo) else {
continue
}
return feature
Expand All @@ -45,4 +47,3 @@ public struct AutoPresentationClient: Sendable {
return nil
}
}

4 changes: 3 additions & 1 deletion Sources/AutoPresentationClient/Live.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension AutoPresentationClient {
impl.availableFeatures
},
isEligibleForPresentation: {
impl.isEligibleForPresentation($0, placement: $1, userInfo: $2)
impl.isEligibleForPresentation($0, event: $1, placement: $2, userInfo: $3)
},
increment: {
await impl.increment($0)
Expand Down Expand Up @@ -55,6 +55,7 @@ private final class AutoPresentationClientImpl {

func isEligibleForPresentation(
_ feature: AutoPresentation.Feature,
event: AutoPresentation.Event?,
placement: Placement?,
userInfo: [AutoPresentation.UserInfoKey: Any]?
) -> Bool {
Expand Down Expand Up @@ -96,6 +97,7 @@ private final class AutoPresentationClientImpl {
}

let isEligibleForPresentation = condition.isEligibleForPresentation(
event,
placement,
userInfo
)
Expand Down
30 changes: 4 additions & 26 deletions Sources/AutoPresentationClient/Model.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import DuckCore
@_exported import Tagged

public enum AutoPresentation {
Expand All @@ -12,30 +11,9 @@ public enum AutoPresentation {
public typealias UserInfoKey = Tagged<FeatureTag, String>

public typealias UserInfo = [UserInfoKey: Any]
}

public struct FeatureCondition: Sendable {
public var isEligibleForPresentation: @Sendable (
_ for: Placement?,
_ userInfo: UserInfo?
) -> Bool

public var increment: @Sendable () async -> Void
public var logEvent: @Sendable (Event) async -> Void
public var reset: @Sendable () async -> Void

public init(
isEligibleForPresentation: @escaping @Sendable (
_ for: Placement?,
_ userInfo: UserInfo?
) -> Bool,
increment: @escaping @Sendable () async -> Void,
logEvent: @escaping @Sendable (Event) async -> Void,
reset: @escaping @Sendable () async -> Void
) {
self.isEligibleForPresentation = isEligibleForPresentation
self.increment = increment
self.logEvent = logEvent
self.reset = reset
}
}
extension AutoPresentation.Event {
public static var newSession: Self { "start_session" }
public static var sceneDidBecomeActive: Self { "scene_become_active" }
}
2 changes: 1 addition & 1 deletion Sources/AutoPresentationClient/TestValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extension AutoPresentationClient: TestDependencyKey {
extension AutoPresentationClient {
public static let noop = Self(
availableFeatures: { [] },
isEligibleForPresentation: { _, _, _ in false },
isEligibleForPresentation: { _, _, _, _ in false },
increment: { _ in },
logEvent: { _ in },
reset: {}
Expand Down
7 changes: 4 additions & 3 deletions Sources/AutoPresentationRateUs/Condition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension AutoPresentation.FeatureCondition {

return AutoPresentation.FeatureCondition(
isEligibleForPresentation: {
impl.isEligibleForPresentation(for: $0, userInfo: $1)
impl.isEligibleForPresentation(event: $0, placement: $1, userInfo: $2)
},
increment: {
await impl.increment()
Expand Down Expand Up @@ -44,14 +44,15 @@ private final class RateUsConditionImpl {
// MARK: - Conformance

func isEligibleForPresentation(
for placement: Placement?,
event: AutoPresentation.Event?,
placement: Placement?,
userInfo: AutoPresentation.UserInfo?
) -> Bool {
guard remoteSettings.isRateUsEnabled else {
return false
}

if placement == .newSession {
if event == .newSession {
return
isEligibleNewSessionWhenNeverPresented ||
isPresentationDelayExpired
Expand Down