NewsDetailVC.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // NewsDetailVC.swift
  3. // Sirani
  4. //
  5. // Created by Anhar Solehudin on 06/04/19.
  6. // Copyright © 2019 Anhar Solehudin. All rights reserved.
  7. //
  8. import UIKit
  9. import WebKit
  10. class NewsDetailVC: UIViewController {
  11. // Outlets
  12. @IBOutlet weak var NewsText: UITextView!
  13. @IBOutlet weak var progressLoading: UIView!
  14. @IBOutlet weak var tanggalLabel: UILabel!
  15. @IBOutlet weak var titleLabel: UILabel!
  16. @IBOutlet weak var thumbImageView: UIImageView!
  17. // Initialize
  18. private var presenter: NewsDetailPresenter!
  19. // Segue Id
  20. private let unwindToHomeSegueId = "unwindNewsDetailToHomeSegueId"
  21. private let unwindToNewsListSegueId = "unwindNewsDetailToNewsListSegueId"
  22. let SEGUE_HOME_ID = 21
  23. let SEGUE_NEWS_LIST_ID = 22
  24. // Constant
  25. private let alertNoConnectionTitle = "Informasi"
  26. private let alertNoConnectionMsg = "Sepertinya Anda sedang tidak tersambung pada koneksi internet"
  27. private let alertNoConnectionActionYes = "OK"
  28. // Public Variable, Use by Segue
  29. var newsId: Int = 0
  30. var date: String?
  31. var judul: String?
  32. var urlThumb: String?
  33. var segueId: Int = 0
  34. // Variables
  35. private var loading = false
  36. // View Lifecycles
  37. override var preferredStatusBarStyle: UIStatusBarStyle {
  38. return .lightContent
  39. }
  40. override func viewWillAppear(_ animated: Bool) {
  41. presenter.attachView(view: self)
  42. }
  43. override func viewDidLoad() {
  44. super.viewDidLoad()
  45. presenter = NewsDetailPresenter(siraniApiServices: SiraniApiServices())
  46. thumbImageView.layer.cornerRadius = 16
  47. thumbImageView.clipsToBounds = true
  48. titleLabel.font = UIFont.systemFont(ofSize: 18, weight: .bold)
  49. titleLabel.textColor = UIColor.siraniTextPrimary
  50. tanggalLabel.font = UIFont.systemFont(ofSize: 12, weight: .semibold)
  51. tanggalLabel.textColor = UIColor.siraniPrimaryBlue
  52. NewsText.font = UIFont.systemFont(ofSize: 14, weight: .regular)
  53. NewsText.textColor = UIColor.siraniTextPrimary
  54. tanggalLabel.text = self.date
  55. titleLabel.text = self.judul
  56. thumbImageView.loadFrom(URLAddress: self.urlThumb ?? "")
  57. }
  58. override func viewDidAppear(_ animated: Bool) {
  59. loadData()
  60. }
  61. override func viewDidDisappear(_ animated: Bool) {
  62. presenter.detachView()
  63. }
  64. // private functions
  65. private func loadData() {
  66. if !loading {
  67. if ConnectionHelper.isConnectedToNetwork() {
  68. print("Load data \(newsId)")
  69. self.presenter.loadData(newsId: self.newsId)
  70. } else {
  71. showNoConnection()
  72. }
  73. }
  74. }
  75. // Actions
  76. @IBAction func onBtnBackTap(_ sender: Any) {
  77. if segueId == SEGUE_HOME_ID {
  78. performSegue(withIdentifier: unwindToHomeSegueId, sender: self)
  79. } else if segueId == SEGUE_NEWS_LIST_ID {
  80. performSegue(withIdentifier: unwindToNewsListSegueId, sender: self)
  81. }
  82. }
  83. }
  84. // NewsDetailViewDelegate
  85. extension NewsDetailVC: NewsDetailViewDelegate {
  86. func showNoConnection() {
  87. let alertNoConnection = UIAlertController(
  88. title: alertNoConnectionTitle,
  89. message: alertNoConnectionMsg,
  90. preferredStyle: .alert
  91. )
  92. alertNoConnection.addAction(
  93. UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil)
  94. )
  95. self.present(alertNoConnection, animated: true, completion: nil)
  96. }
  97. func showLoading() {
  98. loading = true
  99. progressLoading.show()
  100. }
  101. func hideLoading() {
  102. loading = false
  103. progressLoading.hide()
  104. }
  105. func setNewsDetail(newsDetail: NewsDetail) {
  106. var news = newsDetail.news
  107. news = news.replacingOccurrences(of: "&lt;", with: "<")
  108. news = news.replacingOccurrences(of: "&quot;", with: "\"")
  109. news = news.replacingOccurrences(of: "&amp;", with: "&")
  110. news = news.replacingOccurrences(of: "&apos;", with: "'")
  111. news = news.replacingOccurrences(of: "&gt;", with: ">")
  112. DispatchQueue.main.async {
  113. self.NewsText.setHTMLFromString(news)
  114. self.NewsText.isEditable = false
  115. }
  116. }
  117. }
  118. extension UITextView {
  119. func setHTMLFromString(_ htmlText: String) {
  120. let modifiedFont = String(format:"<span style=\"font-family: '-apple-system', 'HelveticaNeue'; font-size: %f\">%@</span>", self.font!.pointSize, htmlText)
  121. let data = modifiedFont.data(using: String.Encoding.utf8, allowLossyConversion: true)!
  122. let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
  123. .documentType: NSAttributedString.DocumentType.html,
  124. .characterEncoding: String.Encoding.utf8.rawValue
  125. ]
  126. if let attributedString = try? NSAttributedString(data: data, options: options, documentAttributes: nil) {
  127. self.attributedText = attributedString
  128. }
  129. }
  130. }