Skip to content

Commit d726f3c

Browse files
committed
Fixed the layout of LegacyLicenseView
1 parent 2fe25f4 commit d726f3c

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

Sources/LicenseList/LegacyLicenseView.swift

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import SwiftUI
99

1010
public struct LegacyLicenseView: View {
11-
@State private var sentences = [LegacyLicenseSentence]()
11+
@State private var lines = [[LegacyLicenseSentence]]()
1212

1313
private let library: Library
1414

@@ -18,38 +18,45 @@ public struct LegacyLicenseView: View {
1818

1919
public var body: some View {
2020
ScrollView {
21-
VStack(spacing: 0) {
22-
ForEach(sentences, id: \.body) { sentence in
23-
if sentence.isHyperLink {
24-
hyperLinkText(sentence.body)
25-
} else {
21+
VStack(alignment: .leading, spacing: 0) {
22+
ForEach(lines.indices, id: \.self) { index in
23+
if lines[index].count == 1,
24+
let sentence = lines[index].first {
2625
Text(sentence.body)
2726
.font(.caption)
27+
} else {
28+
lines[index].reduce(Text("")) { result, sentence in
29+
if sentence.isHyperLink {
30+
return result + Text(sentence.body)
31+
.font(.caption)
32+
.foregroundColor(Color.blue)
33+
} else {
34+
return result + Text(sentence.body)
35+
.font(.caption)
36+
}
37+
}
38+
.onTapGesture {
39+
if let linkText = lines[index].first(where: { $0.isHyperLink })?.body,
40+
let url = URL(string: linkText),
41+
UIApplication.shared.canOpenURL(url) {
42+
UIApplication.shared.open(url)
43+
}
44+
}
2845
}
2946
}
3047
}
3148
.frame(maxWidth: .infinity, alignment: .leading)
3249
.padding()
3350
}
3451
.onAppear {
35-
sentences = resolve(library.licenseBody)
52+
lines = resolve(library.licenseBody)
3653
}
3754
.navigationBarTitleInlineIfPossible(library.name)
3855
}
3956

40-
private func hyperLinkText(_ linkText: String) -> some View {
41-
Text(linkText)
42-
.font(.caption)
43-
.foregroundColor(Color.blue)
44-
.onTapGesture {
45-
if let url = URL(string: linkText),
46-
UIApplication.shared.canOpenURL(url) {
47-
UIApplication.shared.open(url)
48-
}
49-
}
50-
}
51-
52-
private func resolve(_ inputText: String) -> [LegacyLicenseSentence] {
53-
return inputText.split(URL.regexPattern)
57+
private func resolve(_ inputText: String) -> [[LegacyLicenseSentence]] {
58+
return inputText
59+
.components(separatedBy: .newlines)
60+
.map { $0.split(URL.regexPattern) }
5461
}
5562
}

0 commit comments

Comments
 (0)