@@ -13,49 +13,23 @@ import Cartography
13protocol UpdateableWithChannel: class {
13protocol UpdateableWithChannel: class {
14 var channel: Model.Channels? { get set }
14 var channel: Model.Channels? { get set }
16−protocol NewPostDelegate {
16+
17− func addPost(post: Post)
19protocol UpdateableWithUser: class {
17protocol UpdateableWithUser: class {
20 var user: Model.Users? { get set }
18 var user: Model.Users? { get set }
22// MARK:- Controller with posts and channels availiable.
20// MARK:- Controller with posts and channels availiable.
23// Search is availiable within every table (posts and channels). Has button-functionality for boths post and chnnels
21// 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
26 var changedChannelName: ((String)->())?
24 var changedChannelName: ((String)->())?
28 @IBOutlet weak var tableView: UITableView!
26 @IBOutlet weak var tableView: UITableView!
30− var dictionary: [Int: Model.Users]? {
33− var newPosts: [Model.Posts] = []
35− posts!.forEach({ (post) in
37− newPosts.append(Model.Posts(body: post.body, authorId: post.authorId, id: post.id, user: dictionary![post.authorId]!, date: post.updated, tags: post.tags))
39− post.tags.forEach { Model.Channels.fullTags.insert($0) }
43− news.dataSourse = newPosts
44− tableView.reloadData()
48− var posts: [Model.Posts]?
50 var channel: Model.Channels?
28 var channel: Model.Channels?
51− var anonymousChannel: (users: [Int: Model.Users],posts: [Model.Posts])?
29+ var anonymousChannel: (users: [Int: Model.Users], posts: [Model.Posts])?
53 // MARK: - Output -
31 // MARK: - Output -
54 var onSelectChannel: (() -> Void)?
32 var onSelectChannel: (() -> Void)?
56− func addPost(post: Post) {
57− //news.dataSourse.insert(post, at: 0)
60 var searchController = UISearchController(searchResultsController: nil)
34 var searchController = UISearchController(searchResultsController: nil)
@@ -101,16 +75,6 @@ class NewsController: UIViewController, UISearchControllerDelegate, NewPostDeleg
101 print("news clear")
75 print("news clear")
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
114 override func viewWillAppear(_ animated: Bool) {
78 override func viewWillAppear(_ animated: Bool) {
115 super.viewWillAppear(animated)
79 super.viewWillAppear(animated)
116 searchController.isActive = false
80 searchController.isActive = false
@@ -127,26 +91,23 @@ class NewsController: UIViewController, UISearchControllerDelegate, NewPostDeleg
127 if let channel = channel, let id = channel.id {
91 if let channel = channel, let id = channel.id {
129 Model.getChannel(with: id) { [weak self] in
93 Model.getChannel(with: id) { [weak self] in
130− self?.posts = $0.posts
94+ self?.news.dataSourse = $0.posts
131− self?.dictionary = $0.users
95+ self?.news.dictionary = $0.users
132 self?.refreshContr.endRefreshing()
96 self?.refreshContr.endRefreshing()
133 self?.changedChannelName?(channel.name)
97 self?.changedChannelName?(channel.name)
136− } else if let _ = posts {
139 Model.getLast { [weak self] in
100 Model.getLast { [weak self] in
140− self?.posts = $0.posts
101+ self?.news.dataSourse = $0.posts
141− self?.dictionary = $0.users
102+ self?.news.dictionary = $0.users
142 self?.refreshContr.endRefreshing()
103 self?.refreshContr.endRefreshing()
143 self?.changedChannelName?("General")
104 self?.changedChannelName?("General")
147 if let anonChannel = anonymousChannel {
108 if let anonChannel = anonymousChannel {
148− posts = anonChannel.posts
109+ news.dataSourse = anonChannel.posts
149− dictionary = anonChannel.users
110+ news.dictionary = anonChannel.users
150 changedChannelName?("Anonymous")
111 changedChannelName?("Anonymous")
@@ -157,76 +118,4 @@ class NewsController: UIViewController, UISearchControllerDelegate, NewPostDeleg
157 tableView.dataSource = news
118 tableView.dataSource = news
158 tableView.reloadData()
119 tableView.reloadData()
162− // for animating the banner
163− var topConstraint: NSLayoutConstraint?
165− let bannerView: UIView = {
167− view.backgroundColor = .blue
168− view.layer.cornerRadius = 25.0
169− view.clipsToBounds = true
173− let statusLabel: UILabel = {
174− let label = UILabel()
176− label.textColor = .white
177− label.textAlignment = .center
182− private func setUpBanner()
184− view.addSubview(bannerView)
185− bannerView.addSubview(statusLabel)
186− statusLabel.edgesToSuperview()
188− let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
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!)
194− bannerView.edgesToSuperview(excluding: [.bottom, .top, .left], insets: .right(20), usingSafeArea: true)
195− bannerView.height(50)
196− bannerView.width(50)
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)
207− // animation for banner
208− func changeConstraint(isVisible: Bool){
209− topConstraint?.constant = isVisible ? 50 : -300
211− UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
212− self.view.layoutIfNeeded()
218− var isBannerVisible: Bool = false
220− func scrollViewDidScroll(_ scrollView: UIScrollView) {
221− print(scrollView.contentOffset.y)
222− if scrollView.contentOffset.y >= 50 && !isBannerVisible{
223− isBannerVisible = true
224− changeConstraint(isVisible: true)
227− if isBannerVisible && scrollView.contentOffset.y == 0{
228− isBannerVisible = false
229− changeConstraint(isVisible: false)