git.iko.soy/dashared/GDproject
on master help sign in
resolved #8. updated for new version of denis
commit658140bf7db9f7160157348cae3b5f23e8d15791 authorDarya Rednikina <rednikina.com@yandex.ru> dateFri 8 Mar 2019, 22:33 (8 years ago) parent8d28882
6 files changed+63−52
GDproject/Base.lproj/Main.storyboard+2−2 GDproject/Controller/ News and channels/NewsController.swift+7−14 GDproject/Controller/ News and channels/PostViewCell.swift+1−1 GDproject/Controller/Log In/LogInViewController.swift+1−3 GDproject/Controller/Profile/ProfileViewController.swift+9−4 GDproject/Simple model/Model.swift+43−28
GDproject/Base.lproj/Main.storyboard+2−2
@@ -74,8 +74,8 @@
7474 <nil key="highlightedColor"/>
7575 </label>
7676 <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"/>
78− <state key="normal" title="Button"/>
77+ <rect key="frame" x="246" y="0.0" width="97" height="30"/>
78+ <state key="normal" title="New message"/>
7979 </button>
8080 </subviews>
8181 </stackView>
GDproject/Controller/ News and channels/NewsController.swift+7−14
@@ -33,10 +33,9 @@ class NewsController: UITableViewController, UISearchControllerDelegate, NewPost
3333 var newPosts: [Model.Posts] = []
3434
3535 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))
3737 })
3838
39−
4039 news.dataSourse = newPosts
4140 tableView.reloadData()
4241 }
@@ -44,15 +43,7 @@ class NewsController: UITableViewController, UISearchControllerDelegate, NewPost
4443
4544 var channel: Channel?{
4645 didSet{
47− let hh = channel?.posts.map({ (post) -> Int in
48− post.authorId
49− })
50−
5146 navigationItem.title = channel?.title ?? ""
52− Model.getUsers(for: hh ?? []) { [weak self] (dict) in
53− self?.dictionary = dict
54− self?.refreshContr.endRefreshing()
55− }
5647 }
5748 }
5849
@@ -99,8 +90,9 @@ class NewsController: UITableViewController, UISearchControllerDelegate, NewPost
9990 }
10091
10192 @objc func refreshPostsData( _ ff: UIRefreshControl){
102− Model.getLast { [weak self] (channel) in
103− self?.channel = channel
93+ Model.getLast { [weak self] (tuple) in
94+ self?.channel = tuple.channel
95+ self?.dictionary = tuple.users
10496 self?.refreshContr.endRefreshing()
10597 }
10698 }
@@ -124,8 +116,9 @@ class NewsController: UITableViewController, UISearchControllerDelegate, NewPost
124116 searchController.isActive = false
125117 // TODO:- display something if no posts are availiable
126118
127− Model.getLast { [weak self] (channel) in
128− self?.channel = channel
119+ Model.getLast {
120+ self.channel = $0.channel
121+ self.dictionary = $0.users
129122 }
130123
131124 // tableView.reloadData()
GDproject/Controller/ News and channels/PostViewCell.swift+1−1
@@ -88,7 +88,7 @@ class PostViewCell: UITableViewCell
8888 views.append(createTextView(with: attachment.markdown, isFullVersoin))
8989 }
9090
91− nameLabel.setTitle("\(post.user?.firstName ?? "") \(post.user?.secondName ?? "")", for: .normal)
91+ nameLabel.setTitle("\(post.user?.firstName ?? "") \(post.user?.lastName ?? "")", for: .normal)
9292 nameLabel.addTarget(self, action: #selector(displayProfile), for: .touchUpInside)
9393 fullNameLabel.text = "\(post.authorId)"
9494 setUpInStackView(isFullVersoin)
GDproject/Controller/Log In/LogInViewController.swift+1−3
@@ -132,9 +132,7 @@ class LogInViewController: UIViewController {
132132
133133 UIView.animate(withDuration: 0, delay: 0, options: .curveEaseOut, animations: {
134134 self.view.layoutIfNeeded()
135− }) { (completed) in
136−
137− }
135+ })
138136 }
139137 }
140138
GDproject/Controller/Profile/ProfileViewController.swift+9−4
@@ -33,10 +33,15 @@ class ProfileViewController: UIViewController
3333
3434 func fill(with user: Model.Users){
3535 self.facultyLabel.text = "Студент: Факультет Компьютерных Наук"
36− self.nameLabel.text = "\(user.firstName)"
37− self.surnameLabel.text = "\(user.secondName)"
36+ self.nameLabel.text = "\(user.firstName) \(user.middleName)"
37+ self.surnameLabel.text = "\(user.lastName)"
3838 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+ }
4045 }
4146
4247 var user: Model.Users? {
@@ -55,7 +60,7 @@ class ProfileViewController: UIViewController
5560 var newPosts: [Model.Posts] = []
5661
5762 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))
5964 })
6065
6166 self.posts.dataSourse = newPosts
GDproject/Simple model/Model.swift+43−28
@@ -10,46 +10,61 @@ import Foundation
1010import Alamofire
1111
1212class 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+ }
1320
1421 private static let baseUrl = "https://valera-denis.herokuapp.com"
1522
16− static let url = URL(string:"\(baseUrl)/authenticate")!
23+ static let url = URL(string:"\(baseUrl)/authentication/login")!
1724 static let url1 = URL(string:"\(baseUrl)/posts/last")!
1825 static let urlForDrafts = URL(string:"\(baseUrl)/drafts/create")!
1926 static let urlForPublish = URL(string:"\(baseUrl)/drafts/publish")!
2027 static let urlForUpdate = URL(string:"\(baseUrl)/drafts/update")!
2128 static let urlForPostsForUser = URL(string:"\(baseUrl)/posts/forUser")!
2229 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+ }
2437
2538 struct Posts: Codable {
2639 var body: [Attachments]
2740 var authorId: Int
2841 var id: Int
2942 var user: Model.Users?
43+ var updated: String
3044
31− init(body: [Attachments], authorId: Int, id: Int) {
45+ init(body: [Attachments], authorId: Int, id: Int, date: String) {
3246 self.body = body
3347 self.authorId = authorId
3448 self.id = id
49+ self.updated = date
3550 }
3651
37− init(body: [Attachments], authorId: Int, id: Int, user: Model.Users) {
52+ init(body: [Attachments], authorId: Int, id: Int, user: Model.Users, date: String) {
3853 self.body = body
3954 self.authorId = authorId
4055 self.id = id
4156 self.user = user
42− }
43−
44− mutating func setUser(with: Users){
45− self.user = with
57+ self.updated = date
4658 }
4759 }
4860
4961 struct Users: Codable{
50− var secondName: String
62+
63+ var middleName: String
64+ var lastName: String
5165 var firstName: String
5266 var id: Int
67+
5368 }
5469
5570 struct Attachments: Codable {
@@ -74,8 +89,15 @@ class Model{
7489
7590 AF.request(request).response { (responce) in
7691
77− guard let realResponse = responce.response, realResponse.statusCode == 204 else {
78− print("Not a 204 response")
92+ guard let realResponse = responce.response, realResponse.statusCode == 200 else
93+ {
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 {
79101 completion(false)
80102 return
81103 }
@@ -86,13 +108,13 @@ class Model{
86108
87109 HTTPCookieStorage.shared.setCookie(cookies[0])
88110
89− DataStorage.standard.setIsLoggedIn(value: true, with: id)
111+ DataStorage.standard.setIsLoggedIn(value: true, with: personId)
90112 completion(true)
91113 }
92114 }
93115
94116
95− static func getLast(completion: @escaping ((Channel)->())){
117+ static func getLast(completion: @escaping (((users:[Int: Users], channel:Channel))->())){
96118 let jsonString = "30"
97119 var request = URLRequest(url: url1)
98120
@@ -105,18 +127,15 @@ class Model{
105127 AF.request(request).responseJSON {
106128 (response) in
107129
108− if response.response?.statusCode == 200 {
109− print("success")
110− } else {
111− print(response.response?.statusCode)
112− }
130+ isValidTocken?(response.response?.statusCode ?? 498)
113131
114132 let decoder = JSONDecoder()
115133 guard let json = response.data else { return }
116−
117− guard let newPost = try? decoder.decode([Posts].self, from: json) else { return }
118134
119− completion(Channel(title: "# General", subtitle: "No subtitle", hashtags: ["ПИ"], people: ["No"], posts: newPost))
135+ guard let newQueery = try? decoder.decode(QueryPosts.self, from: json) else { print("no")
136+ return }
137+
138+ completion((newQueery.users, Channel(title: "# General", subtitle: "No subtitle", hashtags: ["ПИ"], people: ["No"], posts: newQueery.posts)))
120139 }
121140 }
122141
@@ -168,11 +187,7 @@ class Model{
168187 AF.request(request).response {
169188 (response) in
170189
171− if response.response?.statusCode == 204 {
172− print("post created")
173− } else {
174− print("failure in creating and publishing post")
175− }
190+ isValidTocken?(response.response?.statusCode ?? 498)
176191 }
177192 }
178193
@@ -202,9 +217,9 @@ class Model{
202217 let decoder = JSONDecoder()
203218 guard let json = response.data else { return }
204219
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 }
206221
207− completion(newPost)
222+ completion(newPost.posts)
208223 }
209224 }
210225