connect add comment client

This commit is contained in:
Ryan Nystrom
2017-07-23 08:54:18 -04:00
parent 9c374c92cc
commit 3dee5844cb
3 changed files with 69 additions and 3 deletions

View File

@@ -0,0 +1,61 @@
//
// AddCommentClient.swift
// Freetime
//
// Created by Ryan Nystrom on 7/15/17.
// Copyright © 2017 Ryan Nystrom. All rights reserved.
//
import Foundation
protocol AddCommentListener: class {
func didSendComment(client: AddCommentClient, id: String, commentFields: CommentFields, reactionFields: ReactionFields)
func didFailSendingComment(client: AddCommentClient)
}
final class AddCommentClient {
private class ListenerWrapper: NSObject {
weak var listener: AddCommentListener? = nil
}
private var listeners = [ListenerWrapper]()
private let client: GithubClient
private let subjectId: String
init(client: GithubClient, subjectId: String) {
self.client = client
self.subjectId = subjectId
}
// MARK: Public API
func addListener(listener: AddCommentListener) {
let wrapper = ListenerWrapper()
wrapper.listener = listener
listeners.append(wrapper)
}
func addComment(body: String) {
client.apollo.perform(mutation: AddCommentMutation(subjectId: subjectId, body: body)) { (result, error) in
if let commentNode = result?.data?.addComment?.commentEdge.node {
let fragments = commentNode.fragments
for listener in self.listeners {
listener.listener?.didSendComment(
client: self,
id: fragments.nodeFields.id,
commentFields: fragments.commentFields,
reactionFields: fragments.reactionFields
)
}
} else {
for listener in self.listeners {
listener.listener?.didFailSendingComment(client: self)
}
}
ShowErrorStatusBar(graphQLErrors: result?.errors, networkError: error)
}
}
}

View File

@@ -22,7 +22,7 @@ AddCommentListener {
private let repo: String
private let number: Int
private var subjectId: String? = nil
private var addCommentClient: AddCommentClient? = nil
private var models = [ListDiffable]()
lazy private var feed: Feed = { Feed(viewController: self, delegate: self, collectionView: self.collectionView) }()
@@ -161,7 +161,12 @@ AddCommentListener {
number: number,
width: view.bounds.width
) { subjectId, results in
self.subjectId = subjectId
if let subjectId = subjectId {
let addCommentClient = AddCommentClient(client: self.client, subjectId: subjectId)
addCommentClient.addListener(listener: self)
self.addCommentClient = addCommentClient
}
self.models = results
self.feed.finishLoading(dismissRefresh: true)
}

View File

@@ -32,7 +32,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>951</string>
<string>952</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSApplicationQueriesSchemes</key>