git.iko.soy/dashared/GDproject
on master help sign in
still working on denis new version. channels can be listed. a lot to be done on channels. almost moved to new API
commit8cecd53c1ed29c02c3178709528d83a6d7eeb850 authorDarya Rednikina <rednikina.com@yandex.ru> dateWed 13 Mar 2019, 00:24 (8 years ago) parentfb5ea63
6 files changed+244−99
GDproject/Controller/ News and channels/ChannelController.swift+90−36 GDproject/Controller/ News and channels/ChannelListController.swift+32−29 GDproject/Controller/ News and channels/ChannelsCoordinator.swift+2−2 GDproject/Controller/ News and channels/NewsController.swift+32−11 GDproject/Controller/ News and channels/NewsVC.swift+1−1 GDproject/Simple model/Model.swift+87−20
GDproject/Controller/ News and channels/ChannelController.swift+90−36
@@ -9,7 +9,12 @@
99import UIKit
1010
1111protocol DataDelegate {
12− func passData(for row: Int, channel: Channel)
12+ func passData(for row: Int, channel: Model.Channels)
13+}
14+
15+enum ActiveTable {
16+ case tags
17+ case people
1318}
1419
1520class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UISearchBarDelegate
@@ -17,12 +22,15 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
1722 @IBOutlet weak var tableView: UITableView!
1823
1924 var index: Int?
20− var channel: Channel?
25+ var channel: Model.Channels?
2126 var myProtocol: DataDelegate?
2227
23− var dataSource: [String] = []
28+ var dataSourcePeople: [Int] = []
29+ var dataSourceTags: [String] = []
30+
31+ var activeDataSource: ActiveTable = .people
2432
25− var fullPeople: [String] = ["Other Ann", "Other Ilya", "Other Kostya", "Pasha", "Valera"]
33+ var fullPeople: [Int] = [5051,69]
2634
2735 var fullTags: [String] = ["# happy", "# sad", "# meme", "# shershakov", "# pupsik"]
2836
@@ -32,22 +40,30 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
3240
3341 override func viewDidLoad() {
3442 super.viewDidLoad()
43+
44+ dataSourcePeople = channel!.people
45+ dataSourceTags = channel!.tags
46+
3547 tableView.delegate = self
3648 tableView.dataSource = self
37− dataSource = channel!.people
3849 tableView.register(UITableViewCell.self, forCellReuseIdentifier: itemCellId)
3950 tableView.reloadData()
4051 textField.delegate = self
4152 searchBar.delegate = self
4253 searchBar.selectedScopeButtonIndex = 0
43− navigationItem.title = channel?.title
44− textField.placeholder = channel?.title
54+
55+ navigationItem.title = channel?.name
56+ textField.placeholder = channel?.name
57+
4558 textField.addTarget(self, action: #selector(changedText(_:)), for: .editingChanged)
4659 }
4760
61+ override func viewWillDisappear(_ animated: Bool) {
62+ Model.createChannel(with: channel!)
63+ }
4864 @objc func changedText(_ textField: UITextField){
4965 navigationItem.title = textField.text
50− channel?.title = textField.text!
66+ channel?.name = textField.text!
5167 myProtocol?.passData(for: index!, channel: channel!)
5268 }
5369
@@ -56,7 +72,12 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
5672 }
5773
5874 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
59− return dataSource.count
75+ switch activeDataSource {
76+ case .people:
77+ return dataSourcePeople.count
78+ case .tags:
79+ return dataSourceTags.count
80+ }
6081 }
6182
6283 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
@@ -67,11 +88,11 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
6788 if cell.accessoryType == .none{
6889 switch searchBar.selectedScopeButtonIndex{
6990 case 0:
70− channel?.people.append(dataSource[indexPath.row])
91+ channel?.people.append(dataSourcePeople[indexPath.row])
7192 print("people = \(channel!.people)")
7293 case 1:
73− channel?.hashtags.append(dataSource[indexPath.row])
74− print("hashtags = \(channel!.hashtags)")
94+ channel?.tags.append(dataSourceTags[indexPath.row])
95+ print("hashtags = \(channel!.tags)")
7596 default:
7697 break
7798 }
@@ -82,12 +103,10 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
82103
83104 switch searchBar.selectedScopeButtonIndex{
84105 case 0:
85− channel?.people = channel!.people.filter({ (s) -> Bool in
86− !fullPeople[indexPath.row].contains(s)
87− })
106+ channel?.people = channel!.people.filter{ $0 == fullPeople[indexPath.row] }
88107 // TODO: dataPass
89108 case 1:
90− channel?.hashtags = channel!.hashtags.filter({ (s) -> Bool in
109+ channel?.tags = channel!.tags.filter({ (s) -> Bool in
91110 !fullTags[indexPath.row].contains(s)
92111 })
93112 // TODO: dataPass
@@ -99,13 +118,18 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
99118 } else
100119 {
101120 tableView.beginUpdates()
102− dataSource.remove(at: indexPath.row)
121+ switch activeDataSource{
122+ case .people:
123+ dataSourcePeople.remove(at: indexPath.row)
124+ case .tags:
125+ dataSourceTags.remove(at: indexPath.row)
126+ }
103127 switch searchBar.selectedScopeButtonIndex{
104128 case 0:
105129 channel?.people.remove(at: indexPath.row)
106130 // TODO: dataPass
107131 case 1:
108− channel?.hashtags.remove(at: indexPath.row)
132+ channel?.tags.remove(at: indexPath.row)
109133 // TODO: dataPass
110134 default:
111135 break
@@ -118,10 +142,11 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
118142 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
119143 let cell = tableView.dequeueReusableCell(withIdentifier: itemCellId, for: indexPath)
120144
121− cell.textLabel?.text = dataSource[indexPath.row]
145+ cell.textLabel?.text = activeDataSource == .people ? String(dataSourcePeople[indexPath.row]) : dataSourceTags[indexPath.row]
122146 cell.selectionStyle = .none
123147
124− if channel!.hashtags.contains(dataSource[indexPath.row]) || channel!.people.contains(dataSource[indexPath.row])
148+ if activeDataSource == .tags && channel!.tags.contains(dataSourceTags[indexPath.row]) ||
149+ activeDataSource == .people && channel!.people.contains(dataSourcePeople[indexPath.row])
125150 {
126151 cell.accessoryType = .checkmark
127152 } else {
@@ -147,7 +172,7 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
147172 {
148173 let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 70))
149174
150− if flag {
175+ if activeDataSource == .tags {
151176 searchBar.selectedScopeButtonIndex = 1
152177 } else {
153178 searchBar.selectedScopeButtonIndex = 0
@@ -165,7 +190,12 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
165190
166191 func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
167192 if searchText.isEmpty {
168− dataSource = searchBar.selectedScopeButtonIndex == 0 ? fullPeople : fullTags
193+ switch activeDataSource{
194+ case .people:
195+ dataSourcePeople = fullPeople
196+ case .tags:
197+ dataSourceTags = fullTags
198+ }
169199 tableView.reloadData()
170200 } else {
171201 filterTableView(ind: searchBar.selectedScopeButtonIndex, text: searchText)
@@ -175,12 +205,10 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
175205 func filterTableView(ind: Int, text: String){
176206 switch ind {
177207 case 0:
178− dataSource = fullPeople.filter({ (s) -> Bool in
179− s.lowercased().contains(text.lowercased())
180− })
208+ dataSourcePeople = fullPeople.filter { if let numb = Int(text) { return $0 == numb } else {return false}}
181209 tableView.reloadData()
182210 case 1:
183− dataSource = fullTags.filter({ (s) -> Bool in
211+ dataSourceTags = fullTags.filter({ (s) -> Bool in
184212 s.lowercased().contains(text.lowercased())
185213 })
186214 tableView.reloadData()
@@ -189,33 +217,59 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
189217 }
190218 }
191219
192− var flag = false
193−
194220 func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
195221 if searchBar.isFirstResponder {
196− dataSource = searchBar.selectedScopeButtonIndex == 0 ? fullPeople : fullTags
197− flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
222+ switch activeDataSource{
223+ case .people :
224+ dataSourcePeople = fullPeople
225+ dataSourceTags = []
226+ case .tags:
227+ dataSourceTags = fullTags
228+ dataSourcePeople = []
229+ }
230+ activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
198231 if (!(searchBar.text?.isEmpty ?? true))
199232 {
200233 filterTableView(ind: selectedScope, text: searchBar.text!)
201234 }
202235 tableView.reloadData()
203236 } else if !(searchBar.isFirstResponder){
204− dataSource = searchBar.selectedScopeButtonIndex == 0 ? channel!.people : channel!.hashtags
205− flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
237+ switch activeDataSource{
238+ case .people :
239+ dataSourcePeople = channel!.people
240+ dataSourceTags = []
241+ case .tags:
242+ dataSourceTags = channel!.tags
243+ dataSourcePeople = []
244+ }
245+ activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
206246 tableView.reloadData()
207247 }
208248 }
209249
210250 func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
211− dataSource = searchBar.selectedScopeButtonIndex == 0 ? channel!.people : channel!.hashtags
212− flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
251+ switch activeDataSource{
252+ case .people :
253+ dataSourcePeople = channel!.people
254+ dataSourceTags = []
255+ case .tags:
256+ dataSourceTags = channel!.tags
257+ dataSourcePeople = []
258+ }
259+ activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
213260 tableView.reloadData()
214261 }
215262
216263 func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
217− dataSource = searchBar.selectedScopeButtonIndex == 0 ? fullPeople : fullTags
218− flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
264+ switch activeDataSource{
265+ case .people :
266+ dataSourcePeople = fullPeople
267+ dataSourceTags = []
268+ case .tags:
269+ dataSourceTags = fullTags
270+ dataSourcePeople = []
271+ }
272+ activeDataSource = searchBar.selectedScopeButtonIndex == 0 ? .people : .tags
219273 tableView.reloadData()
220274 }
221275
GDproject/Controller/ News and channels/ChannelListController.swift+32−29
@@ -17,13 +17,13 @@ struct ChannelData{
1717class ChannelListController: UITableViewController, DataDelegate {
1818
1919 // MARK: - Output -
20− var onChannelSelected: ((Channel) -> Void)?
20+ var onChannelSelected: ((Model.Channels) -> Void)?
2121
22− // MARK:- filter search controller
23− var filteredDataSource = [Channel]()
22+ // MARK: - filter search controller
23+ var filteredDataSource = [Model.Channels]()
2424
2525 var myProtocol: DataDelegate?
26− var displayingChannel: Channel?
26+ var displayingChannel: Model.Channels?
2727
2828 var isFiltering: Bool {
2929 return searchController.isActive && !searchBarIsEmpty()
@@ -35,15 +35,15 @@ class ChannelListController: UITableViewController, DataDelegate {
3535
3636 func filterContentForSearchText(_ searchText: String, scope: String = "All")
3737 {
38− filteredDataSource = ChannelListController.dataSource.filter({(keys : Channel) -> Bool in
39− return keys.title.lowercased().contains(searchText.lowercased())
38+ filteredDataSource = dataSource.filter({(keys : Model.Channels) -> Bool in
39+ return keys.name.lowercased().contains(searchText.lowercased())
4040 })
4141
4242 tableView.reloadData()
4343 }
4444
45− func passData(for row: Int, channel: Channel) {
46− ChannelListController.dataSource[row] = channel
45+ func passData(for row: Int, channel: Model.Channels) {
46+ dataSource[row] = channel
4747 }
4848
4949 var searchController = UISearchController(searchResultsController: nil)
@@ -68,34 +68,37 @@ class ChannelListController: UITableViewController, DataDelegate {
6868 {
6969 super.viewWillAppear(animated)
7070 searchController.isActive = false
71− tableView.reloadData()
71+ Model.channelsList { [weak self] (channels) in
72+ self?.dataSource = [ChannelListController.generalChannel] + channels
73+ }
7274 }
7375
7476 @objc func addChannel()
7577 {
76− // insertion
77− tableView.beginUpdates()
78− ChannelListController.dataSource.insert(Channel(title: "Untitled", subtitle: "No", hashtags: [], people: [], posts: []), at: 1)
79− tableView.insertRows(at: [IndexPath(row: 1, section: 0)], with: .none)
80− tableView.endUpdates()
81−
8278 // editing mode is on automatically
8379 let vc = storyboard?.instantiateViewController(withIdentifier: channelControllerId) as! ChannelController
8480 vc.index = 1
8581 vc.myProtocol = self
86− vc.channel = ChannelListController.dataSource[1]
82+ vc.channel = Model.Channels(people: [], name: "Untitled", tags: [])
8783 navigationController?.pushViewController(vc, animated: true)
8884 }
8985
90− static var dataSource : [Channel] = [
91− Channel(title: "General", subtitle: "All posts", hashtags: ["All"], people: ["All"], posts: [])
92− ]
86+ static let generalChannel = Model.Channels(people: [], name: "General", id: -1, tags: [])
87+
88+ var dataSource : [Model.Channels] = [
89+ Model.Channels(people: [], name: "General", id: -1, tags: [])
90+ ]
91+ {
92+ didSet{
93+ self.tableView.reloadData()
94+ }
95+ }
9396
9497 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
9598 if isFiltering {
9699 return filteredDataSource.count
97100 } else {
98− return ChannelListController.dataSource.count
101+ return dataSource.count
99102 }
100103 }
101104
@@ -106,11 +109,11 @@ class ChannelListController: UITableViewController, DataDelegate {
106109
107110
108111 if isFiltering{
109− cell.textLabel?.text = filteredDataSource[indexPath.row].title
110− cell.detailTextLabel?.text = filteredDataSource[indexPath.row].subtitle
112+ cell.textLabel?.text = filteredDataSource[indexPath.row].name
113+ cell.detailTextLabel?.text = filteredDataSource[indexPath.row].tags.reduce(String(), { (r, next) -> String in "\(r) \(next)" })
111114 } else {
112− cell.textLabel?.text = ChannelListController.dataSource[indexPath.row].title
113− cell.detailTextLabel?.text = ChannelListController.dataSource[indexPath.row].subtitle
115+ cell.textLabel?.text = dataSource[indexPath.row].name
116+ cell.detailTextLabel?.text = dataSource[indexPath.row].tags.reduce(String(), { (r, next) -> String in "\(r) \(next)" })
114117 }
115118
116119 return cell
@@ -133,19 +136,19 @@ class ChannelListController: UITableViewController, DataDelegate {
133136 let vc = self?.storyboard?.instantiateViewController(withIdentifier: channelControllerId) as! ChannelController
134137 vc.index = indexPath.row
135138 vc.myProtocol = self!
136− vc.channel = ChannelListController.dataSource[indexPath.row]
139+ vc.channel = self?.dataSource[indexPath.row]
137140 self?.navigationController?.pushViewController(vc, animated: true)
138141 }
139142 editButton.backgroundColor = .green
140143
141144
142145 let deleteButton = UITableViewRowAction(style: .normal, title: "Delete") { [weak self] (action, indexPath) in
143− if (ChannelListController.dataSource[indexPath.row] == self!.displayingChannel!)
146+ if (self!.dataSource[indexPath.row].id == self!.displayingChannel?.id ?? -1)
144147 {
145− self?.myProtocol?.passData(for: 0, channel: ChannelListController.dataSource[0])
148+ self?.myProtocol?.passData(for: 0, channel: self!.dataSource[0])
146149 }
147150 self?.tableView.beginUpdates()
148− ChannelListController.dataSource.remove(at: indexPath.row)
151+ self?.dataSource.remove(at: indexPath.row)
149152 self?.tableView.deleteRows(at: [indexPath], with: .none)
150153 self?.tableView.endUpdates()
151154 }
@@ -163,7 +166,7 @@ class ChannelListController: UITableViewController, DataDelegate {
163166 myProtocol?.passData(for: 0, channel: filteredDataSource[indexPath.row])
164167 navigationController?.popViewController(animated: true)
165168 } else {
166− myProtocol?.passData(for: 0, channel: ChannelListController.dataSource[indexPath.row])
169+ myProtocol?.passData(for: 0, channel: dataSource[indexPath.row])
167170 navigationController?.popViewController(animated: true)
168171 //onChannelSelected?(ChannelListController.dataSource[indexPath.row])
169172 }
GDproject/Controller/ News and channels/ChannelsCoordinator.swift+2−2
@@ -12,11 +12,11 @@ import UIKit
1212final class ChannelsCoordinator{
1313
1414 // MARK: - Properties
15− private var channel: Channel { didSet { updateInterfaces() } }
15+ private var channel: Model.Channels { didSet { updateInterfaces() } }
1616 private weak var navigationController: UINavigationController?
1717
1818 // MARK:- Init
19− init(currentChannel: Channel, navigationController: UINavigationController) {
19+ init(currentChannel: Model.Channels, navigationController: UINavigationController) {
2020 self.channel = currentChannel
2121 self.navigationController = navigationController
2222 }
GDproject/Controller/ News and channels/NewsController.swift+32−11
@@ -11,7 +11,7 @@ import TinyConstraints
1111import Cartography
1212
1313protocol UpdateableWithChannel: class {
14− var channel: Channel? { get set }
14+ var channel: Model.Channels? { get set }
1515}
1616protocol NewPostDelegate {
1717 func addPost(post: Post)
@@ -23,8 +23,12 @@ protocol UpdateableWithUser: class {
2323// Search is availiable within every table (posts and channels). Has button-functionality for boths post and chnnels
2424class NewsController: UITableViewController, UISearchControllerDelegate, NewPostDelegate, UpdateableWithChannel, DataDelegate
2525{
26− func passData(for row: Int, channel: Channel) {
27− self.channel = channel
26+ func passData(for row: Int, channel: Model.Channels) {
27+ if channel.id == -1{
28+ self.channel = nil
29+ } else {
30+ self.channel = channel
31+ }
2832 }
2933
3034 var dictionary: [Int: Model.Users]? {
@@ -32,7 +36,7 @@ class NewsController: UITableViewController, UISearchControllerDelegate, NewPost
3236
3337 var newPosts: [Model.Posts] = []
3438
35− channel?.posts.forEach({ (post) in
39+ posts!.forEach({ (post) in
3640 newPosts.append(Model.Posts(body: post.body, authorId: post.authorId, id: post.id, user: dictionary![post.authorId]!, date: post.updated, tags: post.tags))
3741 })
3842
@@ -42,10 +46,15 @@ class NewsController: UITableViewController, UISearchControllerDelegate, NewPost
4246 }
4347 }
4448
45− var channel: Channel?{
49+ var posts: [Model.Posts]? {
50+ didSet{
51+
52+ }
53+ }
54+
55+ var channel: Model.Channels? {
4656 didSet{
47− print(channel!.posts.count)
48− navigationItem.title = channel?.title ?? ""
57+ navigationItem.title = channel?.name ?? ""
4958 }
5059 }
5160
@@ -94,7 +103,7 @@ class NewsController: UITableViewController, UISearchControllerDelegate, NewPost
94103 @objc func refreshPostsData( _ ff: UIRefreshControl){
95104 print("I am here")
96105 Model.getLast { [weak self] (tuple) in
97− self?.channel = tuple.channel
106+ self?.posts = tuple.posts
98107 self?.dictionary = tuple.users
99108 self?.refreshContr.endRefreshing()
100109 }
@@ -119,9 +128,21 @@ class NewsController: UITableViewController, UISearchControllerDelegate, NewPost
119128 searchController.isActive = false
120129 // TODO:- display something if no posts are availiable
121130
122− Model.getLast {
123− self.channel = $0.channel
124− self.dictionary = $0.users
131+ if let channel = channel, let id = channel.id {
132+
133+ Model.getChannel(with: id) { [weak self] in
134+ self?.posts = $0.posts
135+ self?.dictionary = $0.users
136+ }
137+
138+ } else{
139+
140+ navigationItem.title = "@&% General"
141+ Model.getLast { [weak self] in
142+ self?.posts = $0.posts
143+ self?.dictionary = $0.users
144+ }
145+
125146 }
126147
127148 // tableView.reloadData()
GDproject/Controller/ News and channels/NewsVC.swift+1−1
@@ -28,7 +28,7 @@ struct PostCellData{
2828class NewsVC: UIViewController, UITableViewDelegate, UITableViewDataSource{
2929
3030 var dataSourse: [Model.Posts] = []{
31− didSet{
31+ didSet {
3232 cellDataSourse = []
3333 dataSourse.forEach { (item) in
3434 cellDataSourse.append(PostCellData(attributedData: PostCellData.create(with: item.body)))
GDproject/Simple model/Model.swift+87−20
@@ -10,7 +10,9 @@ import Foundation
1010import Alamofire
1111
1212class Model{
13+
1314 static let invalidTocken = 498
15+
1416 private static var isValidTocken: ((Int)->())? = {
1517 responce in
1618 print("\(responce)")
@@ -20,6 +22,7 @@ class Model{
2022 }
2123
2224 private static let baseUrl = "https://valera-denis.herokuapp.com"
25+ static let decoder = JSONDecoder()
2326
2427 static let url = URL(string:"\(baseUrl)/authentication/login")!
2528 static let url1 = URL(string:"\(baseUrl)/posts/last")!
@@ -27,6 +30,9 @@ class Model{
2730 static let urlForUsers = URL(string:"\(baseUrl)/users")!
2831 static let createAndPublishURL = URL(string:"\(baseUrl)/posts/publish")!
2932 static let channelsGetURL = URL(string: "\(baseUrl)/channels/get")!
33+ static let channelsUpdateURL = URL(string: "\(baseUrl)/channels/update")!
34+ static let channelsListURL = URL(string: "\(baseUrl)/channels")!
35+ static let channelsCreateURL = URL(string: "\(baseUrl)/channels/create")!
3036
3137
3238 struct QueryPosts: Codable{
@@ -117,6 +123,43 @@ class Model{
117123 }
118124 }
119125
126+ struct Channels: Codable {
127+
128+ var people: [Int]
129+ var name: String
130+ var id: Int?
131+ var tags: [String]
132+
133+ enum CodingKeys: String, CodingKey {
134+ case people
135+ case name
136+ case id
137+ case tags
138+ }
139+
140+ init(people: [Int], name: String, id: Int, tags: [String]) {
141+ self.id = id
142+ self.people = people
143+ self.tags = tags
144+ self.name = name
145+ }
146+
147+ init(people: [Int], name: String, tags: [String]) {
148+ self.people = people
149+ self.tags = tags
150+ self.name = name
151+ }
152+
153+ func encode(to encoder: Encoder) throws {
154+ var container = encoder.container(keyedBy: CodingKeys.self)
155+ try container.encode(people, forKey: .people)
156+ try container.encode(name, forKey: .name)
157+ // try container.encode(id, forKey: .id)
158+ try container.encode(tags, forKey: .tags)
159+ }
160+
161+ }
162+
120163 static func authenticate(with id: Int, completion: @escaping ((Bool)->())) {
121164
122165 let json: [String:Any] = ["authenticationId" : id]
@@ -156,7 +199,7 @@ class Model{
156199 }
157200
158201
159− static func getLast(completion: @escaping (((users:[Int: Users], channel:Channel))->())){
202+ static func getLast(completion: @escaping (((users:[Int: Users], posts:[Posts]))->())){
160203 let jsonString = "30"
161204 var request = URLRequest(url: url1)
162205
@@ -171,21 +214,19 @@ class Model{
171214
172215 isValidTocken?(response.response?.statusCode ?? 498)
173216
174− let decoder = JSONDecoder()
175217 guard let json = response.data else { return }
176218
177219 guard let newQueery = try? decoder.decode(QueryPosts.self, from: json) else { print("no")
178220 return }
179221
180222 idUser = newQueery.users
181− completion((newQueery.users, Channel(title: "# General", subtitle: "No subtitle", hashtags: ["ПИ"], people: ["No"], posts: newQueery.posts)))
223+ completion((newQueery.users, newQueery.posts))
182224 }
183225 }
184226
185227
186228 static func createAndPublish(body: [Attachments], tags: [String]){
187229 let jsonUpd = CreatedPost(body: body, tags: tags)
188−
189230 var request = URLRequest(url: createAndPublishURL)
190231
191232 request.httpMethod = "POST"
@@ -220,11 +261,8 @@ class Model{
220261 AF.request(request).responseJSON {
221262 (response) in
222263
223− if response.response?.statusCode == 200 {
224− print("success")
225− }
264+ isValidTocken?(response.response?.statusCode ?? 498)
226265
227− let decoder = JSONDecoder()
228266 guard let json = response.data else { return }
229267
230268 guard let newPost = try? decoder.decode(QueryPosts.self, from: json) else { return }
@@ -249,11 +287,8 @@ class Model{
249287 AF.request(request).responseJSON {
250288 (response) in
251289
252− if response.response?.statusCode == 200 {
253− print("successddd")
254− }
290+ isValidTocken?(response.response?.statusCode ?? 498)
255291
256− let decoder = JSONDecoder()
257292 guard let json = response.data else { return }
258293
259294 guard let users = try? decoder.decode([Users].self, from: json) else { return }
@@ -267,7 +302,7 @@ class Model{
267302 }
268303
269304 // get channel (with id): in responce -- PostQuery
270− static func getChannels(with channelId: Int, completion: @escaping (([Posts])->())){
305+ static func getChannel(with channelId: Int, completion: @escaping (((users:[Int: Users], posts:[Posts]))->())){
271306 let json = [
272307 "request" : channelId,
273308 "limit": 10
@@ -275,7 +310,7 @@ class Model{
275310
276311 let jsonData = try? JSONSerialization.data(withJSONObject: json)
277312
278− var request = URLRequest(url: urlForPostsForUser)
313+ var request = URLRequest(url: channelsGetURL)
279314 request.httpMethod = "POST"
280315
281316 // insert json data to the request
@@ -285,16 +320,48 @@ class Model{
285320 AF.request(request).responseJSON {
286321 (response) in
287322
288− if response.response?.statusCode == 200 {
289− print("success")
290− }
323+ isValidTocken?(response.response?.statusCode ?? 498)
324+
291325
292− let decoder = JSONDecoder()
293326 guard let json = response.data else { return }
294327
295− guard let newPost = try? decoder.decode(QueryPosts.self, from: json) else { return }
328+ guard let newQueery = try? decoder.decode(QueryPosts.self, from: json) else { return }
296329
297− completion(newPost.posts)
330+ idUser = newQueery.users
331+ completion((newQueery.users, newQueery.posts))
332+ }
333+ }
334+
335+
336+ static func createChannel(with channel: Channels) {
337+
338+ var request = URLRequest(url: channelsCreateURL)
339+ request.httpMethod = "POST"
340+ request.httpBody = try? JSONEncoder().encode(channel)
341+
342+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
343+ AF.request(request).response {
344+ (response) in
345+
346+ isValidTocken?(response.response?.statusCode ?? 498)
347+ }
348+
349+ }
350+
351+ static func channelsList(completion: @escaping (([Channels])->())){
352+ var request = URLRequest(url: channelsListURL)
353+ request.httpMethod = "POST"
354+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
355+
356+ AF.request(request).responseJSON { (response) in
357+
358+ isValidTocken?(response.response?.statusCode ?? 498)
359+
360+ guard let json = response.data else { return }
361+
362+ guard let channelsList = try? decoder.decode([Channels].self, from: json) else { return }
363+
364+ completion(channelsList)
298365 }
299366 }
300367}