Skip to content

Commit 017c0b3

Browse files
committed
Replace JSONEncodable with Encodable
1 parent 295c930 commit 017c0b3

File tree

6 files changed

+26
-86
lines changed

6 files changed

+26
-86
lines changed

iCookTV/Controllers/HistoryViewController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ class HistoryViewController: UIViewController,
7777
isLoading = true
7878
DispatchQueue.global().async {
7979
do {
80-
let history = try HistoryManager.history.map(Video.init)
80+
let decoder = JSONDecoder()
81+
let history = try HistoryManager.history.map { try decoder.decode(Video.self, from: $0) }
8182
DispatchQueue.main.sync {
8283
self.dataSource.append(history, toCollectionView: self.collectionView)
8384
self.setOverlayViewHidden(self.dataSource.numberOfItems > 0, animated: true)

iCookTV/Controllers/LaunchViewController.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import UIKit
2828
import Alamofire
29-
import Freddy
3029

3130
class LaunchViewController: UIViewController, DataFetching {
3231

iCookTV/Helpers/HistoryManager.swift

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
//
2626

2727
import Foundation
28-
import Freddy
2928

3029
struct HistoryManager {
3130

@@ -46,16 +45,11 @@ struct HistoryManager {
4645
// MARK: - Public Methods
4746

4847
/// Returns the deserialized video history read from the cache directory.
49-
static var history: [JSON] {
48+
static var history: [Data] {
5049
if let path = cache?.path, let records = NSArray(contentsOfFile: path) as? [Data] {
51-
do {
52-
return try records.map { try JSON(data: $0) }
53-
} catch {
54-
Tracker.track(error)
55-
return [JSON]()
56-
}
50+
return records
5751
} else {
58-
return [JSON]()
52+
return []
5953
}
6054
}
6155

@@ -71,19 +65,17 @@ struct HistoryManager {
7165
}
7266
Debug.print(path)
7367

74-
let json = video.toJSON()
75-
var records = history
68+
let decoder = JSONDecoder()
69+
var records: [Video] = history.flatMap { try? decoder.decode(Video.self, from: $0) }
7670

7771
// Keep the latest video at top.
78-
for (index, element) in self.history.enumerated() where element["id"] == json["id"] {
79-
records.remove(at: index)
80-
break
81-
}
82-
records.insert(json, at: 0)
72+
records = records.filter { $0.id != video.id }
73+
records.insert(video, at: 0)
8374
Debug.print("records.count =", records.count)
8475

8576
do {
86-
let data = try records.map { try $0.serialize() } as NSArray
77+
let encoder = JSONEncoder()
78+
let data = try records.map { try encoder.encode($0) } as NSArray
8779
data.write(toFile: path, atomically: true)
8880
} catch {
8981
Tracker.track(error)

iCookTV/Models/Category.swift

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,13 @@
2525
//
2626

2727
import Foundation
28-
import Freddy
2928

30-
struct Category: Codable, JSONDecodable {
29+
struct Category: Codable {
3130

3231
let id: String
3332
let name: String
3433
let coverURLs: [String]
3534

36-
// MARK: - JSONDecodable
37-
38-
init(json value: JSON) throws {
39-
id = try value.getString(at: "id")
40-
name = try value.getString(at: "attributes", "name", or: "")
41-
coverURLs = try value.getArray(at: "attributes", "cover-urls").map(String.init)
42-
}
43-
4435
// MARK: - Codable
4536

4637
private enum CodingKeys: String, CodingKey {

iCookTV/Models/Video.swift

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@
2525
//
2626

2727
import Foundation
28-
import Freddy
2928

30-
struct Video: Codable, JSONDecodable, JSONEncodable {
29+
struct Video: Codable {
3130

3231
let id: String
3332
let title: String
@@ -49,50 +48,6 @@ struct Video: Codable, JSONDecodable, JSONEncodable {
4948
return (hours > 0 ? "\(hours):" : "") + String(format: "%d:%02d", minutes, seconds)
5049
}
5150

52-
// MARK: - JSONDecodable
53-
54-
init(json value: JSON) throws {
55-
let nullable: JSON.SubscriptingOptions = [.NullBecomesNil, .MissingKeyBecomesNil]
56-
id = try value.getString(at: "id")
57-
title = try value.getString(at: "attributes", "title")
58-
subtitle = try value.getString(at: "attributes", "subtitle", alongPath: nullable)
59-
description = try value.getString(at: "attributes", "description", alongPath: nullable)
60-
length = try value.getInt(at: "attributes", "length", or: 0)
61-
youtube = try value.getString(at: "attributes", "embed-url")
62-
source = try value.getString(at: "attributes", "video-url", alongPath: nullable)
63-
cover = try value.getString(at: "attributes", "cover-url")
64-
}
65-
66-
// MARK: - JSONEncodable
67-
68-
func toJSON() -> JSON {
69-
var attributes: [String: JSON] = [
70-
"title": .string(title),
71-
"embed-url": .string(youtube),
72-
"cover-url": .string(cover),
73-
"length": .int(length)
74-
]
75-
76-
if let source = source {
77-
attributes["video-url"] = .string(source)
78-
}
79-
80-
if let subtitle = subtitle {
81-
attributes["subtitle"] = .string(subtitle)
82-
}
83-
84-
if let description = description {
85-
attributes["description"] = .string(description)
86-
}
87-
88-
let json: [String: JSON] = [
89-
"id": .string(id),
90-
"attributes": .dictionary(attributes)
91-
]
92-
93-
return .dictionary(json)
94-
}
95-
9651
// MARK: - Codable
9752

9853
private enum CodingKeys: String, CodingKey {

iCookTVTests/VideoSpec.swift

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
//
2626

2727
@testable import iCookTV
28-
import Freddy
2928
import Nimble
3029
import Quick
3130

@@ -34,8 +33,6 @@ class VideoSpec: QuickSpec {
3433
override func spec() {
3534

3635
let data: Data = Resources.testData(named: "Video.json")!
37-
let json = try! JSON(data: data as Data)
38-
3936
let decoder = JSONDecoder()
4037
let video = try! decoder.decode(Video.self, from: data)
4138

@@ -52,15 +49,20 @@ class VideoSpec: QuickSpec {
5249
}
5350
}
5451

55-
describe("toJSON()") {
56-
let converted = video.toJSON()
57-
58-
it("should convert Video to JSON") {
59-
expect(converted["id"]).to(equal(json["id"]))
52+
describe("encoding") {
53+
let encoder = JSONEncoder()
54+
let data = try! encoder.encode(video)
55+
let jsonString = String(data: data, encoding: .utf8)
6056

61-
for key in ["title", "embed-url", "video-url", "cover-url", "length", "subtitle", "description"] {
62-
expect(converted["attributes"]?[key]).to(equal(json["attributes"]?[key]))
63-
}
57+
it("should encode Video to JSON") {
58+
expect(jsonString).to(contain("\"id\":\"42\""))
59+
expect(jsonString).to(contain("\"title\":\"Lorem\""))
60+
expect(jsonString).to(contain("\"subtitle\":\"ipsum\""))
61+
expect(jsonString).to(contain("\"description\":\"dolor sit amet\""))
62+
expect(jsonString).to(contain("\"length\":123"))
63+
expect(jsonString).to(contain("\"embed-url\":\"https:\\/\\/www.youtube.com\\/watch?v=3345678\""))
64+
expect(jsonString).to(contain("\"video-url\":\"https:\\/\\/vide.os\\/source.m3u8\""))
65+
expect(jsonString).to(contain("\"cover-url\":\"https:\\/\\/imag.es\\/cover.jpg\""))
6466
}
6567
}
6668

0 commit comments

Comments
 (0)