| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- //
- // JCSearchResultViewController.swift
- // JChat
- //
- // Created by deng on 2017/5/5.
- // Copyright © 2017年 HXHG. All rights reserved.
- //
- import UIKit
- class JCSearchResultViewController: UIViewController {
- var message: JMSGMessage?
- var fromUser: JMSGUser!
- weak var delegate: UIViewController?
-
- override func viewDidLoad() {
- super.viewDidLoad()
- _init()
- }
-
- override func viewDidAppear(_ animated: Bool) {
- super.viewDidAppear(animated)
- navigationController?.isNavigationBarHidden = true
- if searchController != nil {
- searchController.searchBar.isHidden = false
- }
- }
-
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillAppear(animated)
- navigationController?.isNavigationBarHidden = false
- }
-
- deinit {
- NotificationCenter.default.removeObserver(self)
- }
-
- var searchController: UISearchController!
- fileprivate lazy var tableView: UITableView = {
- var tableView = UITableView(frame: .zero, style: .grouped)
- tableView.keyboardDismissMode = .onDrag
- tableView.delegate = self
- tableView.dataSource = self
- tableView.sectionIndexBackgroundColor = .clear
- tableView.register(JCContacterCell.self, forCellReuseIdentifier: "JCContacterCell")
- tableView.frame = CGRect(x: 0, y: 64, width: self.view.width, height: self.view.height - 64)
- return tableView
- }()
- fileprivate lazy var tagArray = ["联系人", "群组"]
- fileprivate lazy var users: [JMSGUser] = []
- fileprivate lazy var groups: [JMSGGroup] = []
-
- dynamic lazy var filteredUsersArray: [JMSGUser] = []
- dynamic lazy var filteredGroupsArray: [JMSGGroup] = []
-
- fileprivate var searchString = ""
-
- private lazy var tipsLabel: UILabel = UILabel(frame: CGRect(x: 0, y: 100, width: self.view.width, height: 22.5))
- private lazy var networkErrorView: UIView = {
- let tipsView = UIView(frame: CGRect(x: 0, y: 64, width: self.view.width, height: self.view.height))
- var tipsLabel: UILabel = UILabel(frame: CGRect(x: 0, y: 100, width: tipsView.width, height: 22.5))
- tipsLabel.textColor = UIColor(netHex: 0x999999)
- tipsLabel.textAlignment = .center
- tipsLabel.font = UIFont.systemFont(ofSize: 16)
- tipsLabel.text = "无法连接网络"
- tipsView.addSubview(tipsLabel)
- tipsView.isHidden = true
- tipsView.backgroundColor = .white
- return tipsView
- }()
- fileprivate var selectGroup: JMSGGroup!
- fileprivate var selectUser: JMSGUser!
-
- private func _init() {
- view.backgroundColor = UIColor(netHex: 0xe8edf3)
- automaticallyAdjustsScrollViewInsets = false
- navigationController?.automaticallyAdjustsScrollViewInsets = false
-
- tipsLabel.font = UIFont.systemFont(ofSize: 16)
- tipsLabel.textColor = UIColor(netHex: 0x999999)
- tipsLabel.textAlignment = .center
- view.addSubview(tipsLabel)
-
- _getDate()
- view.addSubview(tableView)
- view.addSubview(networkErrorView)
-
- if JCNetworkManager.isNotReachable {
- networkErrorView.isHidden = false
- }
-
- NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: NSNotification.Name(rawValue: "kNetworkReachabilityChangedNotification"), object: nil)
- }
-
- func reachabilityChanged(note: NSNotification) {
- if let curReach = note.object as? Reachability {
- let status = curReach.currentReachabilityStatus()
- switch status {
- case NotReachable:
- networkErrorView.isHidden = false
- default :
- networkErrorView.isHidden = true
- }
- }
- }
-
- private func _getDate() {
- users.removeAll()
- groups.removeAll()
-
- JMSGConversation.allConversations { (result, error) in
- if error == nil {
- if let conversations = result as? [JMSGConversation] {
- for conv in conversations {
- if !conv.ex.isGroup {
- let user = conv.target as! JMSGUser
- self.users.append(user)
- }
- }
- if !self.searchString.isEmpty {
- self.filter(self.searchString)
- }
- }
- }
-
- JMSGFriendManager.getFriendList { (result, error) in
- if error == nil {
- let users = result as! [JMSGUser]
- for user in users {
- if !self.users.contains(user) {
- self.users.append(user)
- }
- }
-
- if !self.searchString.isEmpty {
- self.filter(self.searchString)
- }
- }
- }
-
- JMSGGroup.myGroupArray { (result, error) in
- if error != nil {
- return
- }
- for item in result as! [NSNumber] {
- JMSGGroup.groupInfo(withGroupId: "\(item)", completionHandler: { (result, error) in
- guard let group = result as? JMSGGroup else {
- return
- }
- self.groups.append(group)
- if !self.searchString.isEmpty {
- self.filter(self.searchString)
- }
- })
- }
- }
- }
-
-
- }
-
- fileprivate func filter(_ searchString: String) {
- if searchString.isEmpty || searchString == "" {
- return
- }
- filteredUsersArray = _JCFilterUsers(users: users, string: searchString)
- filteredGroupsArray = _JCFilterGroups(groups: groups, string: searchString)
-
- if filteredUsersArray.count == 0 && filteredGroupsArray.count == 0 {
- tableView.isHidden = true
-
- let attr = NSMutableAttributedString(string: "没有搜到 ")
- let attrSearchString = NSAttributedString(string: searchString, attributes: [ NSAttributedString.Key.foregroundColor : UIColor(netHex: 0xfb4747), NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 16.0)])
-
- attr.append(attrSearchString)
- attr.append(NSAttributedString(string: " 相关的信息"))
- tipsLabel.attributedText = attr
- return
- } else {
- tableView.isHidden = false
- }
-
- tableView.reloadData()
- }
- fileprivate func sendBusinessCard() {
- JCAlertView.bulid().setTitle("发送给:\(selectGroup.displayName())")
- .setMessage(fromUser!.displayName() + "的名片")
- .setDelegate(self)
- .addCancelButton("取消")
- .addButton("确定")
- .setTag(10003)
- .show()
- }
- fileprivate func forwardMessage(_ message: JMSGMessage) {
- let alertView = JCAlertView.bulid().setJMessage(message)
- .setDelegate(self)
- .setTag(10001)
- if selectUser == nil {
- alertView.setTitle("发送给:\(selectGroup.displayName())")
- } else {
- alertView.setTitle("发送给:\(selectUser.displayName())")
- }
- alertView.show()
- }
- public func close() {
- searchController.isActive = false
- delegate?.dismiss(animated: true, completion: nil)
- }
- }
- extension JCSearchResultViewController: UIAlertViewDelegate {
- func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {
- if buttonIndex != 1 {
- return
- }
- switch alertView.tag {
- case 10001:
- if selectUser != nil {
- JMSGMessage.forwardMessage(message!, target: selectUser, optionalContent: JMSGOptionalContent.ex.default)
- } else {
- JMSGMessage.forwardMessage(message!, target: selectGroup, optionalContent: JMSGOptionalContent.ex.default)
- }
- case 10003:
- if selectUser != nil {
- JMSGConversation.createSingleConversation(withUsername: selectUser.username) { (result, error) in
- if let conversation = result as? JMSGConversation {
- let message = JMSGMessage.ex.createBusinessCardMessage(conversation, self.fromUser.username, self.fromUser.appKey ?? "")
- JMSGMessage.send(message, optionalContent: JMSGOptionalContent.ex.default)
- }
- }
- } else {
- let msg = JMSGMessage.ex.createBusinessCardMessage(gid: selectGroup.gid, userName: fromUser!.username, appKey: fromUser!.appKey ?? "")
- JMSGMessage.send(msg, optionalContent: JMSGOptionalContent.ex.default)
- }
- default:
- break
- }
- MBProgressHUD_JChat.show(text: "已发送", view: view, 2)
- let time: TimeInterval = 2
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: kReloadAllMessage), object: nil)
- }
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + time) { [weak self] in
- self?.close()
- }
- }
- }
- //Mark: -
- extension JCSearchResultViewController: UITableViewDelegate, UITableViewDataSource {
-
- func numberOfSections(in tableView: UITableView) -> Int {
- if filteredUsersArray.count > 0 && filteredGroupsArray.count > 0 {
- return 2
- }
- if filteredUsersArray.count == 0 && filteredGroupsArray.count == 0 {
- return 0
- }
- return 1
- }
-
- public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- let userCount = filteredUsersArray.count > 3 ? 4 : filteredUsersArray.count
- let groupCount = filteredGroupsArray.count > 3 ? 4 : filteredGroupsArray.count
- if section == 0 && userCount > 0 {
- return userCount
- } else if groupCount > 0 {
- return groupCount
- }
-
- if userCount > 0 && groupCount > 0 {
- if section == 0 {
- return userCount
- } else {
- return groupCount
- }
- }
-
- return 0
- }
-
- func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
- if filteredUsersArray.count <= 0 {
- return tagArray.last
- }
- return tagArray[section]
- }
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- if indexPath.row == 3 {
- return 40
- }
- return 55
- }
-
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
- return 35
- }
-
- func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
- return 0.001
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- if indexPath.row == 3 {
- var cell = tableView.dequeueReusableCell(withIdentifier: "JCMoreCell")
- if cell == nil {
- cell = UITableViewCell(style: .value2, reuseIdentifier: "JCMoreCell")
- }
- return cell!
- }
- return tableView.dequeueReusableCell(withIdentifier: "JCContacterCell", for: indexPath)
- }
-
- func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
-
- var isUser = false
- if indexPath.section == 0 && filteredUsersArray.count > 0 {
- isUser = true
- }
- if indexPath.row == 3 {
- cell.detailTextLabel?.text = isUser ? "查看更多联系人" : "查看更多群组"
- cell.detailTextLabel?.textColor = UIColor(netHex: 0x999999)
- cell.accessoryType = .disclosureIndicator
- return
- }
- guard let cell = cell as? JCContacterCell else {
- return
- }
- if isUser {
- cell.bindDate(filteredUsersArray[indexPath.row])
- } else {
- cell.bindDateWithGroup(group: filteredGroupsArray[indexPath.row])
- }
- }
-
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- tableView.deselectRow(at: indexPath, animated: true)
- var isUser = false
- if indexPath.section == 0 && filteredUsersArray.count > 0 {
- isUser = true
- }
-
- if indexPath.row == 3 {
- let vc = JCMoreResultViewController()
- vc.fromUser = fromUser
- vc.message = message
- vc.delegate = self
- if isUser {
- vc.users = filteredUsersArray
- } else {
- vc.groups = filteredGroupsArray
- }
- vc.searchResultView = self
- vc.searchController = self.searchController
- if searchController != nil {
- searchController.searchBar.resignFirstResponder()
- }
- navigationController?.pushViewController(vc, animated: true)
- return
- }
-
- if isUser {
- let user = filteredUsersArray[indexPath.row]
- selectUser = user
- if let message = message {
- forwardMessage(message)
- return
- }
- if fromUser != nil {
- sendBusinessCard()
- return
- }
- let vc = JCUserInfoViewController()
- vc.user = user
- if searchController != nil {
- searchController.searchBar.resignFirstResponder()
- searchController.searchBar.isHidden = true
- }
- navigationController?.pushViewController(vc, animated: true)
- } else {
- let group = filteredGroupsArray[indexPath.row]
- selectGroup = group
- if let message = self.message {
- forwardMessage(message)
- return
- }
- if self.fromUser != nil {
- sendBusinessCard()
- return
- }
- JMSGConversation.createGroupConversation(withGroupId: group.gid) { (result, error) in
- let conv = result as! JMSGConversation
- let vc = JCChatViewController(conversation: conv)
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: kUpdateConversation), object: nil, userInfo: nil)
- if self.searchController != nil {
- self.searchController.searchBar.resignFirstResponder()
- self.searchController.searchBar.isHidden = true
- }
- self.navigationController?.pushViewController(vc, animated: true)
- }
- }
- }
- }
- extension JCSearchResultViewController: UISearchResultsUpdating {
- func updateSearchResults(for searchController: UISearchController) {
- self.searchController = searchController
- searchString = searchController.searchBar.text!
- filter(searchString)
- }
- }
- @inline(__always)
- internal func _JCFilterUsers(users: [JMSGUser], string: String) -> [JMSGUser] {
- let filteredUsersArray = users.filter( { (user: JMSGUser) -> Bool in
- let str = string.uppercased()
- let notename = user.noteName?.uppercased().contains(str) ?? false
- let nickname = user.nickname?.uppercased().contains(str) ?? false
- let username = user.username.uppercased().contains(str)
- if notename || nickname || username {
- return true
- } else {
- return false
- }
- })
- return filteredUsersArray
- }
- @inline(__always)
- internal func _JCFilterGroups(groups: [JMSGGroup], string: String) -> [JMSGGroup] {
- let filteredGroupsArray = groups.filter( { (group: JMSGGroup) -> Bool in
- let str = string.uppercased()
- if group.name?.uppercased().contains(str) ?? false ||
- group.gid.uppercased().contains(str) {
- return true
- } else {
- return false
- }
- })
- return filteredGroupsArray
- }
|