// // NewsDetailVC.swift // Sirani // // Created by Anhar Solehudin on 06/04/19. // Copyright © 2019 Anhar Solehudin. All rights reserved. // import UIKit import WebKit class NewsDetailVC: UIViewController { // Outlets @IBOutlet weak var NewsText: UITextView! @IBOutlet weak var progressLoading: UIView! @IBOutlet weak var tanggalLabel: UILabel! @IBOutlet weak var titleLabel: UILabel! @IBOutlet weak var thumbImageView: UIImageView! // Initialize private var presenter: NewsDetailPresenter! // Segue Id private let unwindToHomeSegueId = "unwindNewsDetailToHomeSegueId" private let unwindToNewsListSegueId = "unwindNewsDetailToNewsListSegueId" let SEGUE_HOME_ID = 21 let SEGUE_NEWS_LIST_ID = 22 // Constant private let alertNoConnectionTitle = "Informasi" private let alertNoConnectionMsg = "Sepertinya Anda sedang tidak tersambung pada koneksi internet" private let alertNoConnectionActionYes = "OK" // Public Variable, Use by Segue var newsId: Int = 0 var date: String? var judul: String? var urlThumb: String? var segueId: Int = 0 // Variables private var loading = false // View Lifecycles override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent } override func viewWillAppear(_ animated: Bool) { presenter.attachView(view: self) } override func viewDidLoad() { super.viewDidLoad() presenter = NewsDetailPresenter(siraniApiServices: SiraniApiServices()) thumbImageView.layer.cornerRadius = 16 thumbImageView.clipsToBounds = true titleLabel.font = UIFont.systemFont(ofSize: 18, weight: .bold) titleLabel.textColor = UIColor.siraniTextPrimary tanggalLabel.font = UIFont.systemFont(ofSize: 12, weight: .semibold) tanggalLabel.textColor = UIColor.siraniPrimaryBlue NewsText.font = UIFont.systemFont(ofSize: 14, weight: .regular) NewsText.textColor = UIColor.siraniTextPrimary tanggalLabel.text = self.date titleLabel.text = self.judul thumbImageView.loadFrom(URLAddress: self.urlThumb ?? "") } override func viewDidAppear(_ animated: Bool) { loadData() } override func viewDidDisappear(_ animated: Bool) { presenter.detachView() } // private functions private func loadData() { if !loading { if ConnectionHelper.isConnectedToNetwork() { print("Load data \(newsId)") self.presenter.loadData(newsId: self.newsId) } else { showNoConnection() } } } // Actions @IBAction func onBtnBackTap(_ sender: Any) { if segueId == SEGUE_HOME_ID { performSegue(withIdentifier: unwindToHomeSegueId, sender: self) } else if segueId == SEGUE_NEWS_LIST_ID { performSegue(withIdentifier: unwindToNewsListSegueId, sender: self) } } } // NewsDetailViewDelegate extension NewsDetailVC: NewsDetailViewDelegate { func showNoConnection() { let alertNoConnection = UIAlertController( title: alertNoConnectionTitle, message: alertNoConnectionMsg, preferredStyle: .alert ) alertNoConnection.addAction( UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil) ) self.present(alertNoConnection, animated: true, completion: nil) } func showLoading() { loading = true progressLoading.show() } func hideLoading() { loading = false progressLoading.hide() } func setNewsDetail(newsDetail: NewsDetail) { var news = newsDetail.news news = news.replacingOccurrences(of: "<", with: "<") news = news.replacingOccurrences(of: """, with: "\"") news = news.replacingOccurrences(of: "&", with: "&") news = news.replacingOccurrences(of: "'", with: "'") news = news.replacingOccurrences(of: ">", with: ">") DispatchQueue.main.async { self.NewsText.setHTMLFromString(news) self.NewsText.isEditable = false } } } extension UITextView { func setHTMLFromString(_ htmlText: String) { let modifiedFont = String(format:"%@", self.font!.pointSize, htmlText) let data = modifiedFont.data(using: String.Encoding.utf8, allowLossyConversion: true)! let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [ .documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue ] if let attributedString = try? NSAttributedString(data: data, options: options, documentAttributes: nil) { self.attributedText = attributedString } } }