@@ -25,14 +25,13 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
2525 var channel: Model.Channels?
2626 var myProtocol: DataDelegate?
2727
28− var dataSourcePeople: [Int] = []
29− var dataSourceTags: [String] = []
28+ var fullTags: [String] = Array(Model.Channels.fullTags)
3029
31− var activeDataSource: ActiveTable = .people
30+ var dataSourcePeople: [Model.Users] = []
3231
33− var fullPeople: [Int] = [5051,69]
32+ var dataSourceTags: [String] = []
3433
35− var fullTags: [String] = ["# happy", "# sad", "# meme", "# shershakov", "# pupsik"]
34+ var activeDataSource: ActiveTable = .people
3635
3736 @IBOutlet weak var viewww: UIView!
3837
@@ -41,8 +40,29 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
4140 override func viewDidLoad() {
4241 super.viewDidLoad()
4342
44− dataSourcePeople = channel!.people
4543 dataSourceTags = channel!.tags
44+ setUpController()
45+ tableView.reloadData()
46+ }
47+
48+ override func viewWillAppear(_ animated: Bool) {
49+ super.viewWillAppear(animated)
50+ Model.getUsers(for: channel!.people) { [weak self] (people) in
51+ self?.dataSourcePeople = Array(people.values)
52+ }
53+ }
54+
55+ // TODO: update channel
56+ override func viewWillDisappear(_ animated: Bool) {
57+ super.viewWillDisappear(animated)
58+ if let _ = channel?.id { print("update")
59+ Model.updateChannel(with: channel!)
60+ } else { print("create")
61+ Model.createChannel(with: channel!)
62+ }
63+ }
64+
65+ func setUpController(){
4666
4767 tableView.delegate = self
4868 tableView.dataSource = self
@@ -58,21 +78,12 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
5878 textField.addTarget(self, action: #selector(changedText(_:)), for: .editingChanged)
5979 }
6080
61− // TODO: update channel
62− override func viewWillDisappear(_ animated: Bool) {
63− if let _ = channel?.id {
64− print("update")
65− Model.updateChannel(with: channel!)
66− } else {
67− print("create")
68− Model.createChannel(with: channel!)
69− }
70− }
81+
7182
7283 @objc func changedText(_ textField: UITextField){
7384 navigationItem.title = textField.text
7485 channel?.name = textField.text!
75− myProtocol?.passData(for: index!, channel: channel!)
86+ // myProtocol?.passData(for: index!, channel: channel!)
7687 }
7788
7889 func numberOfSections(in tableView: UITableView) -> Int {
@@ -89,14 +100,12 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
89100 }
90101
91102 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
92− if searchBar.isFirstResponder {
93− if let cell = tableView.cellForRow(at: indexPath)
94− {
95−
103+ if searchBar.isFirstResponder, let cell = tableView.cellForRow(at: indexPath)
104+ {
96105 if cell.accessoryType == .none{
97106 switch searchBar.selectedScopeButtonIndex{
98107 case 0:
99− channel?.people.append(dataSourcePeople[indexPath.row])
108+ channel?.people.append(dataSourcePeople[indexPath.row].id)
100109 print("people = \(channel!.people)")
101110 case 1:
102111 channel?.tags.append(dataSourceTags[indexPath.row])
@@ -104,27 +113,33 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
104113 default:
105114 break
106115 }
107−
108116 cell.accessoryType = .checkmark
109117 } else {
110118 cell.accessoryType = .none
111119
112120 switch searchBar.selectedScopeButtonIndex{
113121 case 0:
114− channel?.people = channel!.people.filter{ $0 == fullPeople[indexPath.row] }
115− // TODO: dataPass
122+ let filtered = channel!.people.filter{ $0 != Model.Channels.fullPeople[indexPath.row].id }
123+ channel?.people = filtered
116124 case 1:
117− channel?.tags = channel!.tags.filter({ (s) -> Bool in
118− !fullTags[indexPath.row].contains(s)
119− })
120− // TODO: dataPass
125+ let filtered = channel!.tags.filter{
126+ !fullTags[indexPath.row].contains($0)
127+ }
128+ channel?.tags = filtered
121129 default:
122130 break
123131 }
124132 }
125− }
126− } else
133+ }
134+ else
127135 {
136+ switch activeDataSource{
137+ case .people:
138+ channel?.people.remove(at: indexPath.row)
139+ case .tags:
140+ channel?.tags.remove(at: indexPath.row)
141+ }
142+
128143 tableView.beginUpdates()
129144 switch activeDataSource{
130145 case .people:
@@ -132,29 +147,20 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
132147 case .tags:
133148 dataSourceTags.remove(at: indexPath.row)
134149 }
135− switch searchBar.selectedScopeButtonIndex{
136− case 0:
137− channel?.people.remove(at: indexPath.row)
138− // TODO: dataPass
139− case 1:
140− channel?.tags.remove(at: indexPath.row)
141− // TODO: dataPass
142− default:
143− break
144− }
145150 tableView.deleteRows(at: [indexPath], with: .none)
146151 tableView.endUpdates()
147152 }
148153 }
149154
150− func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
155+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
156+ {
151157 let cell = tableView.dequeueReusableCell(withIdentifier: itemCellId, for: indexPath)
152158
153− cell.textLabel?.text = activeDataSource == .people ? String(dataSourcePeople[indexPath.row]) : dataSourceTags[indexPath.row]
159+ cell.textLabel?.text = activeDataSource == .people ? "\(dataSourcePeople[indexPath.row].firstName) \(dataSourcePeople[indexPath.row].middleName) \(dataSourcePeople[indexPath.row].lastName)" : dataSourceTags[indexPath.row]
154160 cell.selectionStyle = .none
155161
156162 if activeDataSource == .tags && channel!.tags.contains(dataSourceTags[indexPath.row]) ||
157− activeDataSource == .people && channel!.people.contains(dataSourcePeople[indexPath.row])
163+ activeDataSource == .people && channel!.people.contains(dataSourcePeople[indexPath.row].id)
158164 {
159165 cell.accessoryType = .checkmark
160166 } else {
@@ -200,7 +206,7 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
200206 if searchText.isEmpty {
201207 switch activeDataSource {
202208 case .people:
203− dataSourcePeople = fullPeople
209+ dataSourcePeople = Model.Channels.fullPeople
204210 case .tags:
205211 dataSourceTags = fullTags
206212 }
@@ -213,7 +219,10 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
213219 func filterTableView(ind: Int, text: String){
214220 switch ind {
215221 case 0:
216− dataSourcePeople = fullPeople.filter { if let numb = Int(text) { return $0 == numb } else {return false}}
222+ dataSourcePeople = Model.Channels.fullPeople.filter {
223+ let fullName = "\($0.firstName) \($0.middleName) \($0.lastName)"
224+ return fullName.lowercased().contains(text.lowercased())
225+ }
217226 tableView.reloadData()
218227 case 1:
219228 dataSourceTags = fullTags.filter({ (s) -> Bool in
@@ -226,17 +235,16 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
226235 }
227236
228237 func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
238+ activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
229239 if searchBar.isFirstResponder {
230240 switch activeDataSource{
231241 case .people :
232− dataSourcePeople = fullPeople
242+ dataSourcePeople = Model.Channels.fullPeople
233243 dataSourceTags = []
234244 case .tags:
235245 dataSourceTags = fullTags
236246 dataSourcePeople = []
237247 }
238− print("\(searchBar.selectedScopeButtonIndex)")
239− activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
240248 if (!(searchBar.text?.isEmpty ?? true))
241249 {
242250 filterTableView(ind: selectedScope, text: searchBar.text!)
@@ -245,14 +253,14 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
245253 } else if !(searchBar.isFirstResponder){
246254 switch activeDataSource{
247255 case .people :
248− dataSourcePeople = channel!.people
256+ dataSourcePeople = channel!.people.map({ (item) -> Model.Users in
257+ Model.Channels.fullPeopleDict[item]!
258+ })
249259 dataSourceTags = []
250260 case .tags:
251261 dataSourceTags = channel!.tags
252262 dataSourcePeople = []
253263 }
254− print(" not active \(searchBar.selectedScopeButtonIndex)")
255− activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
256264 tableView.reloadData()
257265 }
258266 }
@@ -260,7 +268,9 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
260268 func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
261269 switch activeDataSource{
262270 case .people :
263− dataSourcePeople = channel!.people
271+ dataSourcePeople = channel!.people.map({ (item) -> Model.Users in
272+ Model.Channels.fullPeopleDict[item]!
273+ })
264274 dataSourceTags = []
265275 case .tags:
266276 dataSourceTags = channel!.tags
@@ -273,7 +283,7 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
273283 func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
274284 switch activeDataSource{
275285 case .people :
276− dataSourcePeople = fullPeople
286+ dataSourcePeople = Model.Channels.fullPeople
277287 dataSourceTags = []
278288 case .tags:
279289 dataSourceTags = fullTags