Skip to content

Commit 5b5867d

Browse files
combine type and content into single enum
1 parent d9c89c2 commit 5b5867d

File tree

3 files changed

+66
-42
lines changed

3 files changed

+66
-42
lines changed

Demo/PowerSyncExample/Components/SearchResultRow.swift

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,78 @@ struct SearchResultRow: View {
66
var body: some View {
77
HStack {
88

9-
Image(systemName: item.type == .list ? "list.bullet" : "checkmark.circle")
10-
.foregroundColor(.secondary)
9+
Image(
10+
systemName: {
11+
switch item.content {
12+
case .list:
13+
return "list.bullet"
14+
case .todo:
15+
return "checkmark.circle"
16+
}
17+
}()
18+
)
19+
.foregroundColor(.secondary)
20+
21+
switch item.content {
22+
case .list(let listContent):
23+
Text(listContent.name)
1124

12-
if let list = item.listContent {
13-
Text(list.name)
14-
} else if let todo = item.todo {
25+
case .todo(let todo):
1526
Text(todo.description)
1627
.strikethrough(todo.isComplete, color: .secondary)
1728
.foregroundColor(todo.isComplete ? .secondary : .primary)
18-
} else {
19-
Text("Unknown item")
2029
}
2130

2231
Spacer()
2332

24-
Image(systemName: "chevron.right")
25-
.font(.caption.weight(.bold))
26-
.foregroundColor(.secondary.opacity(0.5))
33+
Image(systemName: "chevron.right")
34+
.font(.caption.weight(.bold))
35+
.foregroundColor(.secondary.opacity(0.5))
2736
}
2837
.contentShape(Rectangle())
2938
}
3039
}
3140

3241
#Preview {
3342
List {
34-
SearchResultRow(item: SearchResultItem(
35-
id: UUID().uuidString,
36-
type: .list,
37-
content: ListContent(id: UUID().uuidString, name: "Groceries", createdAt: "now", ownerId: "user1")
38-
))
39-
SearchResultRow(item: SearchResultItem(
40-
id: UUID().uuidString,
41-
type: .todo,
42-
content: Todo(id: UUID().uuidString, listId: "list1", description: "Buy milk", isComplete: false)
43-
))
44-
SearchResultRow(item: SearchResultItem(
45-
id: UUID().uuidString,
46-
type: .todo,
47-
content: Todo(id: UUID().uuidString, listId: "list1", description: "Walk the dog", isComplete: true)
48-
))
43+
SearchResultRow(
44+
item: SearchResultItem(
45+
id: UUID().uuidString,
46+
content: .list(
47+
ListContent(
48+
id: UUID().uuidString,
49+
name: "Groceries",
50+
createdAt: "now",
51+
ownerId: "user1"
52+
)
53+
)
54+
)
55+
)
56+
SearchResultRow(
57+
item: SearchResultItem(
58+
id: UUID().uuidString,
59+
content: .todo(
60+
Todo(
61+
id: UUID().uuidString,
62+
listId: "list1",
63+
description: "Buy milk",
64+
isComplete: false
65+
)
66+
)
67+
)
68+
)
69+
SearchResultRow(
70+
item: SearchResultItem(
71+
id: UUID().uuidString,
72+
content: .todo(
73+
Todo(
74+
id: UUID().uuidString,
75+
listId: "list1",
76+
description: "Walk the dog",
77+
isComplete: true
78+
)
79+
)
80+
)
81+
)
4982
}
5083
}
Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
import Foundation
22

3-
enum SearchResultType {
4-
case list
5-
case todo
3+
enum SearchResultContent: Hashable {
4+
case list(ListContent)
5+
case todo(Todo)
66
}
77

88
struct SearchResultItem: Identifiable, Hashable {
99
let id: String
10-
let type: SearchResultType
11-
let content: AnyHashable
12-
13-
var listContent: ListContent? {
14-
content as? ListContent
15-
}
16-
17-
var todo: Todo? {
18-
content as? Todo
19-
}
10+
let content: SearchResultContent
2011

2112
func hash(into hasher: inout Hasher) {
2213
hasher.combine(id)
23-
hasher.combine(type)
14+
hasher.combine(content)
2415
}
2516

2617
static func == (lhs: SearchResultItem, rhs: SearchResultItem) -> Bool {
27-
lhs.id == rhs.id && lhs.type == rhs.type
18+
lhs.id == rhs.id && lhs.content == rhs.content
2819
}
2920
}

Demo/PowerSyncExample/Screens/SearchScreen.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ struct SearchScreen: View {
7575

7676
self.searchResults = results.compactMap { item in
7777
if let list = item as? ListContent {
78-
return SearchResultItem(id: list.id, type: .list, content: list)
78+
return SearchResultItem(id: list.id, content: .list(list))
7979
} else if let todo = item as? Todo {
80-
return SearchResultItem(id: todo.id, type: .todo, content: todo)
80+
return SearchResultItem(id: todo.id, content: .todo(todo))
8181
}
8282
return nil
8383
}

0 commit comments

Comments
 (0)