Newer
Older
Meterage_iOS / Meterage / ViewControllers / Notice / NoticeDetailViewController.swift
//
//  NoticeDetailViewController.swift
//  Meterage
//
//  Created by 203 on 2023/2/6.
//

import Alamofire
import UIKit
import DefaultsKit
import SwiftyJSON
import RichTextView

class NoticeDetailViewController: UIViewController, TransferValueDelegate, UIScrollViewDelegate {

    private var paramDic: [String: Any] = [:]

    func transfer(controller: UIViewController, dic: [String: Any]) {
        paramDic = dic
    }

    @IBOutlet weak var noticeDateView: UILabel!
    @IBOutlet weak var noticePublisherView: UILabel!
    @IBOutlet weak var noticeCompanyView: UILabel!
    @IBOutlet weak var noticeNoView: UILabel!
    @IBOutlet weak var noticeTitleView: UILabel!
    @IBOutlet weak var noticeSketchView: UILabel!
    @IBOutlet weak var minioFileView: UILabel!

    private let defaults = Defaults.shared
    private let device = DeviceSizeUtil.shared

    private var htmlScrollView: UIScrollView!
    private var htmlLabelView: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .mainBackground
        navigationItem.title = "通知公告"
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.mainTextColor]
        // Do any additional setup after loading the view.

        //数据请求
        let baseURL = defaults.get(for: serverConfigKey)!
        let token = defaults.get(for: tokenKey) ?? ""
        let noticeListURL = baseURL + Constant.noticeList.rawValue + "?limit=20&offset=1"
        let paramDic: [String: Any] = ["noticeNo": paramDic["noticeNo"]!, "noticeTitle": paramDic["noticeTitle"]!, "noticePublisher": paramDic["noticePublisher"]!, "noticeStartTime": "", "noticeEndTime": ""]
        Alamofire.request(HttpRequestCreator.shared.createPostRequest(url: noticeListURL, dic: paramDic, token: token)).responseJSON { [self] response in
            switch response.result {
            case let .success(value):
                let notice = NoticeListModel(respJson: JSON(value)).data.rows[0]
                noticeDateView.text = notice.noticeTime
                noticePublisherView.text = "发布人  \(String(describing: notice.noticePublisher!))"
                noticeCompanyView.text = "发布单位  \(String(describing: notice.noticeCompany!))"

                noticeNoView.text = notice.noticeNo
                noticeTitleView.text = notice.noticeTitle
                noticeSketchView.text = notice.noticeSketch

                htmlScrollView = UIScrollView()
                htmlScrollView.layer.cornerRadius = 7
                htmlScrollView.layer.masksToBounds = true
                htmlScrollView.backgroundColor = .white
                htmlScrollView.delegate = self
                view.addSubview(htmlScrollView)
                let safeAreaHeight = device.getSafeAreaFrame(self).height
                let htmlScrollViewHeight = safeAreaHeight - 80 - 105 - 35 - 2 * 10
                htmlScrollView.snp.makeConstraints { make in
                    make.centerX.equalToSuperview()
                    make.width.equalTo(SCREEN_WIDTH - 20)
                    make.height.equalTo(htmlScrollViewHeight - 20)
                    make.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(205)
                }

                let strHtml = notice.noticeContent!
                htmlLabelView = UILabel(frame: CGRect(x: 0, y: 0, width: (SCREEN_WIDTH - 20), height: htmlScrollViewHeight - 20))
                htmlScrollView.addSubview(htmlLabelView)
                htmlLabelView.layer.cornerRadius = 10
                htmlLabelView.layer.masksToBounds = true
                htmlLabelView.text = notice.noticeContent
                htmlLabelView.numberOfLines = 0 //Line break when the current line is full display.
                htmlLabelView.lineBreakMode = NSLineBreakMode.byClipping; //Tips:Supported six types.
                do {
                    let srtData = strHtml.data(using: .unicode, allowLossyConversion: true)!
                    let strOptions = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]//Tips:Supported four types.
                    let attrStr = try NSAttributedString(data: srtData, options: strOptions, documentAttributes: nil)
                    htmlLabelView.attributedText = attrStr
                } catch let error as NSError {
                    print(error.localizedDescription)
                }

                if notice.minioFileName == nil {
                    minioFileView.text = "暂无附件"
                } else {
                    if notice.minioFileName!.isEmpty {
                        minioFileView.text = "暂无附件"
                    } else {
                        let minioFileName = notice.minioFileName!
                        let minioFileAttrStr: NSMutableAttributedString = NSMutableAttributedString(string: minioFileName)
                        let range: NSRange = NSRange(location: 0, length: minioFileName.count)
                        minioFileAttrStr.addAttribute(.underlineStyle, value: 1, range: range)
                        minioFileView.attributedText = minioFileAttrStr
                        minioFileView.textColor = .mainThemeColor
                    }
                }
            case .failure:
                AlertHub.shared.showWaringAlert(controller: self, message: "请求失败,请检查网络")
            }
        }
    }
}