Skip to content

Commit 42f2f97

Browse files
committed
Implement List view (for displaying lists of selectable items)
There are still a few things to iron out such as whether to be a scroll view by default or not.
1 parent d5d26d6 commit 42f2f97

File tree

86 files changed

+1918
-589
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+1918
-589
lines changed

Examples/Package.resolved

Lines changed: 33 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Examples/Sources/NotesExample/ContentView.swift

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
import Foundation
22
import 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

1034
struct 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

Comments
 (0)