8
8
import SwiftUI
9
9
10
10
public struct LegacyLicenseView : View {
11
- @State private var sentences = [ LegacyLicenseSentence] ( )
11
+ @State private var lines = [ [ LegacyLicenseSentence] ] ( )
12
12
13
13
private let library : Library
14
14
@@ -18,39 +18,45 @@ public struct LegacyLicenseView: View {
18
18
19
19
public var body : some View {
20
20
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 {
26
25
Text ( sentence. body)
27
26
. 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
+ }
28
45
}
29
46
}
30
47
}
31
48
. frame ( maxWidth: . infinity, alignment: . leading)
32
49
. padding ( )
33
50
}
34
51
. onAppear {
35
- sentences = resolve ( library. licenseBody)
52
+ lines = resolve ( library. licenseBody)
36
53
}
37
54
. navigationBarTitleInlineIfPossible ( library. name)
38
55
}
39
56
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
- let pattern : String = " https?://[A-Za-z0-9 \\ . \\ - \\ [ \\ ]!@#$%&=+/?:_]+ "
54
- return inputText. split ( pattern)
57
+ private func resolve( _ inputText: String ) -> [ [ LegacyLicenseSentence ] ] {
58
+ return inputText
59
+ . components ( separatedBy: . newlines)
60
+ . map { $0. split ( URL . regexPattern) }
55
61
}
56
62
}
0 commit comments