git.iko.soy/dashared/GDproject
on master help sign in
resolved #8. updated for new version of denis
658140bDarya Rednikina
GDproject/Base.lproj/Main.storyboard+2−2
@@ -74,8 +74,8 @@
74 <nil key="highlightedColor"/>
74 <nil key="highlightedColor"/>
75 </label>
75 </label>
76 <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="yir-N6-Q6f">
76 <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="yir-N6-Q6f">
77− <rect key="frame" x="297" y="0.0" width="46" height="30"/>
77+ <rect key="frame" x="246" y="0.0" width="97" height="30"/>
78− <state key="normal" title="Button"/>
78+ <state key="normal" title="New message"/>
79 </button>
79 </button>
80 </subviews>
80 </subviews>
81 </stackView>
81 </stackView>
GDproject/Controller/ News and channels/NewsController.swift+7−14
@@ -33,10 +33,9 @@ class NewsController: UITableViewController, UISearchControllerDelegate, NewPost
33 var newPosts: [Model.Posts] = []
33 var newPosts: [Model.Posts] = []
34
34
35 channel?.posts.forEach({ (post) in
35 channel?.posts.forEach({ (post) in
36− newPosts.append(Model.Posts(body: post.body, authorId: post.authorId, id: post.id, user: dictionary![post.authorId]!))
36+ newPosts.append(Model.Posts(body: post.body, authorId: post.authorId, id: post.id, user: dictionary![post.authorId]!, date: post.updated))
37 })
37 })
38
38
39−
40 news.dataSourse = newPosts
39 news.dataSourse = newPosts
41 tableView.reloadData()
40 tableView.reloadData()
42 }
41 }
@@ -44,15 +43,7 @@ class NewsController: UITableViewController, UISearchControllerDelegate, NewPost
44
43
45 var channel: Channel?{
44 var channel: Channel?{
46 didSet{
45 didSet{
47− let hh = channel?.posts.map({ (post) -> Int in
48− post.authorId
49− })
50−
51 navigationItem.title = channel?.title ?? ""
46 navigationItem.title = channel?.title ?? ""
52− Model.getUsers(for: hh ?? []) { [weak self] (dict) in
53− self?.dictionary = dict
54− self?.refreshContr.endRefreshing()
55− }
56 }
47 }
57 }
48 }
58
49
@@ -99,8 +90,9 @@ class NewsController: UITableViewController, UISearchControllerDelegate, NewPost
99 }
90 }
100
91
101 @objc func refreshPostsData( _ ff: UIRefreshControl){
92 @objc func refreshPostsData( _ ff: UIRefreshControl){
102− Model.getLast { [weak self] (channel) in
93+ Model.getLast { [weak self] (tuple) in
103− self?.channel = channel
94+ self?.channel = tuple.channel
95+ self?.dictionary = tuple.users
104 self?.refreshContr.endRefreshing()
96 self?.refreshContr.endRefreshing()
105 }
97 }
106 }
98 }
@@ -124,8 +116,9 @@ class NewsController: UITableViewController, UISearchControllerDelegate, NewPost
124 searchController.isActive = false
116 searchController.isActive = false
125 // TODO:- display something if no posts are availiable
117 // TODO:- display something if no posts are availiable
126
118
127− Model.getLast { [weak self] (channel) in
119+ Model.getLast {
128− self?.channel = channel
120+ self.channel = $0.channel
121+ self.dictionary = $0.users
129 }
122 }
130
123
131 // tableView.reloadData()
124 // tableView.reloadData()
GDproject/Controller/ News and channels/PostViewCell.swift+1−1
@@ -88,7 +88,7 @@ class PostViewCell: UITableViewCell
88 views.append(createTextView(with: attachment.markdown, isFullVersoin))
88 views.append(createTextView(with: attachment.markdown, isFullVersoin))
89 }
89 }
90
90
91− nameLabel.setTitle("\(post.user?.firstName ?? "") \(post.user?.secondName ?? "")", for: .normal)
91+ nameLabel.setTitle("\(post.user?.firstName ?? "") \(post.user?.lastName ?? "")", for: .normal)
92 nameLabel.addTarget(self, action: #selector(displayProfile), for: .touchUpInside)
92 nameLabel.addTarget(self, action: #selector(displayProfile), for: .touchUpInside)
93 fullNameLabel.text = "\(post.authorId)"
93 fullNameLabel.text = "\(post.authorId)"
94 setUpInStackView(isFullVersoin)
94 setUpInStackView(isFullVersoin)
GDproject/Controller/Log In/LogInViewController.swift+1−3
@@ -132,9 +132,7 @@ class LogInViewController: UIViewController {
132
132
133 UIView.animate(withDuration: 0, delay: 0, options: .curveEaseOut, animations: {
133 UIView.animate(withDuration: 0, delay: 0, options: .curveEaseOut, animations: {
134 self.view.layoutIfNeeded()
134 self.view.layoutIfNeeded()
135− }) { (completed) in
135+ })
136−
137− }
138 }
136 }
139 }
137 }
140
138
GDproject/Controller/Profile/ProfileViewController.swift+9−4
@@ -33,10 +33,15 @@ class ProfileViewController: UIViewController
33
33
34 func fill(with user: Model.Users){
34 func fill(with user: Model.Users){
35 self.facultyLabel.text = "Студент: Факультет Компьютерных Наук"
35 self.facultyLabel.text = "Студент: Факультет Компьютерных Наук"
36− self.nameLabel.text = "\(user.firstName)"
36+ self.nameLabel.text = "\(user.firstName) \(user.middleName)"
37− self.surnameLabel.text = "\(user.secondName)"
37+ self.surnameLabel.text = "\(user.lastName)"
38 self.profileImageView.image = #imageLiteral(resourceName: "kitten").roundedImage
38 self.profileImageView.image = #imageLiteral(resourceName: "kitten").roundedImage
39− self.placeLabel.text = "Москва"
39+ self.placeLabel.text = "📍Москва, Кочновский пр.3"
40+ if user.id == DataStorage.standard.getUserId(){
41+ newMessageButton.isHidden = true
42+ } else {
43+ newMessageButton.isHidden = false
44+ }
40 }
45 }
41
46
42 var user: Model.Users? {
47 var user: Model.Users? {
@@ -55,7 +60,7 @@ class ProfileViewController: UIViewController
55 var newPosts: [Model.Posts] = []
60 var newPosts: [Model.Posts] = []
56
61
57 dataSourse?.forEach({ (post) in
62 dataSourse?.forEach({ (post) in
58− newPosts.append(Model.Posts(body: post.body, authorId: post.authorId, id: post.id, user: user!))
63+ newPosts.append(Model.Posts(body: post.body, authorId: post.authorId, id: post.id, user: user!, date: post.updated))
59 })
64 })
60
65
61 self.posts.dataSourse = newPosts
66 self.posts.dataSourse = newPosts
GDproject/Simple model/Model.swift+43−28
@@ -10,46 +10,61 @@ import Foundation
10import Alamofire
10import Alamofire
11
11
12class Model{
12class Model{
13+ static let invalidTocken = 498
14+ private static var isValidTocken: ((Int)->())? = {
15+ responce in
16+ if responce == invalidTocken {
17+ DataStorage.standard.setIsLoggedIn(value: false, with: 0)
18+ }
19+ }
13
20
14 private static let baseUrl = "https://valera-denis.herokuapp.com"
21 private static let baseUrl = "https://valera-denis.herokuapp.com"
15
22
16− static let url = URL(string:"\(baseUrl)/authenticate")!
23+ static let url = URL(string:"\(baseUrl)/authentication/login")!
17 static let url1 = URL(string:"\(baseUrl)/posts/last")!
24 static let url1 = URL(string:"\(baseUrl)/posts/last")!
18 static let urlForDrafts = URL(string:"\(baseUrl)/drafts/create")!
25 static let urlForDrafts = URL(string:"\(baseUrl)/drafts/create")!
19 static let urlForPublish = URL(string:"\(baseUrl)/drafts/publish")!
26 static let urlForPublish = URL(string:"\(baseUrl)/drafts/publish")!
20 static let urlForUpdate = URL(string:"\(baseUrl)/drafts/update")!
27 static let urlForUpdate = URL(string:"\(baseUrl)/drafts/update")!
21 static let urlForPostsForUser = URL(string:"\(baseUrl)/posts/forUser")!
28 static let urlForPostsForUser = URL(string:"\(baseUrl)/posts/forUser")!
22 static let urlForUsers = URL(string:"\(baseUrl)/users")!
29 static let urlForUsers = URL(string:"\(baseUrl)/users")!
23− static let createAndPublishURL = URL(string:"\(baseUrl)/drafts/createAndPublish")!
30+ static let createAndPublishURL = URL(string:"\(baseUrl)/posts/publish")!
31+
32+
33+ struct QueryPosts: Codable{
34+ var users: [Int: Users]
35+ var posts: [Posts]
36+ }
24
37
25 struct Posts: Codable {
38 struct Posts: Codable {
26 var body: [Attachments]
39 var body: [Attachments]
27 var authorId: Int
40 var authorId: Int
28 var id: Int
41 var id: Int
29 var user: Model.Users?
42 var user: Model.Users?
43+ var updated: String
30
44
31− init(body: [Attachments], authorId: Int, id: Int) {
45+ init(body: [Attachments], authorId: Int, id: Int, date: String) {
32 self.body = body
46 self.body = body
33 self.authorId = authorId
47 self.authorId = authorId
34 self.id = id
48 self.id = id
49+ self.updated = date
35 }
50 }
36
51
37− init(body: [Attachments], authorId: Int, id: Int, user: Model.Users) {
52+ init(body: [Attachments], authorId: Int, id: Int, user: Model.Users, date: String) {
38 self.body = body
53 self.body = body
39 self.authorId = authorId
54 self.authorId = authorId
40 self.id = id
55 self.id = id
41 self.user = user
56 self.user = user
42− }
57+ self.updated = date
43−
44− mutating func setUser(with: Users){
45− self.user = with
46 }
58 }
47 }
59 }
48
60
49 struct Users: Codable{
61 struct Users: Codable{
62+
63+ var middleName: String
50− var secondName: String
64+ var lastName: String
51 var firstName: String
65 var firstName: String
52 var id: Int
66 var id: Int
67+
53 }
68 }
54
69
55 struct Attachments: Codable {
70 struct Attachments: Codable {
@@ -74,8 +89,15 @@ class Model{
74
89
75 AF.request(request).response { (responce) in
90 AF.request(request).response { (responce) in
76
91
77− guard let realResponse = responce.response, realResponse.statusCode == 204 else {
92+ guard let realResponse = responce.response, realResponse.statusCode == 200 else
93+ {
78− print("Not a 204 response")
94+ print("Not a 200 response")
95+ completion(false)
96+ return
97+ }
98+
99+ guard let json = responce.data else { return }
100+ guard let personId = Int(String(data: json, encoding: String.Encoding.utf8)!) else {
79 completion(false)
101 completion(false)
80 return
102 return
81 }
103 }
@@ -86,13 +108,13 @@ class Model{
86
108
87 HTTPCookieStorage.shared.setCookie(cookies[0])
109 HTTPCookieStorage.shared.setCookie(cookies[0])
88
110
89− DataStorage.standard.setIsLoggedIn(value: true, with: id)
111+ DataStorage.standard.setIsLoggedIn(value: true, with: personId)
90 completion(true)
112 completion(true)
91 }
113 }
92 }
114 }
93
115
94
116
95− static func getLast(completion: @escaping ((Channel)->())){
117+ static func getLast(completion: @escaping (((users:[Int: Users], channel:Channel))->())){
96 let jsonString = "30"
118 let jsonString = "30"
97 var request = URLRequest(url: url1)
119 var request = URLRequest(url: url1)
98
120
@@ -105,18 +127,15 @@ class Model{
105 AF.request(request).responseJSON {
127 AF.request(request).responseJSON {
106 (response) in
128 (response) in
107
129
108− if response.response?.statusCode == 200 {
109− print("success")
110− } else {
111− print(response.response?.statusCode)
130+ isValidTocken?(response.response?.statusCode ?? 498)
112− }
113
131
114 let decoder = JSONDecoder()
132 let decoder = JSONDecoder()
115 guard let json = response.data else { return }
133 guard let json = response.data else { return }
116−
117− guard let newPost = try? decoder.decode([Posts].self, from: json) else { return }
118
134
135+ guard let newQueery = try? decoder.decode(QueryPosts.self, from: json) else { print("no")
136+ return }
137+
119− completion(Channel(title: "# General", subtitle: "No subtitle", hashtags: ["ПИ"], people: ["No"], posts: newPost))
138+ completion((newQueery.users, Channel(title: "# General", subtitle: "No subtitle", hashtags: ["ПИ"], people: ["No"], posts: newQueery.posts)))
120 }
139 }
121 }
140 }
122
141
@@ -168,11 +187,7 @@ class Model{
168 AF.request(request).response {
187 AF.request(request).response {
169 (response) in
188 (response) in
170
189
171− if response.response?.statusCode == 204 {
190+ isValidTocken?(response.response?.statusCode ?? 498)
172− print("post created")
173− } else {
174− print("failure in creating and publishing post")
175− }
176 }
191 }
177 }
192 }
178
193
@@ -202,9 +217,9 @@ class Model{
202 let decoder = JSONDecoder()
217 let decoder = JSONDecoder()
203 guard let json = response.data else { return }
218 guard let json = response.data else { return }
204
219
205− guard let newPost = try? decoder.decode([Posts].self, from: json) else { return }
220+ guard let newPost = try? decoder.decode(QueryPosts.self, from: json) else { return }
206
221
207− completion(newPost)
222+ completion(newPost.posts)
208 }
223 }
209 }
224 }
210
225