NewsDetailVC.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. tanggalLabel.text = self.date
  47. titleLabel.text = self.judul
  48. thumbImageView.loadFrom(URLAddress: self.urlThumb ?? "")
  49. }
  50. override func viewDidAppear(_ animated: Bool) {
  51. loadData()
  52. }
  53. override func viewDidDisappear(_ animated: Bool) {
  54. presenter.detachView()
  55. }
  56. // private functions
  57. private func loadData() {
  58. if !loading {
  59. if ConnectionHelper.isConnectedToNetwork() {
  60. print("Load data \(newsId)")
  61. ConnectionHelper.checkIpAddress { [weak self] success in
  62. guard let self = self else { return }
  63. DispatchQueue.main.async {
  64. if success {
  65. self.presenter.loadData(newsId: self.newsId)
  66. } else {
  67. CommonHelper.showNotAllowConnection(in: self)
  68. }
  69. }
  70. }
  71. } else {
  72. showNoConnection()
  73. }
  74. }
  75. }
  76. // Actions
  77. @IBAction func onBtnBackTap(_ sender: Any) {
  78. if segueId == SEGUE_HOME_ID {
  79. performSegue(withIdentifier: unwindToHomeSegueId, sender: self)
  80. } else if segueId == SEGUE_NEWS_LIST_ID {
  81. performSegue(withIdentifier: unwindToNewsListSegueId, sender: self)
  82. }
  83. }
  84. }
  85. // NewsDetailViewDelegate
  86. extension NewsDetailVC: NewsDetailViewDelegate {
  87. func showNoConnection() {
  88. let alertNoConnection = UIAlertController(
  89. title: alertNoConnectionTitle,
  90. message: alertNoConnectionMsg,
  91. preferredStyle: .alert
  92. )
  93. alertNoConnection.addAction(
  94. UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil)
  95. )
  96. self.present(alertNoConnection, animated: true, completion: nil)
  97. }
  98. func showLoading() {
  99. loading = true
  100. progressLoading.show()
  101. }
  102. func hideLoading() {
  103. loading = false
  104. progressLoading.hide()
  105. }
  106. func setNewsDetail(newsDetail: NewsDetail) {
  107. var news = newsDetail.news
  108. news = news.replacingOccurrences(of: "&lt;", with: "<")
  109. news = news.replacingOccurrences(of: "&quot;", with: "\"")
  110. news = news.replacingOccurrences(of: "&amp;", with: "&")
  111. news = news.replacingOccurrences(of: "&apos;", with: "'")
  112. news = news.replacingOccurrences(of: "&gt;", with: ">")
  113. DispatchQueue.main.async {
  114. self.NewsText.setHTMLFromString(news)
  115. self.NewsText.isEditable = false
  116. }
  117. }
  118. }
  119. extension UITextView {
  120. func setHTMLFromString(_ htmlText: String) {
  121. let modifiedFont = String(format:"<span style=\"font-family: '-apple-system', 'HelveticaNeue'; font-size: %f\">%@</span>", self.font!.pointSize, htmlText)
  122. let data = modifiedFont.data(using: String.Encoding.utf8, allowLossyConversion: true)!
  123. let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
  124. .documentType: NSAttributedString.DocumentType.html,
  125. .characterEncoding: String.Encoding.utf8.rawValue
  126. ]
  127. if let attributedString = try? NSAttributedString(data: data, options: options, documentAttributes: nil) {
  128. self.attributedText = attributedString
  129. }
  130. }
  131. }