From 1400778675c74df8c4e3235cc06e67c9e4b3b058 Mon Sep 17 00:00:00 2001 From: Darya Rednikina Date: Sat, 9 Mar 2019 21:00:30 +0300 Subject: [PATCH] closes: #9. decreased number of requests --- .../FullPostController.swift | 2 +- .../ News and channels/NewsVC.swift | 29 +++++++++++++++++-- .../ News and channels/PostViewCell.swift | 17 ++++------- GDproject/Simple model/Model.swift | 1 + 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/GDproject/Controller/ News and channels/FullPostController.swift b/GDproject/Controller/ News and channels/FullPostController.swift index 8880c24..72634db 100644 --- a/GDproject/Controller/ News and channels/FullPostController.swift +++ b/GDproject/Controller/ News and channels/FullPostController.swift @@ -94,7 +94,7 @@ class FullPostController: UITableViewController { let cell = tableView.dequeueReusableCell(withIdentifier: postCellId) as! PostViewCell - cell.fill(with: post!.body, true, post: post!) + cell.fill(with: PostCellData.create(with: post!.body), true, post: post!) cell.selectionStyle = .none return cell } diff --git a/GDproject/Controller/ News and channels/NewsVC.swift b/GDproject/Controller/ News and channels/NewsVC.swift index e512955..3b1be60 100644 --- a/GDproject/Controller/ News and channels/NewsVC.swift +++ b/GDproject/Controller/ News and channels/NewsVC.swift @@ -7,10 +7,35 @@ // import UIKit +import MarkdownKit + +struct PostCellData{ + var attributedData = NSAttributedString() + + static func create(with attachments: [Model.Attachments]) -> NSAttributedString{ + var markdown = "" + + attachments.forEach { (attachment) in markdown.append(attachment.markdown) } + + let markdownParser = MarkdownParser(font: UIFont.systemFont(ofSize: 16)) + markdownParser.enabledElements = .disabledAutomaticLink + markdownParser.code.textBackgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1) + + return markdownParser.parse(markdown) + } +} class NewsVC: UIViewController, UITableViewDelegate, UITableViewDataSource{ - var dataSourse: [Model.Posts] = [] + var dataSourse: [Model.Posts] = []{ + didSet{ + dataSourse.forEach { (item) in + cellDataSourse.append(PostCellData(attributedData: PostCellData.create(with: item.body))) + } + } + } + + var cellDataSourse: [PostCellData] = [] var type: HeaderType = .NONE @@ -37,7 +62,7 @@ class NewsVC: UIViewController, UITableViewDelegate, UITableViewDataSource{ { let cell = tableView.dequeueReusableCell(withIdentifier: postCellId, for: indexPath) as! PostViewCell - cell.fill(with: dataSourse[indexPath.row].body, false, post: dataSourse[indexPath.row]) + cell.fill(with: cellDataSourse[indexPath.row].attributedData, false, post: dataSourse[indexPath.row]) switch type { case .NEWS: diff --git a/GDproject/Controller/ News and channels/PostViewCell.swift b/GDproject/Controller/ News and channels/PostViewCell.swift index cb17e79..8912776 100644 --- a/GDproject/Controller/ News and channels/PostViewCell.swift +++ b/GDproject/Controller/ News and channels/PostViewCell.swift @@ -53,13 +53,8 @@ class PostViewCell: UITableViewCell fatalError("init(coder:) has not been implemented") } - func createTextView(with text: String, _ isSelectable: Bool) -> UITextView + func createTextView(with text: NSAttributedString, _ isSelectable: Bool) -> UITextView { - let markdownParser = MarkdownParser(font: UIFont.systemFont(ofSize: 16)) - let markdown = text - markdownParser.enabledElements = .disabledAutomaticLink - markdownParser.code.textBackgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1) - let textView = UITextView() textView.isScrollEnabled = false textView.isEditable = false @@ -69,24 +64,22 @@ class PostViewCell: UITableViewCell } else { textView.isUserInteractionEnabled = false } - textView.attributedText = markdownParser.parse(markdown) + + textView.attributedText = text return textView } var views: [UIView] = [] var post: Model.Posts? - func fill(with info: [Model.Attachments], _ isFullVersoin: Bool, post: Model.Posts) + func fill(with info: NSAttributedString, _ isFullVersoin: Bool, post: Model.Posts) { self.post = post // important contentView.subviews.forEach({ $0.removeFromSuperview() }) views = [] - for attachment in info - { - views.append(createTextView(with: attachment.markdown, isFullVersoin)) - } + views.append(createTextView(with: info, isFullVersoin)) nameLabel.setTitle("\(post.user?.firstName ?? "") \(post.user?.lastName ?? "")", for: .normal) nameLabel.addTarget(self, action: #selector(displayProfile), for: .touchUpInside) diff --git a/GDproject/Simple model/Model.swift b/GDproject/Simple model/Model.swift index 0181c28..c9aa65f 100644 --- a/GDproject/Simple model/Model.swift +++ b/GDproject/Simple model/Model.swift @@ -135,6 +135,7 @@ class Model{ guard let newQueery = try? decoder.decode(QueryPosts.self, from: json) else { print("no") return } + idUser = newQueery.users completion((newQueery.users, Channel(title: "# General", subtitle: "No subtitle", hashtags: ["ПИ"], people: ["No"], posts: newQueery.posts))) } }