Advertisement
Dillon25

Untitled

Jul 13th, 2020
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.04 KB | None | 0 0
  1. //CommentViewController
  2. func sendSubComment() {
  3.         if let text = textView.text,
  4.         let username = self.username,
  5.         let user_picture = profile?.picture,
  6.         let user_id = profile?.sub,
  7.         let parent_id = parent_id {
  8.             let comment = Comment(post_id: "2d44a588-e47a-43c5-bd16-94e7073e4e14", username: username, user_picture: user_picture.absoluteString, user_id: user_id, text: text, parent_id: parent_id)
  9.  
  10.             let postRequest = CommentPostRequest(endpoint: "sub_comment")
  11.             postRequest.save(comment) { (result) in
  12.                 switch result {
  13.                 case .success(let comment):                
  14.                     self.delegate2?.didSendSubComment()
  15.              
  16.                 case .failure(let error):
  17.                     print("An error occurred: \(error)")
  18.                 }
  19.             }
  20.         }
  21.     }
  22.  
  23. //CommentCell
  24. var viewModel: CommentViewModel? {
  25.         didSet {
  26.             if let item = viewModel {
  27.                 item.subComments.forEach { subComment in
  28.                     print("didSet subComment")
  29.                  
  30.                 }
  31.  
  32.                item.subCommentDidInserts = { [weak self] insertedSubComments in
  33.                print("got inserted sub comments")
  34.                guard let `self` = self else { return }
  35.                insertedSubComments.forEach { subComment in
  36.                
  37.           }
  38.         }
  39.       }
  40. extension CommentCell: TableViewDelegate {
  41.     func didSendSubComment() {
  42.         if let id = parent_id {
  43.         viewModel?.loadSubCommentsAfterReply(comment_id: id)
  44.         }
  45.     }
  46. }
  47.  
  48. //CommentViewModel
  49. var subComments: [Comments] = []
  50. var subCommentDidInserts: (([Comments]) -> ())?
  51.  
  52. func loadSubCommentsAfterReply(comment_id:String?) {
  53.         if let id = comment_id {
  54.         let getSubComments = GETSubComments(id: id, path: "subComments", offset: "0")
  55.             getSubComments.getAllById {
  56.                    self.subComments += $0
  57.                    self.subCommentDidInserts?($0)
  58.         }
  59.       }
  60.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement