Fix starred story hashes being parsed incorrectly

This commit is contained in:
Anh Do 2020-03-22 00:46:42 -04:00
parent 59f3fb4b3d
commit cfaacd0ada
No known key found for this signature in database
GPG Key ID: 451E3092F917B62D
2 changed files with 22 additions and 6 deletions

View File

@ -15,8 +15,8 @@ typealias NewsBlurStoryHash = NewsBlurStoryHashesResponse.StoryHash
struct NewsBlurStoryHashesResponse: Decodable {
typealias StoryHashDictionary = [String: [StoryHash]]
var unread: StoryHashDictionary?
var starred: StoryHashDictionary?
var unread: [StoryHash]?
var starred: [StoryHash]?
struct StoryHash: Hashable, Codable {
var hash: String
@ -35,12 +35,13 @@ extension NewsBlurStoryHashesResponse {
// Parse unread
if let unreadContainer = try? container.nestedContainer(keyedBy: NewsBlurGenericCodingKeys.self, forKey: .unread) {
self.unread = try NewsBlurStoryHashesResponse.extractHashes(container: unreadContainer)
let storyHashes = try NewsBlurStoryHashesResponse.extractHashes(container: unreadContainer)
self.unread = storyHashes.values.flatMap { $0 }
}
// Parse starred
if let starredContainer = try? container.nestedContainer(keyedBy: NewsBlurGenericCodingKeys.self, forKey: .starred) {
self.starred = try NewsBlurStoryHashesResponse.extractHashes(container: starredContainer)
if let starredContainer = try? container.nestedUnkeyedContainer(forKey: .starred) {
self.starred = try NewsBlurStoryHashesResponse.extractArray(container: starredContainer)
}
}
@ -61,4 +62,19 @@ extension NewsBlurStoryHashesResponse {
return dict
}
static func extractArray(container: UnkeyedDecodingContainer) throws -> [StoryHash] {
var hashes: [StoryHash] = []
var hashArrayContainer = container
while !hashArrayContainer.isAtEnd {
var hashContainer = try hashArrayContainer.nestedUnkeyedContainer()
let hash = try hashContainer.decode(String.self)
let timestamp = try (hashContainer.decode(String.self) as NSString).doubleValue
let storyHash = StoryHash(hash: hash, timestamp: Date(timeIntervalSince1970: timestamp))
hashes.append(storyHash)
}
return hashes
}
}

View File

@ -104,7 +104,7 @@ final class NewsBlurAPICaller: NSObject {
switch result {
case .success((_, let payload)):
let hashes = payload?.unread ?? payload?.starred
completion(.success(hashes?.values.flatMap { $0 }))
completion(.success(hashes))
case .failure(let error):
completion(.failure(error))
}