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 pathSubredditListCategoryViewController.swift
115 lines (95 loc) · 4.13 KB
/
SubredditListCategoryViewController.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
//
// SubredditListCategoryViewControllerTableViewController.swift
// reddift
//
// Created by sonson on 2015/12/14.
// Copyright © 2015年 sonson. All rights reserved.
//
import UIKit
/**
View controller to show NSFW categories of subreddits at redditlist.com.
*/
class SubredditListNSFWCategoryViewController: SubredditListCategoryViewController {
}
/**
View controller to show SFW categories of subreddits at redditlist.com.
*/
class SubredditListSFWCategoryViewController: SubredditListCategoryViewController {
}
/**
View controller to show categories of subreddits at redditlist.com.
*/
class SubredditListCategoryViewController: UITableViewController {
var targetType: SubredditListCategoryTargetType = .NSFW
var categoryLists: [String: [SubredditListItem]] = [:]
var categoryTitles: [String] = []
var lastUpdateDate = NSDate()
init() {
super.init(nibName: nil, bundle: nil)
targetType = SubredditListCategoryTargetType(viewController: self)
self.title = targetType.tabbarTitle
self.tabBarItem = UITabBarItem(title: self.title, image: UIImage(named: "folder"), tag: 0)
}
override init(nibName: String?, bundle: Bundle?) {
super.init(nibName: nibName, bundle: bundle)
targetType = SubredditListCategoryTargetType(viewController: self)
self.title = targetType.tabbarTitle
self.tabBarItem = UITabBarItem(title: self.title, image: UIImage(named: "folder"), tag: 0)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
targetType = SubredditListCategoryTargetType(viewController: self)
self.title = targetType.tabbarTitle
self.tabBarItem = UITabBarItem(title: self.title, image: UIImage(named: "folder"), tag: 0)
}
@IBAction func close(sender: AnyObject) {
dismiss(animated: true, completion: nil)
}
func loadJSON() {
let session: URLSession = URLSession(configuration: URLSessionConfiguration.default)
let request = URLRequest(url: targetType.url)
let task = session.dataTask(with: request, completionHandler: { (data: Data?, _, _) -> Void in
if let data = data {
(self.categoryLists, self.categoryTitles, self.lastUpdateDate) = SubredditListItem.ReddiftJSON2List(data: data as NSData, showsNSFW: true)
}
DispatchQueue.main.async(execute: { () -> Void in
self.tableView.reloadData()
})
})
task.resume()
}
override func viewDidLoad() {
let barbutton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(SubredditListCategoryViewController.close(sender:)))
self.navigationItem.rightBarButtonItem = barbutton
super.viewDidLoad()
tableView.register(SubredditListViewRightValueCell.self, forCellReuseIdentifier: "Cell")
loadJSON()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return categoryTitles.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath)
cell.textLabel?.text = categoryTitles[indexPath.row]
cell.accessoryType = .disclosureIndicator
let title = categoryTitles[indexPath.row]
if let list = categoryLists[title] {
cell.detailTextLabel?.text = "\(list.count) subreddits"
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let title = categoryTitles[indexPath.row] as String?, let list = categoryLists[title] {
let vc = SubredditListViewController()
vc.list = list
vc.title = title
navigationController?.pushViewController(vc, animated: true)
}
}
}