11import Foundation
22import SwiftCrossUI
33
4- struct Note : Codable , Equatable {
4+ struct Note : Codable , Equatable , Identifiable {
55 var id = UUID ( )
66 var title : String
77 var content : String
8+
9+ var truncatedDescription : String {
10+ let firstLine = content. split ( separator: " \n " ) . first ?? [ ]
11+ let words = firstLine. split ( separator: " " , omittingEmptySubsequences: false )
12+ let limit = 20
13+ var output = " "
14+ for (index, word) in words. enumerated ( ) {
15+ let addition = ( index == 0 ? " " : " " ) + word
16+ guard output. count + addition. count <= limit else {
17+ if index == 0 {
18+ // We should at least output a little snippet
19+ output += word. prefix ( limit)
20+ }
21+ break
22+ }
23+ output += addition
24+ }
25+ if content. isEmpty {
26+ output = " No content "
27+ } else if output. count < content. count {
28+ output += " ... "
29+ }
30+ return output
31+ }
832}
933
1034struct ContentView : View {
@@ -14,11 +38,7 @@ struct ContentView: View {
1438 Note ( title: " Hello, world! " , content: " Welcome SwiftCrossNotes! " ) ,
1539 Note (
1640 title: " Shopping list " ,
17- content: """
18- - Carrots
19- - Mushrooms
20- - Pasta
21- """
41+ content: " Carrots, mushrooms, and party pies "
2242 ) ,
2343 ]
2444
@@ -53,21 +73,27 @@ struct ContentView: View {
5373 var body : some View {
5474 NavigationSplitView {
5575 VStack {
56- ForEach ( notes) { note in
57- Button ( note. title) {
58- selectedNoteId = note. id
76+ ScrollView {
77+ List ( notes, selection: $selectedNoteId) { note in
78+ VStack ( alignment: . leading, spacing: 0 ) {
79+ Text ( note. title. isEmpty ? " Untitled " : note. title)
80+ Text ( note. truncatedDescription)
81+ . foregroundColor ( . gray)
82+ . font ( . system( size: 12 ) )
83+ }
5984 }
85+ . padding ( 10 )
6086 }
61- Spacer ( )
6287 if let error = error {
6388 Text ( error)
6489 . foregroundColor ( . red)
6590 }
6691 Button ( " New note " ) {
67- let note = Note ( title: " Untitled " , content: " " )
92+ let note = Note ( title: " " , content: " " )
6893 notes. append ( note)
6994 selectedNoteId = note. id
7095 }
96+ . padding ( 10 )
7197 }
7298 . onChange ( of: notes) {
7399 do {
@@ -91,23 +117,26 @@ struct ContentView: View {
91117 self . error = " Failed to load notes "
92118 }
93119 }
94- . padding ( 10 )
95120 } detail: {
96- VStack {
121+ VStack ( alignment : . leading ) {
97122 if let selectedNote = selectedNote {
98- VStack ( spacing: 0 ) {
123+ VStack ( alignment : . leading , spacing: 4 ) {
99124 Text ( " Title " )
100- TextField ( " Title " , selectedNote. title)
101- } . padding ( . bottom , 10 )
125+ TextField ( " Title " , text : selectedNote. title)
126+ }
102127
103- VStack ( spacing: 0 ) {
128+ VStack ( alignment : . leading , spacing: 4 ) {
104129 Text ( " Content " )
105- TextField ( " Content " , selectedNote. content)
130+ TextField ( " Content " , text : selectedNote. content)
106131 }
107132 } else {
108133 Text ( " Select a note... " )
109134 }
110- } . padding ( 10 )
135+ }
136+ . frame ( maxWidth: 400 )
137+ . padding ( 10 )
138+
139+ Spacer ( )
111140 }
112141 }
113142}
0 commit comments