// // MessageViewController.swift // MTP2_iOS // // Created by Handy_Cao on 2020/10/23. // Copyright © 2020 Muchinfo. All rights reserved. // import UIKit import NVActivityIndicatorView import WHToast import GTMRefresh import IBAnimatable /// 消息视图容器控制类 class MessageViewController: BaseViewController { // MARK: - 属性列表 /// 通知 @IBOutlet weak var message: UIButton! /// 公告 @IBOutlet weak var notice: UIButton! /// 消息数量 @IBOutlet weak var msgCount: AnimatableLabel! { didSet { msgCount.layer.cornerRadius = 10.0 msgCount.layer.masksToBounds = true } } /// 公告数量 @IBOutlet weak var noticeCount: AnimatableLabel! { didSet { noticeCount.layer.cornerRadius = 10.0 noticeCount.layer.masksToBounds = true } } /// 分割线 @IBOutlet weak var line: UIImageView! /// 属性列表 @IBOutlet weak var tableView: UITableView! /// CellIdentifier let CellIdentifier = "Message_Cell" /// 公告消息 var notices: [MoNotice] = [] { didSet { /// 刷新列表数据 tableView.reloadData() /// 是否隐藏无数据按钮 noDataButton.isHidden = notices.count != 0 } } /// 消息类型 var msgType: Int = 2 // MARK: - 生命周期相关 required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) /// 设置TabBar图片 self.tabBarItem.image = UIImage(named: "message_normal")?.withRenderingMode(.alwaysOriginal) self.tabBarItem.selectedImage = UIImage(named: "message_selected")?.withRenderingMode(.alwaysOriginal) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. /// 不需要返回按钮 addBackBarButtonItem(true) /// UI界面初始化 buildView() /// 数据初始化 initData() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) /// 查询数据 self.tableView.triggerRefreshing() } // MARK: - 初始化 /// UI界面初始化 fileprivate func buildView() { /// Loding..... addLoadingView() /// 添加下拉刷新控件 self.tableView.gtm_addRefreshHeaderView(refreshHeader: DefaultGTMRefreshHeader()) { /// 重置请求条件 self.initData() } } /// 数据初始化 fileprivate func initData() { /// 查询公告消息 self.requestMsgCount() /// 查询公告消息 self.requestNotices(msgType) } // MARK: - 交互相关 @IBAction fileprivate func onButtonPressed(_ sender: UIControl) { switch sender { case message: /// 查询数据 msgType = 2 self.requestNotices(msgType) self.message.setTitleColor(UIColorFromHex(rgbValue: 0x3384F3), for: .normal) self.notice.setTitleColor(UIColorFromHex(rgbValue: 0x333333), for: .normal) default: /// 查询数据 msgType = 1 self.requestNotices(msgType) self.notice.setTitleColor(UIColorFromHex(rgbValue: 0x3384F3), for: .normal) self.message.setTitleColor(UIColorFromHex(rgbValue: 0x333333), for: .normal) } UIView.animate(withDuration: 0.3) { self.line.centerX = sender.centerX+10.0 } } // MARK: - 接口请求 /// 查询公告消息 fileprivate func requestNotices(_ msgType: Int = 1) { /// 当前未登录 guard let _ = MTP2BusinessCore.shared.accountManager?.loginRsp, let noticeManager = MTP2BusinessCore.shared.noticeManager else { /// 您还未登录,请登录后查看! self.noDataButton.set(image: nil, title: "您还未登录,请登录后查看!", titlePosition: .bottom, additionalSpacing: 0.0, state: .normal) self.noDataButton.isHidden = false return } /// 开启Loading self._anim?.startAnimating() /// 数据查询 noticeManager.requestNotices(nil, nil, msgType, false, nil) { (isComplete, error, notices) in DispatchQueue.main.async { /// stop self._anim?.stopAnimating() /// endRefreshing self.tableView.endRefreshing(isSuccess: true) if isComplete { self.notices = notices ?? [] } else { self.notices = [] WHToast.showError(withMessage: "查询失败,原因:\(error?.retMsg ?? "")", duration: 1.5, finishHandler: {}) } } } } /// 查询公告通知未读数量 fileprivate func requestMsgCount() { /// 当前未登录 guard let _ = MTP2BusinessCore.shared.accountManager?.loginRsp, let noticeManager = MTP2BusinessCore.shared.noticeManager else { return } /// 数据查询 noticeManager.requestNotices(nil, nil, nil, false, nil) { (isComplete, error, notices) in DispatchQueue.main.async { if isComplete, let msgs = notices, msgs.count != 0 { var msgCount = 0, noticeCount = 0 for (_, obj) in msgs.enumerated() { if !obj.readed { if obj.msgtype == 2 { msgCount += 1 } else { noticeCount += 1 } } } if msgCount != 0 { self.msgCount.text = "\(msgCount)" } if noticeCount != 0 { self.noticeCount.text = "\(noticeCount)" } self.msgCount.isHidden = msgCount == 0 self.noticeCount.isHidden = noticeCount == 0 } } } } // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. if segue.identifier == "ShowNoticeDetail" { /// 详情 (segue.destination as! MessageDetailViewController).model = sender as? MoNotice } } } // MARK: - UITableViewDelegate, UITableViewDataSource extension MessageViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return notices.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! MessageCell cell.model = notices[indexPath.row] return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.performSegue(withIdentifier: "ShowNoticeDetail", sender: notices[indexPath.row]) } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 90.0 } } // MARK: - PurchaseCell class MessageCell: BaseTableViewCell { /// 未读标识 @IBOutlet weak var read: AnimatableView! /// 发布时间 @IBOutlet weak var date: UILabel! /// 公告标题 @IBOutlet weak var title: UILabel! /// 公告详情 @IBOutlet weak var detail: UILabel! /// 公告图标 @IBOutlet weak var iconImage: UIImageView! override var model: MoNotice? { didSet { guard let obj = model else { return } /// 发布时间 date.text = DateUtils.getTDateString(obj.createtime, "yyyy-MM-dd HH:mm") /// 公告标题 title.text = obj.title /// 公告详情 detail.text = obj.content /// 未读标识 read.isHidden = obj.readed /// 公告图标 guard let url = StringUtils.getImageUrl(obj.msgiconurl) else { return } iconImage.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder_image"), options: .queryDiskDataSync, context: nil) } } }