Add support for the base timeline Reddit feed

This commit is contained in:
Maurice Parker 2020-05-07 19:33:15 -05:00
parent 40ec650476
commit 4d9975f28a
3 changed files with 53 additions and 5 deletions

View File

@ -52,6 +52,7 @@
5133BB4F2460EC230001E3D0 /* RedditMedia.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5133BB4E2460EC230001E3D0 /* RedditMedia.swift */; };
5133BB512460EE730001E3D0 /* RedditMediaEmbed.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5133BB502460EE730001E3D0 /* RedditMediaEmbed.swift */; };
5139A6382459822D004D960C /* CloudKitArticleStatusUpdate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5139A6372459822D004D960C /* CloudKitArticleStatusUpdate.swift */; };
5141000A2464DAA6001DA326 /* RedditSort.swift in Sources */ = {isa = PBXBuildFile; fileRef = 514100092464DAA6001DA326 /* RedditSort.swift */; };
5144EA49227B497600D19003 /* FeedbinAPICaller.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5144EA48227B497600D19003 /* FeedbinAPICaller.swift */; };
5144EA4E227B829A00D19003 /* FeedbinAccountDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5144EA4D227B829A00D19003 /* FeedbinAccountDelegate.swift */; };
514BF5202391B0DB00902FE8 /* SingleArticleFetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 514BF51F2391B0DB00902FE8 /* SingleArticleFetcher.swift */; };
@ -314,6 +315,7 @@
5133BB4E2460EC230001E3D0 /* RedditMedia.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RedditMedia.swift; sourceTree = "<group>"; };
5133BB502460EE730001E3D0 /* RedditMediaEmbed.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RedditMediaEmbed.swift; sourceTree = "<group>"; };
5139A6372459822D004D960C /* CloudKitArticleStatusUpdate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CloudKitArticleStatusUpdate.swift; sourceTree = "<group>"; };
514100092464DAA6001DA326 /* RedditSort.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RedditSort.swift; sourceTree = "<group>"; };
5144EA48227B497600D19003 /* FeedbinAPICaller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedbinAPICaller.swift; sourceTree = "<group>"; };
5144EA4D227B829A00D19003 /* FeedbinAccountDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedbinAccountDelegate.swift; sourceTree = "<group>"; };
514BF51F2391B0DB00902FE8 /* SingleArticleFetcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SingleArticleFetcher.swift; sourceTree = "<group>"; };
@ -651,6 +653,7 @@
5133BB4E2460EC230001E3D0 /* RedditMedia.swift */,
5133BB502460EE730001E3D0 /* RedditMediaEmbed.swift */,
5100E4CC246243990010A63D /* RedditPreview.swift */,
514100092464DAA6001DA326 /* RedditSort.swift */,
5133BB4A2460BDF30001E3D0 /* RedditSubreddit.swift */,
);
path = Reddit;
@ -1309,6 +1312,7 @@
512DD4CD2431098700C17B1F /* CloudKitAccountZoneDelegate.swift in Sources */,
51BFDECE238B508D00216323 /* ContainerIdentifier.swift in Sources */,
9E1D1555233431A600F4944C /* FeedlyOperation.swift in Sources */,
5141000A2464DAA6001DA326 /* RedditSort.swift in Sources */,
84F1F06E2243524700DA0616 /* AccountMetadata.swift in Sources */,
9EF1B10723590D61000A486A /* FeedlyGetStreamIdsOperation.swift in Sources */,
84245C851FDDD8CB0074AFBB /* FeedbinSubscription.swift in Sources */,

View File

@ -31,6 +31,7 @@ public final class RedditFeedProvider: FeedProvider {
var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "RedditFeedProvider")
private static let homeURL = "https://www.reddit.com"
private static let server = "www.reddit.com"
private static let apiBase = "https://oauth.reddit.com"
private static let userAgentHeaders = UserAgent.headers() as! [String: String]
@ -109,20 +110,26 @@ public final class RedditFeedProvider: FeedProvider {
let path = urlComponents.path
if path == "" || path == "/" {
let name = NSLocalizedString("Reddit Timeline", comment: "Reddit Timeline")
let metaData = FeedProviderFeedMetaData(name: name, homePageURL: "https://www.reddit.com")
let name = NSLocalizedString("Reddit Best", comment: "Reddit Best")
let metaData = FeedProviderFeedMetaData(name: name, homePageURL: Self.homeURL)
completion(.success(metaData))
return
}
let splitPath = path.split(separator: "/")
if splitPath.count == 1, let sort = RedditSort(rawValue: String(splitPath[0])) {
let name = "Reddit \(sort.displayName)"
let metaData = FeedProviderFeedMetaData(name: name, homePageURL: Self.homeURL)
completion(.success(metaData))
return
}
guard splitPath.count > 1 else {
completion(.failure(RedditFeedProviderError.unknown))
return
}
let name = "\(splitPath[0])/\(splitPath[1])"
let homePageURL = "https://www.reddit.com/\(name)"
let homePageURL = "https://www.reddit.com/\(splitPath[0])/\(splitPath[1])"
subreddit(urlComponents) { result in
switch result {
@ -145,7 +152,12 @@ public final class RedditFeedProvider: FeedProvider {
return
}
let api = "\(urlComponents.path).json"
let api: String
if urlComponents.path == "" || urlComponents.path == "/" {
api = "/best.json"
} else {
api = "\(urlComponents.path).json"
}
fetch(api: api, parameters: [:], resultType: RedditLinkListing.self) { result in
switch result {

View File

@ -0,0 +1,32 @@
//
// RedditSort.swift
// Account
//
// Created by Maurice Parker on 5/7/20.
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
//
import Foundation
enum RedditSort: String, CaseIterable {
case best
case rising
case hot
case new
case top
var displayName: String {
switch self {
case .best:
return NSLocalizedString("Best", comment: "Best")
case .rising:
return NSLocalizedString("Rising", comment: "Rising")
case .hot:
return NSLocalizedString("Hot", comment: "Hot")
case .new:
return NSLocalizedString("New", comment: "New")
case .top:
return NSLocalizedString("Top", comment: "Top")
}
}
}