| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- //
- // 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())
-
- 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:"<span style=\"font-family: '-apple-system', 'HelveticaNeue'; font-size: %f\">%@</span>", 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
- }
- }
- }
|