@@ -9,7 +9,12 @@
99import UIKit
1010
1111protocol DataDelegate {
12− func passData(for row: Int, channel: Channel)
12+ func passData(for row: Int, channel: Model.Channels)
13+}
14+
15+enum ActiveTable {
16+ case tags
17+ case people
1318}
1419
1520class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UISearchBarDelegate
@@ -17,12 +22,15 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
1722 @IBOutlet weak var tableView: UITableView!
1823
1924 var index: Int?
20− var channel: Channel?
25+ var channel: Model.Channels?
2126 var myProtocol: DataDelegate?
2227
23− var dataSource: [String] = []
28+ var dataSourcePeople: [Int] = []
29+ var dataSourceTags: [String] = []
30+
31+ var activeDataSource: ActiveTable = .people
2432
25− var fullPeople: [String] = ["Other Ann", "Other Ilya", "Other Kostya", "Pasha", "Valera"]
33+ var fullPeople: [Int] = [5051,69]
2634
2735 var fullTags: [String] = ["# happy", "# sad", "# meme", "# shershakov", "# pupsik"]
2836
@@ -32,22 +40,30 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
3240
3341 override func viewDidLoad() {
3442 super.viewDidLoad()
43+
44+ dataSourcePeople = channel!.people
45+ dataSourceTags = channel!.tags
46+
3547 tableView.delegate = self
3648 tableView.dataSource = self
37− dataSource = channel!.people
3849 tableView.register(UITableViewCell.self, forCellReuseIdentifier: itemCellId)
3950 tableView.reloadData()
4051 textField.delegate = self
4152 searchBar.delegate = self
4253 searchBar.selectedScopeButtonIndex = 0
43− navigationItem.title = channel?.title
44− textField.placeholder = channel?.title
54+
55+ navigationItem.title = channel?.name
56+ textField.placeholder = channel?.name
57+
4558 textField.addTarget(self, action: #selector(changedText(_:)), for: .editingChanged)
4659 }
4760
61+ override func viewWillDisappear(_ animated: Bool) {
62+ Model.createChannel(with: channel!)
63+ }
4864 @objc func changedText(_ textField: UITextField){
4965 navigationItem.title = textField.text
50− channel?.title = textField.text!
66+ channel?.name = textField.text!
5167 myProtocol?.passData(for: index!, channel: channel!)
5268 }
5369
@@ -56,7 +72,12 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
5672 }
5773
5874 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
59− return dataSource.count
75+ switch activeDataSource {
76+ case .people:
77+ return dataSourcePeople.count
78+ case .tags:
79+ return dataSourceTags.count
80+ }
6081 }
6182
6283 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
@@ -67,11 +88,11 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
6788 if cell.accessoryType == .none{
6889 switch searchBar.selectedScopeButtonIndex{
6990 case 0:
70− channel?.people.append(dataSource[indexPath.row])
91+ channel?.people.append(dataSourcePeople[indexPath.row])
7192 print("people = \(channel!.people)")
7293 case 1:
73− channel?.hashtags.append(dataSource[indexPath.row])
74− print("hashtags = \(channel!.hashtags)")
94+ channel?.tags.append(dataSourceTags[indexPath.row])
95+ print("hashtags = \(channel!.tags)")
7596 default:
7697 break
7798 }
@@ -82,12 +103,10 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
82103
83104 switch searchBar.selectedScopeButtonIndex{
84105 case 0:
85− channel?.people = channel!.people.filter({ (s) -> Bool in
86− !fullPeople[indexPath.row].contains(s)
87− })
106+ channel?.people = channel!.people.filter{ $0 == fullPeople[indexPath.row] }
88107 // TODO: dataPass
89108 case 1:
90− channel?.hashtags = channel!.hashtags.filter({ (s) -> Bool in
109+ channel?.tags = channel!.tags.filter({ (s) -> Bool in
91110 !fullTags[indexPath.row].contains(s)
92111 })
93112 // TODO: dataPass
@@ -99,13 +118,18 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
99118 } else
100119 {
101120 tableView.beginUpdates()
102− dataSource.remove(at: indexPath.row)
121+ switch activeDataSource{
122+ case .people:
123+ dataSourcePeople.remove(at: indexPath.row)
124+ case .tags:
125+ dataSourceTags.remove(at: indexPath.row)
126+ }
103127 switch searchBar.selectedScopeButtonIndex{
104128 case 0:
105129 channel?.people.remove(at: indexPath.row)
106130 // TODO: dataPass
107131 case 1:
108− channel?.hashtags.remove(at: indexPath.row)
132+ channel?.tags.remove(at: indexPath.row)
109133 // TODO: dataPass
110134 default:
111135 break
@@ -118,10 +142,11 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
118142 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
119143 let cell = tableView.dequeueReusableCell(withIdentifier: itemCellId, for: indexPath)
120144
121− cell.textLabel?.text = dataSource[indexPath.row]
145+ cell.textLabel?.text = activeDataSource == .people ? String(dataSourcePeople[indexPath.row]) : dataSourceTags[indexPath.row]
122146 cell.selectionStyle = .none
123147
124− if channel!.hashtags.contains(dataSource[indexPath.row]) || channel!.people.contains(dataSource[indexPath.row])
148+ if activeDataSource == .tags && channel!.tags.contains(dataSourceTags[indexPath.row]) ||
149+ activeDataSource == .people && channel!.people.contains(dataSourcePeople[indexPath.row])
125150 {
126151 cell.accessoryType = .checkmark
127152 } else {
@@ -147,7 +172,7 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
147172 {
148173 let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 70))
149174
150− if flag {
175+ if activeDataSource == .tags {
151176 searchBar.selectedScopeButtonIndex = 1
152177 } else {
153178 searchBar.selectedScopeButtonIndex = 0
@@ -165,7 +190,12 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
165190
166191 func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
167192 if searchText.isEmpty {
168− dataSource = searchBar.selectedScopeButtonIndex == 0 ? fullPeople : fullTags
193+ switch activeDataSource{
194+ case .people:
195+ dataSourcePeople = fullPeople
196+ case .tags:
197+ dataSourceTags = fullTags
198+ }
169199 tableView.reloadData()
170200 } else {
171201 filterTableView(ind: searchBar.selectedScopeButtonIndex, text: searchText)
@@ -175,12 +205,10 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
175205 func filterTableView(ind: Int, text: String){
176206 switch ind {
177207 case 0:
178− dataSource = fullPeople.filter({ (s) -> Bool in
179− s.lowercased().contains(text.lowercased())
180− })
208+ dataSourcePeople = fullPeople.filter { if let numb = Int(text) { return $0 == numb } else {return false}}
181209 tableView.reloadData()
182210 case 1:
183− dataSource = fullTags.filter({ (s) -> Bool in
211+ dataSourceTags = fullTags.filter({ (s) -> Bool in
184212 s.lowercased().contains(text.lowercased())
185213 })
186214 tableView.reloadData()
@@ -189,33 +217,59 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
189217 }
190218 }
191219
192− var flag = false
193−
194220 func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
195221 if searchBar.isFirstResponder {
196− dataSource = searchBar.selectedScopeButtonIndex == 0 ? fullPeople : fullTags
197− flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
222+ switch activeDataSource{
223+ case .people :
224+ dataSourcePeople = fullPeople
225+ dataSourceTags = []
226+ case .tags:
227+ dataSourceTags = fullTags
228+ dataSourcePeople = []
229+ }
230+ activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
198231 if (!(searchBar.text?.isEmpty ?? true))
199232 {
200233 filterTableView(ind: selectedScope, text: searchBar.text!)
201234 }
202235 tableView.reloadData()
203236 } else if !(searchBar.isFirstResponder){
204− dataSource = searchBar.selectedScopeButtonIndex == 0 ? channel!.people : channel!.hashtags
205− flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
237+ switch activeDataSource{
238+ case .people :
239+ dataSourcePeople = channel!.people
240+ dataSourceTags = []
241+ case .tags:
242+ dataSourceTags = channel!.tags
243+ dataSourcePeople = []
244+ }
245+ activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
206246 tableView.reloadData()
207247 }
208248 }
209249
210250 func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
211− dataSource = searchBar.selectedScopeButtonIndex == 0 ? channel!.people : channel!.hashtags
212− flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
251+ switch activeDataSource{
252+ case .people :
253+ dataSourcePeople = channel!.people
254+ dataSourceTags = []
255+ case .tags:
256+ dataSourceTags = channel!.tags
257+ dataSourcePeople = []
258+ }
259+ activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
213260 tableView.reloadData()
214261 }
215262
216263 func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
217− dataSource = searchBar.selectedScopeButtonIndex == 0 ? fullPeople : fullTags
218− flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
264+ switch activeDataSource{
265+ case .people :
266+ dataSourcePeople = fullPeople
267+ dataSourceTags = []
268+ case .tags:
269+ dataSourceTags = fullTags
270+ dataSourcePeople = []
271+ }
272+ activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
219273 tableView.reloadData()
220274 }
221275