@@ -9,7 +9,12 @@
9import UIKit
9import UIKit
11protocol DataDelegate {
11protocol DataDelegate {
12− func passData(for row: Int, channel: Channel)
12+ func passData(for row: Int, channel: Model.Channels)
15class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UISearchBarDelegate
20class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UISearchBarDelegate
@@ -17,12 +22,15 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
17 @IBOutlet weak var tableView: UITableView!
22 @IBOutlet weak var tableView: UITableView!
19 var index: Int?
24 var index: Int?
20− var channel: Channel?
25+ var channel: Model.Channels?
21 var myProtocol: DataDelegate?
26 var myProtocol: DataDelegate?
23− var dataSource: [String] = []
28+ var dataSourcePeople: [Int] = []
29+ var dataSourceTags: [String] = []
31+ var activeDataSource: ActiveTable = .people
25− var fullPeople: [String] = ["Other Ann", "Other Ilya", "Other Kostya", "Pasha", "Valera"]
33+ var fullPeople: [Int] = [5051,69]
27 var fullTags: [String] = ["# happy", "# sad", "# meme", "# shershakov", "# pupsik"]
35 var fullTags: [String] = ["# happy", "# sad", "# meme", "# shershakov", "# pupsik"]
@@ -32,22 +40,30 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
33 override func viewDidLoad() {
41 override func viewDidLoad() {
34 super.viewDidLoad()
42 super.viewDidLoad()
44+ dataSourcePeople = channel!.people
45+ dataSourceTags = channel!.tags
35 tableView.delegate = self
47 tableView.delegate = self
36 tableView.dataSource = self
48 tableView.dataSource = self
37− dataSource = channel!.people
38 tableView.register(UITableViewCell.self, forCellReuseIdentifier: itemCellId)
49 tableView.register(UITableViewCell.self, forCellReuseIdentifier: itemCellId)
39 tableView.reloadData()
50 tableView.reloadData()
40 textField.delegate = self
51 textField.delegate = self
41 searchBar.delegate = self
52 searchBar.delegate = self
42 searchBar.selectedScopeButtonIndex = 0
53 searchBar.selectedScopeButtonIndex = 0
43− navigationItem.title = channel?.title
55+ navigationItem.title = channel?.name
44− textField.placeholder = channel?.title
56+ textField.placeholder = channel?.name
45 textField.addTarget(self, action: #selector(changedText(_:)), for: .editingChanged)
58 textField.addTarget(self, action: #selector(changedText(_:)), for: .editingChanged)
61+ override func viewWillDisappear(_ animated: Bool) {
62+ Model.createChannel(with: channel!)
48 @objc func changedText(_ textField: UITextField){
64 @objc func changedText(_ textField: UITextField){
49 navigationItem.title = textField.text
65 navigationItem.title = textField.text
50− channel?.title = textField.text!
66+ channel?.name = textField.text!
51 myProtocol?.passData(for: index!, channel: channel!)
67 myProtocol?.passData(for: index!, channel: channel!)
@@ -56,7 +72,12 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
58 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
74 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
75+ switch activeDataSource {
59− return dataSource.count
77+ return dataSourcePeople.count
79+ return dataSourceTags.count
62 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
83 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
@@ -67,11 +88,11 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
67 if cell.accessoryType == .none{
88 if cell.accessoryType == .none{
68 switch searchBar.selectedScopeButtonIndex{
89 switch searchBar.selectedScopeButtonIndex{
70− channel?.people.append(dataSource[indexPath.row])
91+ channel?.people.append(dataSourcePeople[indexPath.row])
71 print("people = \(channel!.people)")
92 print("people = \(channel!.people)")
73− channel?.hashtags.append(dataSource[indexPath.row])
94+ channel?.tags.append(dataSourceTags[indexPath.row])
74− print("hashtags = \(channel!.hashtags)")
95+ print("hashtags = \(channel!.tags)")
@@ -82,12 +103,10 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
83 switch searchBar.selectedScopeButtonIndex{
104 switch searchBar.selectedScopeButtonIndex{
85− channel?.people = channel!.people.filter({ (s) -> Bool in
106+ channel?.people = channel!.people.filter{ $0 == fullPeople[indexPath.row] }
86− !fullPeople[indexPath.row].contains(s)
88 // TODO: dataPass
107 // TODO: dataPass
90− channel?.hashtags = channel!.hashtags.filter({ (s) -> Bool in
109+ channel?.tags = channel!.tags.filter({ (s) -> Bool in
91 !fullTags[indexPath.row].contains(s)
110 !fullTags[indexPath.row].contains(s)
93 // TODO: dataPass
112 // TODO: dataPass
@@ -99,13 +118,18 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
101 tableView.beginUpdates()
120 tableView.beginUpdates()
121+ switch activeDataSource{
123+ dataSourcePeople.remove(at: indexPath.row)
102− dataSource.remove(at: indexPath.row)
125+ dataSourceTags.remove(at: indexPath.row)
103 switch searchBar.selectedScopeButtonIndex{
127 switch searchBar.selectedScopeButtonIndex{
105 channel?.people.remove(at: indexPath.row)
129 channel?.people.remove(at: indexPath.row)
106 // TODO: dataPass
130 // TODO: dataPass
108− channel?.hashtags.remove(at: indexPath.row)
132+ channel?.tags.remove(at: indexPath.row)
109 // TODO: dataPass
133 // TODO: dataPass
@@ -118,10 +142,11 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
118 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
142 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
119 let cell = tableView.dequeueReusableCell(withIdentifier: itemCellId, for: indexPath)
143 let cell = tableView.dequeueReusableCell(withIdentifier: itemCellId, for: indexPath)
121− cell.textLabel?.text = dataSource[indexPath.row]
145+ cell.textLabel?.text = activeDataSource == .people ? String(dataSourcePeople[indexPath.row]) : dataSourceTags[indexPath.row]
122 cell.selectionStyle = .none
146 cell.selectionStyle = .none
148+ if activeDataSource == .tags && channel!.tags.contains(dataSourceTags[indexPath.row]) ||
124− if channel!.hashtags.contains(dataSource[indexPath.row]) || channel!.people.contains(dataSource[indexPath.row])
149+ activeDataSource == .people && channel!.people.contains(dataSourcePeople[indexPath.row])
126 cell.accessoryType = .checkmark
151 cell.accessoryType = .checkmark
@@ -147,7 +172,7 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
148 let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 70))
173 let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 70))
150− if flag {
175+ if activeDataSource == .tags {
151 searchBar.selectedScopeButtonIndex = 1
176 searchBar.selectedScopeButtonIndex = 1
153 searchBar.selectedScopeButtonIndex = 0
178 searchBar.selectedScopeButtonIndex = 0
@@ -165,7 +190,12 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
166 func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
191 func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
167 if searchText.isEmpty {
192 if searchText.isEmpty {
168− dataSource = searchBar.selectedScopeButtonIndex == 0 ? fullPeople : fullTags
193+ switch activeDataSource{
195+ dataSourcePeople = fullPeople
197+ dataSourceTags = fullTags
169 tableView.reloadData()
199 tableView.reloadData()
171 filterTableView(ind: searchBar.selectedScopeButtonIndex, text: searchText)
201 filterTableView(ind: searchBar.selectedScopeButtonIndex, text: searchText)
@@ -175,12 +205,10 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
175 func filterTableView(ind: Int, text: String){
205 func filterTableView(ind: Int, text: String){
176 switch ind {
206 switch ind {
178− dataSource = fullPeople.filter({ (s) -> Bool in
208+ dataSourcePeople = fullPeople.filter { if let numb = Int(text) { return $0 == numb } else {return false}}
179− s.lowercased().contains(text.lowercased())
181 tableView.reloadData()
209 tableView.reloadData()
183− dataSource = fullTags.filter({ (s) -> Bool in
211+ dataSourceTags = fullTags.filter({ (s) -> Bool in
184 s.lowercased().contains(text.lowercased())
212 s.lowercased().contains(text.lowercased())
186 tableView.reloadData()
214 tableView.reloadData()
@@ -189,33 +217,59 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
194 func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
220 func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
195 if searchBar.isFirstResponder {
221 if searchBar.isFirstResponder {
196− dataSource = searchBar.selectedScopeButtonIndex == 0 ? fullPeople : fullTags
222+ switch activeDataSource{
224+ dataSourcePeople = fullPeople
227+ dataSourceTags = fullTags
228+ dataSourcePeople = []
197− flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
230+ activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
198 if (!(searchBar.text?.isEmpty ?? true))
231 if (!(searchBar.text?.isEmpty ?? true))
200 filterTableView(ind: selectedScope, text: searchBar.text!)
233 filterTableView(ind: selectedScope, text: searchBar.text!)
202 tableView.reloadData()
235 tableView.reloadData()
203 } else if !(searchBar.isFirstResponder){
236 } else if !(searchBar.isFirstResponder){
204− dataSource = searchBar.selectedScopeButtonIndex == 0 ? channel!.people : channel!.hashtags
237+ switch activeDataSource{
239+ dataSourcePeople = channel!.people
242+ dataSourceTags = channel!.tags
243+ dataSourcePeople = []
205− flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
245+ activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
206 tableView.reloadData()
246 tableView.reloadData()
210 func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
250 func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
211− dataSource = searchBar.selectedScopeButtonIndex == 0 ? channel!.people : channel!.hashtags
251+ switch activeDataSource{
253+ dataSourcePeople = channel!.people
256+ dataSourceTags = channel!.tags
257+ dataSourcePeople = []
212− flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
259+ activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
213 tableView.reloadData()
260 tableView.reloadData()
216 func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
263 func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
217− dataSource = searchBar.selectedScopeButtonIndex == 0 ? fullPeople : fullTags
264+ switch activeDataSource{
266+ dataSourcePeople = fullPeople
269+ dataSourceTags = fullTags
270+ dataSourcePeople = []
218− flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
272+ activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
219 tableView.reloadData()
273 tableView.reloadData()