Skip to content

Instantly share code, notes, and snippets.

@shakked
Created January 24, 2016 17:23
Show Gist options
  • Save shakked/98e789468465408cd016 to your computer and use it in GitHub Desktop.
Save shakked/98e789468465408cd016 to your computer and use it in GitHub Desktop.
//
// UserInteractionFetcher.swift
// Instagrowth
//
// Created by Zachary Shakked on 1/17/16.
// Copyright © 2016 Zachary Shakked. All rights reserved.
//
import UIKit
import AFNetworking
enum UserInteractionType {
case Likes
case Comments
}
class UserInteractionFetcher : NSObject {
let postSummaries : [PostSummary]
let accessToken : String
private var likes : [Like] = []
private lazy var likesQueue : NSOperationQueue = NSOperationQueue()
private var likesCompletion : ((Bool, [Like]) -> (Void))?
private var comments : [Comment] = []
private lazy var commentsQueue : NSOperationQueue = NSOperationQueue()
private var commentsCompletion : ((Bool, [Comment]) -> (Void))?
init(postSummaries: [PostSummary], accessToken: String) {
self.postSummaries = postSummaries
self.accessToken = accessToken
super.init()
}
func fetchLikes(completion: (Bool, [Like]) -> (Void)) {
likesCompletion = completion
var operations : [NSOperation] = []
for postSummary in postSummaries {
let request : NSURLRequest = userInteractionRequest(.Likes, postSummary: postSummary)
let operation = AFHTTPRequestOperation(request: request)
operation.responseSerializer = AFJSONResponseSerializer()
operation.setCompletionBlockWithSuccess({ (operations, result) -> Void in
guard let datas = result["data"] as? [NSDictionary] else { return }
self.likes += Like.parse(datas, postSummary: postSummary)
}, failure: { (operations, error) -> Void in
completion(false, [])
})
operations.append(operation)
}
likesQueue.addOperations(operations, waitUntilFinished: false)
likesQueue.addObserver(self, forKeyPath: "operations", options: NSKeyValueObservingOptions.init(rawValue: 0), context: nil)
}
func fetchComments(completion: (Bool, [Comment]) -> (Void)) {
commentsCompletion = completion
var operations : [NSOperation] = []
for postSummary in postSummaries {
let request : NSURLRequest = userInteractionRequest(.Comments, postSummary: postSummary)
let operation = AFHTTPRequestOperation(request: request)
operation.responseSerializer = AFJSONResponseSerializer()
operation.setCompletionBlockWithSuccess({ (operations, result) -> Void in
guard let datas = result["data"] as? [NSDictionary] else { return }
self.comments += Comment.parse(datas, postSummary: postSummary)
}, failure: { (operations, error) -> Void in
completion(false, [])
})
operations.append(operation)
}
commentsQueue.addOperations(operations, waitUntilFinished: false)
commentsQueue.addObserver(self, forKeyPath: "operations", options: NSKeyValueObservingOptions.init(rawValue: 0), context: nil)
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
guard let queue = object as? NSOperationQueue else {
return
}
if queue.operationCount == 0 && queue == commentsQueue {
commentsCompletion?(true, self.comments ?? [])
} else if queue.operationCount == 0 && queue == likesQueue {
likesCompletion?(true, self.likes ?? [])
}
}
private func userInteractionRequest(type: UserInteractionType, postSummary: PostSummary) -> NSURLRequest {
let params = "/media/\(postSummary.id)/\(type == .Likes ? "likes" : "comments")|access_token=\(accessToken)"
let sig = HMAC.signWithKey(UserSettings.InstagramClientSecret(), usingData: params)
let baseURL = type == .Likes ? postSummary.likesURL.absoluteString : postSummary.commentsURL.absoluteString
let paramters = "?access_token=\(accessToken)&sig=\(sig)"
let url = NSURL(string: "\(baseURL)\(paramters)")!
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "GET"
return request
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment