Advertisement
Dillon25

Untitled

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