@@ -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,14 +22,16 @@ 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 fullTags: [String] = Array(Model.Channels.fullTags)
30+ var dataSourcePeople: [Model.Users] = []
25− var fullPeople: [String] = ["Other Ann", "Other Ilya", "Other Kostya", "Pasha", "Valera"]
32+ var dataSourceTags: [String] = []
27− var fullTags: [String] = ["# happy", "# sad", "# meme", "# shershakov", "# pupsik"]
34+ var activeDataSource: ActiveTable = .people
29 @IBOutlet weak var viewww: UIView!
36 @IBOutlet weak var viewww: UIView!
@@ -32,23 +39,58 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
33 override func viewDidLoad() {
40 override func viewDidLoad() {
34 super.viewDidLoad()
41 super.viewDidLoad()
43+ dataSourceTags = channel!.tags
45+ tableView.reloadData()
48+ var reloadtable: Bool = false {
50+ tableView.reloadData()
54+ override func viewWillAppear(_ animated: Bool) {
55+ super.viewWillAppear(animated)
56+ Model.getUsers(for: channel!.people) { [weak self] (people) in
57+ self?.dataSourcePeople = Array(people.values)
58+ self?.reloadtable = true
62+ // TODO: update channel
63+ override func viewWillDisappear(_ animated: Bool) {
64+ super.viewWillDisappear(animated)
65+ if let _ = channel?.id { print("update")
66+ Model.updateChannel(with: channel!)
67+ } else { print("create")
68+ Model.createChannel(with: channel!)
72+ func setUpController(){
35 tableView.delegate = self
74 tableView.delegate = self
36 tableView.dataSource = self
75 tableView.dataSource = self
37− dataSource = channel!.people
38 tableView.register(UITableViewCell.self, forCellReuseIdentifier: itemCellId)
76 tableView.register(UITableViewCell.self, forCellReuseIdentifier: itemCellId)
39 tableView.reloadData()
77 tableView.reloadData()
40 textField.delegate = self
78 textField.delegate = self
41 searchBar.delegate = self
79 searchBar.delegate = self
42 searchBar.selectedScopeButtonIndex = 0
80 searchBar.selectedScopeButtonIndex = 0
43− navigationItem.title = channel?.title
82+ navigationItem.title = channel?.name
44− textField.placeholder = channel?.title
83+ textField.placeholder = channel?.name
45 textField.addTarget(self, action: #selector(changedText(_:)), for: .editingChanged)
85 textField.addTarget(self, action: #selector(changedText(_:)), for: .editingChanged)
48 @objc func changedText(_ textField: UITextField){
90 @objc func changedText(_ textField: UITextField){
49 navigationItem.title = textField.text
91 navigationItem.title = textField.text
50− channel?.title = textField.text!
92+ channel?.name = textField.text!
51− myProtocol?.passData(for: index!, channel: channel!)
93+ // myProtocol?.passData(for: index!, channel: channel!)
54 func numberOfSections(in tableView: UITableView) -> Int {
96 func numberOfSections(in tableView: UITableView) -> Int {
@@ -56,72 +98,76 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
58 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
100 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
101+ switch activeDataSource {
59− return dataSource.count
103+ return dataSourcePeople.count
105+ return dataSourceTags.count
62 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
109 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
63− if searchBar.isFirstResponder {
64− if let cell = tableView.cellForRow(at: indexPath)
110+ if searchBar.isFirstResponder, let cell = tableView.cellForRow(at: indexPath)
67 if cell.accessoryType == .none{
112 if cell.accessoryType == .none{
68 switch searchBar.selectedScopeButtonIndex{
113 switch searchBar.selectedScopeButtonIndex{
70− channel?.people.append(dataSource[indexPath.row])
115+ channel?.people.append(dataSourcePeople[indexPath.row].id)
71 print("people = \(channel!.people)")
116 print("people = \(channel!.people)")
73− channel?.hashtags.append(dataSource[indexPath.row])
118+ channel?.tags.append(dataSourceTags[indexPath.row])
74− print("hashtags = \(channel!.hashtags)")
119+ print("hashtags = \(channel!.tags)")
79 cell.accessoryType = .checkmark
123 cell.accessoryType = .checkmark
81 cell.accessoryType = .none
125 cell.accessoryType = .none
83 switch searchBar.selectedScopeButtonIndex{
127 switch searchBar.selectedScopeButtonIndex{
85− channel?.people = channel!.people.filter({ (s) -> Bool in
86− !fullPeople[indexPath.row].contains(s)
129+ let filtered = channel!.people.filter{ $0 != Model.Channels.fullPeople[indexPath.row].id }
87− })
130+ channel?.people = filtered
90− channel?.hashtags = channel!.hashtags.filter({ (s) -> Bool in
132+ let filtered = channel!.tags.filter{
91− !fullTags[indexPath.row].contains(s)
133+ !fullTags[indexPath.row].contains($0)
93− // TODO: dataPass
135+ channel?.tags = filtered
101− tableView.beginUpdates()
143+ switch activeDataSource{
102− dataSource.remove(at: indexPath.row)
103− switch searchBar.selectedScopeButtonIndex{
104− case 0:
144+ case .people:
105 channel?.people.remove(at: indexPath.row)
145 channel?.people.remove(at: indexPath.row)
107− case 1:
146+ case .tags:
108− channel?.hashtags.remove(at: indexPath.row)
147+ channel?.tags.remove(at: indexPath.row)
109− // TODO: dataPass
148+ }
111− break
150+ tableView.beginUpdates()
151+ switch activeDataSource{
153+ dataSourcePeople.remove(at: indexPath.row)
155+ dataSourceTags.remove(at: indexPath.row)
113 tableView.deleteRows(at: [indexPath], with: .none)
157 tableView.deleteRows(at: [indexPath], with: .none)
114 tableView.endUpdates()
158 tableView.endUpdates()
118− func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
162+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
119 let cell = tableView.dequeueReusableCell(withIdentifier: itemCellId, for: indexPath)
164 let cell = tableView.dequeueReusableCell(withIdentifier: itemCellId, for: indexPath)
121− cell.textLabel?.text = dataSource[indexPath.row]
166+ cell.textLabel?.text = activeDataSource == .people ? "\(dataSourcePeople[indexPath.row].firstName) \(dataSourcePeople[indexPath.row].middleName) \(dataSourcePeople[indexPath.row].lastName)" : dataSourceTags[indexPath.row]
122 cell.selectionStyle = .none
167 cell.selectionStyle = .none
169+ if activeDataSource == .tags && channel!.tags.contains(dataSourceTags[indexPath.row]) ||
124− if channel!.hashtags.contains(dataSource[indexPath.row]) || channel!.people.contains(dataSource[indexPath.row])
170+ activeDataSource == .people && channel!.people.contains(dataSourcePeople[indexPath.row].id)
126 cell.accessoryType = .checkmark
172 cell.accessoryType = .checkmark
@@ -147,7 +193,7 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
148 let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 70))
194 let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 70))
150− if flag {
196+ if activeDataSource == .tags {
151 searchBar.selectedScopeButtonIndex = 1
197 searchBar.selectedScopeButtonIndex = 1
153 searchBar.selectedScopeButtonIndex = 0
199 searchBar.selectedScopeButtonIndex = 0
@@ -165,7 +211,12 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
166 func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
212 func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
167 if searchText.isEmpty {
213 if searchText.isEmpty {
168− dataSource = searchBar.selectedScopeButtonIndex == 0 ? fullPeople : fullTags
214+ switch activeDataSource {
216+ dataSourcePeople = Model.Channels.fullPeople
218+ dataSourceTags = fullTags
169 tableView.reloadData()
220 tableView.reloadData()
171 filterTableView(ind: searchBar.selectedScopeButtonIndex, text: searchText)
222 filterTableView(ind: searchBar.selectedScopeButtonIndex, text: searchText)
@@ -175,12 +226,13 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
175 func filterTableView(ind: Int, text: String){
226 func filterTableView(ind: Int, text: String){
176 switch ind {
227 switch ind {
178− dataSource = fullPeople.filter({ (s) -> Bool in
229+ dataSourcePeople = Model.Channels.fullPeople.filter {
230+ let fullName = "\($0.firstName) \($0.middleName) \($0.lastName)"
179− s.lowercased().contains(text.lowercased())
231+ return fullName.lowercased().contains(text.lowercased())
181 tableView.reloadData()
233 tableView.reloadData()
183− dataSource = fullTags.filter({ (s) -> Bool in
235+ dataSourceTags = fullTags.filter({ (s) -> Bool in
184 s.lowercased().contains(text.lowercased())
236 s.lowercased().contains(text.lowercased())
186 tableView.reloadData()
238 tableView.reloadData()
@@ -189,33 +241,62 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
194 func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
244 func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
245+ activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
195 if searchBar.isFirstResponder {
246 if searchBar.isFirstResponder {
196− dataSource = searchBar.selectedScopeButtonIndex == 0 ? fullPeople : fullTags
247+ switch activeDataSource{
197− flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
248+ case .people :
249+ dataSourcePeople = Model.Channels.fullPeople
252+ dataSourceTags = fullTags
253+ dataSourcePeople = []
198 if (!(searchBar.text?.isEmpty ?? true))
255 if (!(searchBar.text?.isEmpty ?? true))
200 filterTableView(ind: selectedScope, text: searchBar.text!)
257 filterTableView(ind: selectedScope, text: searchBar.text!)
202 tableView.reloadData()
259 tableView.reloadData()
203 } else if !(searchBar.isFirstResponder){
260 } else if !(searchBar.isFirstResponder){
204− dataSource = searchBar.selectedScopeButtonIndex == 0 ? channel!.people : channel!.hashtags
261+ switch activeDataSource{
205− flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
262+ case .people :
263+ dataSourcePeople = channel!.people.map({ (item) -> Model.Users in
264+ Model.Channels.fullPeopleDict[item]!
268+ dataSourceTags = channel!.tags
269+ dataSourcePeople = []
206 tableView.reloadData()
271 tableView.reloadData()
210 func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
275 func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
211− dataSource = searchBar.selectedScopeButtonIndex == 0 ? channel!.people : channel!.hashtags
276+ switch activeDataSource{
278+ dataSourcePeople = channel!.people.map({ (item) -> Model.Users in
279+ Model.Channels.fullPeopleDict[item]!
283+ dataSourceTags = channel!.tags
284+ dataSourcePeople = []
212− flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
286+ activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
213 tableView.reloadData()
287 tableView.reloadData()
216 func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
290 func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
217− dataSource = searchBar.selectedScopeButtonIndex == 0 ? fullPeople : fullTags
291+ switch activeDataSource{
293+ dataSourcePeople = Model.Channels.fullPeople
296+ dataSourceTags = fullTags
297+ dataSourcePeople = []
218− flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
299+ activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
219 tableView.reloadData()
300 tableView.reloadData()