git.iko.soy/dashared/GDproject
on master help sign in
almost fixed editing channel
commit7d736e3319cca9fb45cacdda2fd272d7fdf5ea76 authorDarya Rednikina <rednikina.com@yandex.ru> dateWed 13 Mar 2019, 22:48 (8 years ago) parent49bc15b
4 files changed+107−58
GDproject/Controller/ News and channels/ChannelController.swift+64−54 GDproject/Controller/ News and channels/ChannelListController.swift+4−1 GDproject/Controller/ News and channels/SimplifiedChannelsList.swift+11 GDproject/Simple model/Model.swift+28−3
GDproject/Controller/ News and channels/ChannelController.swift+64−54
@@ -25,14 +25,13 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
2525 var channel: Model.Channels?
2626 var myProtocol: DataDelegate?
2727
28− var dataSourcePeople: [Int] = []
29− var dataSourceTags: [String] = []
28+ var fullTags: [String] = Array(Model.Channels.fullTags)
3029
31− var activeDataSource: ActiveTable = .people
30+ var dataSourcePeople: [Model.Users] = []
3231
33− var fullPeople: [Int] = [5051,69]
32+ var dataSourceTags: [String] = []
3433
35− var fullTags: [String] = ["# happy", "# sad", "# meme", "# shershakov", "# pupsik"]
34+ var activeDataSource: ActiveTable = .people
3635
3736 @IBOutlet weak var viewww: UIView!
3837
@@ -41,8 +40,29 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
4140 override func viewDidLoad() {
4241 super.viewDidLoad()
4342
44− dataSourcePeople = channel!.people
4543 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(){
4666
4767 tableView.delegate = self
4868 tableView.dataSource = self
@@ -58,21 +78,12 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
5878 textField.addTarget(self, action: #selector(changedText(_:)), for: .editingChanged)
5979 }
6080
61− // TODO: update channel
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− }
81+
7182
7283 @objc func changedText(_ textField: UITextField){
7384 navigationItem.title = textField.text
7485 channel?.name = textField.text!
75− myProtocol?.passData(for: index!, channel: channel!)
86+ // myProtocol?.passData(for: index!, channel: channel!)
7687 }
7788
7889 func numberOfSections(in tableView: UITableView) -> Int {
@@ -89,14 +100,12 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
89100 }
90101
91102 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
92− if searchBar.isFirstResponder {
93− if let cell = tableView.cellForRow(at: indexPath)
94− {
95−
103+ if searchBar.isFirstResponder, let cell = tableView.cellForRow(at: indexPath)
104+ {
96105 if cell.accessoryType == .none{
97106 switch searchBar.selectedScopeButtonIndex{
98107 case 0:
99− channel?.people.append(dataSourcePeople[indexPath.row])
108+ channel?.people.append(dataSourcePeople[indexPath.row].id)
100109 print("people = \(channel!.people)")
101110 case 1:
102111 channel?.tags.append(dataSourceTags[indexPath.row])
@@ -104,27 +113,33 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
104113 default:
105114 break
106115 }
107−
108116 cell.accessoryType = .checkmark
109117 } else {
110118 cell.accessoryType = .none
111119
112120 switch searchBar.selectedScopeButtonIndex{
113121 case 0:
114− channel?.people = channel!.people.filter{ $0 == fullPeople[indexPath.row] }
115− // TODO: dataPass
122+ let filtered = channel!.people.filter{ $0 != Model.Channels.fullPeople[indexPath.row].id }
123+ channel?.people = filtered
116124 case 1:
117− channel?.tags = channel!.tags.filter({ (s) -> Bool in
118− !fullTags[indexPath.row].contains(s)
119− })
120− // TODO: dataPass
125+ let filtered = channel!.tags.filter{
126+ !fullTags[indexPath.row].contains($0)
127+ }
128+ channel?.tags = filtered
121129 default:
122130 break
123131 }
124132 }
125− }
126− } else
133+ }
134+ else
127135 {
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+
128143 tableView.beginUpdates()
129144 switch activeDataSource{
130145 case .people:
@@ -132,29 +147,20 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
132147 case .tags:
133148 dataSourceTags.remove(at: indexPath.row)
134149 }
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− }
145150 tableView.deleteRows(at: [indexPath], with: .none)
146151 tableView.endUpdates()
147152 }
148153 }
149154
150− func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
155+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
156+ {
151157 let cell = tableView.dequeueReusableCell(withIdentifier: itemCellId, for: indexPath)
152158
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]
154160 cell.selectionStyle = .none
155161
156162 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)
158164 {
159165 cell.accessoryType = .checkmark
160166 } else {
@@ -200,7 +206,7 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
200206 if searchText.isEmpty {
201207 switch activeDataSource {
202208 case .people:
203− dataSourcePeople = fullPeople
209+ dataSourcePeople = Model.Channels.fullPeople
204210 case .tags:
205211 dataSourceTags = fullTags
206212 }
@@ -213,7 +219,10 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
213219 func filterTableView(ind: Int, text: String){
214220 switch ind {
215221 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+ }
217226 tableView.reloadData()
218227 case 1:
219228 dataSourceTags = fullTags.filter({ (s) -> Bool in
@@ -226,17 +235,16 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
226235 }
227236
228237 func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
238+ activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
229239 if searchBar.isFirstResponder {
230240 switch activeDataSource{
231241 case .people :
232− dataSourcePeople = fullPeople
242+ dataSourcePeople = Model.Channels.fullPeople
233243 dataSourceTags = []
234244 case .tags:
235245 dataSourceTags = fullTags
236246 dataSourcePeople = []
237247 }
238− print("\(searchBar.selectedScopeButtonIndex)")
239− activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
240248 if (!(searchBar.text?.isEmpty ?? true))
241249 {
242250 filterTableView(ind: selectedScope, text: searchBar.text!)
@@ -245,14 +253,14 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
245253 } else if !(searchBar.isFirstResponder){
246254 switch activeDataSource{
247255 case .people :
248− dataSourcePeople = channel!.people
256+ dataSourcePeople = channel!.people.map({ (item) -> Model.Users in
257+ Model.Channels.fullPeopleDict[item]!
258+ })
249259 dataSourceTags = []
250260 case .tags:
251261 dataSourceTags = channel!.tags
252262 dataSourcePeople = []
253263 }
254− print(" not active \(searchBar.selectedScopeButtonIndex)")
255− activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
256264 tableView.reloadData()
257265 }
258266 }
@@ -260,7 +268,9 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
260268 func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
261269 switch activeDataSource{
262270 case .people :
263− dataSourcePeople = channel!.people
271+ dataSourcePeople = channel!.people.map({ (item) -> Model.Users in
272+ Model.Channels.fullPeopleDict[item]!
273+ })
264274 dataSourceTags = []
265275 case .tags:
266276 dataSourceTags = channel!.tags
@@ -273,7 +283,7 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
273283 func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
274284 switch activeDataSource{
275285 case .people :
276− dataSourcePeople = fullPeople
286+ dataSourcePeople = Model.Channels.fullPeople
277287 dataSourceTags = []
278288 case .tags:
279289 dataSourceTags = fullTags
GDproject/Controller/ News and channels/ChannelListController.swift+4−1
@@ -49,13 +49,16 @@ class ChannelListController: UITableViewController, DataDelegate {
4949 }
5050
5151 func passData(for row: Int, channel: Model.Channels) {
52− dataSource[row] = channel
52+ // dataSource[row] = channel
5353 }
5454
5555 var searchController = UISearchController(searchResultsController: nil)
5656
5757 override func viewDidLoad() {
5858 super.viewDidLoad()
59+
60+ Model.usersAllGet()
61+
5962 setUpNavigationBar()
6063 navigationItem.title = "Channels"
6164 searchController.searchResultsUpdater = self
GDproject/Controller/ News and channels/SimplifiedChannelsList.swift+11
@@ -9,6 +9,7 @@
99
1010/// class for ADDing person to existing (or new) channel: like playlist in itunes
1111import UIKit
12+import TinyConstraints
1213
1314class SimplifiedChannelsList: UITableViewController {
1415
@@ -23,6 +24,8 @@ class SimplifiedChannelsList: UITableViewController {
2324 override func viewDidLoad() {
2425 super.viewDidLoad()
2526 setUpNavigationButtons()
27+ navigationItem.largeTitleDisplayMode = .never
28+ self.navigationItem.setHidesBackButton(true, animated:true)
2629 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
2730 }
2831
@@ -53,6 +56,14 @@ class SimplifiedChannelsList: UITableViewController {
5356
5457 override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
5558 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))
5667 return viewHeader
5768 }
5869
GDproject/Simple model/Model.swift+28−3
@@ -27,6 +27,7 @@ class Model{
2727 static let postsForUserURL = URL(string:"\(baseUrl)/posts/forUser")!
2828 static let postsPublishURL = URL(string:"\(baseUrl)/posts/publish")!
2929 static let usersURL = URL(string:"\(baseUrl)/users")!
30+ static let usersAllURL = URL(string:"\(baseUrl)/users/all")!
3031 static let channelsGetURL = URL(string: "\(baseUrl)/channels/get")!
3132 static let channelsUpdateURL = URL(string: "\(baseUrl)/channels/update")!
3233 static let channelsListURL = URL(string: "\(baseUrl)/channels")!
@@ -125,7 +126,8 @@ class Model{
125126 struct Channels: Codable {
126127
127128 static var fullTags = Set<String>()
128− static var fullPeople = [Int:Users]()
129+ static var fullPeople = [Users]()
130+ static var fullPeopleDict = [Int:Users]()
129131
130132 var people: [Int]
131133 var name: String
@@ -294,11 +296,12 @@ class Model{
294296
295297 guard let users = try? decoder.decode([Users].self, from: json) else { return }
296298
299+ var dict: [Int:Users] = [:]
297300 users.forEach({ (user) in
298− Model.idUser[user.id] = user
301+ dict[user.id] = user
299302 })
300303
301− completion(Model.idUser)
304+ completion(dict)
302305 }
303306 }
304307
@@ -392,4 +395,26 @@ class Model{
392395 isValidTocken?(response.response?.statusCode ?? 498)
393396 }
394397 }
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+ }
395420}