-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathShowViewController.swift
107 lines (87 loc) · 3.66 KB
/
ShowViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// ShowViewController.swift
// AKTrakt
//
// Created by Florian Morello on 31/05/16.
// Copyright © 2016 CocoaPods. All rights reserved.
//
import Foundation
import UIKit
import AKTrakt
import AlamofireImage
class ShowViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var collectionView: UICollectionView!
var show: TraktShow!
var casting: [TraktCharacter] = []
lazy var trakt: Trakt = {
return Trakt.autoload()
} ()
override func viewDidLoad() {
super.viewDidLoad()
title = show.title
if let image = view.viewWithTag(1) as? UIImageView, url = show.imageURL(.FanArt, thatFits: image) {
image.af_setImageWithURL(url, placeholderImage: nil)
}
loadSeasons()
loadCasting()
}
func loadSeasons() {
TraktRequestSeasons(showId: show.id, extended: [.Episodes, .Images]).request(trakt) { [weak self] seasons, error in
guard seasons != nil else {
return
}
self?.show?.seasons = seasons!
self?.tableView.reloadData()
}
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "SEASON \(show.seasons[section].number)"
}
func loadCasting() {
TraktRequestMediaPeople(type: TraktShow.self, id: show.id, extended: .Images).request(trakt) { [weak self] casting, _, error in
guard casting != nil else {
return
}
self?.casting = casting!
self?.collectionView.reloadData()
}
}
}
extension ShowViewController: UICollectionViewDataSource, UICollectionViewDelegate {
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return casting.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
return collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
}
func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
let character = casting[indexPath.row]
if let image = cell.viewWithTag(1) as? UIImageView, url = character.person.imageURL(.HeadShot, thatFits: image) {
image.layer.cornerRadius = 25
image.af_setImageWithURL(url, placeholderImage: nil)
}
}
}
extension ShowViewController: UITableViewDataSource, UITableViewDelegate {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return show.seasons.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return show.seasons[section].episodes.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return tableView.dequeueReusableCellWithIdentifier("episode") ?? UITableViewCell()
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let episode = show.seasons[indexPath.section].episodes[indexPath.row]
cell.textLabel?.text = "Episode \(episode.number)"
cell.detailTextLabel?.text = episode.title
// if let url = show.seasons[indexPath.section].episodes[indexPath.row].imageURL(.Thumb, thatFits: cell.imageView) {
// cell.imageView?.af_setImageWithURL(url, placeholderImage: nil)
// }
}
}