-
Notifications
You must be signed in to change notification settings - Fork 0
/
DetailedViewController.swift
226 lines (188 loc) · 9.31 KB
/
DetailedViewController.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
//
// DetailedViewController.swift
// CSCI571_HW9
//
// Created by mmlab on 4/21/20.
// Copyright © 2020 csci571. All rights reserved.
//
import UIKit
import Alamofire
import SwiftSpinner
import Toast_Swift
class DetailedViewController: UIViewController {
// @IBOutlet weak var testingLable: UILabel!
@IBOutlet weak var firstView: UIView!
@IBOutlet weak var detailTitle: UILabel!
@IBOutlet weak var detailImg: UIImageView!
@IBOutlet weak var detailSection: UILabel!
@IBOutlet weak var viewBtn: UIButton!
@IBOutlet weak var detailDate: UILabel!
@IBOutlet weak var detailDescrip: UILabel!
@IBOutlet weak var navController: UINavigationItem!
var detailArticleId = String()
var bookmarkImage = UIImage()
var twitterImage = UIImage()
var bookmarkButton = UIBarButtonItem()
var twitterButton = UIBarButtonItem()
@IBAction func openFullArticle(_ sender: UIButton) {
let url = NSURL(string: fullURL!)
UIApplication.shared.open(url! as URL)
}
var card:Card?
var fullURL:String?
var imgName:String?
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = false
// Do any additional setup after loading the view.
//let tapGesture = UITapGestureRecognizer(target: self, action: #selector(DetailedViewController.imageTapped(gesture:)))
getArticle() { (resArr) in
let date = resArr[0]["date"] as!String
let description = resArr[0]["description"] as!String
self.detailArticleId = self.card!.articleId
self.detailImg.image = self.card?.image
self.detailTitle.text = self.card?.title
self.detailSection.text = self.card?.section
self.detailDate.text = date
self.detailDescrip.attributedText = description.htmlToAttributedString
self.viewBtn.setTitle("View Full Article", for: .normal)
self.viewBtn.titleLabel?.font = UIFont(name: "Arial-MT", size: 27)
self.fullURL = resArr[0]["webURL"] as!String
self.navController.title = self.card?.title
self.imgName = self.card?.imgName
let defaults = UserDefaults.standard
var savedCards = defaults.object(forKey: "SavedCards") as? [String] ?? [String]()
if savedCards.contains(self.detailArticleId){
self.bookmarkImage = UIImage(systemName: "bookmark.fill")!
}
else {
self.bookmarkImage = UIImage(systemName: "bookmark")!
}
self.bookmarkButton = UIBarButtonItem(image: self.bookmarkImage, style: .bordered, target: self, action: #selector(self.didTapBookmarkButton(sender:)))
self.twitterImage = UIImage(named: "twitter.png")!
self.twitterButton = UIBarButtonItem(image: self.twitterImage, style: .bordered, target: self, action: #selector(self.didTapTwitterkButton(sender:)))
self.navController.rightBarButtonItems = [self.twitterButton, self.bookmarkButton]
//self.setImgConstraints()
//self.setTitleConstraints()
self.configureTitle()
//self.setSectionDateConstraints()
self.configureSectionDate()
self.configureDescrip()
}
}
@objc func didTapBookmarkButton(sender: AnyObject){
let defaults = UserDefaults.standard
var savedCards = defaults.object(forKey: "SavedCards") as? [String] ?? [String]()
var savedNews = defaults.object(forKey: "SavedNews") as? [Any] ?? [Any]()
if savedCards.count == 0 || (savedCards.count > 0 && savedCards.contains(detailArticleId) == false){
savedCards.append(detailArticleId)
defaults.set(savedCards, forKey: "SavedCards")
savedNews.append(["articleId":detailArticleId, "img":imgName, "title":detailTitle.text, "date":detailDate.text, "section":detailSection.text])
defaults.set(savedNews, forKey: "SavedNews")
self.bookmarkImage = UIImage(systemName: "bookmark.fill")!
self.view.makeToast("Article Bookmarked. Check out the Bookmarks tab to view", duration: 3.0, position: .bottom)
}
else if savedCards.count > 0 && savedCards.contains(detailArticleId){
var idx = 0
for i in 0..<savedCards.count {
if savedCards[i] == detailArticleId{
idx = i
break
}
}
savedCards.remove(at: idx)
savedNews.remove(at: idx)
defaults.set(savedCards, forKey: "SavedCards")
defaults.set(savedNews, forKey: "SavedNews")
self.bookmarkImage = UIImage(systemName: "bookmark")!
self.view.makeToast("Article Removed from Bookmarks", duration: 3.0, position: .bottom)
}
self.bookmarkButton = UIBarButtonItem(image: self.bookmarkImage, style: .bordered, target: self, action: #selector(self.didTapBookmarkButton(sender:)))
self.navController.rightBarButtonItems = [self.twitterButton, self.bookmarkButton]
}
@objc func didTapTwitterkButton(sender: AnyObject){
let twitterURL = "https://twitter.com/intent/tweet?text=Check%20out%20this%20Article!&url=\(fullURL!)&hashtags=CSCI571_NewsApp"
//let escapedShareString = shareString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
let url = NSURL(string: twitterURL)
UIApplication.shared.open(url! as URL)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.mySpinner()
navigationItem.largeTitleDisplayMode = .never
// navigationController?.navigationBar.prefersLargeTitles = false
}
/*
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
//navigationItem.largeTitleDisplayMode = .never
navigationController?.navigationBar.prefersLargeTitles = true
}
*/
func mySpinner() {
SwiftSpinner.show(duration: 3.0, title: "Loading Detailed Article..", animated: true)
}
func configureDescrip(){
self.detailDescrip.numberOfLines = 30
self.detailDescrip.font = detailDescrip.font.withSize(18)
}
func setSectionDateConstraints(){
self.detailSection.topAnchor.constraint(equalTo: detailTitle.bottomAnchor, constant: 20).isActive = true
self.detailDate.topAnchor.constraint(equalTo: detailTitle.bottomAnchor, constant: 20).isActive = true
}
func configureSectionDate(){
self.detailSection.sizeToFit()
var sdColor = UIColor(red: 206.0/255.0, green: 205.0/255.0, blue: 206.0/255.0, alpha: 1.0)
detailDate.textColor = sdColor
detailSection.textColor = sdColor
}
func setImgConstraints(){
self.detailImg.topAnchor.constraint(equalTo: firstView.bottomAnchor, constant: 5).isActive = true
self.detailImg.widthAnchor.constraint(equalTo: firstView.widthAnchor).isActive = true
self.detailImg.leadingAnchor.constraint(equalTo: firstView.leadingAnchor).isActive = true
}
func configureTitle(){
//self.detailTitle.sizeToFit()
self.detailTitle.numberOfLines = 0
self.detailTitle.textAlignment = .center
self.detailTitle.font = UIFont(name:"HelveticaNeue-Bold", size: 18.0)
}
func setTitleConstraints(){
self.detailTitle.topAnchor.constraint(equalTo: self.detailImg.bottomAnchor, constant: 5).isActive = true
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
func getArticle(completionhandler:@escaping ([AnyObject]) -> ()){
//var CardArr = [Card]()
Alamofire.request("https://hw9104062222.wl.r.appspot.com/getArticle/"+card!.articleId.replacingOccurrences(of: "/", with: "%2F"))
// .responseJSON(completionHandler: { response in
.responseJSON { response in
if response.result.isSuccess {
let responseArr = response.value as! [AnyObject]
//print(responseArr)
completionhandler(responseArr)
} else {
print("error: \(String(describing: response.error))")
}
}
}
}
extension String {
var htmlToAttributedString: NSAttributedString? {
guard let data = data(using: .utf8) else { return NSAttributedString() }
do {
return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
return NSAttributedString()
}
}
var htmlToString: String {
return htmlToAttributedString?.string ?? ""
}
}