Skip to content

Commit

Permalink
Allow for disabling paging results and using a custom API base URL
Browse files Browse the repository at this point in the history
  • Loading branch information
amyleecodes committed Mar 12, 2017
1 parent eb172d4 commit aa7fbdc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
35 changes: 27 additions & 8 deletions Library/SwiftyGiphyAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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)!)

Expand Down
11 changes: 11 additions & 0 deletions Library/SwiftyGiphyViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion SwiftyGiphy.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit aa7fbdc

Please sign in to comment.