Skip to content

Commit e6ef356

Browse files
committed
Add telemetry
1 parent 3da9f2c commit e6ef356

File tree

13 files changed

+232
-42
lines changed

13 files changed

+232
-42
lines changed

Modules/Sources/Support/SupportDataProvider.swift

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,27 @@ import Foundation
22
import WordPressCore
33

44
public enum SupportFormAction {
5-
case viewSupportForm
5+
case viewApplicationLogList
6+
case viewApplicationLog(String)
7+
case deleteApplicationLogs([String])
8+
case deleteAllApplicationLogs
9+
10+
case viewSupportBotConversationList
11+
case startSupportBotConversation
12+
case viewSupportBotConversation(conversationId: UInt64)
13+
case replyToSupportBotMessage(conversationId: UInt64)
14+
case failToCreateBotConversation(Error)
15+
case failToReplyToBotConversation(Error)
16+
17+
case viewSupportTicketList
18+
case viewSupportTicket(ticketId: UInt64)
19+
case createSupportTicket
20+
case replyToSupportTicket(ticketId: UInt64)
21+
case failToCreateSupportTicket(Error)
22+
case failToReplyToSupportTicket(Error)
23+
24+
case viewDiagnostics
25+
case emptyDiskCache(bytesSaved: Int64)
626
}
727

828
@MainActor
@@ -53,7 +73,23 @@ public final class SupportDataProvider: ObservableObject, Sendable {
5373
}
5474

5575
public func sendMessage(message: String, in conversation: BotConversation? = nil) async throws -> BotConversation {
56-
try await self.botConversationDataProvider.sendMessage(message: message, in: conversation)
76+
if let conversation {
77+
self.userDid(.replyToSupportBotMessage(conversationId: conversation.id))
78+
} else {
79+
self.userDid(.startSupportBotConversation)
80+
}
81+
82+
do {
83+
return try await self.botConversationDataProvider.sendMessage(message: message, in: conversation)
84+
} catch {
85+
if conversation != nil {
86+
self.userDid(.failToCreateBotConversation(error))
87+
} else {
88+
self.userDid(.failToReplyToBotConversation(error))
89+
}
90+
91+
throw error
92+
}
5793
}
5894

5995
// Support Conversations Data Source
@@ -71,12 +107,19 @@ public final class SupportDataProvider: ObservableObject, Sendable {
71107
user: SupportUser,
72108
attachments: [URL]
73109
) async throws -> Conversation {
74-
try await self.supportConversationDataProvider.replyToSupportConversation(
75-
id: id,
76-
message: message,
77-
user: user,
78-
attachments: attachments
79-
)
110+
self.userDid(.replyToSupportTicket(ticketId: id))
111+
112+
do {
113+
return try await self.supportConversationDataProvider.replyToSupportConversation(
114+
id: id,
115+
message: message,
116+
user: user,
117+
attachments: attachments
118+
)
119+
} catch {
120+
self.userDid(.failToReplyToSupportTicket(error))
121+
throw error
122+
}
80123
}
81124

82125
public func createSupportConversation(
@@ -85,12 +128,19 @@ public final class SupportDataProvider: ObservableObject, Sendable {
85128
user: SupportUser,
86129
attachments: [URL]
87130
) async throws -> Conversation {
88-
try await self.supportConversationDataProvider.createSupportConversation(
89-
subject: subject,
90-
message: message,
91-
user: user,
92-
attachments: attachments
93-
)
131+
self.userDid(.createSupportTicket)
132+
133+
do {
134+
return try await self.supportConversationDataProvider.createSupportConversation(
135+
subject: subject,
136+
message: message,
137+
user: user,
138+
attachments: attachments
139+
)
140+
} catch {
141+
self.userDid(.failToCreateSupportTicket(error))
142+
throw error
143+
}
94144
}
95145

96146
// Application Logs
@@ -103,10 +153,12 @@ public final class SupportDataProvider: ObservableObject, Sendable {
103153
}
104154

105155
public func deleteApplicationLogs(in list: [ApplicationLog]) async throws {
156+
self.userDid(.deleteApplicationLogs(list.map({ $0.id })))
106157
try await self.applicationLogProvider.deleteApplicationLogs(in: list)
107158
}
108159

109160
public func deleteAllApplicationLogs() async throws {
161+
self.userDid(.deleteAllApplicationLogs)
110162
try await self.applicationLogProvider.deleteAllApplicationLogs()
111163
}
112164
}

Modules/Sources/Support/UI/Application Logs/ActivityLogDetailView.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct ActivityLogDetailView: View {
88

99
enum ViewState: Equatable {
1010
case loading
11-
case loaded(String, Bool)
11+
case loaded(String, isSharing: Bool)
1212
case error(Error)
1313

1414
static func == (lhs: ActivityLogDetailView.ViewState, rhs: ActivityLogDetailView.ViewState) -> Bool {
@@ -26,7 +26,7 @@ struct ActivityLogDetailView: View {
2626
private var state: ViewState = .loading
2727

2828
@State
29-
private var isSharing: Bool = false
29+
private var isDisplayingShareSheet: Bool = false
3030

3131
@State
3232
private var sharingIsDisabled: Bool = true
@@ -53,26 +53,29 @@ struct ActivityLogDetailView: View {
5353
.disabled(self.sharingIsDisabled)
5454
}
5555
}
56-
.sheet(isPresented: self.$isSharing, onDismiss: {
56+
.sheet(isPresented: self.$isDisplayingShareSheet, onDismiss: {
5757
guard case .loaded(let content, _) = self.state else {
5858
return
5959
}
6060

61-
self.state = .loaded(content, false)
61+
self.state = .loaded(content, isSharing: false)
6262
}, content: {
6363
ActivityLogSharingView(applicationLog: applicationLog) {
6464
AnyView(erasing: Text("TODO: A new support request with the application log attached"))
6565
}
6666
.presentationDetents([.medium])
6767
})
68+
.onAppear {
69+
self.dataProvider.userDid(.viewApplicationLog(self.applicationLog.id))
70+
}
6871
.task(self.loadLogContent)
6972
.refreshable(action: self.loadLogContent)
7073
.onChange(of: state) { oldValue, newValue in
7174
if case .loaded(_, let isSharing) = state {
7275
self.sharingIsDisabled = false
73-
self.isSharing = isSharing
76+
self.isDisplayingShareSheet = isSharing
7477
} else {
75-
self.isSharing = false
78+
self.isDisplayingShareSheet = false
7679
self.sharingIsDisabled = true
7780
}
7881
}
@@ -108,7 +111,7 @@ struct ActivityLogDetailView: View {
108111
do {
109112
let content = try await self.dataProvider.readApplicationLog(applicationLog)
110113

111-
self.state = .loaded(content, false)
114+
self.state = .loaded(content, isSharing: false)
112115
} catch {
113116
self.state = .error(error)
114117
}
@@ -119,13 +122,14 @@ struct ActivityLogDetailView: View {
119122
return
120123
}
121124

122-
state = .loaded(content, true)
125+
state = .loaded(content, isSharing: true)
123126
}
124127
}
125128

126129
#Preview {
127130
NavigationStack {
128131
ActivityLogDetailView(
129-
applicationLog: SupportDataProvider.applicationLog ).environmentObject(SupportDataProvider.testing)
132+
applicationLog: SupportDataProvider.applicationLog
133+
).environmentObject(SupportDataProvider.testing)
130134
}
131135
}

Modules/Sources/Support/UI/Application Logs/ActivityLogListView.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ public struct ActivityLogListView: View {
6868
}, message: {
6969
Text("You won't be able to get them back.")
7070
})
71+
.onAppear {
72+
self.dataProvider.userDid(.viewApplicationLogList)
73+
}
7174
.refreshable {
7275
await self.loadLogFiles()
7376
}

Modules/Sources/Support/UI/Bot Conversations/ConversationListView.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ public struct ConversationListView: View {
120120
.overlay {
121121
OverlayProgressView(shouldBeVisible: state.isPartiallyLoaded)
122122
}
123+
.onAppear {
124+
self.dataProvider.userDid(.viewSupportBotConversationList)
125+
}
123126
.task(self.loadConversations)
124127
.refreshable(action: self.reloadConversations)
125128
}

Modules/Sources/Support/UI/Bot Conversations/ConversationView.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,13 @@ public struct ConversationView: View {
295295
.overlay {
296296
OverlayProgressView(shouldBeVisible: state.isPartiallyLoaded)
297297
}
298+
.onAppear {
299+
if let conversationId {
300+
self.dataProvider.userDid(.viewSupportBotConversation(conversationId: conversationId))
301+
} else {
302+
self.dataProvider.userDid(.startSupportBotConversation)
303+
}
304+
}
298305
.task(self.loadExistingConversation)
299306
.refreshable(action: self.reloadConversation)
300307
}
@@ -447,7 +454,6 @@ public struct ConversationView: View {
447454
private func sendMessage(_ message: String) {
448455
self.state = self.state.transitioningToSendingMessage(message: message, task: Task {
449456
do {
450-
451457
let thinkingTask = Task.delayedAndRunOnMainActor(for: .seconds(1.5)) {
452458
self.state = self.state.transitioningToThinking()
453459
}

Modules/Sources/Support/UI/Diagnostics/DiagnosticsView.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import WordPressCore
33

44
public struct DiagnosticsView: View {
55

6+
@EnvironmentObject
7+
private var dataProvider: SupportDataProvider
8+
69
public init() {}
710

811
public var body: some View {
@@ -17,7 +20,9 @@ public struct DiagnosticsView: View {
1720
}
1821
.navigationTitle("Diagnostics")
1922
.background(.background)
20-
23+
.onAppear {
24+
dataProvider.userDid(.viewDiagnostics)
25+
}
2126
}
2227
}
2328

Modules/Sources/Support/UI/Diagnostics/EmptyDiskCacheView.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import WordPressCore
33

44
struct EmptyDiskCacheView: View {
55

6+
@EnvironmentObject
7+
private var dataProvider: SupportDataProvider
8+
69
enum ViewState: Equatable {
710
case loading
811
case loaded(usage: DiskCache.DiskCacheUsage)
@@ -77,7 +80,7 @@ struct EmptyDiskCacheView: View {
7780
.font(.footnote)
7881
.foregroundStyle(.secondary)
7982
} else {
80-
Text("^[\(usage.count) cache files](inflect: true) (\(usage.formattedDiskUsage))")
83+
Text("^[\(usage.fileCount) cache files](inflect: true) (\(usage.formattedDiskUsage))")
8184
.font(.footnote)
8285
.foregroundStyle(.secondary)
8386
}
@@ -102,7 +105,8 @@ struct EmptyDiskCacheView: View {
102105
}
103106

104107
}
105-
}.task(self.fetchDiskCacheUsage)
108+
}
109+
.task(self.fetchDiskCacheUsage)
106110
}
107111
}
108112

@@ -121,6 +125,12 @@ struct EmptyDiskCacheView: View {
121125

122126
// Simulated async cache clearing with progress updates.
123127
private func clearDiskCache() async {
128+
guard case .loaded(let usage) = state else {
129+
return
130+
}
131+
132+
self.dataProvider.userDid(.emptyDiskCache(bytesSaved: usage.byteCount))
133+
124134
self.state = .clearing(progress: 0, result: "")
125135

126136
do {

Modules/Sources/Support/UI/Support Conversations/ScreenshotPicker.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ struct ScreenshotPicker: View {
6767
selection: $selectedPhotos,
6868
maxSelectionCount: maxScreenshots,
6969
matching: .images
70-
) {
70+
) { [imageCount = attachedImages.count] in
7171
HStack {
7272
Image(systemName: "camera.fill")
73-
Text(attachedImages.isEmpty ? Localization.addScreenshots : Localization.addMoreScreenshots)
73+
Text(imageCount == 0 ? Localization.addScreenshots : Localization.addMoreScreenshots)
7474
}
7575
.frame(maxWidth: .infinity)
7676
.padding()

Modules/Sources/Support/UI/Support Conversations/SupportConversationListView.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ public struct SupportConversationListView: View {
8181
.overlay {
8282
OverlayProgressView(shouldBeVisible: self.state.isPartiallyLoaded)
8383
}
84+
.onAppear {
85+
self.dataProvider.userDid(.viewSupportTicketList)
86+
}
8487
.task(self.loadConversations)
8588
.refreshable(action: self.reloadConversations)
8689
}

Modules/Sources/Support/UI/Support Conversations/SupportConversationView.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,11 @@ public struct SupportConversationView: View {
9999
.environmentObject(dataProvider)
100100
}
101101
}
102-
.task(self.loadConversation)
103-
.refreshable(action: self.reloadConversation)
104102
.onAppear {
105-
debugPrint("💬 onAppear – detail")
106-
}
107-
.onDisappear {
108-
debugPrint("💬 onDisappear – detail")
103+
self.dataProvider.userDid(.viewSupportTicket(ticketId: conversationSummary.id))
109104
}
105+
.task(self.loadConversation)
106+
.refreshable(action: self.reloadConversation)
110107
}
111108

112109
@ViewBuilder

0 commit comments

Comments
 (0)