@@ -25,14 +25,13 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
25 var channel: Model.Channels?
25 var channel: Model.Channels?
26 var myProtocol: DataDelegate?
26 var myProtocol: DataDelegate?
28− var dataSourcePeople: [Int] = []
28+ var fullTags: [String] = Array(Model.Channels.fullTags)
29− var dataSourceTags: [String] = []
31− var activeDataSource: ActiveTable = .people
30+ var dataSourcePeople: [Model.Users] = []
33− var fullPeople: [Int] = [5051,69]
32+ var dataSourceTags: [String] = []
35− var fullTags: [String] = ["# happy", "# sad", "# meme", "# shershakov", "# pupsik"]
34+ var activeDataSource: ActiveTable = .people
37 @IBOutlet weak var viewww: UIView!
36 @IBOutlet weak var viewww: UIView!
@@ -41,8 +40,29 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
41 override func viewDidLoad() {
40 override func viewDidLoad() {
42 super.viewDidLoad()
41 super.viewDidLoad()
44− dataSourcePeople = channel!.people
45 dataSourceTags = channel!.tags
43 dataSourceTags = channel!.tags
45+ tableView.reloadData()
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)
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!)
65+ func setUpController(){
47 tableView.delegate = self
67 tableView.delegate = self
48 tableView.dataSource = self
68 tableView.dataSource = self
@@ -58,21 +78,12 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
58 textField.addTarget(self, action: #selector(changedText(_:)), for: .editingChanged)
78 textField.addTarget(self, action: #selector(changedText(_:)), for: .editingChanged)
61− // TODO: update channel
81+
62− override func viewWillDisappear(_ animated: Bool) {
63− if let _ = channel?.id {
65− Model.updateChannel(with: channel!)
68− Model.createChannel(with: channel!)
72 @objc func changedText(_ textField: UITextField){
83 @objc func changedText(_ textField: UITextField){
73 navigationItem.title = textField.text
84 navigationItem.title = textField.text
74 channel?.name = textField.text!
85 channel?.name = textField.text!
75− myProtocol?.passData(for: index!, channel: channel!)
86+ // myProtocol?.passData(for: index!, channel: channel!)
78 func numberOfSections(in tableView: UITableView) -> Int {
89 func numberOfSections(in tableView: UITableView) -> Int {
@@ -89,14 +100,12 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
91 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
102 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
92− if searchBar.isFirstResponder {
93− if let cell = tableView.cellForRow(at: indexPath)
103+ if searchBar.isFirstResponder, let cell = tableView.cellForRow(at: indexPath)
96 if cell.accessoryType == .none{
105 if cell.accessoryType == .none{
97 switch searchBar.selectedScopeButtonIndex{
106 switch searchBar.selectedScopeButtonIndex{
99− channel?.people.append(dataSourcePeople[indexPath.row])
108+ channel?.people.append(dataSourcePeople[indexPath.row].id)
100 print("people = \(channel!.people)")
109 print("people = \(channel!.people)")
102 channel?.tags.append(dataSourceTags[indexPath.row])
111 channel?.tags.append(dataSourceTags[indexPath.row])
@@ -104,27 +113,33 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
108 cell.accessoryType = .checkmark
116 cell.accessoryType = .checkmark
110 cell.accessoryType = .none
118 cell.accessoryType = .none
112 switch searchBar.selectedScopeButtonIndex{
120 switch searchBar.selectedScopeButtonIndex{
114− channel?.people = channel!.people.filter{ $0 == fullPeople[indexPath.row] }
122+ let filtered = channel!.people.filter{ $0 != Model.Channels.fullPeople[indexPath.row].id }
115− // TODO: dataPass
123+ channel?.people = filtered
117− channel?.tags = channel!.tags.filter({ (s) -> Bool in
125+ let filtered = channel!.tags.filter{
118− !fullTags[indexPath.row].contains(s)
126+ !fullTags[indexPath.row].contains($0)
120− // TODO: dataPass
128+ channel?.tags = filtered
136+ switch activeDataSource{
138+ channel?.people.remove(at: indexPath.row)
140+ channel?.tags.remove(at: indexPath.row)
128 tableView.beginUpdates()
143 tableView.beginUpdates()
129 switch activeDataSource{
144 switch activeDataSource{
130 case .people:
145 case .people:
@@ -132,29 +147,20 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
132 case .tags:
147 case .tags:
133 dataSourceTags.remove(at: indexPath.row)
148 dataSourceTags.remove(at: indexPath.row)
135− switch searchBar.selectedScopeButtonIndex{
137− channel?.people.remove(at: indexPath.row)
140− channel?.tags.remove(at: indexPath.row)
145 tableView.deleteRows(at: [indexPath], with: .none)
150 tableView.deleteRows(at: [indexPath], with: .none)
146 tableView.endUpdates()
151 tableView.endUpdates()
150− func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
155+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
151 let cell = tableView.dequeueReusableCell(withIdentifier: itemCellId, for: indexPath)
157 let cell = tableView.dequeueReusableCell(withIdentifier: itemCellId, for: indexPath)
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]
154 cell.selectionStyle = .none
160 cell.selectionStyle = .none
156 if activeDataSource == .tags && channel!.tags.contains(dataSourceTags[indexPath.row]) ||
162 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)
159 cell.accessoryType = .checkmark
165 cell.accessoryType = .checkmark
@@ -200,7 +206,7 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
200 if searchText.isEmpty {
206 if searchText.isEmpty {
201 switch activeDataSource {
207 switch activeDataSource {
202 case .people:
208 case .people:
203− dataSourcePeople = fullPeople
209+ dataSourcePeople = Model.Channels.fullPeople
204 case .tags:
210 case .tags:
205 dataSourceTags = fullTags
211 dataSourceTags = fullTags
@@ -213,7 +219,10 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
213 func filterTableView(ind: Int, text: String){
219 func filterTableView(ind: Int, text: String){
214 switch ind {
220 switch ind {
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())
217 tableView.reloadData()
226 tableView.reloadData()
219 dataSourceTags = fullTags.filter({ (s) -> Bool in
228 dataSourceTags = fullTags.filter({ (s) -> Bool in
@@ -226,17 +235,16 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
228 func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
237 func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
238+ activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
229 if searchBar.isFirstResponder {
239 if searchBar.isFirstResponder {
230 switch activeDataSource{
240 switch activeDataSource{
231 case .people :
241 case .people :
232− dataSourcePeople = fullPeople
242+ dataSourcePeople = Model.Channels.fullPeople
233 dataSourceTags = []
243 dataSourceTags = []
234 case .tags:
244 case .tags:
235 dataSourceTags = fullTags
245 dataSourceTags = fullTags
236 dataSourcePeople = []
246 dataSourcePeople = []
238− print("\(searchBar.selectedScopeButtonIndex)")
239− activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
240 if (!(searchBar.text?.isEmpty ?? true))
248 if (!(searchBar.text?.isEmpty ?? true))
242 filterTableView(ind: selectedScope, text: searchBar.text!)
250 filterTableView(ind: selectedScope, text: searchBar.text!)
@@ -245,14 +253,14 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
245 } else if !(searchBar.isFirstResponder){
253 } else if !(searchBar.isFirstResponder){
246 switch activeDataSource{
254 switch activeDataSource{
247 case .people :
255 case .people :
248− dataSourcePeople = channel!.people
256+ dataSourcePeople = channel!.people.map({ (item) -> Model.Users in
257+ Model.Channels.fullPeopleDict[item]!
249 dataSourceTags = []
259 dataSourceTags = []
250 case .tags:
260 case .tags:
251 dataSourceTags = channel!.tags
261 dataSourceTags = channel!.tags
252 dataSourcePeople = []
262 dataSourcePeople = []
254− print(" not active \(searchBar.selectedScopeButtonIndex)")
255− activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
256 tableView.reloadData()
264 tableView.reloadData()
@@ -260,7 +268,9 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
260 func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
268 func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
261 switch activeDataSource{
269 switch activeDataSource{
262 case .people :
270 case .people :
263− dataSourcePeople = channel!.people
271+ dataSourcePeople = channel!.people.map({ (item) -> Model.Users in
272+ Model.Channels.fullPeopleDict[item]!
264 dataSourceTags = []
274 dataSourceTags = []
265 case .tags:
275 case .tags:
266 dataSourceTags = channel!.tags
276 dataSourceTags = channel!.tags
@@ -273,7 +283,7 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
273 func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
283 func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
274 switch activeDataSource{
284 switch activeDataSource{
275 case .people :
285 case .people :
276− dataSourcePeople = fullPeople
286+ dataSourcePeople = Model.Channels.fullPeople
277 dataSourceTags = []
287 dataSourceTags = []
278 case .tags:
288 case .tags:
279 dataSourceTags = fullTags
289 dataSourceTags = fullTags