-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathReaderSubscriptionsView.swift
More file actions
140 lines (123 loc) · 4.34 KB
/
Copy pathReaderSubscriptionsView.swift
File metadata and controls
140 lines (123 loc) · 4.34 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import SwiftUI
import WordPressData
import WordPressUI
import WordPressShared
struct ReaderSubscriptionsView: View {
@FetchRequest(
sortDescriptors: [SortDescriptor(\.title, order: .forward)],
predicate: NSPredicate(format: "following = YES")
)
private var subscriptions: FetchedResults<ReaderSiteTopic>
@State private var searchText = ""
@State private var isShowingMainAddSubscriptonPopover = false
@State private var searchResults: [ReaderSiteTopic] = []
@StateObject private var viewModel = ReaderSubscriptionsViewModel()
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
var isShowingSearchResuts: Bool { !searchText.isEmpty }
var onSelection: (_ subscription: ReaderSiteTopic) -> Void = { _ in }
var body: some View {
Group {
if subscriptions.isEmpty {
GeometryReader { proxy in
ScrollView { // Makes it compatible with refreshable()
stateView.frame(width: proxy.size.width, height: proxy.size.height)
}
}
} else {
main
}
}
.refreshable {
await viewModel.refresh()
}
.toolbar {
ReaderSubscriptionAddButton(style: .navigation)
if !subscriptions.isEmpty {
EditButton()
}
}
.navigationTitle(SharedStrings.Reader.subscriptions)
.tint(Color(UIAppColor.primary))
}
@ViewBuilder
private var stateView: some View {
if let error = viewModel.error {
EmptyStateView.failure(error: error) {
Task { await viewModel.refresh() }
}
} else if viewModel.isRefreshing {
ProgressView()
} else {
emptyStateView
}
}
private var emptyStateView: some View {
EmptyStateView {
Label(SharedStrings.Reader.subscriptions, systemImage: "doc.text.magnifyingglass")
} description: {
Text(Strings.emptyStateDetails)
} actions: {
ReaderSubscriptionAddButton(style: .compact)
}
}
private var main: some View {
List {
if isShowingSearchResuts {
ForEach(searchResults, id: \.objectID, content: makeSubscriptionCell)
.onDelete(perform: delete)
} else {
ForEach(subscriptions, id: \.objectID, content: makeSubscriptionCell)
.onDelete(perform: delete)
}
}
.listStyle(.plain)
.searchable(text: $searchText)
.onReceive(subscriptions.publisher) { _ in
if !searchText.isEmpty {
reloadSearchResults(searchText: searchText)
}
}
.onChange(of: searchText) {
reloadSearchResults(searchText: $0)
}
}
private func makeSubscriptionCell(for site: ReaderSiteTopic) -> some View {
Button {
onSelection(site)
} label: {
ReaderSubscriptionCell(site: site, onDelete: delete)
}
.swipeActions(edge: .leading) {
if let siteURL = URL(string: site.siteURL) {
ShareLink(item: siteURL).tint(.blue)
}
}
.swipeActions(edge: .trailing) {
Button(SharedStrings.Reader.unfollow, role: .destructive) {
ReaderSubscriptionHelper().unfollow(site)
}.tint(.red)
}
}
private func delete(at offsets: IndexSet) {
for site in offsets.map(getSubscription) {
delete(site)
}
}
private func getSubscription(at index: Int) -> ReaderSiteTopic {
if isShowingSearchResuts {
searchResults[index]
} else {
subscriptions[index]
}
}
private func delete(_ site: ReaderSiteTopic) {
ReaderSubscriptionHelper().unfollow(site)
}
private func reloadSearchResults(searchText: String) {
let ranking = StringRankedSearch(searchTerm: searchText)
searchResults = ranking.search(in: subscriptions) { "\($0.title) \($0.siteURL)" }
}
}
private enum Strings {
static let emptyStateDetails = NSLocalizedString("reader.subscriptions.emptyStateDetails", value: "The sites you discover and subscribe to will appear here", comment: "Empty state details")
}