This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathSubredditListRankingViewController.swift
152 lines (129 loc) · 6.02 KB
/
SubredditListRankingViewController.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// SubredditListRankingViewController.swift
// reddift
//
// Created by sonson on 2015/10/26.
// Copyright © 2015年 sonson. All rights reserved.
//
import UIKit
/**
View controller to show ranking of all subreddits at redditlist.com.
*/
class SubredditListAllRankingViewController: SubredditListRankingViewController {
}
/**
View controller to show ranking of SFW subreddits at redditlist.com.
*/
class SubredditListSFWRankingViewController: SubredditListRankingViewController {
}
/**
View controller to show ranking of NSFW subreddits at redditlist.com.
*/
class SubredditListNSFWRankingViewController: SubredditListRankingViewController {
}
/**
View controller to show ranking of subreddits at redditlist.com.
*/
class SubredditListRankingViewController: UITableViewController {
var targetType: SubredditListRankingTargetType = .All
var orderType: SubredditListRankingOrderType = .RecentActivity
var type: String = ""
var list: [String: [SubredditListItem]] = [:]
var lastUpdateDate = NSDate()
init() {
super.init(nibName: nil, bundle: nil)
targetType = SubredditListRankingTargetType(viewController: self)
self.title = targetType.tabbarTitle
self.tabBarItem = UITabBarItem(title: self.title, image: UIImage(named: "crown"), selectedImage: UIImage(named: "crown"))
}
override init(nibName: String?, bundle: Bundle?) {
super.init(nibName: nibName, bundle: bundle)
targetType = SubredditListRankingTargetType(viewController: self)
self.title = targetType.tabbarTitle
self.tabBarItem = UITabBarItem(title: self.title, image: UIImage(named: "crown"), selectedImage: UIImage(named: "crown"))
}
required init?(coder: NSCoder) {
super.init(coder: coder)
targetType = SubredditListRankingTargetType(viewController: self)
self.title = targetType.tabbarTitle
self.tabBarItem = UITabBarItem(title: self.title, image: UIImage(named: "crown"), selectedImage: UIImage(named: "crown"))
}
@IBAction func close(sender: AnyObject) {
dismiss(animated: true, completion: nil)
}
@IBAction func didChangeSegementedController(sender: AnyObject) {
if let segmentedController = sender as? UISegmentedControl {
switch segmentedController.selectedSegmentIndex {
case 0:
orderType = .RecentActivity
case 1:
orderType = .Subscriber
case 2:
orderType = .Growth
default:
orderType = .RecentActivity
}
tableView.reloadData()
}
}
func loadJSON() {
let session: URLSession = URLSession(configuration: URLSessionConfiguration.default)
let url = targetType.url
let request = URLRequest(url: url)
let task = session.dataTask(with: request, completionHandler: { (data: Data?, _, _) -> Void in
if let data = data {
let (subscribersRankingList, growthRankingList, recentActivityList, lastUpdateDate) = SubredditListItem.SubredditListJSON2List(data: data as NSData)
self.lastUpdateDate = lastUpdateDate
self.list[SubredditListRankingOrderType.RecentActivity.jsonKey] = recentActivityList
self.list[SubredditListRankingOrderType.Subscriber.jsonKey] = subscribersRankingList
self.list[SubredditListRankingOrderType.Growth.jsonKey] = growthRankingList
}
DispatchQueue.main.async(execute: { () -> Void in
self.tableView.reloadData()
})
})
task.resume()
}
func initializeNavigationBar() {
// create close button on navigation bar
let barbutton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(SubredditListRankingViewController.close(sender:)))
self.navigationItem.rightBarButtonItem = barbutton
// set prompt, that is a small title on the top of navigation bar
navigationItem.prompt = targetType.title
// set segmented control on navigation bar
let control = UISegmentedControl(items: [#imageLiteral(resourceName: "update"), #imageLiteral(resourceName: "bookmark"), #imageLiteral(resourceName: "trend")])
navigationItem.titleView = control
control.selectedSegmentIndex = 0
control.addTarget(self, action: #selector(SubredditListRankingViewController.didChangeSegementedController(sender:)), for: .valueChanged)
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(SubredditListViewRightValueCell.self, forCellReuseIdentifier: "Cell")
initializeNavigationBar()
loadJSON()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let list = self.list[orderType.jsonKey] else { return 0 }
return list.count
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let list = self.list[orderType.jsonKey] else { return }
let subredditListItem = list[indexPath.row]
NotificationCenter.default.post(name: SubredditSelectTabBarControllerDidOpenSubredditName, object: nil, userInfo: ["subreddit": subredditListItem.subreddit])
dismiss(animated: true, completion: nil)
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath)
guard let list = self.list[orderType.jsonKey] else { return cell }
let subredditListItem = list[indexPath.row]
cell.textLabel?.text = subredditListItem.title
cell.detailTextLabel?.text = "\(subredditListItem.score)"
return cell
}
}