123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- //
- // OOContactSearchViewModel.swift
- // o2app
- //
- // Created by 刘振兴 on 2017/11/28.
- // Copyright © 2017年 zone. All rights reserved.
- //
- import UIKit
- class OOContactSearchViewModel: NSObject {
-
- //搜索结果数据
- private var searchContactDatas:[OOContacSearchSectionHeaderType:[DataModel]] = [:]
-
- private let contactAPI = OOMoyaProvider<OOContactAPI>()
-
- typealias AddBlock = (_ msg:String?) -> Void
-
- var updateBlock:AddBlock?
-
- override init() {
- super.init()
- initData()
- }
-
- func initData(){
- searchContactDatas[.unit] = []
- searchContactDatas[.person] = []
- searchContactDatas[.group] = []
- }
- }
- // MARK:- 搜索
- extension OOContactSearchViewModel{
- func searchRefreshData(_ searchText:String){
- //搜索组织
- //搜索人员
- //组织群组
- //self.initData()
- contactAPI.request(.unitLike(searchText)) { (result) in
- let myResult = OOResult<BaseModelClass<[OOUnitModel]>>(result)
- self.searchContactDatas[.unit] = []
- if myResult.isResultSuccess() {
- if let model = myResult.model?.data {
- model.forEach({ (uModel) in
- self.searchContactDatas[.unit]?.append(uModel)
- })
- }
- }
- guard let block = self.updateBlock else {
- return
- }
- if myResult.isResultSuccess() {
- block(nil)
- }else{
- block(myResult.error.debugDescription)
- }
- }
-
- contactAPI.request(.personLike(searchText)) { (result) in
- let myResult = OOResult<BaseModelClass<[OOPersonModel]>>(result)
- self.searchContactDatas[.person] = []
- if myResult.isResultSuccess() {
- if let model = myResult.model?.data {
- model.forEach({ (pModel) in
- self.searchContactDatas[.person]?.append(pModel)
- })
- }
- }
- guard let block = self.updateBlock else {
- return
- }
- if myResult.isResultSuccess() {
- block(nil)
- }else{
- block(myResult.error.debugDescription)
- }
- }
-
- contactAPI.request(.groupLike(searchText)) { (result) in
- let myResult = OOResult<BaseModelClass<[OOGroupModel]>>(result)
- self.searchContactDatas[.group] = []
- if myResult.isResultSuccess() {
- if let model = myResult.model?.data {
- model.forEach({ (gModel) in
- self.searchContactDatas[.group]?.append(gModel)
- })
- }
- }
- guard let block = self.updateBlock else {
- return
- }
- if myResult.isResultSuccess() {
- block(nil)
- }else{
- block(myResult.error.debugDescription)
- }
- }
-
-
- }
-
- // MARK: - 获取icon
- func getIconOfPerson(_ person:OOPersonModel,compeletionBlock:@escaping (_ image:UIImage?,_ errMsg:String?) -> Void) {
- contactAPI.request(.iconByPerson(person.id!)) { (result) in
- switch result {
- case .success(let res):
- guard let image = UIImage(data: res.data) else {
- compeletionBlock(#imageLiteral(resourceName: "icon_?"),"image transform error")
- return
- }
- compeletionBlock(image,nil)
- break
- case .failure(let err):
- compeletionBlock(#imageLiteral(resourceName: "icon_?"),err.errorDescription)
- break
- }
- // if let err = result.error {
- // compeletionBlock(#imageLiteral(resourceName: "icon_?"),err.errorDescription)
- // }else{
- // let data = result.value?.data
- // guard let image = UIImage(data: data!) else {
- // compeletionBlock(#imageLiteral(resourceName: "icon_?"),"image transform error")
- // return
- // }
- // compeletionBlock(image,nil)
- // }
- }
- }
- }
- extension OOContactSearchViewModel {
- func numberOfSectionsForSearch() -> Int {
- return searchContactDatas.count
- }
-
- func numberOfRowsInSectionForSearch(_ section: Int) -> Int {
- return searchContactDatas[OOContacSearchSectionHeaderType(rawValue:section)!]!.count
- }
-
- func nodeForIndexPathForSearch(_ indexPath:IndexPath) -> DataModel? {
- let type = OOContacSearchSectionHeaderType(rawValue: indexPath.section)
- let item = searchContactDatas[type!]![indexPath.row]
- return item
- }
-
- func headerHeightOfSection(_ section:Int) -> CGFloat {
- return 40.0
- }
-
- func footerHeightOfSection(_ section:Int) -> CGFloat {
- return 10.0
- }
-
- func headerTypeOfSection(_ section:Int) -> OOContacSearchSectionHeaderType {
- return OOContacSearchSectionHeaderType(rawValue: section)!
- }
- }
|