diff --git a/Yep/Services/YepService.swift b/Yep/Services/YepService.swift index 442f33df..7652044c 100644 --- a/Yep/Services/YepService.swift +++ b/Yep/Services/YepService.swift @@ -1307,8 +1307,8 @@ func dribbbleWorkOfUserWithUserID(userID: String, #failureHandler: ((Reason, Str description = shotInfo["description"] as? String, htmlURLString = shotInfo["html_url"] as? String, imagesInfo = shotInfo["images"] as? JSONDictionary, - likesCount = shotInfo["comments_count"] as? Int, - commentsCount = shotInfo["likes_count"] as? Int { + likesCount = shotInfo["likes_count"] as? Int, + commentsCount = shotInfo["comments_count"] as? Int { if let normal = imagesInfo["normal"] as? String, teaser = imagesInfo["teaser"] as? String { @@ -1338,3 +1338,73 @@ func dribbbleWorkOfUserWithUserID(userID: String, #failureHandler: ((Reason, Str } } + +struct InstagramWork { + + struct Media { + + struct Images { + let lowResolution: String + let standardResolution: String + let thumbnail: String + } + + let linkURLString: String + let images: Images + let likesCount: Int + let commentsCount: Int + } + + let medias: [Media] +} + +func instagramWorkOfUserWithUserID(userID: String, #failureHandler: ((Reason, String?) -> Void)?, #completion: InstagramWork -> Void) { + + let parse: JSONDictionary -> InstagramWork? = { data in + println("instagramData:\(data)") + + if let mediaData = data["media"] as? [JSONDictionary] { + var medias = Array() + + for mediaInfo in mediaData { + if let + linkURLString = mediaInfo["link"] as? String, + imagesInfo = mediaInfo["images"] as? JSONDictionary, + likesInfo = mediaInfo["likes"] as? JSONDictionary, + commentsInfo = mediaInfo["comments"] as? JSONDictionary { + if let + lowResolutionInfo = imagesInfo["low_resolution"] as? JSONDictionary, + standardResolutionInfo = imagesInfo["standard_resolution"] as? JSONDictionary, + thumbnailInfo = imagesInfo["thumbnail"] as? JSONDictionary, + + lowResolution = lowResolutionInfo["url"] as? String, + standardResolution = standardResolutionInfo["url"] as? String, + thumbnail = thumbnailInfo["url"] as? String, + + likesCount = likesInfo["count"] as? Int, + commentsCount = commentsInfo["count"] as? Int { + + let images = InstagramWork.Media.Images(lowResolution: lowResolution, standardResolution: standardResolution, thumbnail: thumbnail) + + let media = InstagramWork.Media(linkURLString: linkURLString, images: images, likesCount: likesCount, commentsCount: commentsCount) + + medias.append(media) + } + } + } + + return InstagramWork(medias: medias) + } + + return nil + } + + let resource = authJsonResource(path: "/api/v1/users/\(userID)/instagram", method: .GET, requestParameters: [:], parse: parse) + + if let failureHandler = failureHandler { + apiRequest({_ in}, baseURL, resource, failureHandler, completion) + } else { + apiRequest({_ in}, baseURL, resource, defaultFailureHandler, completion) + } +} + diff --git a/Yep/ViewControllers/SocialWorks/SocialWorkInstagramViewController.swift b/Yep/ViewControllers/SocialWorks/SocialWorkInstagramViewController.swift index 80379e80..b06d4de3 100644 --- a/Yep/ViewControllers/SocialWorks/SocialWorkInstagramViewController.swift +++ b/Yep/ViewControllers/SocialWorks/SocialWorkInstagramViewController.swift @@ -22,7 +22,7 @@ class SocialWorkInstagramViewController: UIViewController { return CGRectGetWidth(self.instagramCollectionView.bounds) }() - var dribbbleShots = Array() { + var instagramMedias = Array() { didSet { updateInstagramCollectionView() } @@ -33,6 +33,37 @@ class SocialWorkInstagramViewController: UIViewController { instagramCollectionView.registerNib(UINib(nibName: instagramMediaCellIdentifier, bundle: nil), forCellWithReuseIdentifier: instagramMediaCellIdentifier) + + // 获取 Instagram Work + + var userID: String? + + if let profileUser = profileUser { + switch profileUser { + case .DiscoveredUserType(let discoveredUser): + userID = discoveredUser.id + case .UserType(let user): + userID = user.userID + } + + } else { + userID = YepUserDefaults.userID.value + } + + if let userID = userID { + + instagramWorkOfUserWithUserID(userID, failureHandler: { (reason, errorMessage) -> Void in + defaultFailureHandler(reason, errorMessage) + + }, completion: { instagramWork in + println("instagramWork: \(instagramWork.medias.count)") + + dispatch_async(dispatch_get_main_queue()) { + self.instagramMedias = instagramWork.medias + } + }) + } + } // MARK: Actions @@ -50,14 +81,16 @@ extension SocialWorkInstagramViewController: UICollectionViewDataSource, UIColle } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { - return 30 + return instagramMedias.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier(instagramMediaCellIdentifier, forIndexPath: indexPath) as! InstagramMediaCell - cell.imageView.image = UIImage(named: "Cover3")! + let media = instagramMedias[indexPath.item] + + cell.imageView.kf_setImageWithURL(NSURL(string: media.images.lowResolution)!) return cell }