Skip to content

Commit 295c930

Browse files
committed
Replace JSONDecodable with Decodable
1 parent 755246a commit 295c930

File tree

7 files changed

+58
-22
lines changed

7 files changed

+58
-22
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ bootstrap:
22
gem install bundler -v 2.0.2
33
bundle install
44
# pod install
5-
bundle exec pod keys set BaseAPIURL "https://polydice.com/iCook-tvOS/demo/"
5+
bundle exec pod keys set BaseAPIURL "https://bcyl.in/Try-tvOS/demo/"
6+
# "https://polydice.com/iCook-tvOS/demo/": An SSL error has occurred and a secure connection to the server cannot be made.
67
bundle exec pod keys set CrashlyticsAPIKey "-"
78
bundle exec pod keys set TreasureDataAPIKey "-"
89
bundle exec pod install

iCookTV.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
B5761A581CCF5752008CCC08 /* ResourceHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5761A571CCF5752008CCC08 /* ResourceHelper.swift */; };
4646
B5761A5A1CCF6CCD008CCC08 /* VideoSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5761A591CCF6CCD008CCC08 /* VideoSpec.swift */; };
4747
B5761A5C1CCF6D78008CCC08 /* Video.json in Resources */ = {isa = PBXBuildFile; fileRef = B5761A5B1CCF6D78008CCC08 /* Video.json */; };
48+
B57C930A22FF149800BBAC40 /* KeyPathDecoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = B53CD05422FEFF1C00492E27 /* KeyPathDecoding.swift */; };
4849
B586EFDB1CCA21B300EA8218 /* InsetLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = B586EFDA1CCA21B300EA8218 /* InsetLabel.swift */; };
4950
B58DE38B1CB8B54200C00266 /* CoverBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58DE38A1CB8B54200C00266 /* CoverBuilder.swift */; };
5051
B58E906D1D5F806600AC184D /* VideosDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58E906C1D5F806600AC184D /* VideosDataSource.swift */; };
@@ -104,6 +105,7 @@
104105
B52EC3B01CB26F1B0072762C /* CGRect+Grid.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CGRect+Grid.swift"; sourceTree = "<group>"; };
105106
B52EC3B21CB3A02E0072762C /* UIFont+TV.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIFont+TV.swift"; sourceTree = "<group>"; };
106107
B53115921CC69E7C00E75292 /* HistoryManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HistoryManager.swift; sourceTree = "<group>"; };
108+
B53CD05422FEFF1C00492E27 /* KeyPathDecoding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyPathDecoding.swift; sourceTree = "<group>"; };
107109
B53E39771CA1494000EB1EEE /* UIColor+TV.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIColor+TV.swift"; sourceTree = "<group>"; };
108110
B53E397A1CA14B4200EB1EEE /* CategoriesViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CategoriesViewController.swift; sourceTree = "<group>"; };
109111
B543C3E91CD1E85E008C512B /* OverlayViewPresentable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OverlayViewPresentable.swift; sourceTree = "<group>"; };
@@ -299,6 +301,7 @@
299301
B58DE38A1CB8B54200C00266 /* CoverBuilder.swift */,
300302
B543C3EE1CD1FD0B008C512B /* GroundControl.swift */,
301303
B53115921CC69E7C00E75292 /* HistoryManager.swift */,
304+
B53CD05422FEFF1C00492E27 /* KeyPathDecoding.swift */,
302305
B543C3F01CD21530008C512B /* Tracker.swift */,
303306
);
304307
path = Helpers;
@@ -661,6 +664,7 @@
661664
B50BFC071CC8AF14004F853D /* HistoryViewController.swift in Sources */,
662665
B51A95391CCF1C5100E5ED97 /* iCookTVKeys.swift in Sources */,
663666
B586EFDB1CCA21B300EA8218 /* InsetLabel.swift in Sources */,
667+
B57C930A22FF149800BBAC40 /* KeyPathDecoding.swift in Sources */,
664668
B500D9AF1CBA3B7900622198 /* LaunchViewController.swift in Sources */,
665669
B50A202B1DC4B53C00ACBF1B /* LoadingIndicatorPresentable.swift in Sources */,
666670
B500D99F1CB93D0600622198 /* MainMenuView.swift in Sources */,

iCookTV/Controllers/VideoPlayerController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import UIKit
2828
import AVKit
2929
import Alamofire
30-
import Freddy
3130

3231
class VideoPlayerController: AVPlayerViewController, Trackable {
3332

@@ -90,8 +89,9 @@ class VideoPlayerController: AVPlayerViewController, Trackable {
9089
}
9190

9291
do {
93-
let json = try JSON(data: data)
94-
let video = try Video(json: json["data"] ?? nil)
92+
let decoder = JSONDecoder()
93+
let parsed = try decoder.decode(DataKeyPathDecoding<Video>.self, from: data)
94+
let video = parsed.data
9595
video.convertToPlayerItemWithCover(self?.coverImage) { [weak self] in
9696
self?.setPlayerItem($0)
9797
}

iCookTV/Controllers/VideosViewController.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
import Keys
3130

3231
class VideosViewController: UIViewController,

iCookTV/Extensions/DataRequest+Result.swift

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import Foundation
2828
import Alamofire
29-
import Freddy
3029

3130
enum Result<T> {
3231
case success(T)
@@ -55,10 +54,10 @@ enum Result<T> {
5554

5655
extension DataRequest {
5756

58-
func responseJSONObject(
57+
func responseResult(
5958
queue: DispatchQueue? = nil,
6059
options: JSONSerialization.ReadingOptions = .allowFragments,
61-
completion: @escaping (Result<JSON>) -> Void
60+
completion: @escaping (Result<Data>) -> Void
6261
) -> Self {
6362
let serializer = DataRequest.jsonResponseSerializer(options: options)
6463

@@ -68,13 +67,7 @@ extension DataRequest {
6867
completion(.failure(error))
6968
return
7069
}
71-
72-
do {
73-
let json = try JSON(data: data)
74-
completion(.success(json))
75-
} catch {
76-
completion(.failure(error))
77-
}
70+
completion(.success(data))
7871
}
7972
}
8073

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// KeyPathDecoding.swift
3+
// TryTVOS
4+
//
5+
// Created by Ben on 10/08/2019.
6+
// Copyright © 2019 bcylin.
7+
//
8+
// Permission is hereby granted, free of charge, to any person obtaining a copy
9+
// of this software and associated documentation files (the "Software"), to deal
10+
// in the Software without restriction, including without limitation the rights
11+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
// copies of the Software, and to permit persons to whom the Software is
13+
// furnished to do so, subject to the following conditions:
14+
//
15+
// The above copyright notice and this permission notice shall be included in all
16+
// copies or substantial portions of the Software.
17+
//
18+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
// SOFTWARE.
25+
//
26+
27+
import Foundation
28+
29+
/// A type for decoding nested data structure.
30+
struct DataKeyPathDecoding<T: Decodable>: Decodable {
31+
32+
let data: T
33+
let links: Links?
34+
35+
struct Links: Decodable {
36+
let next: String?
37+
}
38+
}

iCookTV/Protocols/DataFetching.swift

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

2727
import Alamofire
28-
import Freddy
2928

3029
protocol DataFetching: class {
3130
/// A reference to the pagination object that prevents duplicate requests.
@@ -37,23 +36,25 @@ protocol DataFetching: class {
3736
/// - request: An Alamofire URL request.
3837
/// - sessionManager: A session manager to send the request. Optional, default is SessionManager.default.
3938
/// - completion: A closure to be executed once the request has finished.
40-
func fetch<T: JSONDecodable>(request: URLRequestConvertible, with sessionManager: SessionManager, completion: @escaping (Result<[T]>) -> Void)
39+
func fetch<T: Decodable>(request: URLRequestConvertible, with sessionManager: SessionManager, completion: @escaping (Result<[T]>) -> Void)
4140
}
4241

4342

4443
extension DataFetching {
4544

46-
func fetch<T: JSONDecodable>(request: URLRequestConvertible, with sessionManager: SessionManager = SessionManager.default, completion: @escaping (Result<[T]>) -> Void) {
45+
func fetch<T: Decodable>(request: URLRequestConvertible, with sessionManager: SessionManager = SessionManager.default, completion: @escaping (Result<[T]>) -> Void) {
4746
guard pagination.isReady else {
4847
return
4948
}
5049

51-
pagination.currentRequest = sessionManager.request(request).responseJSONObject(queue: pagination.queue) { [weak self] result in
50+
pagination.currentRequest = sessionManager.request(request).responseResult(queue: pagination.queue) { [weak self] result in
5251
switch result {
53-
case let .success(json):
52+
case let .success(data):
5453
do {
55-
let items = try json.getArray(at: "data").map(T.init)
56-
self?.pagination.hasNextPage = json["links"]?["next"] != nil
54+
let decoder = JSONDecoder()
55+
let parsed = try decoder.decode(DataKeyPathDecoding<[T]>.self, from: data)
56+
let items = parsed.data
57+
self?.pagination.hasNextPage = parsed.links?.next != nil
5758

5859
DispatchQueue.main.sync { completion(.success(items)) }
5960
} catch {

0 commit comments

Comments
 (0)