-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeTableViewController.swift
136 lines (88 loc) · 4.08 KB
/
HomeTableViewController.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
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
//
// HomeTableViewController.swift
// Twitter
//
// Created by Navid Chowdhury on 10/7/21.
// Copyright © 2021 Dan. All rights reserved.
//
import UIKit
class HomeTableViewController: UITableViewController {
var tweetArray = [NSDictionary]()
var numberOfTweet: Int!
let myRefreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
myRefreshControl.addTarget(self, action: #selector(loadTweet), for: .valueChanged)
tableView.refreshControl = myRefreshControl
self.tableView.rowHeight = UITableView.automaticDimension
self.tableView.estimatedRowHeight = 147
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
loadTweet()
}
@objc func loadTweet(){
numberOfTweet = 20
let myUrl = "https://api.twitter.com/1.1/statuses/home_timeline.json"
let myParams = ["count": numberOfTweet]
TwitterAPICaller.client?.getDictionariesRequest(url: myUrl, parameters: myParams, success: { [self] (tweets: [NSDictionary]) in
tweetArray.removeAll()
for tweet in tweets {
self.tweetArray.append(tweet)
}
self.tableView.reloadData()
self.myRefreshControl.endRefreshing()
}, failure: { (Error) in
print("Could not retrieve tewwets! oh no!!")
})
}
func loadMoreTweets(){
let myUrl = "https://api.twitter.com/1.1/statuses/home_timeline.json"
numberOfTweet = numberOfTweet + 20
let myParams = ["count": numberOfTweet]
TwitterAPICaller.client?.getDictionariesRequest(url: myUrl, parameters: myParams, success: { [self] (tweets: [NSDictionary]) in
tweetArray.removeAll()
for tweet in tweets {
self.tweetArray.append(tweet)
}
self.tableView.reloadData()
self.myRefreshControl.endRefreshing()
}, failure: { (Error) in
print("Could not retrieve tewwets! oh no!!")
})
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath){
if indexPath.row + 1 == tweetArray.count{
loadMoreTweets()
}
}
@IBAction func onLogout(_ sender: Any) {
TwitterAPICaller.client?.logout()
self.dismiss(animated: true, completion: nil)
UserDefaults.standard.set(false, forKey: "userLoggedIn")
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tweetCell", for: indexPath) as! TweetCellTableViewCell
let user = tweetArray[indexPath.row]["user"] as! NSDictionary
cell.userNameLabel.text = user["name"] as! String
cell.tweetContent.text = tweetArray[indexPath.row]["text"] as! String
let imageUrl = URL(string: (user["profile_image_url_https"] as? String)!)
let data = try? Data(contentsOf: imageUrl!)
if let imageData = data {
cell.profileImageView.image = UIImage(data: imageData)
}
cell.setFavorite(tweetArray[indexPath.row]["favorited"] as! Bool)
cell.tweetId = tweetArray[indexPath.row]["id"] as! Int
cell.setRetweeted(tweetArray[indexPath.row]["retweeted"] as! Bool)
return cell
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return tweetArray.count
}
}