-
Notifications
You must be signed in to change notification settings - Fork 22
/
Survey.swift
124 lines (105 loc) · 3.2 KB
/
Survey.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// File.swift
//
//
// Created by Yusuf Tör on 25/07/2023.
//
import Foundation
/// A survey attached to a paywall.
@objc(SWKSurvey)
@objcMembers
final public class Survey: NSObject, Decodable {
/// The id of the survey.
public let id: String
/// The assigned key for the survey.
///
/// A user will only see one survey per assignment key.
public internal(set) var assignmentKey: String
/// The title of the survey's alert controller.
public let title: String
/// The message of the survey's alert controller.
public let message: String
/// The options to display in the alert controller.
public let options: [SurveyOption]
/// An enum whose cases indicate when the survey should show.
public internal(set) var presentationCondition: SurveyShowCondition
/// The probability that the survey will present to the user.
public internal(set) var presentationProbability: Double
/// Whether the "Other" option should appear to allow a user to provide a custom
/// response.
public let includeOtherOption: Bool
/// Whether a close button should appear to allow users to skip the survey.
public let includeCloseOption: Bool
/// Rolls dice to see if survey should present or is in holdout.
///
/// - Returns: `true` if user is in holdout, false if survey should present.
func shouldAssignHoldout(
isDebuggerLaunched: Bool,
storage: Storage,
randomiser: (Range<Double>) -> Double = Double.random
) -> Bool {
if isDebuggerLaunched {
return false
}
// Return immediately if no chance to present.
if presentationProbability == 0 {
return true
}
// Choose random number to present the survey with
// the probability of presentationProbability.
let randomNumber = randomiser(0..<1)
guard randomNumber < presentationProbability else {
return true
}
return false
}
/// Determines whether a survey with the same `assignmentKey` has been
/// seen before.
func hasSeenSurvey(storage: Storage) -> Bool {
let existingAssignmentKey = storage.get(SurveyAssignmentKey.self)
if existingAssignmentKey == nil {
return false
}
if existingAssignmentKey == assignmentKey {
return true
}
return false
}
init(
id: String,
assignmentKey: String,
title: String,
message: String,
options: [SurveyOption],
presentationProbability: Double,
includeOtherOption: Bool,
includeCloseOption: Bool,
presentationCondition: SurveyShowCondition
) {
self.id = id
self.assignmentKey = assignmentKey
self.title = title
self.message = message
self.options = options
self.presentationProbability = presentationProbability
self.includeOtherOption = includeOtherOption
self.includeCloseOption = includeCloseOption
self.presentationCondition = presentationCondition
}
}
// MARK: - Stubbable
extension Survey: Stubbable {
static func stub() -> Survey {
return Survey(
id: UUID().uuidString,
assignmentKey: "abc",
title: "test",
message: "test",
options: [.stub()],
presentationProbability: 1,
includeOtherOption: true,
includeCloseOption: true,
presentationCondition: .onManualClose
)
}
}