@@ -13,49 +13,23 @@ import Cartography
1313protocol UpdateableWithChannel: class {
1414 var channel: Model.Channels? { get set }
1515}
16−protocol NewPostDelegate {
17− func addPost(post: Post)
18−}
16+
1917protocol UpdateableWithUser: class {
2018 var user: Model.Users? { get set }
2119}
2220// MARK:- Controller with posts and channels availiable.
2321// Search is availiable within every table (posts and channels). Has button-functionality for boths post and chnnels
24−class NewsController: UIViewController, UISearchControllerDelegate, NewPostDelegate, UpdateableWithChannel
22+class NewsController: UIViewController, UISearchControllerDelegate, UpdateableWithChannel
2523{
2624 var changedChannelName: ((String)->())?
2725
2826 @IBOutlet weak var tableView: UITableView!
2927
30− var dictionary: [Int: Model.Users]? {
31− didSet {
32−
33− var newPosts: [Model.Posts] = []
34−
35− posts!.forEach({ (post) in
36−
37− newPosts.append(Model.Posts(body: post.body, authorId: post.authorId, id: post.id, user: dictionary![post.authorId]!, date: post.updated, tags: post.tags))
38−
39− post.tags.forEach { Model.Channels.fullTags.insert($0) }
40−
41− })
42−
43− news.dataSourse = newPosts
44− tableView.reloadData()
45− }
46− }
47−
48− var posts: [Model.Posts]?
49−
5028 var channel: Model.Channels?
51− var anonymousChannel: (users: [Int: Model.Users],posts: [Model.Posts])?
29+ var anonymousChannel: (users: [Int: Model.Users], posts: [Model.Posts])?
5230
5331 // MARK: - Output -
5432 var onSelectChannel: (() -> Void)?
55−
56− func addPost(post: Post) {
57− //news.dataSourse.insert(post, at: 0)
58− }
5933
6034 var searchController = UISearchController(searchResultsController: nil)
6135
@@ -101,16 +75,6 @@ class NewsController: UIViewController, UISearchControllerDelegate, NewPostDeleg
10175 print("news clear")
10276 }
10377
104− let label : UILabel = {
105− let label = UILabel()
106− label.text = "No posts to display yet!"
107− label.font = UIFont.systemFont(ofSize: 18)
108− label.textColor = .black
109− label.isHidden = true
110− label.numberOfLines = 0
111− return label
112− }()
113−
11478 override func viewWillAppear(_ animated: Bool) {
11579 super.viewWillAppear(animated)
11680 searchController.isActive = false
@@ -127,26 +91,23 @@ class NewsController: UIViewController, UISearchControllerDelegate, NewPostDeleg
12791 if let channel = channel, let id = channel.id {
12892
12993 Model.getChannel(with: id) { [weak self] in
130− self?.posts = $0.posts
131− self?.dictionary = $0.users
94+ self?.news.dataSourse = $0.posts
95+ self?.news.dictionary = $0.users
13296 self?.refreshContr.endRefreshing()
13397 self?.changedChannelName?(channel.name)
13498 }
135−
136− } else if let _ = posts {
137− print("here!")
13899 } else {
139100 Model.getLast { [weak self] in
140− self?.posts = $0.posts
141− self?.dictionary = $0.users
101+ self?.news.dataSourse = $0.posts
102+ self?.news.dictionary = $0.users
142103 self?.refreshContr.endRefreshing()
143104 self?.changedChannelName?("General")
144105 }
145106 }
146107 default:
147108 if let anonChannel = anonymousChannel {
148− posts = anonChannel.posts
149− dictionary = anonChannel.users
109+ news.dataSourse = anonChannel.posts
110+ news.dictionary = anonChannel.users
150111 changedChannelName?("Anonymous")
151112 }
152113 }
@@ -157,76 +118,4 @@ class NewsController: UIViewController, UISearchControllerDelegate, NewPostDeleg
157118 tableView.dataSource = news
158119 tableView.reloadData()
159120 }
160−
161−
162− // for animating the banner
163− var topConstraint: NSLayoutConstraint?
164−
165− let bannerView: UIView = {
166− let view = UIView()
167− view.backgroundColor = .blue
168− view.layer.cornerRadius = 25.0
169− view.clipsToBounds = true
170− return view
171− }()
172−
173− let statusLabel: UILabel = {
174− let label = UILabel()
175− label.text = "-"
176− label.textColor = .white
177− label.textAlignment = .center
178− return label
179− }()
180−
181− // MARK:- banner
182− private func setUpBanner()
183− {
184− view.addSubview(bannerView)
185− bannerView.addSubview(statusLabel)
186− statusLabel.edgesToSuperview()
187−
188− let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
189−
190− bannerView.addGestureRecognizer(tap)
191− topConstraint = NSLayoutConstraint(item: bannerView, attribute: .top, relatedBy: .equal, toItem: view.safeAreaLayoutGuide, attribute: .top, multiplier: 1, constant: -300)
192− view.addConstraint(topConstraint!)
193−
194− bannerView.edgesToSuperview(excluding: [.bottom, .top, .left], insets: .right(20), usingSafeArea: true)
195− bannerView.height(50)
196− bannerView.width(50)
197− }
198−
199− // when table is scrolling no deletion is availiable
200− @objc func handleTap(sender: UITapGestureRecognizer? = nil) {
201− let indexPath = IndexPath(row: 0, section: 0)
202− self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
203−// isBannerVisible = false
204−// changeConstraint(isVisible: false)
205− }
206−
207− // animation for banner
208− func changeConstraint(isVisible: Bool){
209− topConstraint?.constant = isVisible ? 50 : -300
210−
211− UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
212− self.view.layoutIfNeeded()
213− }) { (completed) in
214−
215− }
216− }
217−
218− var isBannerVisible: Bool = false
219−
220− func scrollViewDidScroll(_ scrollView: UIScrollView) {
221− print(scrollView.contentOffset.y)
222− if scrollView.contentOffset.y >= 50 && !isBannerVisible{
223− isBannerVisible = true
224− changeConstraint(isVisible: true)
225− }
226−
227− if isBannerVisible && scrollView.contentOffset.y == 0{
228− isBannerVisible = false
229− changeConstraint(isVisible: false)
230− }
231− }
232121}