Skip to content

Commit 39bb998

Browse files
committed
Move readble creation to TestCaseResult
1 parent 93871d5 commit 39bb998

File tree

2 files changed

+73
-60
lines changed

2 files changed

+73
-60
lines changed

WooCommerce/Classes/ViewRelated/Connectivity Tool/ConnectivityTool.swift

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,15 @@ final class ConnectivityToolViewController: UIHostingController<ConnectivityTool
4242

4343
private func showContactSupportForm() {
4444
let attachments: [ZendeskAttachment] = {
45-
guard let report = self.viewModel.zendeskAttachment() else { return [] }
46-
return [report]
45+
guard let troubleshootingDescription = self.viewModel.troubleshootingDescription(),
46+
let data = troubleshootingDescription.data(using: .utf8) else { return [] }
47+
return [
48+
ZendeskAttachment(
49+
data: data,
50+
filename: "connectivitytest_log.txt",
51+
contentType: "text/plain"
52+
)
53+
]
4754
}()
4855
let supportController = SupportFormHostingController(viewModel: SupportFormViewModel(attachments: attachments))
4956
supportController.show(from: self)
@@ -182,21 +189,6 @@ struct ConnectivityToolCard: View {
182189
return false
183190
}
184191
}
185-
186-
var reportDescription: String {
187-
switch self {
188-
case .inProgress: return "In progress"
189-
case .success: return "Success"
190-
case .empty(let message): return message
191-
case .error(_, let actions):
192-
return actions.reduce("") { partialResult, action in
193-
if let technicalDetails = action.technicalDetails {
194-
return partialResult.appending("\(technicalDetails)\n")
195-
}
196-
return partialResult
197-
}
198-
}
199-
}
200192
}
201193

202194
/// Represents the icon of the card

WooCommerce/Classes/ViewRelated/Connectivity Tool/ConnectivityToolViewModel.swift

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ final class ConnectivityToolViewModel {
5050
}
5151
}
5252

53-
fileprivate struct TestCaseResult {
54-
let test: ConnectivityToolViewModel.ConnectivityTest
55-
let result: ConnectivityToolCard.ConnectivityState
56-
let timeTaken: Double
57-
}
58-
5953
private var lastResults: [TestCaseResult] = []
6054

6155
/// Sequentially runs all connectivity tests defined in `ConnectivityTest`.
@@ -91,7 +85,6 @@ final class ConnectivityToolViewModel {
9185
// Track test result
9286
trackResponseEvent(for: testCase, success: testResult.isSuccess, timeTaken: timeTaken)
9387

94-
9588
lastResults.append(TestCaseResult(test: testCase,
9689
result: testResult,
9790
timeTaken: timeTaken))
@@ -106,29 +99,13 @@ final class ConnectivityToolViewModel {
10699
cards.append(noConnectionsIssueState())
107100
}
108101

109-
func zendeskAttachment() -> ZendeskAttachment? {
102+
/// This is not a user facing text but will be part of the Zendesk submission for troubleshooting.
103+
func troubleshootingDescription() -> String? {
110104
guard !lastResults.isEmpty else {
111105
return nil
112106
}
113107

114-
var output = ""
115-
lastResults.forEach { result in
116-
output = output
117-
.appending("## \(result.test.reportName) \n")
118-
.appending("Took: \(result.timeTaken)ms \n")
119-
.appending("Result: \n")
120-
.appending(result.result.reportDescription)
121-
.appending("\n")
122-
.appending("\n")
123-
}
124-
125-
guard let data = output.data(using: .utf8) else {
126-
return nil
127-
}
128-
129-
return ZendeskAttachment(data: data,
130-
filename: "connectivitytest_log.txt",
131-
contentType: "text/plain")
108+
return lastResults.map { $0.description() }.joined()
132109
}
133110

134111
/// Perform the test for a provided test case.
@@ -262,12 +239,12 @@ final class ConnectivityToolViewModel {
262239
}
263240

264241
let message: String
265-
let readMoreAction: ConnectivityToolCard.ConnectivityState.Action
266242
let readMore = Localization.Action.readMore
267243
let generalTroubleshootAction = {
268244
UIApplication.shared.open(WooConstants.URLs.troubleshootErrorLoadingData.asURL())
269245
ServiceLocator.analytics.track(event: .ConnectivityTool.readMoreTapped())
270246
}
247+
var readMoreAction = ConnectivityToolCard.ConnectivityState.Action(title: readMore, systemImage: SystemImages.readMore.rawValue, action: generalTroubleshootAction)
271248
let jetpackTroubleshootAction = {
272249
UIApplication.shared.open(WooConstants.URLs.troubleshootJetpackConnection.asURL())
273250
ServiceLocator.analytics.track(event: .ConnectivityTool.readMoreTapped())
@@ -277,7 +254,8 @@ final class ConnectivityToolViewModel {
277254
switch (error, error.isTimeoutError) {
278255
case (_, true):
279256
message = Localization.ErrorMessage.timeout
280-
readMoreAction = .init(title: readMore, systemImage: SystemImages.readMore.rawValue, action: generalTroubleshootAction)
257+
return .error(message, [readMoreAction, retryAction()])
258+
281259
case (let decodingError as DecodingError, _):
282260
message = Localization.ErrorMessage.decodingError
283261
let technicalDetails = formatDecodingError(decodingError, operation: operation)
@@ -287,17 +265,25 @@ final class ConnectivityToolViewModel {
287265
systemImage: SystemImages.viewDetails.rawValue,
288266
technicalDetails: technicalDetails
289267
)
290-
readMoreAction = .init(title: readMore, systemImage: SystemImages.readMore.rawValue, action: generalTroubleshootAction)
291268
return .error(message, [viewDetailsAction, readMoreAction, retryAction()])
269+
292270
case (DotcomError.jetpackNotConnected, _):
293271
message = Localization.ErrorMessage.noJetpackConnection
294272
readMoreAction = .init(title: readMore, systemImage: SystemImages.readMore.rawValue, action: jetpackTroubleshootAction)
295-
default:
273+
return .error(message, [readMoreAction, retryAction()])
274+
275+
case (let error, _):
296276
message = Localization.ErrorMessage.generic
277+
let technicalDetails = String(describing: error)
278+
let viewDetailsTitle = Localization.Action.viewDetails
279+
let viewDetailsAction = ConnectivityToolCard.ConnectivityState.Action(
280+
title: viewDetailsTitle,
281+
systemImage: SystemImages.viewDetails.rawValue,
282+
technicalDetails: technicalDetails
283+
)
297284
readMoreAction = .init(title: readMore, systemImage: SystemImages.readMore.rawValue, action: generalTroubleshootAction)
285+
return .error(message, [viewDetailsAction, readMoreAction, retryAction()])
298286
}
299-
300-
return .error(message, [readMoreAction, retryAction()])
301287
}
302288

303289
private func retryAction() -> ConnectivityToolCard.ConnectivityState.Action {
@@ -393,6 +379,52 @@ final class ConnectivityToolViewModel {
393379
}
394380
}
395381

382+
fileprivate struct TestCaseResult {
383+
let test: ConnectivityToolViewModel.ConnectivityTest
384+
let result: ConnectivityToolCard.ConnectivityState
385+
let timeTaken: TimeInterval
386+
387+
/// This is not a user facing text, but will be part of the attachment sent to Zendesk
388+
func description() -> String {
389+
let lines: [String] = [
390+
"## \(caseName)",
391+
"Took: \(formattedTimeTaken)",
392+
"Result: \(resultDescription)",
393+
""
394+
]
395+
return lines.joined(separator: "\n")
396+
}
397+
398+
private var formattedTimeTaken: String {
399+
let milliseconds = timeTaken * 1000
400+
return String(format: "%.0fms", milliseconds)
401+
}
402+
403+
/// This is not a user facing text, but will be part of the attachment sent to Zendesk
404+
private var caseName: String {
405+
switch test {
406+
case .internetConnection: "Internet Connection"
407+
case .wpComServers: "Connecting to WordPress.com Servers"
408+
case .site: "Connecting to your site"
409+
case .siteOrders: "Fetching your site orders"
410+
case .loadingProducts: "Fetching products in your store"
411+
}
412+
}
413+
414+
/// This is not a user facing text, but will be part of the attachment sent to Zendesk
415+
private var resultDescription: String {
416+
switch result {
417+
case .inProgress: return "In progress"
418+
case .success: return "Success"
419+
case .empty(let message): return message
420+
case .error(_, let actions):
421+
let lines = actions.compactMap { $0.technicalDetails }
422+
return lines.joined(separator: "\n")
423+
}
424+
}
425+
}
426+
427+
396428
private extension ConnectivityToolViewModel {
397429
enum Localization {
398430
static let noIssues = NSLocalizedString(
@@ -529,17 +561,6 @@ private extension ConnectivityToolViewModel {
529561
var inProgressCard: ConnectivityTool.Card {
530562
.init(title: title, icon: icon, state: .inProgress)
531563
}
532-
533-
/// This is not a user facing text, but will be part of the attachment sent to Zendesk
534-
var reportName: String {
535-
switch self {
536-
case .internetConnection: "Internet Connection"
537-
case .wpComServers: "Connecting to WordPress.com Servers"
538-
case .site: "Connecting to your site"
539-
case .siteOrders: "Fetching your site orders"
540-
case .loadingProducts: "Fetching products in your store"
541-
}
542-
}
543564
}
544565
}
545566

0 commit comments

Comments
 (0)