From c16dbc125dad7e9ff59a5d718f878d8a7de03775 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sat, 9 May 2020 10:23:37 -0500 Subject: [PATCH] Add support for pseudo subreddits --- .../Reddit/RedditFeedProvider.swift | 42 +++++++++++++------ Shared/Article Rendering/shared.css | 2 + 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/Frameworks/Account/FeedProvider/Reddit/RedditFeedProvider.swift b/Frameworks/Account/FeedProvider/Reddit/RedditFeedProvider.swift index f17dc9343..7ba4c1b37 100644 --- a/Frameworks/Account/FeedProvider/Reddit/RedditFeedProvider.swift +++ b/Frameworks/Account/FeedProvider/Reddit/RedditFeedProvider.swift @@ -36,8 +36,10 @@ public final class RedditFeedProvider: FeedProvider { private static let apiBase = "https://oauth.reddit.com" private static let userAgentHeaders = UserAgent.headers() as! [String: String] - private static let userPaths = ["/home", "/notifications"] - private static let reservedPaths = ["/search", "/explore", "/messages", "/i", "/compose"] + private static let pseudoSubreddits = [ + "popular": NSLocalizedString("Popular", comment: "Popular"), + "all": NSLocalizedString("All", comment: "All") + ] public var username: String? @@ -109,21 +111,16 @@ public final class RedditFeedProvider: FeedProvider { public func metaData(_ urlComponents: URLComponents, completion: @escaping (Result) -> Void) { let path = urlComponents.path - if path == "" || path == "/" { - let name = NSLocalizedString("Reddit Best", comment: "Reddit Best") - let metaData = FeedProviderFeedMetaData(name: name, homePageURL: Self.homeURL) - completion(.success(metaData)) - return - } - + // Reddit Home let splitPath = path.split(separator: "/") - if splitPath.count == 1, let sort = RedditSort(rawValue: String(splitPath[0])) { - let name = "Reddit \(sort.displayName)" + if path == "" || path == "/" || (splitPath.count == 1 && RedditSort(rawValue: String(splitPath[0])) != nil) { + let name = NSLocalizedString("Reddit Home", comment: "Reddit Home") let metaData = FeedProviderFeedMetaData(name: name, homePageURL: Self.homeURL) completion(.success(metaData)) return } + // Subreddits guard splitPath.count > 1, splitPath.count < 4, splitPath[0] == "r" else { completion(.failure(RedditFeedProviderError.unknown)) return @@ -136,6 +133,15 @@ public final class RedditFeedProvider: FeedProvider { let homePageURL = "https://www.reddit.com/\(splitPath[0])/\(splitPath[1])" + // Reddit Popular, Reddit All, etc... + if let subredditName = Self.pseudoSubreddits[String(splitPath[1])] { + let localized = NSLocalizedString("Reddit %@", comment: "Reddit") + let name = NSString.localizedStringWithFormat(localized as NSString, subredditName) as String + let metaData = FeedProviderFeedMetaData(name: name, homePageURL: homePageURL) + completion(.success(metaData)) + return + } + subreddit(urlComponents) { result in switch result { case .success(let subreddit): @@ -163,8 +169,18 @@ public final class RedditFeedProvider: FeedProvider { } else { api = "\(urlComponents.path).json" } - - let identifySubreddit = !urlComponents.path.hasPrefix("/r/") + + let splitPath = urlComponents.path.split(separator: "/") + let identifySubreddit: Bool + if splitPath.count > 1 { + if Self.pseudoSubreddits.keys.contains(String(splitPath[1])) { + identifySubreddit = true + } else { + identifySubreddit = !urlComponents.path.hasPrefix("/r/") + } + } else { + identifySubreddit = true + } fetch(api: api, parameters: [:], resultType: RedditLinkListing.self) { result in switch result { diff --git a/Shared/Article Rendering/shared.css b/Shared/Article Rendering/shared.css index a37c49c9e..3dd1c9997 100644 --- a/Shared/Article Rendering/shared.css +++ b/Shared/Article Rendering/shared.css @@ -109,6 +109,8 @@ body > .systemMessage { h1 { line-height: 1.15em; font-weight: bold; + padding-bottom: 0; + margin-bottom: 5px; } pre {