GroupFilterViewController.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // GroupFilterViewController.swift
  3. // Bark
  4. //
  5. // Created by huangfeng on 2021/6/8.
  6. // Copyright © 2021 Fin. All rights reserved.
  7. //
  8. import Material
  9. import MJRefresh
  10. import RealmSwift
  11. import RxCocoa
  12. import RxDataSources
  13. import RxSwift
  14. import UIKit
  15. class GroupFilterViewController: BaseViewController<GroupFilterViewModel> {
  16. let doneButton: BKButton = {
  17. let btn = BKButton()
  18. btn.setTitle(NSLocalizedString("done"), for: .normal)
  19. btn.setTitleColor(BKColor.lightBlue.darken3, for: .normal)
  20. btn.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
  21. btn.fontSize = 14
  22. return btn
  23. }()
  24. let showAllGroupsButton: BKButton = {
  25. let btn = BKButton()
  26. btn.setTitle(NSLocalizedString("hideAllGroups"), for: .selected)
  27. btn.setTitle(NSLocalizedString("showAllGroups"), for: .normal)
  28. btn.setTitleColor(Color.lightBlue.darken3, for: .normal)
  29. btn.fontSize = 14
  30. return btn
  31. }()
  32. let tableView: UITableView = {
  33. let tableView = UITableView(frame: CGRect.zero, style: .insetGrouped)
  34. tableView.separatorStyle = .singleLine
  35. tableView.separatorColor = BKColor.grey.lighten3
  36. tableView.backgroundColor = BKColor.background.primary
  37. tableView.register(GroupTableViewCell.self, forCellReuseIdentifier: "\(GroupTableViewCell.self)")
  38. return tableView
  39. }()
  40. override func makeUI() {
  41. self.title = NSLocalizedString("group")
  42. self.navigationItem.setRightBarButtonItem(item: UIBarButtonItem(customView: doneButton))
  43. self.view.addSubview(tableView)
  44. self.view.addSubview(showAllGroupsButton)
  45. tableView.snp.makeConstraints { make in
  46. make.top.equalToSuperview()
  47. make.bottom.equalToSuperview().offset((kSafeAreaInsets.bottom + 40) * -1)
  48. make.left.right.equalToSuperview()
  49. }
  50. showAllGroupsButton.snp.makeConstraints { make in
  51. make.centerX.equalToSuperview()
  52. make.height.equalTo(40)
  53. make.bottom.equalToSuperview().offset(-kSafeAreaInsets.bottom)
  54. }
  55. self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 20))
  56. }
  57. override func bindViewModel() {
  58. let output = viewModel.transform(
  59. input: GroupFilterViewModel.Input(
  60. showAllGroups: self.showAllGroupsButton.rx
  61. .tap
  62. .compactMap { [weak self] in
  63. guard let strongSelf = self else { return nil }
  64. return !strongSelf.showAllGroupsButton.isSelected
  65. }
  66. .asDriver(onErrorDriveWith: .empty()),
  67. doneTap: self.doneButton.rx.tap.asDriver()
  68. ))
  69. let dataSource = RxTableViewSectionedReloadDataSource<SectionModel<String, GroupCellViewModel>> { _, tableView, _, item -> UITableViewCell in
  70. guard let cell = tableView.dequeueReusableCell(withIdentifier: "\(GroupTableViewCell.self)") as? GroupTableViewCell else {
  71. return UITableViewCell()
  72. }
  73. cell.bindViewModel(model: item)
  74. return cell
  75. }
  76. output.groups
  77. .drive(tableView.rx.items(dataSource: dataSource))
  78. .disposed(by: rx.disposeBag)
  79. output.isShowAllGroups
  80. .drive(self.showAllGroupsButton.rx.isSelected)
  81. .disposed(by: rx.disposeBag)
  82. output.dismiss.drive(onNext: { [weak self] in
  83. self?.dismiss(animated: true, completion: nil)
  84. })
  85. .disposed(by: rx.disposeBag)
  86. }
  87. }
  88. extension GroupFilterViewController: UITableViewDelegate {}