mirror of
https://github.com/zhigang1992/GitHawk.git
synced 2026-06-05 06:49:56 +08:00
connect add comment client
This commit is contained in:
61
Classes/Issues/AddCommentClient.swift
Normal file
61
Classes/Issues/AddCommentClient.swift
Normal 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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
</dict>
|
||||
</array>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>951</string>
|
||||
<string>952</string>
|
||||
<key>ITSAppUsesNonExemptEncryption</key>
|
||||
<false/>
|
||||
<key>LSApplicationQueriesSchemes</key>
|
||||
|
||||
Reference in New Issue
Block a user