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
commitfb5ea63b8dadd32efa2e58ad07ce4338fe6dbcce authorDarya Rednikina <rednikina.com@yandex.ru> dateTue 12 Mar 2019, 20:43 (8 years ago) parent78a2ade
6 files changed+84−55
GDproject/Controller/ News and channels/NewPostViewController.swift+1−1 GDproject/Controller/ News and channels/NewsController.swift+1−1 GDproject/Controller/ News and channels/PostViewCell.swift+5−1 GDproject/Controller/Profile/BasicInfoController.swift+4−5 GDproject/Controller/Profile/ProfileViewController.swift+1−1 GDproject/Simple model/Model.swift+72−46
GDproject/Controller/ News and channels/NewPostViewController.swift+1−1
@@ -193,7 +193,7 @@ class NewPostViewController: UIViewController, UITextViewDelegate {
193193 var indexOfPost = 0
194194 // MARK:- new post
195195 @objc func newPost(){
196− Model.createAndPublish(string: textView.text!)
196+ Model.createAndPublish(body: [ Model.Attachments(markdown: textView!.text) ], tags: ["ПИ", "люблюСвоюРаботу", "работаРулит", "Шершаков"])
197197 // adding row to uiTableView after adding new post
198198 // myProtocol?.addPost(post: p)
199199 moveBackToParentVC()
GDproject/Controller/ News and channels/NewsController.swift+1−1
@@ -33,7 +33,7 @@ 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]!, 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))
3737 })
3838
3939 news.dataSourse = newPosts
GDproject/Controller/ News and channels/PostViewCell.swift+5−1
@@ -88,8 +88,12 @@ class PostViewCell: UITableViewCell
8888 setUpInStackView(isFullVersoin)
8989 }
9090
91− let hashtags = ["Программная инженерия", "ПИ", "НИУ ВШЭ", "Наука"]
91+ var hashtags = [String]()
92+
9293 func setUpInStackView(_ full : Bool){
94+
95+ hashtags = post!.tags
96+
9397 let nameStackView = UIStackView(arrangedSubviews: [nameLabel, fullNameLabel])
9498 nameStackView.axis = .vertical
9599 contentView.addSubview(nameStackView)
GDproject/Controller/Profile/BasicInfoController.swift+4−5
@@ -39,15 +39,14 @@ class BasicInfoController: UIViewController, UITableViewDelegate, UITableViewDat
3939
4040 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
4141 if indexPath.row == 0 {
42− let cell = tableView.dequeueReusableCell(withIdentifier: basicInfoCellId, for: indexPath) as! BasicInfoCell
43− cell.fill(title: dataSourse[indexPath.section].title)
42+ let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
43+ cell.textLabel?.text = dataSourse[indexPath.section].title
4444 cell.accessoryType = .disclosureIndicator
4545 cell.selectionStyle = .none
4646 return cell
4747 } else {
48− let cell = tableView.dequeueReusableCell(withIdentifier: infoCellId, for: indexPath) as! InfoCell
49− cell.fill(title: dataSourse[indexPath.section].sectionData[indexPath.row - 1])
50− cell.selectionStyle = .none
48+ let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
49+ cell.textLabel?.text = dataSourse[indexPath.section].sectionData[indexPath.row - 1]
5150 return cell
5251 }
5352 }
GDproject/Controller/Profile/ProfileViewController.swift+1−1
@@ -60,7 +60,7 @@ class ProfileViewController: UIViewController
6060 var newPosts: [Model.Posts] = []
6161
6262 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))
6464 })
6565
6666 self.posts.dataSourse = newPosts
GDproject/Simple model/Model.swift+72−46
@@ -13,6 +13,7 @@ class Model{
1313 static let invalidTocken = 498
1414 private static var isValidTocken: ((Int)->())? = {
1515 responce in
16+ print("\(responce)")
1617 if responce == invalidTocken {
1718 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
1819 }
@@ -22,12 +23,10 @@ class Model{
2223
2324 static let url = URL(string:"\(baseUrl)/authentication/login")!
2425 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")!
2826 static let urlForPostsForUser = URL(string:"\(baseUrl)/posts/forUser")!
2927 static let urlForUsers = URL(string:"\(baseUrl)/users")!
3028 static let createAndPublishURL = URL(string:"\(baseUrl)/posts/publish")!
29+ static let channelsGetURL = URL(string: "\(baseUrl)/channels/get")!
3130
3231
3332 struct QueryPosts: Codable{
@@ -41,20 +40,23 @@ class Model{
4140 var id: Int
4241 var user: Model.Users?
4342 var updated: String
43+ var tags: [String]
4444
45− init(body: [Attachments], authorId: Int, id: Int, date: String) {
45+ init(body: [Attachments], authorId: Int, id: Int, date: String,tags: [String]) {
4646 self.body = body
4747 self.authorId = authorId
4848 self.id = id
4949 self.updated = date
50+ self.tags = tags
5051 }
5152
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]) {
5354 self.body = body
5455 self.authorId = authorId
5556 self.id = id
5657 self.user = user
5758 self.updated = date
59+ self.tags = tags
5860 }
5961
6062 func convertDateFormatter() -> String
@@ -62,17 +64,33 @@ class Model{
6264
6365 let dateFormatter = DateFormatter()
6466 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")
6668 let date = dateFormatter.date(from: updated)
6769
6870 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
7072 let timeStamp = dateFormatter.string(from: date!)
7173
7274 return timeStamp
7375 }
7476 }
7577
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+
7694 struct Users: Codable{
7795
7896 var middleName: String
@@ -88,6 +106,15 @@ class Model{
88106 init(markdown: String) {
89107 self.markdown = markdown
90108 }
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+ }
91118 }
92119
93120 static func authenticate(with id: Int, completion: @escaping ((Bool)->())) {
@@ -155,50 +182,17 @@ class Model{
155182 }
156183 }
157184
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−
187185
188− static func createAndPublish(string: String){
189− let jsonUpd = """
190− [
191− {
192− "markdown": "\(string)"
193− }
194− ]
195− """
186+ static func createAndPublish(body: [Attachments], tags: [String]){
187+ let jsonUpd = CreatedPost(body: body, tags: tags)
188+
196189 var request = URLRequest(url: createAndPublishURL)
197190
198191 request.httpMethod = "POST"
199192
200193 // insert json data to the request
201− request.httpBody = jsonUpd.data(using: .utf8)
194+ request.httpBody = try? JSONEncoder().encode(jsonUpd)
195+
202196 request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
203197 AF.request(request).response {
204198 (response) in
@@ -210,7 +204,7 @@ class Model{
210204
211205 static func getPostsForUser(with id: Int, completion: @escaping (([Posts])->())){
212206 let json = [
213− "userId" : id,
207+ "request" : id,
214208 "limit": 10
215209 ]
216210
@@ -271,4 +265,36 @@ class Model{
271265 completion(Model.idUser)
272266 }
273267 }
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+ }
274300}