123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- import UIKit
- import RxSwift
- import RxCocoa
- import RxDataSources
- protocol MessageViewControllerDelegate: class {
- func didSelect(item: MessageItem)
- }
- final class MessageViewController: UIViewController {
-
- @IBOutlet var tableView: UITableView!
- var emptyView : UILabel = {
- let empty = UILabel(frame: CGRect(x: kScreenWidth / 2 - 50, y: 74, width: 50, height: 50))
-
- empty.isHidden = true
- empty.text = "当前没有任何消息"
- empty.textColor = UIColor.gray
- empty.font = UIFont.systemFont(ofSize: 14)
-
- empty.sizeToFit()
- return empty
- }()
-
- var messageViewModel: MessageViewModel? = nil
- let disposeBag = DisposeBag()
-
- weak var delegate: MessageViewControllerDelegate?
-
- var type = MessageType.zan {
- willSet {
- messageViewModel = MessageViewModel(type: newValue)
- }
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
- configureRxDataSource()
- messageViewModel?.reload(isRefresh: true)
- view.addSubview(emptyView)
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(true)
- navigationItem.setRightBars(buttonSpace: 0,
- images: [UIImage(named: "更多-右上角-1")],
- actions: [#selector(clearMessages)],
- target: self)
- }
- var dataSource: RxTableViewSectionedAnimatedDataSource<AnimatableSectionModel<Int, MessageItem>> {
- return RxTableViewSectionedAnimatedDataSource<AnimatableSectionModel<Int, MessageItem>>(configureCell: { (dataSource, tableView, indexPath, item) in
- switch self.type {
- case .system:
- let cell = tableView.dequeueReusableCell(withIdentifier: "messageCell", for: indexPath) as! MessageCell
- cell.setInfo(item)
- return cell
- case .comment, .zan:
- let cell = tableView.dequeueReusableCell(withIdentifier: "commentCell", for: indexPath) as! CommentCell
- cell.setInfo(item, type: self.type)
- return cell
- }
- }, canEditRowAtIndexPath: { _, _ in true })
- }
-
- func configureRxDataSource() {
-
- messageViewModel?.hasData
- .bind(to: emptyView.rx.isHidden)
- .disposed(by: disposeBag)
-
- messageViewModel?.messageContents
- .bind(to: tableView.rx.items(dataSource: dataSource))
- .disposed(by: disposeBag)
-
- tableView.rx.modelDeleted(MessageItem.self)
- .subscribe(onNext: { [unowned self] in self.messageViewModel?.remove($0)})
- .disposed(by: disposeBag)
-
- tableView.rx.modelSelected(MessageItem.self)
- .subscribe(onNext: { [unowned self] in self.delegate?.didSelect(item: $0) })
- .disposed(by: disposeBag)
-
- tableView.rx.willDisplayCell
- .subscribe({ [unowned self] in self.messageViewModel?.preload(indexPath: $0.element?.indexPath)})
- .disposed(by: disposeBag)
- }
-
- @objc func clearMessages() {
- let alert = FFAlertController(title: "", message: "", alertStyle: .actionSheet)
- alert.addAlertAction(alertAction: DestructiveAlertAction(title: "清空消息", handler: { (alertAction) in
- self.messageViewModel?.removeAll()
- }))
- alert.addAlertAction(alertAction: FFAlertAction(title: "取消", handler: nil))
- presentController(alert)
- }
- }
|