git.iko.soy/dashared/GDproject
on master help sign in
Alerts and more
5ca9bbbDarya Rednikina
GDproject/Controller/ News and channels/ChannelController.swift+1−1
@@ -73,7 +73,7 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
73 if let _ = channel?.id { print("update")
73 if let _ = channel?.id { print("update")
74 Model.updateChannel(with: channel!)
74 Model.updateChannel(with: channel!)
75 } else { print("create")
75 } else { print("create")
76− Model.createChannel(with: channel!)
76+ //Model.createChannel(with: channel!)
77 }
77 }
78 return
78 return
79 }
79 }
GDproject/Controller/ News and channels/ChannelListController.swift+1−1
@@ -30,7 +30,7 @@ class ChannelListController: UITableViewController {
30 var displayingChannel: Model.Channels?
30 var displayingChannel: Model.Channels?
31
31
32 var toReload: Bool = false {
32 var toReload: Bool = false {
33− didSet{
33+ didSet {
34 tableView.reloadData()
34 tableView.reloadData()
35 }
35 }
36 }
36 }
GDproject/Controller/ News and channels/ChannelViewController.swift+5−1
@@ -65,7 +65,11 @@ class ChannelViewController: UITableViewController, UpdatableName, UpdatableChan
65 {
65 {
66 Model.updateChannel(with: channel!)
66 Model.updateChannel(with: channel!)
67 } else {
67 } else {
68− Model.createChannel(with: channel!)
68+ Model.createChannel(with: channel!) {
69+ [weak self] in
70+ self?.showAlertOn(result: $0)
71+ return
72+ }
69 }
73 }
70 return
74 return
71 }
75 }
GDproject/Controller/ News and channels/FullPostController.swift+1−18
@@ -26,26 +26,9 @@ class FullPostController: UITableViewController {
26 func setUpNavigationBar(){
26 func setUpNavigationBar(){
27 navigationItem.largeTitleDisplayMode = .never
27 navigationItem.largeTitleDisplayMode = .never
28 navigationItem.title = "Post"
28 navigationItem.title = "Post"
29− navigationItem.rightBarButtonItems = [UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(self.options))]
30 }
29 }
31
30
32− @objc func options(){
31+
33− // drafts
34− // saved
35−
36− let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
37− let editAction = UIAlertAction(title: "Send via message", style: .default)
38− let shareAction = UIAlertAction(title: "Send via project", style: .default)
39− let settingsAction = UIAlertAction(title: "Copy link", style: .default)
40− let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
41−
42− optionMenu.addAction(editAction)
43− optionMenu.addAction(shareAction)
44− optionMenu.addAction(settingsAction)
45− optionMenu.addAction(cancelAction)
46−
47− self.present(optionMenu, animated: true, completion: nil)
48− }
49 // MARK: - Table view data source
32 // MARK: - Table view data source
50
33
51 override func numberOfSections(in tableView: UITableView) -> Int {
34 override func numberOfSections(in tableView: UITableView) -> Int {
GDproject/Controller/ News and channels/NewPostViewController.swift+2−1
@@ -109,7 +109,8 @@ class NewPostViewController: UIViewController, UITextViewDelegate, TagsReceiver
109 override func viewWillAppear(_ animated: Bool) {
109 override func viewWillAppear(_ animated: Bool) {
110 super.viewWillAppear(animated)
110 super.viewWillAppear(animated)
111
111
112− navigationController?.navigationBar.prefersLargeTitles = false
112+ //navigationController?.navigationBar.prefersLargeTitles = false
113+ navigationItem.largeTitleDisplayMode = .never
113 navigationItem.title = "New post"
114 navigationItem.title = "New post"
114 textView.text = NewPostViewController.draft
115 textView.text = NewPostViewController.draft
115 // tagsField.addTags(NewPostViewController.hashTagsDraft)
116 // tagsField.addTags(NewPostViewController.hashTagsDraft)
GDproject/Controller/ News and channels/NewsController.swift+1
@@ -52,6 +52,7 @@ class NewsController: UIViewController, UISearchControllerDelegate, UpdateableWi
52 let updater = TagsSuggestionController()
52 let updater = TagsSuggestionController()
53 updater.delegate = self
53 updater.delegate = self
54 searchController = UISearchController(searchResultsController: updater)
54 searchController = UISearchController(searchResultsController: updater)
55+ navigationItem.largeTitleDisplayMode = .always
55
56
56 navigationController?.navigationBar.prefersLargeTitles = true
57 navigationController?.navigationBar.prefersLargeTitles = true
57 navigationItem.title = "Loading ..."
58 navigationItem.title = "Loading ..."
GDproject/Controller/Coordinators/ProfileCoordinator.swift+15−13
@@ -36,23 +36,25 @@ class ProfileCoordinator: BaseCoordinator {
36 }
36 }
37 }
37 }
38
38
39− profile.onSettings = { [weak self, weak storyboard] in
39+ profile.deleteAllSessions = {
40− let vc = storyboard?.instantiateViewController(withIdentifier: "SettingsViewController") as! SettingsViewController
40+ Model.deactivateAll() {
41− self?.navigationController?.pushViewController(vc, animated: true)
41+ [weak self] in
42+
43+ DataStorage.standard.setIsLoggedIn(value: false, with: 0)
44+ self?.didEndSession?()
45+ }
42 }
46 }
43
47
44− profile.onChannelsListToAddAPerson = { [weak self, weak storyboard] (user) in
48+ profile.onSettings = { [weak self, weak storyboard, weak profile] in
49+
45− let vc = storyboard?.instantiateViewController(withIdentifier: simplifiedChannelsList) as! SimplifiedChannelsList
50+ let vc = storyboard?.instantiateViewController(withIdentifier: "RegisterTableViewController") as! RegisterTableViewController
46− vc.user = user
51+ vc.delegate = profile
52+ vc.userActive = profile?.user
47
53
48− let transition = CATransition()
49− transition.duration = 0.5
50− transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
51− transition.type = CATransitionType.moveIn
52− transition.subtype = CATransitionSubtype.fromTop
53− self?.navigationController?.view.layer.add(transition, forKey: nil)
54− self?.navigationController?.pushViewController(vc, animated: false)
54+ self?.navigationController?.pushViewController(vc, animated: true)
55 }
55 }
56+
57+
56 navigationController?.setViewControllers([profile], animated: false)
58 navigationController?.setViewControllers([profile], animated: false)
57 }
59 }
58}
60}
GDproject/Controller/Log In/RegisterTableViewController.swift+52−11
@@ -11,9 +11,9 @@ import UIKit
11class RegisterTableViewController: UITableViewController, ChosenFactulty
11class RegisterTableViewController: UITableViewController, ChosenFactulty
12{
12{
13
13
14− func presentAlertInvalidData()
14+ func presentAlertInvalidData(message: String)
15 {
15 {
16− let alert = UIAlertController(title: "Invalid data", message: "Some fields are missing. Add more information", preferredStyle: .alert)
16+ let alert = UIAlertController(title: "Invalid data", message: message, preferredStyle: .alert)
17 alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
17 alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
18 self.present(alert, animated: true, completion: nil)
18 self.present(alert, animated: true, completion: nil)
19 }
19 }
@@ -23,12 +23,29 @@ class RegisterTableViewController: UITableViewController, ChosenFactulty
23 tableView.reloadData()
23 tableView.reloadData()
24 }
24 }
25
25
26−
26+ weak var delegate: UpdateUser?
27+
27 var onRegistration: (()->())?
28 var onRegistration: (()->())?
28
29
29 var faculty: Model.Faculty?
30 var faculty: Model.Faculty?
30 var user: Model.NewRegistration = Model.NewRegistration(email: DataStorage.standard.getEmail()!, firstName: nil, middleName: nil, lastName: nil, faculty: nil)
31 var user: Model.NewRegistration = Model.NewRegistration(email: DataStorage.standard.getEmail()!, firstName: nil, middleName: nil, lastName: nil, faculty: nil)
31
32
33+ var userActive: Model.Users? {
34+ didSet {
35+ if let userA = userActive {
36+ user.firstName = userA.firstName
37+ user.middleName = userA.middleName
38+ user.lastName = userA.lastName
39+
40+ faculty = userA.faculty
41+ self.updateUserIfCan = true
42+ tableView.reloadData()
43+ }
44+ }
45+ }
46+
47+ var updateUserIfCan: Bool = false
48+
32 override func viewDidLoad()
49 override func viewDidLoad()
33 {
50 {
34 super.viewDidLoad()
51 super.viewDidLoad()
@@ -38,22 +55,46 @@ class RegisterTableViewController: UITableViewController, ChosenFactulty
38 navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(endRegistration))
55 navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(endRegistration))
39 }
56 }
40
57
58+
41 @objc func endRegistration()
59 @objc func endRegistration()
42 {
60 {
61+ let fieldsMissing = "Some fields are missing. Add more information"
43 updateUser()
62 updateUser()
44− guard let faculty = faculty else { presentAlertInvalidData(); return }
63+ guard let faculty = faculty else { presentAlertInvalidData(message: fieldsMissing); return }
45
64
46 user.faculty = faculty.url
65 user.faculty = faculty.url
47
66
48− guard let _ = user.firstName else { presentAlertInvalidData(); return }
67+ guard let name = user.firstName else { presentAlertInvalidData(message: fieldsMissing); return }
49− guard let _ = user.middleName else { presentAlertInvalidData(); return }
68+ guard let middle = user.middleName else { presentAlertInvalidData(message: fieldsMissing); return }
50− guard let _ = user.lastName else { presentAlertInvalidData(); return }
69+ guard let last = user.lastName else { presentAlertInvalidData(message: fieldsMissing); return }
51
70
71+ if updateUserIfCan {
72+ guard let delegate = delegate else { return }
73+ guard var userA = userActive else { return }
74+
75+ // updating current
76+ userA.firstName = name
77+ userA.faculty = faculty
78+ userA.middleName = middle
79+ userA.lastName = last
80+
81+ Model.userUpdate(with: user) { [weak self] in
82+
83+ if $0 {
84+ delegate.updateUserObj(with: userA)
85+ self?.navigationController?.popViewController(animated: true)
86+ } else {
87+ self?.presentAlertInvalidData(message: "Something went wrong! Try again")
88+ }
89+
90+ }
91+ } else {
92+
52− Model.register(object: user)
93+ Model.register(object: user)
53− { [weak self] in
94+ { [weak self] in
54− self?.onRegistration?()
95+ self?.onRegistration?()
96+ }
55 }
97 }
56−
57 }
98 }
58 // MARK: - Table view data source
99 // MARK: - Table view data source
59
100
GDproject/Controller/Messages/Dialog/DialogViewController.swift+2−1
@@ -248,7 +248,8 @@ class DialogViewController: UIViewController, UpdatableGroup, UITableViewDelegat
248 return cellData.count
248 return cellData.count
249 }
249 }
250
250
251− override func viewWillAppear(_ animated: Bool) {
251+ override func viewWillAppear(_ animated: Bool)
252+ {
252 super.viewWillAppear(animated)
253 super.viewWillAppear(animated)
253 tabBarController?.tabBar.isHidden = true
254 tabBarController?.tabBar.isHidden = true
254
255
GDproject/Controller/Messages/MessagesViewController.swift+2−2
@@ -70,10 +70,10 @@ class MessagesViewController: UITableViewController {
70
70
71 case .groupChat(let group):
71 case .groupChat(let group):
72 cell.textLabel?.text = group.group.name
72 cell.textLabel?.text = group.group.name
73− cell.detailTextLabel?.text = group.lastMessage!.body.markdown
73+ cell.detailTextLabel?.text = group.lastMessage?.body.markdown
74 case .userChat(let userChat):
74 case .userChat(let userChat):
75 cell.textLabel?.text = "👤 \(users[userChat.user]!.fullName())"
75 cell.textLabel?.text = "👤 \(users[userChat.user]!.fullName())"
76− cell.detailTextLabel?.text = userChat.lastMessage!.body.markdown
76+ cell.detailTextLabel?.text = userChat.lastMessage?.body.markdown
77 }
77 }
78
78
79 return cell
79 return cell
GDproject/Controller/Profile/BasicInfoController.swift+16−11
@@ -16,17 +16,20 @@ struct CellData{
16
16
17class BasicInfoController: UIViewController, UITableViewDelegate, UITableViewDataSource {
17class BasicInfoController: UIViewController, UITableViewDelegate, UITableViewDataSource {
18
18
19− var dataSourse: [CellData] = [
19+ var userInfo: Model.Users? {
20− CellData(opened: false, title: "Phone", sectionData: ["+7 901 733 01 79"]),
20+ didSet {
21+ if let userInfo = userInfo {
21− CellData(opened: false, title: "Mail", sectionData: ["vbogomazova@edu.hse.ru"]),
22+ dataSourse = [CellData(opened: false, title: "Contacts", sectionData: [userInfo.email, userInfo.faculty.address]),
22− CellData(opened: false, title: "Research ID", sectionData: ["4567834336789456737"]),
23+ CellData(opened: false, title: "Faculty", sectionData: [userInfo.faculty.campusName, userInfo.faculty.name, userInfo.faculty.address, userInfo.faculty.path]),
23− CellData(opened: false, title: "Published", sectionData: ["Это будет очень длинное название книги 1, чтобы сделать проверку", "Книга 2", "Книга 3"]),
24− CellData(opened: false, title: "Courses", sectionData: ["Курс 1", "Курс 2"]),
24+ CellData(opened: false, title: "Interests", sectionData: userInfo.faculty.tags)]
25− CellData(opened: false, title: "Link", sectionData: ["https://wwww.hse.ru"])
25+ }
26− ]
26+ }
27+ }
28+
29+ var dataSourse: [CellData] = [ ]
27
30
28 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
31 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
29− if (dataSourse[section].opened){
32+ if (dataSourse[section].opened) {
30 return dataSourse[section].sectionData.count + 1
33 return dataSourse[section].sectionData.count + 1
31 } else {
34 } else {
32 return 1
35 return 1
@@ -45,8 +48,10 @@ class BasicInfoController: UIViewController, UITableViewDelegate, UITableViewDat
45 cell.selectionStyle = .none
48 cell.selectionStyle = .none
46 return cell
49 return cell
47 } else {
50 } else {
48− let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
51+ let cell = tableView.dequeueReusableCell(withIdentifier: infoCellId, for: indexPath) as! InfoCell
49− cell.textLabel?.text = dataSourse[indexPath.section].sectionData[indexPath.row - 1]
52+ cell.fill(title: dataSourse[indexPath.section].sectionData[indexPath.row - 1])
53+ cell.accessoryType = .none
54+ cell.selectionStyle = .none
50 return cell
55 return cell
51 }
56 }
52 }
57 }
GDproject/Controller/Profile/InfoCell.swift+2−1
@@ -16,8 +16,9 @@ class InfoCell: UITableViewCell{
16 label.font = UIFont.systemFont(ofSize: 16)
16 label.font = UIFont.systemFont(ofSize: 16)
17 label.sizeToFit()
17 label.sizeToFit()
18 label.isScrollEnabled = false
18 label.isScrollEnabled = false
19− label.isUserInteractionEnabled = false
19+ // label.isUserInteractionEnabled = false
20 label.isEditable = false
20 label.isEditable = false
21+ label.dataDetectorTypes = .all
21 label.textColor = .black
22 label.textColor = .black
22 return label
23 return label
23 }()
24 }()
GDproject/Controller/Profile/ProfileViewController.swift+63−21
@@ -9,8 +9,17 @@
9import UIKit
9import UIKit
10import TinyConstraints
10import TinyConstraints
11
11
12+protocol UpdateUser: class {
13+ func updateUserObj(with user: Model.Users)
14+}
15+
12−class ProfileViewController: UIViewController
16+class ProfileViewController: UIViewController, UpdateUser
13{
17{
18+ func updateUserObj(with user: Model.Users)
19+ {
20+ self.user = user
21+ Model.Channels.fullPeopleDict[user.id] = user
22+ }
14
23
15 @IBOutlet weak var segmentedControl: UISegmentedControl!
24 @IBOutlet weak var segmentedControl: UISegmentedControl!
16
25
@@ -32,6 +41,8 @@ class ProfileViewController: UIViewController
32
41
33 var logOut: (()->())?
42 var logOut: (()->())?
34
43
44+ var deleteAllSessions: (()->())?
45+
35 var onSettings: (()->())?
46 var onSettings: (()->())?
36
47
37 @IBAction func sendMessage(_ sender: UIButton)
48 @IBAction func sendMessage(_ sender: UIButton)
@@ -46,8 +57,6 @@ class ProfileViewController: UIViewController
46 }
57 }
47 }
58 }
48
59
49− var onChannelsListToAddAPerson: ((Model.Users)->())?
50−
51 var protoDictionary: [String: UIImage] = ["135213": #imageLiteral(resourceName: "9"), "135288": #imageLiteral(resourceName: "5051"), "22723" : #imageLiteral(resourceName: "69"), "135083": #imageLiteral(resourceName: "42")]
60 var protoDictionary: [String: UIImage] = ["135213": #imageLiteral(resourceName: "9"), "135288": #imageLiteral(resourceName: "5051"), "22723" : #imageLiteral(resourceName: "69"), "135083": #imageLiteral(resourceName: "42")]
52
61
53 func fill(with user: Model.Users)
62 func fill(with user: Model.Users)
@@ -106,6 +115,15 @@ class ProfileViewController: UIViewController
106 posts.type = .NONE
115 posts.type = .NONE
107 posts.currChannel = channel
116 posts.currChannel = channel
108
117
118+ posts.onFullPost = {
119+ [weak self] (type,post) in
120+
121+ let vc = self?.storyboard?.instantiateViewController(withIdentifier: fullPostControllerId) as! FullPostController
122+ vc.type = type
123+ vc.post = post
124+ self?.navigationController!.pushViewController(vc, animated: true)
125+ }
126+
109 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
127 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
110
128
111 tableView.register(PostViewCell.self, forCellReuseIdentifier: postCellId)
129 tableView.register(PostViewCell.self, forCellReuseIdentifier: postCellId)
@@ -151,29 +169,52 @@ class ProfileViewController: UIViewController
151
169
152 let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
170 let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
153
171
154− let channelAction = UIAlertAction(title: "Add to a channel", style: .default){
172+
155− [weak self] (_) in
156− self?.onChannelsListToAddAPerson?(self!.user!)
157− }
158−
159− let settingsAction = UIAlertAction(title: "Setting", style: .default)
160− { [weak self] (_) in
161− self?.onSettings?()
162− }
163−
164 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
173 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
165
174
175+
176+ if idProfile == DataStorage.standard.getUserId() {
166− let logoutAction = UIAlertAction(title: "Log out", style: .destructive)
177+ let logoutAction = UIAlertAction(title: "Log out", style: .destructive)
167− { [weak self]
178+ { [weak self]
168− (_) in
179+ (_) in
169− self?.logOut?()
180+ self?.logOut?()
181+ }
182+
183+ let deleetAllSessionsAction = UIAlertAction(title: "Delete all sessions", style: .destructive)
184+ { [weak self]
185+ (_) in
186+ self?.deleteAllSessions?()
187+ }
188+
189+ let settingsAction = UIAlertAction(title: "Edit profile", style: .default)
190+ { [weak self] (_) in
191+ self?.onSettings?()
192+ }
193+
194+ optionMenu.addAction(settingsAction)
195+ optionMenu.addAction(logoutAction)
196+ optionMenu.addAction(deleetAllSessionsAction)
197+ } else {
198+ let channelAction = UIAlertAction(title: "Add to a channel", style: .default)
199+ {
200+ [weak self] (_) in
201+
202+ let vc = self?.storyboard?.instantiateViewController(withIdentifier: simplifiedChannelsList) as! SimplifiedChannelsList
203+ vc.user = self?.user
204+
205+ let transition = CATransition()
206+ transition.duration = 0.5
207+ transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
208+ transition.type = CATransitionType.moveIn
209+ transition.subtype = CATransitionSubtype.fromTop
210+ self?.navigationController?.view.layer.add(transition, forKey: nil)
211+ self?.navigationController?.pushViewController(vc, animated: false)
212+ }
213+
214+ optionMenu.addAction(channelAction)
170 }
215 }
171
216
172− optionMenu.addAction(channelAction)
173− optionMenu.addAction(settingsAction)
174− optionMenu.addAction(logoutAction)
175 optionMenu.addAction(cancelAction)
217 optionMenu.addAction(cancelAction)
176−
177 self.present(optionMenu, animated: true, completion: nil)
218 self.present(optionMenu, animated: true, completion: nil)
178 }
219 }
179
220
@@ -203,6 +244,7 @@ class ProfileViewController: UIViewController
203 func changeToBasicInfo(){
244 func changeToBasicInfo(){
204 tableView.delegate = basicInfo
245 tableView.delegate = basicInfo
205 tableView.dataSource = basicInfo
246 tableView.dataSource = basicInfo
247+ basicInfo.userInfo = self.user
206 tableView.reloadData()
248 tableView.reloadData()
207 }
249 }
208}
250}
GDproject/Extentions.swift+30
@@ -77,3 +77,33 @@ extension String {
77 return timeStamp
77 return timeStamp
78 }
78 }
79}
79}
80+
81+extension UIViewController {
82+ func showAlertOn(result: ResultR) {
83+
84+ let message: String
85+
86+ switch result {
87+ case .impossibleContent:
88+ message = "Impossible content"
89+ case .exceededContent:
90+ message = "The content exceeded"
91+ case .longContent:
92+ message = "The content is too long"
93+ case .badAccess:
94+ message = "Try reloading the page again"
95+ case .alreadyRegistered:
96+ message = "User is already registered"
97+ case .tooMuchToAdd:
98+ message = "Limit of channels exceeded"
99+ case .success, .success1:
100+ return
101+ default:
102+ message = "Something went wrong"
103+ }
104+
105+ let alert = UIAlertController(title: "Error", message: message, preferredStyle: .alert)
106+ alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
107+ self.present(alert, animated: true, completion: nil)
108+ }
109+}
GDproject/Simple model/Model.swift+63−9
@@ -10,6 +10,22 @@ import Foundation
10import UIKit
10import UIKit
11import Alamofire
11import Alamofire
12
12
13+enum ResultR: Int {
14+ case internalServerError = 500
15+ case exceededContent = 470
16+ case longContent = 471
17+ case incorrectContent = 472
18+ case impossibleContent = 473
19+ case invalidTocken = 498
20+ case alreadyRegistered = 409
21+ case invalidCode = 403
22+ case badAccess = 400
23+ case tooMuchToAdd = 406
24+ case notFound = 404
25+ case success1 = 204
26+ case success = 200
27+}
28+
13class Model{
29class Model{
14
30
15 static let invalidTocken = 498
31 static let invalidTocken = 498
@@ -17,7 +33,7 @@ class Model{
17 static var hashTagTree: CompletionTree?
33 static var hashTagTree: CompletionTree?
18
34
19 private static var isValidTocken: ((Int)->())? = { responce in
35 private static var isValidTocken: ((Int)->())? = { responce in
20−
36+ print(responce)
21 if responce == invalidTocken {
37 if responce == invalidTocken {
22
38
23 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
39 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
@@ -33,6 +49,7 @@ class Model{
33 static let decoder = JSONDecoder()
49 static let decoder = JSONDecoder()
34
50
35 static let authMeURL = URL(string: "\(baseUrl)/authentication/me")!
51 static let authMeURL = URL(string: "\(baseUrl)/authentication/me")!
52+ static let deactivateURL = URL(string: "\(baseUrl)/deactivateAll")!
36 static let registerMeURL = URL(string: "\(baseUrl)/authentication/register")!
53 static let registerMeURL = URL(string: "\(baseUrl)/authentication/register")!
37 static let authVerifyURL = URL(string: "\(baseUrl)/authentication/verify")!
54 static let authVerifyURL = URL(string: "\(baseUrl)/authentication/verify")!
38 static let authenticationURL = URL(string:"\(baseUrl)/authentication/login")!
55 static let authenticationURL = URL(string:"\(baseUrl)/authentication/login")!
@@ -42,6 +59,7 @@ class Model{
42 static let postsPublishURL = URL(string:"\(baseUrl)/posts/publish")!
59 static let postsPublishURL = URL(string:"\(baseUrl)/posts/publish")!
43 static let usersURL = URL(string:"\(baseUrl)/users")!
60 static let usersURL = URL(string:"\(baseUrl)/users")!
44 static let usersAllURL = URL(string:"\(baseUrl)/users/all")!
61 static let usersAllURL = URL(string:"\(baseUrl)/users/all")!
62+ static let usersUpdateURL = URL(string:"\(baseUrl)/users/update")!
45 static let channelsGetURL = URL(string: "\(baseUrl)/channels/get")!
63 static let channelsGetURL = URL(string: "\(baseUrl)/channels/get")!
46 static let channelsUpdateURL = URL(string: "\(baseUrl)/channels/update")!
64 static let channelsUpdateURL = URL(string: "\(baseUrl)/channels/update")!
47 static let channelsListURL = URL(string: "\(baseUrl)/channels")!
65 static let channelsListURL = URL(string: "\(baseUrl)/channels")!
@@ -251,15 +269,13 @@ class Model{
251 }
269 }
252
270
253 if code == 204 {
271 if code == 204 {
254− let fields = responce.response?.allHeaderFields as? [String :String]
255
272
273+ let fields = responce.response?.allHeaderFields as? [String :String]
256 let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields!, for: responce.response!.url!)
274 let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields!, for: responce.response!.url!)
257−
258 HTTPCookieStorage.shared.setCookie(cookies[0])
275 HTTPCookieStorage.shared.setCookie(cookies[0])
259
276
260 completion()
277 completion()
261 }
278 }
262−
263 }
279 }
264 }
280 }
265
281
@@ -470,7 +486,7 @@ class Model{
470
486
471 static func getUsers(for ids: [Int], completion: @escaping (([Int:Users])->())){
487 static func getUsers(for ids: [Int], completion: @escaping (([Int:Users])->())){
472 let json = "\(Set(ids))"
488 let json = "\(Set(ids))"
473− print(json)
489+
474 var request = URLRequest(url: usersURL)
490 var request = URLRequest(url: usersURL)
475
491
476 request.httpMethod = "POST"
492 request.httpMethod = "POST"
@@ -534,7 +550,7 @@ class Model{
534 }
550 }
535
551
536
552
537− static func createChannel(with channel: Channels) {
553+ static func createChannel(with channel: Channels, completion: @escaping ((ResultR)->())) {
538
554
539 var request = URLRequest(url: channelsCreateURL)
555 var request = URLRequest(url: channelsCreateURL)
540 request.httpMethod = "POST"
556 request.httpMethod = "POST"
@@ -545,9 +561,13 @@ class Model{
545 (response) in
561 (response) in
546
562
547 isValidTocken?(response.response?.statusCode ?? 498)
563 isValidTocken?(response.response?.statusCode ?? 498)
564+
548− if let code = response.response?.statusCode, code == 498 {
565+ if let code = response.response?.statusCode, let result = ResultR(rawValue: code) {
566+ completion(result)
549 return
567 return
550 }
568 }
569+
570+ completion(ResultR.internalServerError)
551 }
571 }
552
572
553 }
573 }
@@ -709,8 +729,6 @@ class Model{
709 guard let json = response.data else { return }
729 guard let json = response.data else { return }
710
730
711 guard let newQueery = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { return }
731 guard let newQueery = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { return }
712−
713− print("\(anonymousChannel) \(newQueery)")
714 completion((newQueery.users, newQueery.response))
732 completion((newQueery.users, newQueery.response))
715 }
733 }
716 }
734 }
@@ -1081,4 +1099,40 @@ class Model{
1081 completion(faculties)
1099 completion(faculties)
1082 }
1100 }
1083 }
1101 }
1102+
1103+ static func userUpdate(with newUser: NewRegistration, completion: @escaping ((Bool)->())) {
1104+ var request = URLRequest(url: usersUpdateURL)
1105+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
1106+ request.httpMethod = "POST"
1107+ request.httpBody = try? JSONEncoder().encode(newUser)
1108+
1109+ AF.request(request).response { (response) in
1110+ isValidTocken?(response.response?.statusCode ?? 498)
1111+
1112+ if let code = response.response?.statusCode, code == 498 {
1113+ return
1114+ }
1115+
1116+ if let code = response.response?.statusCode, code != 204{
1117+ completion (false)
1118+ } else {
1119+ completion(true)
1120+ }
1121+ }
1122+ }
1123+
1124+ static func deactivateAll(completion: @escaping (()->())){
1125+ var request = URLRequest(url: deactivateURL)
1126+ request.httpMethod = "DELETE"
1127+
1128+ AF.request(request).response { (responce) in
1129+ isValidTocken?(responce.response?.statusCode ?? 498)
1130+
1131+ if let code = responce.response?.statusCode, code == 498 {
1132+ return
1133+ }
1134+
1135+ completion()
1136+ }
1137+ }
1084}
1138}