OOPersonListViewModel.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // OOPersonListViewModel.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2018/4/24.
  6. // Copyright © 2018年 zoneland. All rights reserved.
  7. //
  8. import Foundation
  9. import Moya
  10. class OOPersonListViewModel: NSObject {
  11. //HTTP API
  12. private let ooContactAPI = OOMoyaProvider<OOContactAPI>()
  13. //所有人员列表
  14. private var allPersons:[OOPersonModel] = []
  15. //搜索所有人员列表
  16. private var filterPersons:[OOPersonModel] = []
  17. //回调块类型定义
  18. typealias CallbackBlockDefine = (_ parameter:CommonPageModel,_ msg:String?) -> Void
  19. //回调块定义
  20. var callbackExecutor:CallbackBlockDefine?
  21. //搜索回调块定义
  22. typealias SearchBackBlockDefine = (_ msg:String?) -> Void
  23. var searchBackExecutor:SearchBackBlockDefine?
  24. var isSearchActive = false
  25. override init() {
  26. super.init()
  27. }
  28. }
  29. extension OOPersonListViewModel {
  30. func getAllPerson(_ isNextPage:Bool,_ parameters:CommonPageModel,callbackCompleted:@escaping CallbackBlockDefine){
  31. var currentParameters = parameters
  32. if !isNextPage {
  33. allPersons.removeAll()
  34. }
  35. ooContactAPI.request(.personListNext(parameters.nextPageId, parameters.pageSize )) { (result) in
  36. let myResult = OOResult<BaseModelClass<[OOPersonModel]>>(result)
  37. if myResult.isResultSuccess() {
  38. myResult.model?.data?.forEach({ (uModel) in
  39. self.allPersons.append(uModel)
  40. })
  41. currentParameters.setPageTotal((myResult.model?.count)!)
  42. currentParameters.nextPageId = (self.allPersons.last?.id)!
  43. }
  44. if myResult.isResultSuccess() {
  45. callbackCompleted(currentParameters,nil)
  46. }else{
  47. callbackCompleted(currentParameters,myResult.error.debugDescription)
  48. }
  49. }
  50. }
  51. // MARK:- 搜索用户按名字
  52. func filterPerson(_ filter:String,callbackCompleted:@escaping SearchBackBlockDefine){
  53. if filter.isEmpty {
  54. return
  55. }
  56. ooContactAPI.request(.personLike(filter)) { (result) in
  57. self.filterPersons.removeAll()
  58. let myResult = OOResult<BaseModelClass<[OOPersonModel]>>(result)
  59. if myResult.isResultSuccess() {
  60. if let model = myResult.model?.data {
  61. model.forEach({ (pModel) in
  62. self.filterPersons.append(pModel)
  63. })
  64. }
  65. }
  66. if myResult.isResultSuccess() {
  67. callbackCompleted(nil)
  68. }else{
  69. callbackCompleted(myResult.error.debugDescription)
  70. }
  71. }
  72. }
  73. // MARK: - 获取icon
  74. func getIconOfPerson(_ person:OOPersonModel,compeletionBlock:@escaping (_ image:UIImage?,_ errMsg:String?) -> Void) {
  75. ooContactAPI.request(.iconByPerson(person.id!)) { (result) in
  76. switch result {
  77. case .success(let res):
  78. guard let image = UIImage(data: res.data) else {
  79. compeletionBlock(#imageLiteral(resourceName: "icon_?"),"image transform error")
  80. return
  81. }
  82. compeletionBlock(image,nil)
  83. break
  84. case .failure(let err):
  85. compeletionBlock(#imageLiteral(resourceName: "icon_?"),err.errorDescription)
  86. break
  87. }
  88. }
  89. }
  90. }
  91. // MARK:- UITableView DataSource
  92. extension OOPersonListViewModel{
  93. func numberOfSections() -> Int {
  94. return 2
  95. }
  96. func numberOfRowsInSection(_ section: Int) -> Int {
  97. if section == 0 {
  98. return 1
  99. }else if section == 1 {
  100. if isSearchActive == true {
  101. return filterPersons.count
  102. }
  103. return allPersons.count
  104. }
  105. return 0
  106. }
  107. func nodeForIndexPath(_ indexPath:IndexPath) -> OOPersonModel? {
  108. if indexPath.section == 1 {
  109. if isSearchActive == true {
  110. return filterPersons[indexPath.row]
  111. }
  112. return allPersons[indexPath.row]
  113. }
  114. return nil
  115. }
  116. }