git.iko.soy/dashared/GDproject
on master help sign in
changed time on post. upd to new version of denis. channels and hashtags are left
fb5ea63Darya Rednikina
GDproject/Controller/ News and channels/NewPostViewController.swift+1−1
@@ -193,7 +193,7 @@ class NewPostViewController: UIViewController, UITextViewDelegate {
193 var indexOfPost = 0
193 var indexOfPost = 0
194 // MARK:- new post
194 // MARK:- new post
195 @objc func newPost(){
195 @objc func newPost(){
196− Model.createAndPublish(string: textView.text!)
196+ Model.createAndPublish(body: [ Model.Attachments(markdown: textView!.text) ], tags: ["ПИ", "люблюСвоюРаботу", "работаРулит", "Шершаков"])
197 // adding row to uiTableView after adding new post
197 // adding row to uiTableView after adding new post
198 // myProtocol?.addPost(post: p)
198 // myProtocol?.addPost(post: p)
199 moveBackToParentVC()
199 moveBackToParentVC()
GDproject/Controller/ News and channels/NewsController.swift+1−1
@@ -33,7 +33,7 @@ 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]!, date: post.updated))
36+ newPosts.append(Model.Posts(body: post.body, authorId: post.authorId, id: post.id, user: dictionary![post.authorId]!, date: post.updated, tags: post.tags))
37 })
37 })
38
38
39 news.dataSourse = newPosts
39 news.dataSourse = newPosts
GDproject/Controller/ News and channels/PostViewCell.swift+5−1
@@ -88,8 +88,12 @@ class PostViewCell: UITableViewCell
88 setUpInStackView(isFullVersoin)
88 setUpInStackView(isFullVersoin)
89 }
89 }
90
90
91− let hashtags = ["Программная инженерия", "ПИ", "НИУ ВШЭ", "Наука"]
91+ var hashtags = [String]()
92+
92 func setUpInStackView(_ full : Bool){
93 func setUpInStackView(_ full : Bool){
94+
95+ hashtags = post!.tags
96+
93 let nameStackView = UIStackView(arrangedSubviews: [nameLabel, fullNameLabel])
97 let nameStackView = UIStackView(arrangedSubviews: [nameLabel, fullNameLabel])
94 nameStackView.axis = .vertical
98 nameStackView.axis = .vertical
95 contentView.addSubview(nameStackView)
99 contentView.addSubview(nameStackView)
GDproject/Controller/Profile/BasicInfoController.swift+4−5
@@ -39,15 +39,14 @@ class BasicInfoController: UIViewController, UITableViewDelegate, UITableViewDat
39
39
40 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
40 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
41 if indexPath.row == 0 {
41 if indexPath.row == 0 {
42− let cell = tableView.dequeueReusableCell(withIdentifier: basicInfoCellId, for: indexPath) as! BasicInfoCell
42+ let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
43− cell.fill(title: dataSourse[indexPath.section].title)
43+ cell.textLabel?.text = dataSourse[indexPath.section].title
44 cell.accessoryType = .disclosureIndicator
44 cell.accessoryType = .disclosureIndicator
45 cell.selectionStyle = .none
45 cell.selectionStyle = .none
46 return cell
46 return cell
47 } else {
47 } else {
48− let cell = tableView.dequeueReusableCell(withIdentifier: infoCellId, for: indexPath) as! InfoCell
48+ let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
49− cell.fill(title: dataSourse[indexPath.section].sectionData[indexPath.row - 1])
49+ cell.textLabel?.text = dataSourse[indexPath.section].sectionData[indexPath.row - 1]
50− cell.selectionStyle = .none
51 return cell
50 return cell
52 }
51 }
53 }
52 }
GDproject/Controller/Profile/ProfileViewController.swift+1−1
@@ -60,7 +60,7 @@ class ProfileViewController: UIViewController
60 var newPosts: [Model.Posts] = []
60 var newPosts: [Model.Posts] = []
61
61
62 dataSourse?.forEach({ (post) in
62 dataSourse?.forEach({ (post) in
63− newPosts.append(Model.Posts(body: post.body, authorId: post.authorId, id: post.id, user: user!, date: post.updated))
63+ newPosts.append(Model.Posts(body: post.body, authorId: post.authorId, id: post.id, user: user!, date: post.updated, tags: post.tags))
64 })
64 })
65
65
66 self.posts.dataSourse = newPosts
66 self.posts.dataSourse = newPosts
GDproject/Simple model/Model.swift+72−46
@@ -13,6 +13,7 @@ class Model{
13 static let invalidTocken = 498
13 static let invalidTocken = 498
14 private static var isValidTocken: ((Int)->())? = {
14 private static var isValidTocken: ((Int)->())? = {
15 responce in
15 responce in
16+ print("\(responce)")
16 if responce == invalidTocken {
17 if responce == invalidTocken {
17 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
18 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
18 }
19 }
@@ -22,12 +23,10 @@ class Model{
22
23
23 static let url = URL(string:"\(baseUrl)/authentication/login")!
24 static let url = URL(string:"\(baseUrl)/authentication/login")!
24 static let url1 = URL(string:"\(baseUrl)/posts/last")!
25 static let url1 = URL(string:"\(baseUrl)/posts/last")!
25− static let urlForDrafts = URL(string:"\(baseUrl)/drafts/create")!
26− static let urlForPublish = URL(string:"\(baseUrl)/drafts/publish")!
27− static let urlForUpdate = URL(string:"\(baseUrl)/drafts/update")!
28 static let urlForPostsForUser = URL(string:"\(baseUrl)/posts/forUser")!
26 static let urlForPostsForUser = URL(string:"\(baseUrl)/posts/forUser")!
29 static let urlForUsers = URL(string:"\(baseUrl)/users")!
27 static let urlForUsers = URL(string:"\(baseUrl)/users")!
30 static let createAndPublishURL = URL(string:"\(baseUrl)/posts/publish")!
28 static let createAndPublishURL = URL(string:"\(baseUrl)/posts/publish")!
29+ static let channelsGetURL = URL(string: "\(baseUrl)/channels/get")!
31
30
32
31
33 struct QueryPosts: Codable{
32 struct QueryPosts: Codable{
@@ -41,20 +40,23 @@ class Model{
41 var id: Int
40 var id: Int
42 var user: Model.Users?
41 var user: Model.Users?
43 var updated: String
42 var updated: String
43+ var tags: [String]
44
44
45− init(body: [Attachments], authorId: Int, id: Int, date: String) {
45+ init(body: [Attachments], authorId: Int, id: Int, date: String,tags: [String]) {
46 self.body = body
46 self.body = body
47 self.authorId = authorId
47 self.authorId = authorId
48 self.id = id
48 self.id = id
49 self.updated = date
49 self.updated = date
50+ self.tags = tags
50 }
51 }
51
52
52− init(body: [Attachments], authorId: Int, id: Int, user: Model.Users, date: String) {
53+ init(body: [Attachments], authorId: Int, id: Int, user: Model.Users, date: String, tags: [String]) {
53 self.body = body
54 self.body = body
54 self.authorId = authorId
55 self.authorId = authorId
55 self.id = id
56 self.id = id
56 self.user = user
57 self.user = user
57 self.updated = date
58 self.updated = date
59+ self.tags = tags
58 }
60 }
59
61
60 func convertDateFormatter() -> String
62 func convertDateFormatter() -> String
@@ -62,17 +64,33 @@ class Model{
62
64
63 let dateFormatter = DateFormatter()
65 let dateFormatter = DateFormatter()
64 dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"//this your string date format
66 dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"//this your string date format
65− dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
67+ dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
66 let date = dateFormatter.date(from: updated)
68 let date = dateFormatter.date(from: updated)
67
69
68 dateFormatter.dateFormat = "MMM d, yyyy HH:mm" ///this is what you want to convert format
70 dateFormatter.dateFormat = "MMM d, yyyy HH:mm" ///this is what you want to convert format
69− dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
71+ dateFormatter.timeZone = NSTimeZone.local
70 let timeStamp = dateFormatter.string(from: date!)
72 let timeStamp = dateFormatter.string(from: date!)
71
73
72 return timeStamp
74 return timeStamp
73 }
75 }
74 }
76 }
75
77
78+ struct CreatedPost: Codable {
79+ var body = [Attachments]()
80+ var tags = [String]()
81+
82+ enum CodingKeys: String, CodingKey {
83+ case body
84+ case tags
85+ }
86+
87+ func encode(to encoder: Encoder) throws {
88+ var container = encoder.container(keyedBy: CodingKeys.self)
89+ try container.encode(body, forKey: .body)
90+ try container.encode(tags, forKey: .tags)
91+ }
92+ }
93+
76 struct Users: Codable{
94 struct Users: Codable{
77
95
78 var middleName: String
96 var middleName: String
@@ -88,6 +106,15 @@ class Model{
88 init(markdown: String) {
106 init(markdown: String) {
89 self.markdown = markdown
107 self.markdown = markdown
90 }
108 }
109+
110+ enum CodingKeys: String, CodingKey {
111+ case markdown
112+ }
113+
114+ func encode(to encoder: Encoder) throws {
115+ var container = encoder.container(keyedBy: CodingKeys.self)
116+ try container.encode(markdown, forKey: .markdown)
117+ }
91 }
118 }
92
119
93 static func authenticate(with id: Int, completion: @escaping ((Bool)->())) {
120 static func authenticate(with id: Int, completion: @escaping ((Bool)->())) {
@@ -155,50 +182,17 @@ class Model{
155 }
182 }
156 }
183 }
157
184
158− static func createDraft(completion: @escaping ((Int)->())){
159− let jsonString = "[{ \"markdown\": \"# This is a markdown title\nThis is body.\" }]"
160− var request = URLRequest(url: urlForDrafts)
161−
162− request.httpMethod = "POST"
163−
164− // insert json data to the request
165− request.httpBody = jsonString.data(using: .utf8)
166− request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
167−
168− AF.request(request).responseJSON {
169− (response) in
170−
171− if response.response?.statusCode == 200 {
172− print("success in creating draft")
173− } else {
174− print("\(response.response?.statusCode ?? 0)")
175− }
176−
177− //let decoder = JSONDecoder()
178− guard let json = response.data else { return }
179−
180− guard let id = String(data: json, encoding: String.Encoding.utf8) else {return}
181−
182− print("with id \(id)")
183− completion(Int(id) ?? 0)
184− }
185− }
186−
187
185
188− static func createAndPublish(string: String){
186+ static func createAndPublish(body: [Attachments], tags: [String]){
189− let jsonUpd = """
187+ let jsonUpd = CreatedPost(body: body, tags: tags)
190− [
188+
191− {
192− "markdown": "\(string)"
193− }
194− ]
195− """
196 var request = URLRequest(url: createAndPublishURL)
189 var request = URLRequest(url: createAndPublishURL)
197
190
198 request.httpMethod = "POST"
191 request.httpMethod = "POST"
199
192
200 // insert json data to the request
193 // insert json data to the request
201− request.httpBody = jsonUpd.data(using: .utf8)
194+ request.httpBody = try? JSONEncoder().encode(jsonUpd)
195+
202 request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
196 request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
203 AF.request(request).response {
197 AF.request(request).response {
204 (response) in
198 (response) in
@@ -210,7 +204,7 @@ class Model{
210
204
211 static func getPostsForUser(with id: Int, completion: @escaping (([Posts])->())){
205 static func getPostsForUser(with id: Int, completion: @escaping (([Posts])->())){
212 let json = [
206 let json = [
213− "userId" : id,
207+ "request" : id,
214 "limit": 10
208 "limit": 10
215 ]
209 ]
216
210
@@ -271,4 +265,36 @@ class Model{
271 completion(Model.idUser)
265 completion(Model.idUser)
272 }
266 }
273 }
267 }
268+
269+ // get channel (with id): in responce -- PostQuery
270+ static func getChannels(with channelId: Int, completion: @escaping (([Posts])->())){
271+ let json = [
272+ "request" : channelId,
273+ "limit": 10
274+ ]
275+
276+ let jsonData = try? JSONSerialization.data(withJSONObject: json)
277+
278+ var request = URLRequest(url: urlForPostsForUser)
279+ request.httpMethod = "POST"
280+
281+ // insert json data to the request
282+ request.httpBody = jsonData
283+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
284+
285+ AF.request(request).responseJSON {
286+ (response) in
287+
288+ if response.response?.statusCode == 200 {
289+ print("success")
290+ }
291+
292+ let decoder = JSONDecoder()
293+ guard let json = response.data else { return }
294+
295+ guard let newPost = try? decoder.decode(QueryPosts.self, from: json) else { return }
296+
297+ completion(newPost.posts)
298+ }
299+ }
274}
300}