-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArticleWebVC.swift
278 lines (231 loc) · 9.65 KB
/
ArticleWebVC.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//
// ArticleWebVC.swift
// RSSReader
//
// Created by Mitchell Cooper on 11/3/14.
// Copyright (c) 2014 Mitchell Cooper. All rights reserved.
//
import UIKit
class ArticleWebVC: UIViewController, UIWebViewDelegate {
@IBOutlet var webView: UIWebView?
@IBOutlet var indicator: UIActivityIndicatorView?
var activityController: UIActivityViewController!
var titleView: UILabel?
var countLabel: UILabel?
var upButton, downButton: UIBarButtonItem?
var url: URL!
var articleIndex = 0
var articleList: [Article]!
var article: Article { return articleList[articleIndex] }
// initialize with an article
convenience init(articleList list: [Article], index: Int) {
self.init(nibName: "ArticleWebVC", bundle: nil)
articleList = list
setArticleIndex(index)
}
func setArticleIndex(_ index: Int) {
articleIndex = index
navigationItem.title = article.title
loadArticle()
updateArrows()
}
// MARK:- View controller
// the view loaded, so we can load the URL now.
override func viewDidLoad() {
loadArticle()
// menu button
let item = UIBarButtonItem(image: UIImage(named: "icons/menu")!, style: .plain, target: self, action: #selector(ArticleWebVC.menuButtonTapped(_:)))
navigationItem.rightBarButtonItem = item
// toolbar label
countLabel = UILabel()
countLabel!.textColor = UIColor.darkGray
// toolbar items
let shareButton = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(ArticleWebVC.presentShareView(_:)))
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
upButton = UIBarButtonItem(image: UIImage(named: "icons/up"), style: .plain, target: self, action: #selector(ArticleWebVC.upTapped(_:)))
downButton = UIBarButtonItem(image: UIImage(named: "icons/down"), style: .plain, target: self, action: #selector(ArticleWebVC.downTapped(_:)))
let counter = UIBarButtonItem(customView: countLabel!)
setToolbarItems([ shareButton, flexibleSpace, counter, flexibleSpace, upButton!, downButton! ], animated: false)
updateArrows()
// sharing
activityController = UIActivityViewController(activityItems: [ self.article.urlString ], applicationActivities: nil)
}
override func viewWillAppear(_ animated: Bool) {
navigationController?.toolbar.barTintColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1)
navigationController?.setToolbarHidden(false, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
navigationController?.setToolbarHidden(true, animated: animated)
}
// mark the article read when the view appears
override func viewDidAppear(_ animated: Bool) {
markRead()
}
// MARK:- Article
// load the article webpage
func loadArticle() {
indicator?.isHidden = false
indicator?.startAnimating()
webView?.delegate = self
webView?.loadRequest(URLRequest(url: article.url as URL))
markRead()
}
// mark the article read
func markRead() {
// mark article read
article.read = true
// remove dot in article list
let vcs: [AnyObject]! = navigationController?.viewControllers
if vcs == nil { return }
if let vc = vcs[vcs.count - 2] as? ArticleListVC {
// TODO: reloadArticle method
vc.tableView.reloadData()
}
}
// MARK:- Web view delegate
func webViewDidFinishLoad(_ webView: UIWebView) {
indicator?.stopAnimating()
indicator?.isHidden = true
// markRead()
}
// MARK:- Interface actions
// menu button in navigation bar tapped
func menuButtonTapped(_ sender: UIBarButtonItem) {
let sheet = PSTAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
// delete
var action = PSTAlertAction(title: "Delete", style: .destructive) { _ in
let areYouSure = PSTAlertController(title: "Delete this article?", message: "\"\(self.article.title)\" will be deleted permanently.", preferredStyle: .alert)
areYouSure?.addAction(PSTAlertAction(title: "Delete", style: .default) { _ in
// dispose of the current article
self.article.disposeOf()
// delete in the local list
self.articleList.remove(at: self.articleIndex)
// set article index to the current
// because the next article will have taken this one's place
self.setArticleIndex(self.articleIndex)
})
areYouSure?.addAction(PSTAlertAction(title: "Cancel", style: .cancel, handler: nil))
areYouSure?.showWithSender(nil, controller: self, animated: true, completion: nil)
}
sheet?.addAction(action)
// share
action = PSTAlertAction(title: "Share", handler: presentShareView)
sheet?.addAction(action)
// save
if !article.saved {
action = PSTAlertAction(title: "Save for later") { _ in
self.article.saved = true
}
sheet?.addAction(action)
}
// unsave
else {
action = PSTAlertAction(title: "Unsave") { _ in
self.article.saved = false
}
sheet?.addAction(action)
}
// mark unread
if article.read {
action = PSTAlertAction(title: "Mark unread") { _ in
self.article.read = false
}
sheet?.addAction(action)
}
// mark read
else {
action = PSTAlertAction(title: "Mark read") { _ in
self.article.read = true
}
sheet?.addAction(action)
}
// cancel
sheet?.addAction(PSTAlertAction(title: "Cancel", style: .cancel, handler: nil))
sheet?.showWithSender(sender, controller: self, animated: true, completion: nil)
}
// present share controller
func presentShareView(_: AnyObject?) {
present(activityController, animated: true, completion: nil)
}
// up arrow tapped
func upTapped(_: AnyObject) {
setArticleIndex(articleIndex - 1)
}
// down arrow tapped
func downTapped(_: AnyObject) {
setArticleIndex(articleIndex + 1)
}
// enable/disable arrow buttons
// also, update the counter
func updateArrows() {
upButton?.isEnabled = articleIndex != 0
downButton?.isEnabled = articleIndex != articleList.count - 1
countLabel?.text = "\(articleIndex + 1) of \(articleList.count)"
countLabel?.sizeToFit()
}
// This is code for WAMenu, a custom menu that I created for this application,
// but it is not included in this release because it is not yet functional
// in landscape viewing mode.
//
//private struct MenuDefinition {
// typealias Item = (title: String, iconName: String?, color: UIColor?)
//
// static let deleteItem: Item = ("Delete", "x", Colors.deleteColor)
// static let saveItem: Item = ("Save for later", "plus2", Colors.savedColor)
// static let markUnreadItem: Item = ("Mark unread", "mail", Colors.unreadColor)
//
// static let unsaveItem: Item = ("Unsave", "plus2", Colors.savedColor)
// static let markReadItem: Item = ("Mark read", "mail", Colors.unreadColor)
//
//}
//
// private func updateMenu() {
// menu.menuItems.removeAllObjects()
//
// let items = [
// MenuDefinition.deleteItem,
// article.saved ? MenuDefinition.unsaveItem : MenuDefinition.saveItem,
// article.read ? MenuDefinition.markUnreadItem : MenuDefinition.markReadItem
// ]
//
// let font = UIFont.systemFontOfSize(25)
// for item in items {
// let icon = item.iconName == nil ? nil : UIImage(named: "icons/\(item.iconName!)")
// menu.addItem(item.title, icon: icon, color: item.color, font: font)
// }
// }
//
// func menuItemSelected(action: String!) {
// switch action {
//
// // delete
// case MenuDefinition.deleteItem.title:
// let areYouSure = PSTAlertController(title: "Delete this article?", message: "\"\(article.title)\" will be deleted permanently.", preferredStyle: .Alert)
// areYouSure.addAction(PSTAlertAction(title: "Delete", style: .Default) { _ in
// self.article.disposeOf()
// self.navigationController?.popViewControllerAnimated(true)
// })
// areYouSure.addAction(PSTAlertAction(title: "Cancel", style: .Cancel, handler: nil))
// areYouSure.showWithSender(nil, controller: self, animated: true, completion: nil)
//
// // save
// case MenuDefinition.saveItem.title:
// article.saved = true
//
// // mark unread
// case MenuDefinition.markUnreadItem.title:
// article.read = false
//
// // unsave
// case MenuDefinition.unsaveItem.title:
// article.saved = false
//
// // mark read
// case MenuDefinition.markReadItem.title:
// article.read = true
//
// default:
// break
// }
// }
}