Blame view

Twear/Setting/PhoneContactsVC.swift 8.99 KB
582f536d   yangbin   common:2022.1.28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
  //
  //  PhoneContactsVC.swift
  //  Twear
  //
  //  Created by yangbin on 2022/1/15.
  //
  
  import UIKit
  import Contacts
  import SCIndexView
  import SnapKit
  import MBProgressHUD
  
  class PhoneContactsVC: UIViewController {
      
      @IBOutlet weak var tableView: UITableView!
      
      var contactClosure: ((_ array: [ContactModel]) -> ())?
      
      var preContacts: [ContactModel] = []
      
      var contacts: [ContactModel] = []
      var contactList: [[ContactModel]] = []
      var titleArray: [String] = []
  //    var selectedIndex: [IndexPath] = []
      var selectedContacts: [ContactModel] = []
      
      var maxCount: Int = 8
      
      lazy var addButton: UIButton = {
          let addButton = UIButton(frame: CGRect(x: 0, y: 0, width: 70, height: 28))
          addButton.setTitle(LocString("确定"), for: .normal)
          addButton.titleLabel?.contentMode = .right
          addButton.titleLabel?.font = RegularFont(14)
          addButton.setTitleColor(.black, for: .normal)
          addButton.addTarget(self, action: #selector(clickAddButton), for: .touchUpInside)
          return addButton
      }()
      
      lazy var cancelButton: UIButton = {
          let addButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 28))
          addButton.setTitle(LocString("取消"), for: .normal)
          addButton.titleLabel?.font = RegularFont(14)
          addButton.setTitleColor(.black, for: .normal)
          addButton.addTarget(self, action: #selector(clickCancelButton), for: .touchUpInside)
          return addButton
      }()
      
      
      override func viewDidLoad() {
          super.viewDidLoad()
          title = LocString("选择联系人")
          
          navigationItem.leftBarButtonItem =  UIBarButtonItem(customView: cancelButton)
          navigationItem.rightBarButtonItem =  UIBarButtonItem(customView: addButton)
          
          
          tableView.register(UINib.init(nibName: "ContactCell", bundle: Bundle.main), forCellReuseIdentifier: "ContactCell")
          tableView.contentInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
          tableView.tableFooterView = UIView(frame: CGRect.zero)
          //        tableView.style = .grouped
          let indexViewConfiguration = SCIndexViewConfiguration(indexViewStyle: .default)
          indexViewConfiguration?.indexItemSelectedBackgroundColor = .clear
          indexViewConfiguration?.indexItemSelectedTextColor = .darkGray
          //        indexViewConfiguratio
          tableView.sc_indexViewConfiguration = indexViewConfiguration
          tableView.sc_translucentForTableViewInNavigationBar = true
          
          
          
          loadContactsData()
      }
      
      @objc private func clickAddButton() {
  //        if selectedContacts.count > 0 {
  //            for contact in selectedContacts {
  //                contact.isSelected = false
  //            }
              contactClosure?(selectedContacts)
  //        }
          navigationController?.popViewController(animated: true)
      }
      
      @objc private func clickCancelButton() {
          navigationController?.popViewController(animated: true)
      }
      
      func loadContactsData() {
          //获取授权状态
          let status = CNContactStore.authorizationStatus(for: .contacts)
          guard status == .authorized else { return }
          
          let store = CNContactStore()
          let keys = [CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey]
          let request = CNContactFetchRequest(keysToFetch: keys as [CNKeyDescriptor])
          
          do {
              try store.enumerateContacts(with: request) { (contact, _) in
                  for phone in contact.phoneNumbers {
                      let model = ContactModel(name: "\(contact.familyName)\(contact.givenName)", number: phone.value.stringValue)
                      for c in self.preContacts {
                          if c.number == phone.value.stringValue {
                              model.isSelected = true
                              model.isSOS = c.isSOS
                              self.selectedContacts.append(c)
                          }
                      }
                      self.contacts.append(model)
                      print("\(model.name)--\(model.number)   ")
                  }
                  if self.selectedContacts.count == 0 {
                      self.addButton.setTitle("\(LocString("确定"))", for: .normal)
                  } else {
                      self.addButton.setTitle("\(LocString("确定"))(\(self.selectedContacts.count))", for: .normal)
                  }
                  
                  self.sortList(self.contacts)
              }
          } catch {
              print(error)
          }
      }
      
      func sortList(_ array: [ContactModel]) {
          BMChineseSort.sortAndGroup(objectArray: array, key: "name") { isSuccess, _, titleArray, objArray in
              self.tableView.sc_indexViewDataSource = titleArray
              self.titleArray = titleArray
              self.contactList = objArray
  //            for obj in objArray {
  //                for a in obj {
  //
  //                }
  //            }
              self.tableView.reloadData()
          }
      }
      
      
      @IBAction func editingChanged(_ sender: UITextField) {
          let searchText = sender.text
          var list: [ContactModel] = []
          if searchText == "" {
              sortList(contacts)
          } else {
              for contact in contacts {
                  if contact.name.contains(searchText!) {
                      list.append(contact)
                  } else if contact.number.contains(searchText!) {
                      list.append(contact)
                  }
              }
              
              sortList(list)
          }
          //        self.reloadListData()
          //        self.tableView.reloadData()
      }
      
  }
  
  
  extension PhoneContactsVC: UITableViewDelegate, UITableViewDataSource {
      
      
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          return 50
      }
      
      func numberOfSections(in tableView: UITableView) -> Int {
          return contactList.count
      }
      func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
          return 20
      }
      func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
          return 0.5
      }
      
      func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
          
          
          let view = UIView()
          view.backgroundColor = .clear//UIColor.rgbColorFromHex(0xF2F2F2)
          let label = UILabel()
          view.addSubview(label)
          label.snp.makeConstraints { (make) in
              make.centerY.equalToSuperview()
              make.left.equalToSuperview().offset(12)
          }
          label.text = titleArray[section]
          label.font = RegularFont(14)
          //            label.textColor = UIColor.rgbColorFromHex(0x98C1D9)
          //            view.backgroundColor = .red
          return view
          
      }
      
      
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return contactList[section].count
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell", for: indexPath) as! ContactCell
          cell.isPhone = true
          cell.contact = contactList[indexPath.section][indexPath.row]
         
          //        cell.alarmClock = alarmClockArray[indexPath.row]
          //        cell.switchClosure = {[weak self] isOn in
          //            self?.switchValueChanged(isOn, index: indexPath.row)
          //        }
          return cell
      }
      
      
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          let cell = tableView.cellForRow(at: indexPath) as! ContactCell
          cell.isSelected = false
          let contact = contactList[indexPath.section][indexPath.row]
          let selected = contact.isSelected
          if !selected && selectedContacts.count >= maxCount {
              //        if contacts.count >= 8 {
                          MBProgressHUD.showh(LocString("您最多可以添加8个联系人"))
              //            return
              //        }
              return
          }
          contact.isSelected = !selected
  //        if let i = contacts.firstIndex(of:contact) {
  //            contacts[i].isSelected = !selected
  //        }
          
          if !selected {
              selectedContacts.append(contact)
          } else {
              for (i, c) in selectedContacts.enumerated() {
                  if c.number == contact.number && c.name == contact.name {
                      selectedContacts.remove(at: i)
                      break
                  }
              }
              
              
  //            if let i = selectedContacts.firstIndex(of: contact) {
  //                selectedContacts.remove(at: i)
  //            }
          }
          if selectedContacts.count == 0 {
              addButton.setTitle("\(LocString("确定"))", for: .normal)
          } else {
              addButton.setTitle("\(LocString("确定"))(\(selectedContacts.count))", for: .normal)
          }
         
          tableView.reloadData()
  
      }
  }