Created
June 11, 2018 10:14
-
-
Save IgorVoiT/dc52f52f0757cabd75152136781fbee8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import SwiftInstagram | |
class InstagramManager { | |
// MARK: - Properties | |
// MARK: Static | |
static let shared = InstagramManager() | |
// MARK: Private | |
private var instagram: Instagram = Instagram.shared | |
private var instagramUserTotalCountOfMedia: Int = 0 { | |
didSet { | |
if instagramUserTotalCountOfMedia > 0 { | |
getRecentMedia() | |
} | |
} | |
} | |
private var instagramUserRequestedCountOfMedia: Int = 0 | |
private var instagramUserMediaMaxIdForRequest: Int? = nil | |
// MARK: Public | |
var isAuthenticated: Bool { | |
get { | |
return instagram.isAuthenticated | |
} | |
} | |
var userMediaList: [[String: String]] = [[String: String]]() | |
// MARK: - Methods | |
// MARK: Init | |
init() { | |
} | |
// MARK: Public | |
func login(fromNavigationController navigationController: UINavigationController) { | |
instagram.login(from: navigationController, | |
success: { [weak self] in | |
self?.instagramUserTotalCountOfMedia = 0 | |
self?.instagramUserRequestedCountOfMedia = 0 | |
self?.instagramUserMediaMaxIdForRequest = 0 | |
NotificationCenter.default.post(name: NSNotification.Name(rawValue: kInstagramSignedIn), | |
object: nil, | |
userInfo: nil) | |
// Retrive recent media | |
if self?.instagram.isAuthenticated == true { | |
self?.getMedia() | |
} | |
}, | |
failure: { error in | |
print(error.localizedDescription) | |
NotificationCenter.default.post(name: NSNotification.Name(rawValue: kInstagramSignInFailed), | |
object: nil, | |
userInfo: ["message": error.localizedDescription]) | |
}) | |
} | |
func getMedia() { | |
getUser() | |
} | |
func logout() { | |
instagram.logout() | |
// Remove all Instagram data from the Firebase | |
ProfileManager.shared.profile.instagram = [[String: String]]() | |
FirebaseManager.shared.update(profile: ProfileManager.shared.profile) | |
NotificationCenter.default.post(name: NSNotification.Name(rawValue: kInstagramSignedOut), | |
object: nil, | |
userInfo: nil) | |
} | |
// MARK: Private | |
private func getUser() { | |
instagram.user("self", | |
success: { [weak self] instagramUser in | |
NotificationCenter.default.post(name: NSNotification.Name(rawValue: kInstagramUserGot), | |
object: nil, | |
userInfo: nil) | |
if let count = instagramUser.counts?.media { | |
self?.userMediaList = [[String: String]]() | |
self?.instagramUserTotalCountOfMedia = count | |
} | |
}, | |
failure: { error in | |
print(error.localizedDescription) | |
NotificationCenter.default.post(name: NSNotification.Name(rawValue: kInstagramUserGetFailed), | |
object: nil, | |
userInfo: ["message": error.localizedDescription]) | |
}) | |
} | |
private func getRecentMedia() { | |
let instagramUserNotRequestedCountOfMedia: Int = instagramUserTotalCountOfMedia - instagramUserRequestedCountOfMedia | |
if instagramUserNotRequestedCountOfMedia > 0 { | |
var count: Int = 0 | |
if instagramUserNotRequestedCountOfMedia <= 20 { | |
count = instagramUserNotRequestedCountOfMedia | |
} else { | |
count = 20 | |
} | |
instagramUserRequestedCountOfMedia = instagramUserRequestedCountOfMedia + count | |
instagram.recentMedia(fromUser: "self", | |
maxId: instagramUserMediaMaxIdForRequest != nil ? "\(instagramUserMediaMaxIdForRequest!)" : nil, | |
count: count, | |
success: { [weak self] mediaList in | |
if mediaList.count > 0 { | |
self?.parseMediaList(mediaList) | |
} | |
NotificationCenter.default.post(name: NSNotification.Name(rawValue: kInstagramRecentMediaGot), | |
object: nil, | |
userInfo: nil) | |
if let requestedCountOfMedia = self?.instagramUserRequestedCountOfMedia, | |
let totalCountOfMedia = self?.instagramUserTotalCountOfMedia, | |
requestedCountOfMedia < totalCountOfMedia { | |
self?.getRecentMedia() | |
} | |
}, | |
failure: { error in | |
print(error.localizedDescription) | |
NotificationCenter.default.post(name: NSNotification.Name(rawValue: kInstagramRecentMediaGetFailed), | |
object: nil, | |
userInfo: ["message": error.localizedDescription]) | |
}) | |
} | |
} | |
private func parseMediaList(_ mediaList: [InstagramMedia]) { | |
for mediaItem: InstagramMedia in mediaList { | |
if mediaItem.type == "image" { | |
let thumbnailURL: URL = mediaItem.images.thumbnail.url | |
let standardResolutionURL: URL = mediaItem.images.standardResolution.url | |
userMediaList.append(["type" : mediaItem.type, | |
"thumbnailURL": thumbnailURL.absoluteString, | |
"standardResolutionURL": standardResolutionURL.absoluteString]) | |
} else if mediaItem.type == "video", | |
let videos: InstagramMedia.Videos = mediaItem.videos { | |
let lowResolutionURL: URL = videos.lowResolution.url | |
let standardResolutionURL: URL = videos.standardResolution.url | |
userMediaList.append(["type" : mediaItem.type, | |
"lowResolutionURL": lowResolutionURL.absoluteString, | |
"standardResolutionURL": standardResolutionURL.absoluteString]) | |
} else if mediaItem.type == "carousel", | |
let carouselMedia: [InstagramMedia.CarouselMedia] = mediaItem.carouselMedia { | |
for mediaItemInCarousel: InstagramMedia.CarouselMedia in carouselMedia { | |
if mediaItemInCarousel.type == "image", | |
let images: InstagramMedia.Images = mediaItemInCarousel.images { | |
let thumbnailURL: URL = images.thumbnail.url | |
let standardResolutionURL: URL = images.standardResolution.url | |
userMediaList.append(["type" : mediaItemInCarousel.type, | |
"thumbnailURL": thumbnailURL.absoluteString, | |
"standardResolutionURL": standardResolutionURL.absoluteString]) | |
} else if mediaItemInCarousel.type == "video", | |
let videos: InstagramMedia.Videos = mediaItemInCarousel.videos { | |
let lowResolutionURL: URL = videos.lowResolution.url | |
let standardResolutionURL: URL = videos.standardResolution.url | |
userMediaList.append(["type" : mediaItemInCarousel.type, | |
"lowResolutionURL": lowResolutionURL.absoluteString, | |
"standardResolutionURL": standardResolutionURL.absoluteString]) | |
} | |
} | |
} | |
if let mediaItemId: Int = Int((mediaItem.id.components(separatedBy: "_"))[0]) { | |
if instagramUserMediaMaxIdForRequest == nil { | |
instagramUserMediaMaxIdForRequest = mediaItemId | |
} else { | |
if mediaItemId < instagramUserMediaMaxIdForRequest! { | |
instagramUserMediaMaxIdForRequest = mediaItemId | |
} | |
} | |
} | |
} | |
// print("userMediaList \(userMediaList)") | |
// print("userMediaList.count \(userMediaList.count)") | |
if instagramUserRequestedCountOfMedia == instagramUserTotalCountOfMedia { | |
// Add Istagram data to the Firebase | |
ProfileManager.shared.profile.instagram = userMediaList | |
FirebaseManager.shared.update(profile: ProfileManager.shared.profile) | |
NotificationCenter.default.post(name: NSNotification.Name(rawValue: kInstagramAllMediaGot), | |
object: nil, | |
userInfo: nil) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment