Skip to content

Commit 372e4f2

Browse files
committed
Search: Add trending subreddits
1 parent 7f1be93 commit 372e4f2

File tree

6 files changed

+71
-10
lines changed

6 files changed

+71
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Foundation
2+
3+
public struct TrendingSubreddits: Decodable {
4+
public let subredditNames: [String]
5+
}

Packages/Backend/Sources/Backend/Network/API.swift

+20-7
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,21 @@ public class API {
4747

4848
static private func makeURL(endpoint: Endpoint,
4949
basicAuthUser: String?,
50+
forceSignedOutURL: Bool,
5051
isJSONAPI: Bool) -> URL {
5152
var url: URL
5253
if let user = basicAuthUser {
5354
url = URL(string: "\(Self.URL_PREFIX)\(user):@www.\(Self.HOST)")!
5455
} else {
55-
switch OauthClient.shared.authState {
56-
case .authenthicated:
57-
url = URL(string: "\(Self.URL_PREFIX)\(Self.HOST_AUTH_DOMAIN).\(Self.HOST)")!
58-
default:
56+
if forceSignedOutURL {
5957
url = URL(string: "\(Self.URL_PREFIX)www.\(Self.HOST)")!
58+
} else {
59+
switch OauthClient.shared.authState {
60+
case .authenthicated:
61+
url = URL(string: "\(Self.URL_PREFIX)\(Self.HOST_AUTH_DOMAIN).\(Self.HOST)")!
62+
default:
63+
url = URL(string: "\(Self.URL_PREFIX)www.\(Self.HOST)")!
64+
}
6065
}
6166
}
6267
url = url.appendingPathComponent(endpoint.path())
@@ -98,17 +103,24 @@ public class API {
98103

99104
public func request<T: Decodable>(endpoint: Endpoint,
100105
basicAuthUser: String? = nil,
106+
forceSignedOutURL: Bool = false,
101107
httpMethod: String = "GET",
102108
isJSONEndpoint: Bool = true,
103109
queryParamsAsBody: Bool = false,
104110
params: [String: String]? = nil) -> AnyPublisher<T ,NetworkError> {
105111

106112
if basicAuthUser != nil || authenticatedSession != nil ||
107113
OauthClient.shared.authState == .signedOut || OauthClient.shared.authState == .signinInProgress {
108-
let url = Self.makeURL(endpoint: endpoint, basicAuthUser: basicAuthUser, isJSONAPI: isJSONEndpoint)
109-
let request = Self.makeRequest(url: url, httpMethod: httpMethod, queryParamsAsBody: queryParamsAsBody, params: params)
114+
let url = Self.makeURL(endpoint: endpoint,
115+
basicAuthUser: basicAuthUser,
116+
forceSignedOutURL: forceSignedOutURL,
117+
isJSONAPI: isJSONEndpoint)
118+
let request = Self.makeRequest(url: url,
119+
httpMethod: httpMethod,
120+
queryParamsAsBody: queryParamsAsBody,
121+
params: params)
110122

111-
if let session = authenticatedSession {
123+
if let session = authenticatedSession, !forceSignedOutURL {
112124
return executeRequest(publisher: session.dataTaskPublisher(for: request))
113125
} else {
114126
return executeRequest(publisher: signedOutSession.dataTaskPublisher(for: request))
@@ -119,6 +131,7 @@ public class API {
119131
.map {
120132
$0.dataTaskPublisher(for: Self.makeRequest(url: Self.makeURL(endpoint: endpoint,
121133
basicAuthUser: basicAuthUser,
134+
forceSignedOutURL: forceSignedOutURL,
122135
isJSONAPI: isJSONEndpoint),
123136
httpMethod: httpMethod,
124137
queryParamsAsBody: queryParamsAsBody,

Packages/Backend/Sources/Backend/Network/Endpoint.swift

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public enum Endpoint {
1414
case userSaved(username: String)
1515
case userSubmitted(username: String)
1616
case userComments(username: String)
17+
case trendingSubreddits
1718

1819
func path() -> String {
1920
switch self {
@@ -59,6 +60,8 @@ public enum Endpoint {
5960
return "user/\(username)/submitted"
6061
case let .userComments(username):
6162
return "user/\(username)/comments"
63+
case .trendingSubreddits:
64+
return "api/trending_subreddits"
6265
}
6366
}
6467
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Foundation
2+
import Combine
3+
4+
extension TrendingSubreddits {
5+
public static func fetch() -> AnyPublisher<TrendingSubreddits, Never> {
6+
API.shared.request(endpoint: .trendingSubreddits, forceSignedOutURL: true)
7+
.subscribe(on: DispatchQueue.global())
8+
.catch { _ in Empty(completeImmediately: false) }
9+
.eraseToAnyPublisher()
10+
}
11+
}

RedditOs/Features/Search/SearchMainContentView.swift

+20-3
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,31 @@ struct SearchMainContentView: View {
2929
} else {
3030
ToolbarSearchBar(isPopoverEnabled: false)
3131
.padding()
32-
if !searchState.searchText.isEmpty {
33-
List {
32+
List {
33+
if searchState.searchText.isEmpty {
34+
if let trending = searchState.trending {
35+
Section(header: Label("Trending", systemImage: "chart.bar.fill")) {
36+
ForEach(trending.subredditNames, id: \.self) { subreddit in
37+
Text(subreddit)
38+
.padding(.vertical, 4)
39+
.onTapGesture {
40+
uiState.searchRoute = .subreddit(subreddit: subreddit)
41+
}
42+
}
43+
}
44+
}
45+
} else {
3446
GlobalSearchPopoverView()
35-
}.listStyle(PlainListStyle())
47+
}
3648
}
49+
.listStyle(PlainListStyle())
50+
.padding(.horizontal)
3751
Spacer()
3852
}
3953
}.navigationTitle("Search")
54+
.onAppear {
55+
searchState.fetchTrending()
56+
}
4057
}
4158
}
4259

RedditOs/Features/Search/SearchState.swift

+12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class SearchState: ObservableObject {
1414
@Published var searchText = ""
1515
@Published var results: [SubredditSmall]?
1616
@Published var filteredSubscriptions: [Subreddit]?
17+
@Published var trending: TrendingSubreddits?
1718
@Published var isLoading = false
1819

1920
private var currentUser: CurrentUserStore
@@ -22,6 +23,7 @@ class SearchState: ObservableObject {
2223
private var instantSearchCancellable: AnyCancellable?
2324
private var apiPublisher: AnyPublisher<SubredditResponse, Never>?
2425
private var apiCancellable: AnyCancellable?
26+
private var cancellableSet: Set<AnyCancellable> = Set()
2527

2628
init(currentUser: CurrentUserStore = .shared) {
2729
self.currentUser = currentUser
@@ -69,4 +71,14 @@ class SearchState: ObservableObject {
6971
}
7072

7173
}
74+
75+
public func fetchTrending() {
76+
TrendingSubreddits.fetch()
77+
.subscribe(on: DispatchQueue.global())
78+
.receive(on: DispatchQueue.main)
79+
.sink { [weak self] trending in
80+
self?.trending = trending
81+
}
82+
.store(in: &cancellableSet)
83+
}
7284
}

0 commit comments

Comments
 (0)