From aa7fbdc9d69b39b29c1fc8b480ae8fd73073a222 Mon Sep 17 00:00:00 2001 From: Brendan Lee Date: Sun, 12 Mar 2017 17:25:22 -0400 Subject: [PATCH] Allow for disabling paging results and using a custom API base URL --- Library/SwiftyGiphyAPI.swift | 35 +++++++++++++++++++------ Library/SwiftyGiphyViewController.swift | 11 ++++++++ SwiftyGiphy.podspec | 2 +- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/Library/SwiftyGiphyAPI.swift b/Library/SwiftyGiphyAPI.swift index 619dbf9..c323065 100644 --- a/Library/SwiftyGiphyAPI.swift +++ b/Library/SwiftyGiphyAPI.swift @@ -17,6 +17,8 @@ fileprivate typealias GiphyAPIResponseBlock = (_ error: NSError?, _ response: [S fileprivate let kGiphyUnknownResponseError = NSLocalizedString("The server returned an unknown response.", comment: "The error message shown when the server produces something unintelligible.") +fileprivate let kGiphyDefaultAPIBase = URL(string: "https://api.giphy.com/v1/gifs/")! + public enum SwiftyGiphyAPIContentRating: String { case y = "y" @@ -50,7 +52,16 @@ public class SwiftyGiphyAPI { } } - fileprivate static let giphyAPIBase = URL(string: "https://api.giphy.com/v1/gifs/")! + /// This can be overriden to use a custom API base url, in the scenario that you want requests to pass through your own server. + /// - Note: + /// - Changing this URL will disable the requirement for an API key to be set on SwiftyGiphyAPI. You can still set one if you want, but we allow it to be nil in case you want the API key to live on your server. + public var giphyAPIBase: URL = kGiphyDefaultAPIBase + + fileprivate var isUsingDefaultAPIBase: Bool { + get { + return giphyAPIBase == kGiphyDefaultAPIBase + } + } /// This is private, you should use the shared singleton instead of creating your own instance. fileprivate init() { @@ -66,7 +77,7 @@ public class SwiftyGiphyAPI { /// - completion: The completion block to call when done public func getTrending(limit: Int = 25, rating: SwiftyGiphyAPIContentRating = .pg13, offset: Int? = nil, completion: GiphyMultipleGIFResponseBlock?) { - guard let validAPIKey = apiKey else { + guard apiKey != nil || !isUsingDefaultAPIBase else { print("ATTENTION: You need to set your Giphy API key before using SwiftyGiphy.") completion?(networkError(description: NSLocalizedString("You need to set your Giphy API key before using SwiftyGiphy.", comment: "You need to set your Giphy API key before using SwiftyGiphy.")), nil) @@ -75,7 +86,11 @@ public class SwiftyGiphyAPI { var params = [String : Any]() - params["api_key"] = validAPIKey + if let validAPIKey = apiKey + { + params["api_key"] = validAPIKey + } + params["limit"] = limit params["rating"] = rating.rawValue @@ -84,7 +99,7 @@ public class SwiftyGiphyAPI { params["offset"] = currentOffset } - let request = SwiftyGiphyAPI.createRequest(relativePath: "trending", method: "GET", params: params) + let request = createRequest(relativePath: "trending", method: "GET", params: params) send(request: request) { [unowned self] (error, response) in @@ -120,7 +135,7 @@ public class SwiftyGiphyAPI { /// - completion: The completion block to call when done public func getSearch(searchTerm: String, limit: Int = 25, rating: SwiftyGiphyAPIContentRating = .pg13, offset: Int? = nil, completion: GiphyMultipleGIFResponseBlock?) { - guard let validAPIKey = apiKey else { + guard apiKey != nil || !isUsingDefaultAPIBase else { print("ATTENTION: You need to set your Giphy API key before using SwiftyGiphy.") completion?(networkError(description: NSLocalizedString("You need to set your Giphy API key before using SwiftyGiphy.", comment: "You need to set your Giphy API key before using SwiftyGiphy.")), nil) @@ -129,8 +144,12 @@ public class SwiftyGiphyAPI { var params = [String : Any]() + if let validAPIKey = apiKey + { + params["api_key"] = validAPIKey + } + params["q"] = searchTerm - params["api_key"] = validAPIKey params["limit"] = limit params["rating"] = rating.rawValue @@ -139,7 +158,7 @@ public class SwiftyGiphyAPI { params["offset"] = currentOffset } - let request = SwiftyGiphyAPI.createRequest(relativePath: "search", method: "GET", params: params) + let request = createRequest(relativePath: "search", method: "GET", params: params) send(request: request) { [unowned self] (error, response) in @@ -261,7 +280,7 @@ public class SwiftyGiphyAPI { - returns: The generated request, or nil if a JSON error occurred. */ - fileprivate static func createRequest(relativePath:String, method:String, params: [String : Any]?) -> URLRequest + fileprivate func createRequest(relativePath:String, method:String, params: [String : Any]?) -> URLRequest { var request = URLRequest(url: URL(string: relativePath, relativeTo: giphyAPIBase)!) diff --git a/Library/SwiftyGiphyViewController.swift b/Library/SwiftyGiphyViewController.swift index 5b194e3..361cda1 100644 --- a/Library/SwiftyGiphyViewController.swift +++ b/Library/SwiftyGiphyViewController.swift @@ -60,16 +60,23 @@ public class SwiftyGiphyViewController: UIViewController { } } + /// The maximum content rating allowed for the shown gifs public var contentRating: SwiftyGiphyAPIContentRating = .pg13 + /// The maximum allowed size for gifs shown in the feed public var maxSizeInBytes: UInt64 = 2048000 // 2MB size cap by default. We're on mobile, after all. + /// Allow paging the API results. Enabled by default, but you can disable it if you use a custom base URL that doesn't support it. + public var allowResultPaging: Bool = true + + /// The collection view layout that governs the waterfall layout of the gifs. There are a few parameters you can modify, but we recommend the defaults. public var collectionViewLayout: SwiftyGiphyGridLayout? { get { return collectionView.collectionViewLayout as? SwiftyGiphyGridLayout } } + /// The object to receive callbacks for when the user cancels or selects a gif. It is the delegate's responsibility to dismiss the SwiftyGiphyViewController. public weak var delegate: SwiftyGiphyViewControllerDelegate? public override func loadView() { @@ -416,6 +423,10 @@ extension SwiftyGiphyViewController: UIScrollViewDelegate { public func scrollViewDidScroll(_ scrollView: UIScrollView) { + guard allowResultPaging else { + return + } + if scrollView.contentOffset.y + scrollView.bounds.height + 100 >= scrollView.contentSize.height { if searchController.isActive diff --git a/SwiftyGiphy.podspec b/SwiftyGiphy.podspec index f0bad4b..d9dacf7 100644 --- a/SwiftyGiphy.podspec +++ b/SwiftyGiphy.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'SwiftyGiphy' - s.version = '1.0.4' + s.version = '1.0.5' s.summary = 'Provides a UI and API layer for Giphy discovery and integration.' # This description is used to generate tags and improve search results.