TodoTaskViewController.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. //
  2. // TodoTaskViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 16/8/1.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Alamofire
  10. import AlamofireImage
  11. import AlamofireObjectMapper
  12. import ObjectMapper
  13. import CocoaLumberjack
  14. struct TaskURLGenenater {
  15. var url:String
  16. var pageModel:CommonPageModel
  17. func pagingURL() -> String {
  18. var tUrl = self.url
  19. tUrl = AppDelegate.o2Collect.setRequestParameter(tUrl, requestParameter:self.pageModel.toDictionary() as [String : AnyObject]?)!
  20. return tUrl.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!
  21. }
  22. }
  23. class TodoTaskViewController: UITableViewController {
  24. var segmentedControl:SegmentedControl!
  25. // var currentTaskURLGenenater:TaskURLGenenater!
  26. var models:[TodoCellModel<TodoTaskData>] = []
  27. // var filterModels:[TodoCellModel<TodoTaskData>] = []
  28. var emptyTexts:[String] = ["没有要处理的待办","没有要处理的待阅","没有已办数据","没有已阅数据"]
  29. // var urls:[Int:TaskURLGenenater] {
  30. // let todoTaskURL = AppDelegate.o2Collect.generateURLWithAppContextKey(TaskContext.taskContextKey, query: TaskContext.todoTaskListQuery, parameter: nil,coverted: false)
  31. //
  32. // let todoedTaskURL = AppDelegate.o2Collect.generateURLWithAppContextKey(TaskedContext.taskedContextKey, query: TaskedContext.taskedListByPageSizeQuery, parameter: nil,coverted: false)
  33. //
  34. // let readTaskURL = AppDelegate.o2Collect.generateURLWithAppContextKey(ReadContext.readContextKey, query: ReadContext.readListByPageSizeQuery, parameter: nil,coverted: false)
  35. //
  36. // let readedTaskURL = AppDelegate.o2Collect.generateURLWithAppContextKey(ReadedContext.readedContextKey, query: ReadedContext.readedListByPageSizeQuery, parameter: nil,coverted: false)
  37. //
  38. // return [0: TaskURLGenenater(url: todoTaskURL!,pageModel: CommonPageModel()),2: TaskURLGenenater(url: todoedTaskURL!,pageModel: CommonPageModel()),1 : TaskURLGenenater(url: readTaskURL!,pageModel: CommonPageModel()),3: TaskURLGenenater(url: readedTaskURL!,pageModel: CommonPageModel())]
  39. // }
  40. //
  41. //
  42. // //添加搜索功能
  43. // var fileterUrls:[Int:TaskURLGenenater] {
  44. // let todoTaskURL = AppDelegate.o2Collect.generateURLWithAppContextKey(TaskContext.taskContextKey, query: TaskContext.todoTaskListFilterQuery, parameter: nil,coverted: false)
  45. //
  46. // let todoedTaskURL = AppDelegate.o2Collect.generateURLWithAppContextKey(TaskedContext.taskedContextKey, query: TaskedContext.taskedListByPageSizeFilterQuery, parameter: nil,coverted: false)
  47. //
  48. // let readTaskURL = AppDelegate.o2Collect.generateURLWithAppContextKey(ReadContext.readContextKey, query: ReadContext.readListByPageSizeFilterQuery, parameter: nil,coverted: false)
  49. //
  50. // let readedTaskURL = AppDelegate.o2Collect.generateURLWithAppContextKey(ReadedContext.readedContextKey, query: ReadedContext.readedListByPageSizeFilterQuery, parameter: nil,coverted: false)
  51. //
  52. // return [0: TaskURLGenenater(url: todoTaskURL!,pageModel: CommonPageModel()),2: TaskURLGenenater(url: todoedTaskURL!,pageModel: CommonPageModel()),1 : TaskURLGenenater(url: readTaskURL!,pageModel: CommonPageModel()),3: TaskURLGenenater(url: readedTaskURL!,pageModel: CommonPageModel())]
  53. // }
  54. //搜索文本
  55. var searchText = ""
  56. //搜索控件
  57. var searchController:UISearchController = UISearchController(searchResultsController: nil)
  58. //分页查询 最后一条数据的id
  59. var lastId = O2.O2_First_ID
  60. // 下拉刷新还是上拉加载
  61. var isRefresh = false
  62. // 防止重复刷新
  63. var isloading = false
  64. private lazy var viewModel: WorkViewModel = {
  65. return WorkViewModel()
  66. }()
  67. override func viewWillAppear(_ animated: Bool) {
  68. super.viewWillAppear(animated)
  69. self.title = ""
  70. }
  71. override func viewWillDisappear(_ animated: Bool) {
  72. super.viewWillDisappear(animated)
  73. }
  74. override func viewDidLoad() {
  75. super.viewDidLoad()
  76. self.initSegmentedControl()
  77. //添加搜索功能
  78. self.searchController.searchResultsUpdater = self
  79. self.searchController.delegate = self
  80. self.searchController.dimsBackgroundDuringPresentation = false
  81. self.searchController.hidesNavigationBarDuringPresentation = false
  82. definesPresentationContext = true
  83. UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "取消"
  84. let attrs = [NSAttributedString.Key.font: UIFont.init(name: "PingFangTC-Light", size: 14) ?? UIFont.systemFont(ofSize: 14),
  85. NSAttributedString.Key.foregroundColor: O2ThemeManager.color(for: "Base.base_color") ?? UIColor.red]
  86. UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attrs, for: .normal)
  87. self.searchController.searchBar.searchBarStyle = UISearchBar.Style.minimal
  88. self.searchController.searchBar.sizeToFit()
  89. //设置搜索框是否显示
  90. self.tableView.tableHeaderView = self.searchController.searchBar
  91. self.searchController.isActive = true
  92. // self.setSearchBarIsShow()
  93. //分页刷新功能
  94. self.tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
  95. self.headerLoadData()
  96. })
  97. self.tableView.mj_footer = MJRefreshAutoFooter(refreshingBlock: {
  98. self.footerLoadData()
  99. })
  100. self.headerLoadData()
  101. }
  102. //隐藏搜索框
  103. func setSearchBarIsShow(){
  104. // let taskIndex = AppConfigSettings.shared.taskIndex
  105. // if taskIndex == 2 {
  106. // self.tableView.tableHeaderView = self.searchController.searchBar
  107. // }else{
  108. // if self.searchController.isActive {
  109. // self.searchController.isActive = false
  110. // }
  111. // self.tableView.tableHeaderView = nil
  112. // }
  113. }
  114. /// 刷新数据
  115. func headerLoadData(){
  116. DDLogDebug("加载数据。。。。。。。。。")
  117. if self.isloading {
  118. DDLogDebug("正在加载中....")
  119. return
  120. }
  121. self.isloading = true
  122. let taskIndex = AppConfigSettings.shared.taskIndex
  123. self.isRefresh = true
  124. self.lastId = O2.O2_First_ID
  125. self.loadDataList(taskIndex: taskIndex)
  126. }
  127. /// 加载数据
  128. func footerLoadData(){
  129. DDLogDebug("获取更多数据。。。。。。。。。。。。。。")
  130. if self.isloading {
  131. DDLogDebug("正在加载中....")
  132. return
  133. }
  134. self.isloading = true
  135. let taskIndex = AppConfigSettings.shared.taskIndex
  136. self.loadDataList(taskIndex: taskIndex)
  137. }
  138. private func loadDataList(taskIndex: Int) {
  139. let tv = self.tableView as! ZLBaseTableView
  140. tv.emptyTitle = self.emptyTexts[taskIndex]
  141. switch taskIndex {
  142. case 0:
  143. self.viewModel.taskListNext(lastId: self.lastId, key: self.searchText).then { (list) in
  144. if self.isRefresh {
  145. self.models.removeAll()
  146. }
  147. if (list.count > 0) {
  148. self.lastId = list[list.count-1].sourceObj?.id ?? O2.O2_First_ID
  149. for item in list {
  150. self.models.append(item)
  151. }
  152. }
  153. self.isRefresh = false
  154. self.tableView.reloadData()
  155. }.always {
  156. self.isloading = false
  157. if tv.mj_header.isRefreshing(){
  158. tv.mj_header.endRefreshing()
  159. }
  160. }.catch { (err) in
  161. DispatchQueue.main.async {
  162. DDLogError(err.localizedDescription)
  163. self.showError(title: "查询失败!")
  164. }
  165. }
  166. break
  167. case 1:
  168. self.viewModel.readListNext(lastId: self.lastId, key: self.searchText).then { (list) in
  169. if self.isRefresh {
  170. self.models.removeAll()
  171. }
  172. if (list.count > 0) {
  173. self.lastId = list[list.count-1].sourceObj?.id ?? O2.O2_First_ID
  174. for item in list {
  175. self.models.append(item)
  176. }
  177. }
  178. self.isRefresh = false
  179. self.tableView.reloadData()
  180. }.always {
  181. self.isloading = false
  182. if tv.mj_header.isRefreshing(){
  183. tv.mj_header.endRefreshing()
  184. }
  185. }.catch { (err) in
  186. DispatchQueue.main.async {
  187. DDLogError(err.localizedDescription)
  188. self.showError(title: "查询失败!")
  189. }
  190. }
  191. break
  192. case 2:
  193. self.viewModel.taskcompletedListNext(lastId: self.lastId, key: self.searchText).then { (list) in
  194. if self.isRefresh {
  195. self.models.removeAll()
  196. }
  197. if (list.count > 0) {
  198. self.lastId = list[list.count-1].sourceObj?.id ?? O2.O2_First_ID
  199. for item in list {
  200. self.models.append(item)
  201. }
  202. }
  203. self.isRefresh = false
  204. self.tableView.reloadData()
  205. }.always {
  206. self.isloading = false
  207. if tv.mj_header.isRefreshing(){
  208. tv.mj_header.endRefreshing()
  209. }
  210. }.catch { (err) in
  211. DispatchQueue.main.async {
  212. DDLogError(err.localizedDescription)
  213. self.showError(title: "查询失败!")
  214. }
  215. }
  216. break
  217. case 3:
  218. self.viewModel.readcompletedListNext(lastId: self.lastId, key: self.searchText).then { (list) in
  219. if self.isRefresh {
  220. self.models.removeAll()
  221. }
  222. if (list.count > 0) {
  223. self.lastId = list[list.count-1].sourceObj?.id ?? O2.O2_First_ID
  224. for item in list {
  225. self.models.append(item)
  226. }
  227. }
  228. self.isRefresh = false
  229. self.tableView.reloadData()
  230. }.always {
  231. self.isloading = false
  232. if tv.mj_header.isRefreshing(){
  233. tv.mj_header.endRefreshing()
  234. }
  235. }.catch { (err) in
  236. DispatchQueue.main.async {
  237. DDLogError(err.localizedDescription)
  238. self.showError(title: "查询失败!")
  239. }
  240. }
  241. break
  242. default:
  243. break
  244. }
  245. }
  246. //初始化控件
  247. func initSegmentedControl(){
  248. let titleStrings = ["待办", "待阅", "已办", "已阅"]
  249. let titles: [NSAttributedString] = {
  250. let attributes = [NSAttributedString.Key.font: UIFont(name: "PingFangSC-Regular", size: 17.0)!
  251. , NSAttributedString.Key.foregroundColor: UIColor.white]
  252. var titles = [NSAttributedString]()
  253. for titleString in titleStrings {
  254. let title = NSAttributedString(string: titleString, attributes: attributes)
  255. titles.append(title)
  256. }
  257. return titles
  258. }()
  259. let selectedTitles: [NSAttributedString] = {
  260. let attributes = [NSAttributedString.Key.font: UIFont(name: "PingFangSC-Regular", size: 17.0)!, NSAttributedString.Key.foregroundColor: UIColor.darkGray]
  261. var selectedTitles = [NSAttributedString]()
  262. for titleString in titleStrings {
  263. let selectedTitle = NSAttributedString(string: titleString, attributes: attributes)
  264. selectedTitles.append(selectedTitle)
  265. }
  266. return selectedTitles
  267. }()
  268. segmentedControl = SegmentedControl.initWithTitles(titles, selectedTitles: selectedTitles)
  269. segmentedControl.delegate = self
  270. segmentedControl.backgroundColor = UIColor.clear
  271. segmentedControl.selectionBoxColor = UIColor.white
  272. segmentedControl.selectionBoxStyle = .default
  273. segmentedControl.selectionBoxCornerRadius = 15
  274. segmentedControl.frame.size = CGSize(width: 70 * titles.count, height: 30)
  275. segmentedControl.isLongPressEnabled = true
  276. segmentedControl.isUnselectedSegmentsLongPressEnabled = true
  277. segmentedControl.longPressMinimumPressDuration = 1
  278. navigationItem.titleView = segmentedControl
  279. segmentedControl.setSelected(at: AppConfigSettings.shared.taskIndex, animated: true)
  280. }
  281. // MARK: - Table view data source
  282. override func numberOfSections(in tableView: UITableView) -> Int {
  283. return 1
  284. }
  285. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  286. return self.models.count
  287. }
  288. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  289. let cell = tableView.dequeueReusableCell(withIdentifier: "TodoTaskTableViewCell", for: indexPath) as! TodoTaskTableViewCell
  290. let model = self.models[safe: indexPath.row]
  291. if model == nil {
  292. return UITableViewCell()
  293. }
  294. cell.setData(cellModel: model!)
  295. return cell
  296. }
  297. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  298. let todoTask:TodoTaskData? = self.models[indexPath.row].sourceObj
  299. //根据不同的类型跳转显示
  300. switch self.segmentedControl.selectedIndex {
  301. case 0:
  302. self.performSegue(withIdentifier: "showTodoDetailSegue", sender: todoTask)
  303. case 1:
  304. self.performSegue(withIdentifier: "showTodoDetailSegue", sender: todoTask)
  305. case 2:
  306. self.performSegue(withIdentifier: "showTodoedDetailSegue", sender: todoTask)
  307. case 3:
  308. self.performSegue(withIdentifier: "showTodoDetailSegue", sender: todoTask)
  309. default:
  310. DDLogDebug("no click")
  311. }
  312. }
  313. override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  314. return UIView()
  315. }
  316. //prepare
  317. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  318. if segue.identifier == "showTodoDetailSegue" {
  319. let destVC = segue.destination as! TodoTaskDetailViewController
  320. destVC.todoData = sender as? TodoTaskData
  321. destVC.backFlag = 2 //返回标志
  322. }else if segue.identifier == "showTodoedDetailSegue" {
  323. let destVC = segue.destination as! TodoedTaskViewController
  324. destVC.todoTask = sender as? TodoTaskData
  325. }else if segue.identifier == "toSignature" {
  326. DDLogDebug("签名去了。。。。。。。。。")
  327. }
  328. }
  329. //backWindtodoTask
  330. @IBAction func unWindForTodoTask(_ segue:UIStoryboardSegue){
  331. DDLogDebug(segue.identifier!)
  332. self.tableView.mj_header.beginRefreshing()
  333. }
  334. }
  335. //searcher update
  336. extension TodoTaskViewController:UISearchResultsUpdating,UISearchControllerDelegate{
  337. func willPresentSearchController(_ searchController: UISearchController) {
  338. DDLogDebug("willPresentSearchController 1, searchController.isActive = \(searchController.isActive)")
  339. self.models.removeAll()
  340. self.tableView.reloadData()
  341. }
  342. func didPresentSearchController(_ searchController: UISearchController) {
  343. DDLogDebug(" didPresentSearchController 2, searchController.isActive = \(searchController.isActive)")
  344. }
  345. func willDismissSearchController(_ searchController: UISearchController) {
  346. DDLogDebug(" didPresentSearchController 3, searchController.isActive = \(searchController.isActive)")
  347. }
  348. func didDismissSearchController(_ searchController: UISearchController) {
  349. DDLogDebug(" didDismissSearchController 4, searchController.isActive = \(searchController.isActive)")
  350. //self.tableView.mj_header.beginRefreshing()
  351. }
  352. func updateSearchResults(for searchController: UISearchController) {
  353. DDLogDebug("updateSearchResults........")
  354. if let sText = searchController.searchBar.text {
  355. self.searchText = sText
  356. self.headerLoadData()
  357. }else {
  358. DDLogDebug("search text is nil ..................")
  359. }
  360. }
  361. }
  362. extension TodoTaskViewController:SegmentedControlDelegate{
  363. func segmentedControl(_ segmentedControl: SegmentedControl, didSelectIndex selectedIndex: Int) {
  364. DDLogDebug("click \(selectedIndex)")
  365. AppConfigSettings.shared.taskIndex = selectedIndex
  366. self.setSearchBarIsShow()
  367. self.headerLoadData()
  368. //self.tableView.mj_header.beginRefreshing()
  369. }
  370. func segmentedControl(_ segmentedControl: SegmentedControl, didLongPressIndex longPressIndex: Int) {
  371. }
  372. }