O2SearchController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // O2SearchController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2021/5/21.
  6. // Copyright © 2021 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. class O2SearchController: UIViewController {
  11. private lazy var viewmodel: O2SearchViewModel = {
  12. return O2SearchViewModel()
  13. }()
  14. /// 查询结果 ListView
  15. fileprivate lazy var tableview: UITableView = {
  16. var tableview = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.width, height: self.view.height - TAB_BAR_HEIGHT))
  17. tableview.delegate = self
  18. tableview.dataSource = self
  19. tableview.backgroundColor = UIColor(hex: "#F5F5F5")
  20. tableview.register(UINib(nibName: "O2SearchResultCell", bundle: nil), forCellReuseIdentifier: "O2SearchResultCell")
  21. tableview.separatorStyle = .none
  22. return tableview
  23. }()
  24. /// 空数据的View
  25. fileprivate lazy var emptyView: UIView = {
  26. let view = UIView(frame: CGRect(x: 0, y: 0, width: self.view.width, height: self.view.height - TAB_BAR_HEIGHT))
  27. view.backgroundColor = UIColor(hex: "#F5F5F5")
  28. let wuImage = UIImageView(frame: CGRect(x: 0, y: 0, width: 260, height: 163.5))
  29. wuImage.image = UIImage(named: "pic_wu")
  30. view.addSubview(wuImage)
  31. wuImage.snp.makeConstraints { (maker) in
  32. maker.center.equalToSuperview()
  33. }
  34. let tips = UILabel()
  35. tips.text = L10n.Search.searchNoResult
  36. tips.textColor = UIColor(hex: "#B3B3B3")
  37. tips.sizeToFit()
  38. view.addSubview(tips)
  39. tips.snp.makeConstraints { (maker) in
  40. maker.top.equalTo(wuImage.snp.bottom).offset(20)
  41. maker.centerX.equalToSuperview()
  42. }
  43. return view
  44. }()
  45. /// 历史查询关键字
  46. fileprivate lazy var historyView: O2SearchHistoryView = {
  47. let view = O2SearchHistoryView(frame: CGRect(x: 0, y: 0, width: self.view.width, height: self.view.height - TAB_BAR_HEIGHT))
  48. return view
  49. }()
  50. // 搜索框
  51. private var searchBar: UISearchBar?
  52. private var searchKey: String = ""
  53. private var resultList: [O2SearchEntry] = []
  54. private var page = 1
  55. private var totalPage = 1
  56. override func viewDidLoad() {
  57. super.viewDidLoad()
  58. self.title = L10n.Search.search
  59. self.view.backgroundColor = UIColor(hex: "#F5F5F5")
  60. view.addSubview(tableview)
  61. //分页刷新功能
  62. self.tableview.mj_header = MJRefreshNormalHeader(refreshingBlock: {
  63. self.search()
  64. })
  65. self.tableview.mj_footer = MJRefreshAutoFooter(refreshingBlock: {
  66. self.morePage()
  67. })
  68. view.addSubview(emptyView)
  69. emptyView.isHidden = true
  70. view.addSubview(historyView)
  71. historyView.delegate = self
  72. self.searchBarInit()
  73. // 获取焦点
  74. self.searchBar?.becomeFirstResponder()
  75. }
  76. /// 初始化 搜索框
  77. private func searchBarInit() {
  78. let view = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 28))
  79. view.backgroundColor = self.navigationController?.navigationBar.tintColor
  80. view.layer.cornerRadius = 14
  81. view.layer.masksToBounds = true
  82. self.searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: 250, height: 28))
  83. self.searchBar?.placeholder = L10n.Search.placeholder
  84. self.searchBar?.layer.cornerRadius = 14
  85. self.searchBar?.layer.masksToBounds = true
  86. self.searchBar?.backgroundColor = .white
  87. self.searchBar?.delegate = self
  88. self.searchBar?.setImage(O2ThemeManager.image(for: "Icon.icon_sousuo"), for: .search, state: .normal)
  89. view.addSubview(self.searchBar!)
  90. self.navigationItem.titleView = view
  91. }
  92. /// 查询数据
  93. private func search() {
  94. DDLogDebug("search key \(self.searchKey)")
  95. if self.searchKey != "" {
  96. self.showLoading()
  97. self.viewmodel.search(key: self.searchKey).then { (page) in
  98. self.resultList = page.list
  99. self.page = 1
  100. self.totalPage = page.totalPage
  101. self.refreshUI()
  102. }.always {
  103. self.hideLoading()
  104. }.catch { (err) in
  105. DDLogError(err.localizedDescription)
  106. self.showError(title: L10n.errorWithMsg(err.localizedDescription))
  107. }
  108. }
  109. }
  110. private func morePage() {
  111. DDLogDebug("morePage page \(self.page) total \(self.totalPage)")
  112. if self.page < self.totalPage {
  113. self.showLoading()
  114. self.viewmodel.nextPage().then { (page) in
  115. page.list.forEach { (entry) in
  116. self.resultList.append(entry)
  117. }
  118. self.page = page.page
  119. self.totalPage = page.totalPage
  120. self.refreshUI()
  121. }.always {
  122. self.hideLoading()
  123. }.catch { (err) in
  124. DDLogError(err.localizedDescription)
  125. self.showError(title: L10n.errorWithMsg(err.localizedDescription))
  126. }
  127. }
  128. }
  129. // 刷新页面
  130. private func refreshUI() {
  131. DDLogDebug("refreshUI page \(self.page) total \(self.totalPage)")
  132. self.tableview.reloadData()
  133. if self.resultList.count > 0 {
  134. self.emptyView.isHidden = true
  135. self.tableview.isHidden = false
  136. } else {
  137. self.tableview.isHidden = true
  138. self.emptyView.isHidden = false
  139. }
  140. }
  141. }
  142. extension O2SearchController: UISearchBarDelegate {
  143. /// 点击输入法搜索按钮
  144. func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
  145. let key = searchBar.text
  146. DDLogDebug("搜索关键字: \(key ?? "无")")
  147. if key != nil && key != "" {
  148. self.historyView.addSearchHistory(key: key!)
  149. }
  150. self.historyView.isHidden = true // 隐藏搜索历史
  151. searchBar.endEditing(true)
  152. self.searchKey = key ?? ""
  153. self.search()
  154. }
  155. func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  156. if searchText.count == 0 { // 清空了
  157. self.resultList = []
  158. self.searchKey = ""
  159. self.page = 1
  160. self.totalPage = 1
  161. self.tableview.reloadData()
  162. self.historyView.isHidden = false
  163. self.tableview.isHidden = true
  164. }
  165. }
  166. }
  167. extension O2SearchController: O2SearchHistoryDelegate {
  168. /// 点击搜索历史
  169. func clickToSearchTag(tag: String) {
  170. DDLogDebug("查询tag: \(tag)")
  171. self.historyView.isHidden = true // 隐藏搜索历史
  172. self.searchBar?.text = tag
  173. self.searchBar?.endEditing(true)
  174. self.searchKey = tag
  175. self.search()
  176. }
  177. }
  178. extension O2SearchController: UITableViewDelegate, UITableViewDataSource {
  179. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  180. return self.resultList.count
  181. }
  182. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  183. return 190
  184. }
  185. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  186. if let cell = tableview.dequeueReusableCell(withIdentifier: "O2SearchResultCell") as? O2SearchResultCell {
  187. cell.setData(data: self.resultList[indexPath.row], currentKey: self.searchKey)
  188. return cell
  189. }
  190. return UITableViewCell()
  191. }
  192. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  193. tableview.deselectRow(at: indexPath, animated: false)
  194. let entry = self.resultList[indexPath.row]
  195. if entry.type == "cms" {
  196. self.openCmsPage(id: entry.reference ?? "", title: entry.title ?? "")
  197. } else {
  198. self.openWorkPage(work: entry.reference ?? "", title: entry.title ?? "")
  199. }
  200. }
  201. /// 打开文档
  202. private func openCmsPage(id: String, title: String) {
  203. let bbsStoryboard = UIStoryboard(name: "information", bundle: Bundle.main)
  204. let destVC = bbsStoryboard.instantiateViewController(withIdentifier: "CMSSubjectDetailVC") as! CMSItemDetailViewController
  205. destVC.documentId = id
  206. destVC.title = title
  207. destVC.modalPresentationStyle = .fullScreen
  208. self.navigationController?.pushViewController(destVC, animated: false)
  209. }
  210. /// 打开工作
  211. private func openWorkPage(work: String, title: String) {
  212. if work.isEmpty {
  213. DDLogError("没有传入work id")
  214. self.showError(title: "参数不正确!")
  215. return
  216. }
  217. let storyBoard = UIStoryboard(name: "task", bundle: nil)
  218. let destVC = storyBoard.instantiateViewController(withIdentifier: "todoTaskDetailVC") as! TodoTaskDetailViewController
  219. let json = """
  220. {"work":"\(work)", "workCompleted":"", "title":"\(title)"}
  221. """
  222. let todo = TodoTask(JSONString: json)
  223. destVC.todoTask = todo
  224. destVC.backFlag = 3 //隐藏就行
  225. self.show(destVC, sender: nil)
  226. }
  227. /// Cell 圆角背景计算
  228. func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  229. //圆率
  230. let cornerRadius:CGFloat = 10.0
  231. //大小
  232. let bounds:CGRect = cell.bounds
  233. //绘制曲线
  234. // var bezierPath: UIBezierPath? = nil
  235. //一个为一组时,四个角都为圆角
  236. let bezierPath: UIBezierPath? = UIBezierPath(roundedRect: bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
  237. //cell的背景色透明
  238. cell.backgroundColor = .clear
  239. //新建一个图层
  240. let layer = CAShapeLayer()
  241. //图层边框路径
  242. layer.path = bezierPath?.cgPath
  243. //图层填充色,也就是cell的底色
  244. layer.fillColor = UIColor.white.cgColor
  245. //图层边框线条颜色
  246. /*
  247. 如果self.tableView.style = UITableViewStyleGrouped时,每一组的首尾都会有一根分割线,目前我还没找到去掉每组首尾分割线,保留cell分割线的办法。
  248. 所以这里取巧,用带颜色的图层边框替代分割线。
  249. 这里为了美观,最好设为和tableView的底色一致。
  250. 设为透明,好像不起作用。
  251. */
  252. layer.strokeColor = UIColor.white.cgColor
  253. //将图层添加到cell的图层中,并插到最底层
  254. cell.layer.insertSublayer(layer, at: 0)
  255. }
  256. }