lundi 16 novembre 2020

Swift Design Patterns - Actions?

I am learning about iOS development and best practices etc, and I am building an app to implement these things.

I have added the coordinator pattern, which makes sense to me in that it decouples the controllers, enables my app to show any view controller at any point etc.

The one thing I am struggling with from a pattern perspective, is how to I offload my "actions" from the view controller. One of the actions I have is

    func handleCommentPost() {
        print("Handle post comment")
        
        if !self.customView.textView.text.isEmpty {
            if let comment = self.customView.textView.text {
                self.viewModel.createPostComment(feedItem: feedItem, user: self.user, comment: comment) { result in
                    switch result {
                    case .success(let comment):
                        self.customView.textView.text = ""
                        let postDetail = PostDetail(comment: comment, like: nil, user: self.user)
                        self.userComments.insert(postDetail, at: 0)
                        self.applySnapshot()
                        self.customView.textView.resignFirstResponder()
                    case .failure(let error):
                        print("Failed to save comment: \(error)")
                    }
                }
            }

        }
    }

I want to be able to move this into something like an ActionServices or something like that but I don't know what pattern would that. In my mind I feel like I could use Combine, and I could subscribe to this even taking place?

Any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire