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 3bdf9b261a
commit b3194a0fe9
19 changed files with 401 additions and 151 deletions
@@ -40,16 +40,29 @@ enum AnalyticsPromptViewModelResult {
struct AnalyticsPromptViewState: BindableState {
/// The type of prompt to display.
let promptType: AnalyticsPromptType
/// The app's bundle display name.
let appDisplayName: String
/// Localized attributed strings created in the coordinator.
let strings: AnalyticsPromptStringsProtocol
}
/// A collection of strings for the UI that need to be created in
/// the coordinator or mocked in the RiotSwiftUI target.
protocol AnalyticsPromptStringsProtocol {
var appDisplayName: String { get }
var point1: NSAttributedString { get }
var point2: NSAttributedString { get }
var termsNewUser: NSAttributedString { get }
var termsUpgrade: NSAttributedString { get }
}
enum AnalyticsPromptType {
case newUser
case upgrade
case newUser(termsString: NSAttributedString)
case upgrade(termsString: NSAttributedString)
}
extension AnalyticsPromptType {
/// The main description string that should be displayed.
var description: String {
switch self {
case .newUser:
@@ -59,19 +72,15 @@ extension AnalyticsPromptType {
}
}
var termsStrings: (String, String, String) {
/// The terms string that should be displayed.
var termsStrings: NSAttributedString {
switch self {
case .newUser:
return (VectorL10n.analyticsPromptTermsStartNewUser,
VectorL10n.analyticsPromptTermsLinkNewUser,
VectorL10n.analyticsPromptTermsEndNewUser)
case .upgrade:
return (VectorL10n.analyticsPromptTermsStartUpgrade,
VectorL10n.analyticsPromptTermsLinkUpgrade,
VectorL10n.analyticsPromptTermsEndUpgrade)
case .newUser(let termsString), .upgrade(let termsString):
return termsString
}
}
/// The title for the enable button.
var enableButtonTitle: String {
switch self {
case .newUser:
@@ -81,6 +90,7 @@ extension AnalyticsPromptType {
}
}
/// The title for the disable button.
var disableButtonTitle: String {
switch self {
case .newUser:
@@ -91,8 +101,27 @@ extension AnalyticsPromptType {
}
}
extension AnalyticsPromptType: CaseIterable { }
extension AnalyticsPromptType: CaseIterable {
static var allCases: [AnalyticsPromptType] {
let strings = MockAnalyticsPromptStrings()
return [
.newUser(termsString: strings.termsNewUser),
.upgrade(termsString: strings.termsUpgrade)
]
}
}
extension AnalyticsPromptType: Identifiable {
var id: Self { self }
var id: String {
switch self {
case .newUser:
return "newUser"
case .upgrade:
return "upgrade"
}
}
}
extension NSAttributedString.Key {
static let analyticsPromptTermsTextLink = NSAttributedString.Key("TermsTextLink")
}
@@ -37,8 +37,8 @@ class AnalyticsPromptViewModel: AnalyticsPromptViewModelType {
// MARK: - Setup
/// Initialize a view model with the specified prompt type and app display name.
init(promptType: AnalyticsPromptType, appDisplayName: String) {
super.init(initialViewState: AnalyticsPromptViewState(promptType: promptType, appDisplayName: appDisplayName))
init(promptType: AnalyticsPromptType, strings: AnalyticsPromptStringsProtocol) {
super.init(initialViewState: AnalyticsPromptViewState(promptType: promptType, strings: strings))
}
// MARK: - Public
@@ -19,8 +19,6 @@
import SwiftUI
struct AnalyticsPromptCoordinatorParameters {
/// The type of prompt to display.
let promptType: AnalyticsPromptType
/// The session to use if analytics are enabled.
let session: MXSession
/// The navigation router used to display the prompt.
@@ -53,7 +51,17 @@ final class AnalyticsPromptCoordinator: Coordinator {
@available(iOS 14.0, *)
init(parameters: AnalyticsPromptCoordinatorParameters) {
self.parameters = parameters
let viewModel = AnalyticsPromptViewModel(promptType: parameters.promptType, appDisplayName: AppInfo.current.displayName)
let strings = AnalyticsPromptStrings()
let promptType: AnalyticsPromptType
if Analytics.shared.promptShouldDisplayUpgradeMessage {
promptType = .upgrade(termsString: strings.termsUpgrade)
} else {
promptType = .newUser(termsString: strings.termsNewUser)
}
let viewModel = AnalyticsPromptViewModel(promptType: promptType, strings: strings)
let view = AnalyticsPrompt(viewModel: viewModel.context)
_analyticsPromptViewModel = viewModel
@@ -0,0 +1,71 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import DTCoreText
@available(iOS 14.0, *)
struct AnalyticsPromptStrings: AnalyticsPromptStringsProtocol {
let appDisplayName = AppInfo.current.displayName
let point1: NSAttributedString
let point2: NSAttributedString
let termsNewUser: NSAttributedString
let termsUpgrade: NSAttributedString
init() {
self.point1 = Self.parse(VectorL10n.analyticsPromptPoint1)
self.point2 = Self.parse(VectorL10n.analyticsPromptPoint2)
self.termsNewUser = Self.attach(VectorL10n.analyticsPromptTermsLinkNewUser,
to: VectorL10n.analyticsPromptTermsNewUser("%@"))
self.termsUpgrade = Self.attach(VectorL10n.analyticsPromptTermsLinkUpgrade,
to: VectorL10n.analyticsPromptTermsUpgrade("%@"))
}
static func parse(_ htmlString: String) -> NSAttributedString {
// Do some sanitisation before finalizing the string
// let sanitizeCallback: DTHTMLAttributedStringBuilderWillFlushCallback = { element in
// element?.sanitize(with: ["b"], bodyFont: .systemFont(ofSize: UIFont.systemFontSize), imageHandler: nil)
// print("Hello")
// }
let options: [String: Any] = [
DTUseiOS6Attributes: true, // Enable it to be able to display the attributed string in a UITextView
DTDefaultLinkDecoration: false,
// DTWillFlushBlockCallBack: sanitizeCallback
]
guard let attributedString = NSAttributedString(htmlData: htmlString.data(using: .utf8),
options: options,
documentAttributes: nil) else {
return NSAttributedString(string: htmlString)
}
return MXKTools.removeDTCoreTextArtifacts(attributedString)
}
static func attach(_ link: String, to terms: String) -> NSAttributedString {
let baseString = NSMutableAttributedString(string: terms)
let linkRange = (baseString.string as NSString).range(of: "%@")
let formattedLink = NSAttributedString(string: VectorL10n.analyticsPromptTermsLinkNewUser,
attributes: [.analyticsPromptTermsTextLink: true])
baseString.replaceCharacters(in: linkRange, with: formattedLink)
return baseString
}
}
@@ -43,7 +43,8 @@ enum MockAnalyticsPromptScreenState: MockScreenState, CaseIterable {
case .promptType(let analyticsPromptType):
promptType = analyticsPromptType
}
let viewModel = AnalyticsPromptViewModel(promptType: promptType, appDisplayName: "Element")
let viewModel = AnalyticsPromptViewModel(promptType: promptType,
strings: MockAnalyticsPromptStrings())
return (
[promptType, viewModel],
@@ -0,0 +1,52 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
struct MockAnalyticsPromptStrings: AnalyticsPromptStringsProtocol {
var appDisplayName = "Element"
let point1: NSAttributedString
let point2: NSAttributedString
let termsNewUser: NSAttributedString
let termsUpgrade: NSAttributedString
let shortString = NSAttributedString(string: "This is a short string.")
let longString = NSAttributedString(string: "This is a very long string that will be used to test the layout over multiple lines of text to ensure everything is correct.")
init() {
let point1 = NSMutableAttributedString(string: "We ")
point1.append(NSAttributedString(string: "don't", attributes: [.font: UIFont.boldSystemFont(ofSize: UIFont.systemFontSize)]))
point1.append(NSAttributedString(string: " record or profile any account data"))
self.point1 = point1
let point2 = NSMutableAttributedString(string: "We ")
point2.append(NSAttributedString(string: "don't", attributes: [.font: UIFont.boldSystemFont(ofSize: UIFont.systemFontSize)]))
point2.append(NSAttributedString(string: " share information with third parties"))
self.point2 = point2
let termsNewUser = NSMutableAttributedString(string: "You can read all our terms ")
termsNewUser.append(NSAttributedString(string: "here", attributes: [.analyticsPromptTermsTextLink: true]))
termsNewUser.append(NSAttributedString(string: "."))
self.termsNewUser = termsNewUser
let termsUpgrade = NSMutableAttributedString(string: "Read all our terms ")
termsUpgrade.append(NSAttributedString(string: "here", attributes: [.analyticsPromptTermsTextLink: true]))
termsUpgrade.append(NSAttributedString(string: ". Is that OK?"))
self.termsUpgrade = termsUpgrade
}
}
@@ -39,7 +39,7 @@ struct AnalyticsPrompt: View {
VStack {
Text("\(viewModel.viewState.promptType.description)\n")
AnalyticsPromptTermsText(promptType: viewModel.viewState.promptType)
AnalyticsPromptTermsText(attributedString: viewModel.viewState.promptType.termsStrings)
.onTapGesture {
viewModel.send(viewAction: .openTermsURL)
}
@@ -49,27 +49,9 @@ struct AnalyticsPrompt: View {
/// The list of re-assurances about analytics.
private var checkmarkList: some View {
VStack(alignment: .leading) {
Label {
Text(VectorL10n.analyticsPromptPoint1Start)
+ Text(VectorL10n.analyticsPromptPoint1BoldedDont).font(theme.fonts.bodySB)
+ Text(VectorL10n.analyticsPromptPoint1End)
} icon: {
Image(uiImage: Asset.Images.analyticsCheckmark.image)
}
Label {
Text(VectorL10n.analyticsPromptPoint2Start)
+ Text(VectorL10n.analyticsPromptPoint2BoldedDont).font(theme.fonts.bodySB)
+ Text(VectorL10n.analyticsPromptPoint2End)
} icon: {
Image(uiImage: Asset.Images.analyticsCheckmark.image)
}
Label {
Text(VectorL10n.analyticsPromptPoint3)
} icon: {
Image(uiImage: Asset.Images.analyticsCheckmark.image)
}
AnalyticsPromptCheckmarkItem(attributedString: viewModel.viewState.strings.point1)
AnalyticsPromptCheckmarkItem(attributedString: viewModel.viewState.strings.point2)
AnalyticsPromptCheckmarkItem(string: VectorL10n.analyticsPromptPoint3)
}
.font(theme.fonts.body)
}
@@ -101,7 +83,7 @@ struct AnalyticsPrompt: View {
Image(uiImage: Asset.Images.analyticsLogo.image)
.padding(.bottom, 25)
Text(VectorL10n.analyticsPromptTitle(viewModel.viewState.appDisplayName))
Text(VectorL10n.analyticsPromptTitle(viewModel.viewState.strings.appDisplayName))
.font(theme.fonts.title2B)
.foregroundColor(theme.colors.primaryContent)
.padding(.bottom, 2)
@@ -116,6 +98,7 @@ struct AnalyticsPrompt: View {
checkmarkList
.foregroundColor(theme.colors.secondaryContent)
.padding(.bottom, 16)
}
.padding(.top, 50)
.padding(.horizontal, 16)
@@ -0,0 +1,92 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
@available(iOS 14.0, *)
struct AnalyticsPromptCheckmarkItem: View {
// MARK: - Properties
// MARK: Private
@Environment(\.theme) private var theme
/// A string with a bold property.
private struct StringComponent {
let string: String
let isBold: Bool
}
/// Internal representation of the string as composable parts.
private let components: [StringComponent]
// 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
var isBold = false
if let font = attributes[.font] as? UIFont {
isBold = font.fontDescriptor.symbolicTraits.contains(.traitBold)
}
components.append(StringComponent(string: string.substring(with: range), isBold: isBold))
}
self.components = components
}
init(string: String) {
self.components = [StringComponent(string: string, isBold: false)]
}
// MARK: - Views
var label: Text {
components.reduce(Text("")) {
$0 + Text($1.string).font($1.isBold ? theme.fonts.bodySB : theme.fonts.body)
}
}
var body: some View {
Label { label } icon: {
Image(uiImage: Asset.Images.analyticsCheckmark.image)
}
}
}
// MARK: - Previews
@available(iOS 14.0, *)
struct AnalyticsPromptCheckmarkItem_Previews: PreviewProvider {
static let strings = MockAnalyticsPromptStrings()
static var previews: some View {
VStack(alignment:.leading) {
AnalyticsPromptCheckmarkItem(attributedString: strings.point1)
AnalyticsPromptCheckmarkItem(attributedString: strings.point2)
AnalyticsPromptCheckmarkItem(attributedString: strings.longString)
AnalyticsPromptCheckmarkItem(attributedString: strings.shortString)
}
.padding()
}
}
@@ -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)
}
}
}