Support link/html in analytics prompt strings.

Show the new prompt to everyone, even if they previously opted out.
Add docs to Analytics.
This commit is contained in:
Doug
2021-12-07 12:09:26 +00:00
parent 3cf030eb47
commit 2ddb17ec7b
19 changed files with 401 additions and 151 deletions

View File

@@ -26,17 +26,49 @@ struct AnalyticsPromptTermsText: View {
@Environment(\.theme) private var theme
// MARK: Public
/// A string with a link attribute.
private struct StringComponent {
let string: String
let isLink: Bool
}
let promptType: AnalyticsPromptType
/// Internal representation of the string as composable parts.
private let components: [StringComponent]
// MARK: Views
// MARK: - Setup
init(attributedString: NSAttributedString) {
var components = [StringComponent]()
let range = NSRange(location: 0, length: attributedString.length)
let string = attributedString.string as NSString
attributedString.enumerateAttributes(in: range, options: []) { attributes, range, stop in
let isLink = attributes.keys.contains(.analyticsPromptTermsTextLink)
components.append(StringComponent(string: string.substring(with: range), isLink: isLink))
}
self.components = components
}
// MARK: - Views
var body: some View {
let (start, link, end) = promptType.termsStrings
Text(start)
+ Text(link).foregroundColor(theme.colors.accent)
+ Text(end)
components.reduce(Text("")) {
$0 + Text($1.string).foregroundColor($1.isLink ? theme.colors.accent : nil)
}
}
}
// MARK: - Previews
@available(iOS 14.0, *)
struct AnalyticsPromptTermsText_Previews: PreviewProvider {
static let strings = MockAnalyticsPromptStrings()
static var previews: some View {
VStack(spacing: 8) {
AnalyticsPromptTermsText(attributedString: strings.termsNewUser)
AnalyticsPromptTermsText(attributedString: strings.termsUpgrade)
}
}
}