Skip to content

Commit 2107113

Browse files
authored
Lazy render logs to increase performance (#25046)
* Lazy load log files to increase performance. * Refactor nested closures. * Lazy render logs in the new activity log view.
1 parent ee6e037 commit 2107113

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ struct ActivityLogDetailView: View {
88

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

1414
static func == (lhs: ActivityLogDetailView.ViewState, rhs: ActivityLogDetailView.ViewState) -> Bool {
1515
return switch (lhs, rhs) {
1616
case (.loading, .loading): true
17-
case (.loaded(let lhscontent, let lhsisSharing), .loaded(let rhscontent, let rhsisSharing)):
18-
lhscontent == rhscontent && lhsisSharing == rhsisSharing
17+
case (.loaded(let lhsLines, let lhsisSharing), .loaded(let rhsLines, let rhsisSharing)):
18+
lhsLines == rhsLines && lhsisSharing == rhsisSharing
1919
case (.error, .error): true
2020
default: false
2121
}
@@ -38,8 +38,8 @@ struct ActivityLogDetailView: View {
3838
switch self.state {
3939
case .loading:
4040
self.loadingView
41-
case .loaded(let content, _):
42-
self.loadedView(content: content)
41+
case .loaded(let lines, _):
42+
self.loadedView(lines: lines)
4343
case .error(let error):
4444
self.errorView(error: error)
4545
}
@@ -54,11 +54,11 @@ struct ActivityLogDetailView: View {
5454
}
5555
}
5656
.sheet(isPresented: self.$isDisplayingShareSheet, onDismiss: {
57-
guard case .loaded(let content, _) = self.state else {
57+
guard case .loaded(let lines, _) = self.state else {
5858
return
5959
}
6060

61-
self.state = .loaded(content, isSharing: false)
61+
self.state = .loaded(lines, isSharing: false)
6262
}, content: {
6363
ActivityLogSharingView(applicationLog: applicationLog) {
6464
AnyView(erasing: Text("TODO: A new support request with the application log attached"))
@@ -87,15 +87,21 @@ struct ActivityLogDetailView: View {
8787
}
8888

8989
@ViewBuilder
90-
func loadedView(content: String) -> some View {
90+
func loadedView(lines: [String]) -> some View {
9191
ScrollView {
92-
VStack(alignment: .leading) {
93-
TextEditor(text: .constant(content))
94-
.font(.system(.body, design: .monospaced))
95-
.fixedSize(horizontal: false, vertical: true)
96-
.scrollDisabled(true)
97-
.padding()
92+
LazyVStack(alignment: .leading, spacing: 0) {
93+
logLinesContent(lines: lines)
9894
}
95+
.font(.system(.body, design: .monospaced))
96+
.padding()
97+
}
98+
}
99+
100+
@ViewBuilder
101+
private func logLinesContent(lines: [String]) -> some View {
102+
ForEach(Array(lines.enumerated()), id: \.offset) { _, line in
103+
Text(line)
104+
.frame(maxWidth: .infinity, alignment: .leading)
99105
}
100106
}
101107

@@ -110,19 +116,22 @@ struct ActivityLogDetailView: View {
110116
private func loadLogContent() async {
111117
do {
112118
let content = try await self.dataProvider.readApplicationLog(applicationLog)
119+
let lines = content
120+
.split(separator: "\n", omittingEmptySubsequences: false)
121+
.map(String.init)
113122

114-
self.state = .loaded(content, isSharing: false)
123+
self.state = .loaded(lines, isSharing: false)
115124
} catch {
116125
self.state = .error(error)
117126
}
118127
}
119128

120129
private func startSharing() {
121-
guard case .loaded(let content, _) = self.state else {
130+
guard case .loaded(let lines, _) = self.state else {
122131
return
123132
}
124133

125-
state = .loaded(content, isSharing: true)
134+
state = .loaded(lines, isSharing: true)
126135
}
127136
}
128137

WordPress/Classes/ViewRelated/Me/Help & Support/SupportActivityDetailsView.swift

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ struct SupportActivityDetailsView: View {
1212

1313
var body: some View {
1414
ScrollView {
15-
Text(viewModel.logText)
16-
.font(.subheadline)
17-
.frame(maxWidth: .infinity, alignment: .leading)
18-
.padding()
15+
LazyVStack(alignment: .leading, spacing: 0) {
16+
logLinesContent
17+
}
18+
.font(.subheadline)
19+
.padding()
1920
}
2021
.navigationTitle(viewModel.logDate)
2122
.navigationBarTitleDisplayMode(.inline)
@@ -29,10 +30,18 @@ struct SupportActivityDetailsView: View {
2930
}
3031
}
3132
}
33+
34+
@ViewBuilder
35+
private var logLinesContent: some View {
36+
ForEach(Array(viewModel.logLines.enumerated()), id: \.offset) { _, line in
37+
Text(line)
38+
.frame(maxWidth: .infinity, alignment: .leading)
39+
}
40+
}
3241
}
3342

3443
private final class SupportActivityDetailsViewModel: ObservableObject {
35-
let logText: String
44+
let logLines: [String]
3645
let logDate: String
3746

3847
init(logFile: DDLogFileInfo) {
@@ -45,15 +54,17 @@ private final class SupportActivityDetailsViewModel: ObservableObject {
4554

4655
guard let logData = try? Data(contentsOf: URL(fileURLWithPath: logFile.filePath)),
4756
let logText = String(data: logData, encoding: .utf8) else {
48-
self.logText = ""
57+
self.logLines = []
4958
return
5059
}
51-
self.logText = logText
60+
self.logLines = logText
61+
.split(separator: "\n", omittingEmptySubsequences: false)
62+
.map(String.init)
5263
}
5364

5465
func buttonShareTapped() {
5566
let activityVC = UIActivityViewController(
56-
activityItems: [logText],
67+
activityItems: [logLines.joined(separator: "\n")],
5768
applicationActivities: nil
5869
)
5970

0 commit comments

Comments
 (0)