git.iko.soy/dashared/GDproject
on master help sign in
almost fixed editing channel
7d736e3Darya Rednikina
GDproject/Controller/ News and channels/ChannelController.swift+64−54
@@ -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?
27
27
28− var dataSourcePeople: [Int] = []
28+ var fullTags: [String] = Array(Model.Channels.fullTags)
29− var dataSourceTags: [String] = []
30
29
31− var activeDataSource: ActiveTable = .people
30+ var dataSourcePeople: [Model.Users] = []
32
31
33− var fullPeople: [Int] = [5051,69]
32+ var dataSourceTags: [String] = []
34
33
35− var fullTags: [String] = ["# happy", "# sad", "# meme", "# shershakov", "# pupsik"]
34+ var activeDataSource: ActiveTable = .people
36
35
37 @IBOutlet weak var viewww: UIView!
36 @IBOutlet weak var viewww: UIView!
38
37
@@ -41,8 +40,29 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
41 override func viewDidLoad() {
40 override func viewDidLoad() {
42 super.viewDidLoad()
41 super.viewDidLoad()
43
42
44− dataSourcePeople = channel!.people
45 dataSourceTags = channel!.tags
43 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(){
46
66
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)
59 }
79 }
60
80
61− // TODO: update channel
81+
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− }
71
82
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!)
76 }
87 }
77
88
78 func numberOfSections(in tableView: UITableView) -> Int {
89 func numberOfSections(in tableView: UITableView) -> Int {
@@ -89,14 +100,12 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
89 }
100 }
90
101
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)
94− {
104+ {
95−
96 if cell.accessoryType == .none{
105 if cell.accessoryType == .none{
97 switch searchBar.selectedScopeButtonIndex{
106 switch searchBar.selectedScopeButtonIndex{
98 case 0:
107 case 0:
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)")
101 case 1:
110 case 1:
102 channel?.tags.append(dataSourceTags[indexPath.row])
111 channel?.tags.append(dataSourceTags[indexPath.row])
@@ -104,27 +113,33 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
104 default:
113 default:
105 break
114 break
106 }
115 }
107−
108 cell.accessoryType = .checkmark
116 cell.accessoryType = .checkmark
109 } else {
117 } else {
110 cell.accessoryType = .none
118 cell.accessoryType = .none
111
119
112 switch searchBar.selectedScopeButtonIndex{
120 switch searchBar.selectedScopeButtonIndex{
113 case 0:
121 case 0:
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
116 case 1:
124 case 1:
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)
119− })
127+ }
120− // TODO: dataPass
128+ channel?.tags = filtered
121 default:
129 default:
122 break
130 break
123 }
131 }
124 }
132 }
125− }
133+ }
126− } else
134+ else
127 {
135 {
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+
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)
134 }
149 }
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− }
145 tableView.deleteRows(at: [indexPath], with: .none)
150 tableView.deleteRows(at: [indexPath], with: .none)
146 tableView.endUpdates()
151 tableView.endUpdates()
147 }
152 }
148 }
153 }
149
154
150− func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
155+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
156+ {
151 let cell = tableView.dequeueReusableCell(withIdentifier: itemCellId, for: indexPath)
157 let cell = tableView.dequeueReusableCell(withIdentifier: itemCellId, for: indexPath)
152
158
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
155
161
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)
158 {
164 {
159 cell.accessoryType = .checkmark
165 cell.accessoryType = .checkmark
160 } else {
166 } else {
@@ -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
206 }
212 }
@@ -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 {
215 case 0:
221 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+ }
217 tableView.reloadData()
226 tableView.reloadData()
218 case 1:
227 case 1:
219 dataSourceTags = fullTags.filter({ (s) -> Bool in
228 dataSourceTags = fullTags.filter({ (s) -> Bool in
@@ -226,17 +235,16 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
226 }
235 }
227
236
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 = []
237 }
247 }
238− print("\(searchBar.selectedScopeButtonIndex)")
239− activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
240 if (!(searchBar.text?.isEmpty ?? true))
248 if (!(searchBar.text?.isEmpty ?? true))
241 {
249 {
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]!
258+ })
249 dataSourceTags = []
259 dataSourceTags = []
250 case .tags:
260 case .tags:
251 dataSourceTags = channel!.tags
261 dataSourceTags = channel!.tags
252 dataSourcePeople = []
262 dataSourcePeople = []
253 }
263 }
254− print(" not active \(searchBar.selectedScopeButtonIndex)")
255− activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
256 tableView.reloadData()
264 tableView.reloadData()
257 }
265 }
258 }
266 }
@@ -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]!
273+ })
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
GDproject/Controller/ News and channels/ChannelListController.swift+4−1
@@ -49,13 +49,16 @@ class ChannelListController: UITableViewController, DataDelegate {
49 }
49 }
50
50
51 func passData(for row: Int, channel: Model.Channels) {
51 func passData(for row: Int, channel: Model.Channels) {
52− dataSource[row] = channel
52+ // dataSource[row] = channel
53 }
53 }
54
54
55 var searchController = UISearchController(searchResultsController: nil)
55 var searchController = UISearchController(searchResultsController: nil)
56
56
57 override func viewDidLoad() {
57 override func viewDidLoad() {
58 super.viewDidLoad()
58 super.viewDidLoad()
59+
60+ Model.usersAllGet()
61+
59 setUpNavigationBar()
62 setUpNavigationBar()
60 navigationItem.title = "Channels"
63 navigationItem.title = "Channels"
61 searchController.searchResultsUpdater = self
64 searchController.searchResultsUpdater = self
GDproject/Controller/ News and channels/SimplifiedChannelsList.swift+11
@@ -9,6 +9,7 @@
9
9
10/// class for ADDing person to existing (or new) channel: like playlist in itunes
10/// class for ADDing person to existing (or new) channel: like playlist in itunes
11import UIKit
11import UIKit
12+import TinyConstraints
12
13
13class SimplifiedChannelsList: UITableViewController {
14class SimplifiedChannelsList: UITableViewController {
14
15
@@ -23,6 +24,8 @@ class SimplifiedChannelsList: UITableViewController {
23 override func viewDidLoad() {
24 override func viewDidLoad() {
24 super.viewDidLoad()
25 super.viewDidLoad()
25 setUpNavigationButtons()
26 setUpNavigationButtons()
27+ navigationItem.largeTitleDisplayMode = .never
28+ self.navigationItem.setHidesBackButton(true, animated:true)
26 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
29 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
27 }
30 }
28
31
@@ -53,6 +56,14 @@ class SimplifiedChannelsList: UITableViewController {
53
56
54 override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
57 override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
55 let viewHeader = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 30))
58 let viewHeader = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 30))
59+ let label = UILabel()
60+
61+ label.text = "All channels"
62+ label.font = UIFont.boldSystemFont(ofSize: 22)
63+ label.textColor = .black
64+
65+ viewHeader.addSubview(label)
66+ label.edgesToSuperview(insets: .left(16))
56 return viewHeader
67 return viewHeader
57 }
68 }
58
69
GDproject/Simple model/Model.swift+28−3
@@ -27,6 +27,7 @@ class Model{
27 static let postsForUserURL = URL(string:"\(baseUrl)/posts/forUser")!
27 static let postsForUserURL = URL(string:"\(baseUrl)/posts/forUser")!
28 static let postsPublishURL = URL(string:"\(baseUrl)/posts/publish")!
28 static let postsPublishURL = URL(string:"\(baseUrl)/posts/publish")!
29 static let usersURL = URL(string:"\(baseUrl)/users")!
29 static let usersURL = URL(string:"\(baseUrl)/users")!
30+ static let usersAllURL = URL(string:"\(baseUrl)/users/all")!
30 static let channelsGetURL = URL(string: "\(baseUrl)/channels/get")!
31 static let channelsGetURL = URL(string: "\(baseUrl)/channels/get")!
31 static let channelsUpdateURL = URL(string: "\(baseUrl)/channels/update")!
32 static let channelsUpdateURL = URL(string: "\(baseUrl)/channels/update")!
32 static let channelsListURL = URL(string: "\(baseUrl)/channels")!
33 static let channelsListURL = URL(string: "\(baseUrl)/channels")!
@@ -125,7 +126,8 @@ class Model{
125 struct Channels: Codable {
126 struct Channels: Codable {
126
127
127 static var fullTags = Set<String>()
128 static var fullTags = Set<String>()
128− static var fullPeople = [Int:Users]()
129+ static var fullPeople = [Users]()
130+ static var fullPeopleDict = [Int:Users]()
129
131
130 var people: [Int]
132 var people: [Int]
131 var name: String
133 var name: String
@@ -294,11 +296,12 @@ class Model{
294
296
295 guard let users = try? decoder.decode([Users].self, from: json) else { return }
297 guard let users = try? decoder.decode([Users].self, from: json) else { return }
296
298
299+ var dict: [Int:Users] = [:]
297 users.forEach({ (user) in
300 users.forEach({ (user) in
298− Model.idUser[user.id] = user
301+ dict[user.id] = user
299 })
302 })
300
303
301− completion(Model.idUser)
304+ completion(dict)
302 }
305 }
303 }
306 }
304
307
@@ -392,4 +395,26 @@ class Model{
392 isValidTocken?(response.response?.statusCode ?? 498)
395 isValidTocken?(response.response?.statusCode ?? 498)
393 }
396 }
394 }
397 }
398+
399+ static func usersAllGet() {
400+ var request = URLRequest(url: usersAllURL)
401+ request.httpMethod = "POST"
402+
403+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
404+ AF.request(request).response {
405+ (response) in
406+ guard let json = response.data else { return }
407+
408+ guard let users = try? decoder.decode([Users].self, from: json) else { return }
409+
410+ Channels.fullPeople = users
411+ Channels.fullPeopleDict = users.reduce([Int: Users]()) { (dict, person) -> [Int: Users] in
412+ var dict = dict
413+ dict[person.id] = person
414+ return dict
415+ }
416+
417+ isValidTocken?(response.response?.statusCode ?? 498)
418+ }
419+ }
395}
420}