Skip to content

Commit

Permalink
Fix the 'popular' tab showing no results
Browse files Browse the repository at this point in the history
  • Loading branch information
mbernson committed Jan 1, 2024
1 parent 2bd46af commit 92dc760
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 8 additions & 8 deletions CCCApi/Sources/CCCApi/ApiService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,22 @@ public class ApiService: ObservableObject {
return response.events
}

public func popularTalks() async throws -> [Talk] {
let (data, _) = try await session.data(from: baseURL.appendingPathComponent("events").appendingPathComponent("popular"))
let response = try decoder.decode(EventsResponse.self, from: data)
return response.events
}

public enum PopularTalksYear {
case currentYear
case year(Int)

var yearValue: Int {
switch self {
case .currentYear:
Calendar.current.component(.year, from: Date.now)
let calendar = Calendar.current
// The 'popular' API call returns talks that were popular by year.
// Right after the beginning of a new year, it doesn't return anything
// presumably because there aren't enough views on talk for that year yet.
// So here, we use the year that it was one week ago.
let date = calendar.date(byAdding: .weekOfYear, value: -1, to: Date.now) ?? Date.now
return calendar.component(.year, from: date)
case .year(let value):
value
return value
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion CCCTube/Features/Browse/BrowseView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct BrowseView: View {
case .recent:
talks = try await api.recentTalks()
case .popular:
talks = try await api.popularTalks()
talks = try await api.popularTalks(in: .currentYear)
}
} catch {
self.error = error
Expand Down

0 comments on commit 92dc760

Please sign in to comment.