-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
SearchSuggestionsView.swift
100 lines (85 loc) · 2.89 KB
/
SearchSuggestionsView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// GlobalSearchPopoverView.swift
// RedditOs
//
// Created by Thomas Ricouard on 05/08/2020.
//
import SwiftUI
import Backend
struct SearchSuggestionsView: View {
@EnvironmentObject private var uiState: UIState
@EnvironmentObject private var currentUser: CurrentUserStore
@EnvironmentObject private var searchState: SearchState
struct ResultContainerView<Content: View>: View {
private let content: Content
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content()
}
var body: some View {
Group {
content
}.padding(4)
}
}
var body: some View {
Section(header: makeTitle("Quick access")) {
makeQuickAccess()
}
Divider()
Section(header: makeTitle("My subscriptions")) {
makeMySubscriptionsSearch()
}
Divider()
Section(header: makeTitle("Subreddit search")) {
makeSubredditSearch()
}
}
private func makeTitle(_ title: String) -> some View {
Text(title)
.font(.headline)
.fontWeight(.bold)
}
private func makeQuickAccess() -> some View {
ResultContainerView {
SearchSuggestionsResultRow(icon: nil,
name: "Go to r/\(searchState.searchText)")
.onTapGesture {
uiState.searchRoute = .subreddit(subreddit: searchState.searchText)
}
}
}
private func makeMySubscriptionsSearch() -> some View {
ResultContainerView {
if let subs = searchState.filteredSubscriptions {
if subs.isEmpty {
Label("No matching subscriptions for \(searchState.searchText)", systemImage: "magnifyingglass")
} else {
ForEach(subs) { sub in
makeSubRow(icon: sub.iconImg, name: sub.displayName)
}
}
}
}
}
private func makeSubredditSearch() -> some View {
ResultContainerView {
if let results = searchState.results {
if results.isEmpty {
Label("No matching search for \(searchState.searchText)", systemImage: "magnifyingglass")
} else {
ForEach(results) { sub in
makeSubRow(icon: sub.iconImg, name: sub.name)
}
}
} else if searchState.isLoading {
LoadingRow(text: nil)
}
}
}
private func makeSubRow(icon: String?, name: String) -> some View {
SearchSuggestionsResultRow(icon: icon, name: name)
.onTapGesture {
uiState.searchRoute = .subreddit(subreddit: name)
}
}
}