-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomeTimeLineViewController.swift
More file actions
180 lines (155 loc) · 8.33 KB
/
HomeTimeLineViewController.swift
File metadata and controls
180 lines (155 loc) · 8.33 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// HomeTimeLineViewController.swift
// TwitterClone
//
// Created by William Richman on 10/6/14.
// Copyright (c) 2014 Will Richman. All rights reserved.
//
import UIKit
class HomeTimeLineViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var userProfileBackgroundImage: UIImageView!
@IBOutlet weak var userScreenNameLabel: UILabel!
@IBOutlet weak var userProfileNameLabel: UILabel!
@IBOutlet weak var userProfileImage: UIImageView!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var userProfileNameView: UIView!
var tweets : [Tweet]?
var timelineType = "HOME"
var userTimelineShown : String?
var tweetOrigin : Tweet?
var refreshControl : UIRefreshControl?
var imageCache = [String : UIImage]()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerNib(UINib(nibName: "TimelineTweetTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "TWEET_CELL")
self.tableView.dataSource = self
self.tableView.delegate = self
/* Taken from stackOverflow http://stackoverflow.com/questions/24475792/how-to-use-pull-to-refresh-in-swift */
self.refreshControl = UIRefreshControl()
self.refreshControl?.attributedTitle = NSAttributedString(string: "Pull to refresh")
self.refreshControl?.addTarget(self, action: "refreshTweets:", forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(self.refreshControl!)
/* Fetch whichever timeline the view should hold, passing the user's screen name if it is a user timeline */
NetworkController.controller.fetchTimeLine(timelineType, newestTweet: nil, oldestTweet: nil, userScreenname: userTimelineShown) { (errorDescription, tweets) -> Void in
if errorDescription != nil {
//alert the user that something went wrong
} else {
self.tweets = tweets
self.tableView.reloadData()
}
}
/* Taken from AppCoda http://www.appcoda.com/self-sizing-cells/ */
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
/* Format the table header if this is a user timeline, or hide it if it is the home timeline. */
if timelineType == "HOME" {
self.tableView.tableHeaderView = nil
} else if timelineType == "USER" {
self.userScreenNameLabel.text = ("@\(tweetOrigin!.screenName)")
self.userProfileNameLabel.text = tweetOrigin?.profileName
NetworkController.controller.fetchBackgroundProfileImage(self.tweetOrigin!, completionHandler: { (errorDescription, tweetProfileImage) -> Void in
self.userProfileBackgroundImage.contentMode = .ScaleAspectFill
self.userProfileBackgroundImage.clipsToBounds = true
self.userProfileBackgroundImage.image = tweetProfileImage
self.userProfileBackgroundImage.superview?.sendSubviewToBack(self.userProfileBackgroundImage)
})
/* Load profile image from cache if possible */
if let cachedProfileImage = self.imageCache[tweetOrigin!.screenName] {
self.userProfileImage.image = cachedProfileImage
}
else {
NetworkController.controller.fetchProfileImage(tweetOrigin!, completionHandler: { (errorDescription, tweetProfileImage) -> Void in
if errorDescription == nil {
self.imageCache[self.tweetOrigin!.screenName] = tweetProfileImage
self.userProfileImage?.image = self.imageCache[self.tweetOrigin!.screenName]
}
else {
println("Error: \(errorDescription)")
}
})
}
self.userProfileImage.layer.borderColor = UIColor.whiteColor().CGColor;
self.userProfileImage.layer.borderWidth = self.userProfileImage.frame.size.width * 0.05
self.userProfileImage.layer.cornerRadius = self.userProfileImage.frame.size.width * 0.1
self.userProfileImage.clipsToBounds = true
self.userProfileNameView.layer.cornerRadius = self.userProfileNameView.frame.size.height * 0.1
}
}
override func viewDidAppear(animated: Bool) {
}
func sortTweetsAtoZ() {
self.tweets!.sort {$0.text < $1.text}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.tweets != nil {
return self.tweets!.count
} else {
return 0
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("TWEET_CELL", forIndexPath: indexPath) as TimelineTweetTableViewCell
/* This is where we will grab reference to correct tweet and use it to configure the cell*/
let tweet = self.tweets?[indexPath.row]
cell.tweetTextLabel?.text = tweet?.text
cell.userNameTextLabel?.text = tweet?.profileName
cell.userScreenName?.text = ("@\(tweet!.screenName)")
if let cachedProfileImage = self.imageCache[tweet!.screenName] {
cell.profileImage?.image = cachedProfileImage
}
else {
NetworkController.controller.fetchProfileImage(tweet!, completionHandler: { (errorDescription, tweetProfileImage) -> Void in
if errorDescription == nil {
self.imageCache[tweet!.screenName] = tweetProfileImage
cell.profileImage?.image = self.imageCache[tweet!.screenName]
}
else {
println("Error: \(errorDescription)")
}
})
}
cell.profileImage?.layer.borderColor = UIColor.whiteColor().CGColor;
cell.profileImage?.layer.borderWidth = cell.profileImage!.frame.size.width * 0.05
cell.profileImage?.layer.cornerRadius = cell.profileImage!.frame.size.width * 0.1
cell.profileImage?.clipsToBounds = true
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let singleTweetVC = self.storyboard?.instantiateViewControllerWithIdentifier("SINGLE_TWEET_VC") as SingleTweetViewController
var indexPath = tableView.indexPathForSelectedRow()
tableView.deselectRowAtIndexPath(indexPath!, animated: true)
var tweetToDisplay = self.tweets![indexPath!.row] as Tweet
singleTweetVC.tweetShown = tweetToDisplay
self.navigationController?.pushViewController(singleTweetVC, animated: true)
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == tweets!.count - 1 {
NetworkController.controller.fetchTimeLine(timelineType, newestTweet: nil, oldestTweet: self.tweets?.last, userScreenname: userTimelineShown, completionHandler: { (errorDescription, tweets) -> Void in
if errorDescription != nil {
//alert the user that something went wrong
} else {
var interimTweets = tweets!
let tweetRemoved = interimTweets.removeAtIndex(0)
self.tweets? += interimTweets
self.tableView.reloadData()
}
})
}
}
func refreshTweets (sender: AnyObject) {
NetworkController.controller.fetchTimeLine(timelineType, newestTweet: self.tweets?[0], oldestTweet: nil, userScreenname: userTimelineShown) { (errorDescription, tweets) -> Void in
if errorDescription != nil {
//alert the user that something went wrong
self.refreshControl?.endRefreshing()
} else {
println(self.tweets?.count)
var tweetsInterim : [Tweet]? = tweets
tweetsInterim! += self.tweets!
self.tweets = tweetsInterim!
println(self.tweets?.count)
self.tableView.reloadData()
self.refreshControl?.endRefreshing()
}
}
}
}