Clean up ahead of PR.

pull/1/head
Matthew Chen 7 years ago
parent 2c680cadad
commit 18d39f15f2

@ -1,5 +1,5 @@
// //
// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // Copyright (c) 2017 Open Whisper Systems. All rights reserved.
// //
import Foundation import Foundation
@ -122,33 +122,33 @@ extension GiphyError: LocalizedError {
public func pickStillRendition() -> GiphyRendition? { public func pickStillRendition() -> GiphyRendition? {
// Stills are just temporary placeholders, so use the smallest still possible. // Stills are just temporary placeholders, so use the smallest still possible.
return pickRendition(renditionType: .stillPreview, pickingStrategy: .smallerIsBetter, maxFileSize: kPreferedPreviewFileSize) return pickRendition(renditionType: .stillPreview, pickingStrategy:.smallerIsBetter, maxFileSize:kPreferedPreviewFileSize)
} }
public func pickPreviewRendition() -> GiphyRendition? { public func pickPreviewRendition() -> GiphyRendition? {
// Try to pick a small file... // Try to pick a small file...
if let rendition = pickRendition(renditionType: .animatedLowQuality, pickingStrategy: .largerIsBetter, maxFileSize: kPreferedPreviewFileSize) { if let rendition = pickRendition(renditionType: .animatedLowQuality, pickingStrategy:.largerIsBetter, maxFileSize:kPreferedPreviewFileSize) {
return rendition return rendition
} }
// ...but gradually relax the file restriction... // ...but gradually relax the file restriction...
if let rendition = pickRendition(renditionType: .animatedLowQuality, pickingStrategy: .smallerIsBetter, maxFileSize: kPreferedPreviewFileSize * 2) { if let rendition = pickRendition(renditionType: .animatedLowQuality, pickingStrategy:.smallerIsBetter, maxFileSize:kPreferedPreviewFileSize * 2) {
return rendition return rendition
} }
// ...and relax even more until we find an animated rendition. // ...and relax even more until we find an animated rendition.
return pickRendition(renditionType: .animatedLowQuality, pickingStrategy: .smallerIsBetter, maxFileSize: kPreferedPreviewFileSize * 3) return pickRendition(renditionType: .animatedLowQuality, pickingStrategy:.smallerIsBetter, maxFileSize:kPreferedPreviewFileSize * 3)
} }
public func pickSendingRendition() -> GiphyRendition? { public func pickSendingRendition() -> GiphyRendition? {
// Try to pick a small file... // Try to pick a small file...
if let rendition = pickRendition(renditionType: .animatedHighQuality, pickingStrategy: .largerIsBetter, maxFileSize: kPreferedSendingFileSize) { if let rendition = pickRendition(renditionType: .animatedHighQuality, pickingStrategy:.largerIsBetter, maxFileSize:kPreferedSendingFileSize) {
return rendition return rendition
} }
// ...but gradually relax the file restriction... // ...but gradually relax the file restriction...
if let rendition = pickRendition(renditionType: .animatedHighQuality, pickingStrategy: .smallerIsBetter, maxFileSize: kPreferedSendingFileSize * 2) { if let rendition = pickRendition(renditionType: .animatedHighQuality, pickingStrategy:.smallerIsBetter, maxFileSize:kPreferedSendingFileSize * 2) {
return rendition return rendition
} }
// ...and relax even more until we find an animated rendition. // ...and relax even more until we find an animated rendition.
return pickRendition(renditionType: .animatedHighQuality, pickingStrategy: .smallerIsBetter, maxFileSize: kPreferedSendingFileSize * 3) return pickRendition(renditionType: .animatedHighQuality, pickingStrategy:.smallerIsBetter, maxFileSize:kPreferedSendingFileSize * 3)
} }
enum RenditionType { enum RenditionType {
@ -299,12 +299,12 @@ extension GiphyError: LocalizedError {
} }
private func giphyAPISessionManager() -> AFHTTPSessionManager? { private func giphyAPISessionManager() -> AFHTTPSessionManager? {
guard let baseUrl = NSURL(string: kGiphyBaseURL) else { guard let baseUrl = NSURL(string:kGiphyBaseURL) else {
Logger.error("\(TAG) Invalid base URL.") Logger.error("\(TAG) Invalid base URL.")
return nil return nil
} }
let sessionManager = AFHTTPSessionManager(baseURL: baseUrl as URL, let sessionManager = AFHTTPSessionManager(baseURL:baseUrl as URL,
sessionConfiguration: GiphyAPI.giphySessionConfiguration()) sessionConfiguration:GiphyAPI.giphySessionConfiguration())
sessionManager.requestSerializer = AFJSONRequestSerializer() sessionManager.requestSerializer = AFJSONRequestSerializer()
sessionManager.responseSerializer = AFJSONResponseSerializer() sessionManager.responseSerializer = AFJSONResponseSerializer()
@ -319,7 +319,7 @@ extension GiphyError: LocalizedError {
failure(nil) failure(nil)
return return
} }
guard NSURL(string: kGiphyBaseURL) != nil else { guard NSURL(string:kGiphyBaseURL) != nil else {
Logger.error("\(TAG) Invalid base URL.") Logger.error("\(TAG) Invalid base URL.")
failure(nil) failure(nil)
return return
@ -338,11 +338,10 @@ extension GiphyError: LocalizedError {
sessionManager.get(urlString, sessionManager.get(urlString,
parameters: {}, parameters: {},
progress: nil, progress:nil,
success: { task, value in success: { _, value in
Logger.error("\(GiphyAPI.TAG) search request succeeded") Logger.error("\(GiphyAPI.TAG) search request succeeded")
Logger.error("\(GiphyAPI.TAG) search request succeeded \(task.originalRequest?.url)") guard let imageInfos = self.parseGiphyImages(responseJson:value) else {
guard let imageInfos = self.parseGiphyImages(responseJson: value) else {
failure(nil) failure(nil)
return return
} }
@ -356,16 +355,16 @@ extension GiphyError: LocalizedError {
// MARK: Parse API Responses // MARK: Parse API Responses
private func parseGiphyImages(responseJson: Any?) -> [GiphyImageInfo]? { private func parseGiphyImages(responseJson:Any?) -> [GiphyImageInfo]? {
guard let responseJson = responseJson else { guard let responseJson = responseJson else {
Logger.error("\(TAG) Missing response.") Logger.error("\(TAG) Missing response.")
return nil return nil
} }
guard let responseDict = responseJson as? [String: Any] else { guard let responseDict = responseJson as? [String:Any] else {
Logger.error("\(TAG) Invalid response.") Logger.error("\(TAG) Invalid response.")
return nil return nil
} }
guard let imageDicts = responseDict["data"] as? [[String: Any]] else { guard let imageDicts = responseDict["data"] as? [[String:Any]] else {
Logger.error("\(TAG) Invalid response data.") Logger.error("\(TAG) Invalid response data.")
return nil return nil
} }
@ -375,7 +374,7 @@ extension GiphyError: LocalizedError {
} }
// Giphy API results are often incomplete or malformed, so we need to be defensive. // Giphy API results are often incomplete or malformed, so we need to be defensive.
private func parseGiphyImage(imageDict: [String: Any]) -> GiphyImageInfo? { private func parseGiphyImage(imageDict: [String:Any]) -> GiphyImageInfo? {
guard let giphyId = imageDict["id"] as? String else { guard let giphyId = imageDict["id"] as? String else {
Logger.warn("\(TAG) Image dict missing id.") Logger.warn("\(TAG) Image dict missing id.")
return nil return nil
@ -384,18 +383,18 @@ extension GiphyError: LocalizedError {
Logger.warn("\(TAG) Image dict has invalid id.") Logger.warn("\(TAG) Image dict has invalid id.")
return nil return nil
} }
guard let renditionDicts = imageDict["images"] as? [String: Any] else { guard let renditionDicts = imageDict["images"] as? [String:Any] else {
Logger.warn("\(TAG) Image dict missing renditions.") Logger.warn("\(TAG) Image dict missing renditions.")
return nil return nil
} }
var renditions = [GiphyRendition]() var renditions = [GiphyRendition]()
for (renditionName, renditionDict) in renditionDicts { for (renditionName, renditionDict) in renditionDicts {
guard let renditionDict = renditionDict as? [String: Any] else { guard let renditionDict = renditionDict as? [String:Any] else {
Logger.warn("\(TAG) Invalid rendition dict.") Logger.warn("\(TAG) Invalid rendition dict.")
continue continue
} }
guard let rendition = parseGiphyRendition(renditionName: renditionName, guard let rendition = parseGiphyRendition(renditionName:renditionName,
renditionDict: renditionDict) else { renditionDict:renditionDict) else {
continue continue
} }
renditions.append(rendition) renditions.append(rendition)
@ -405,13 +404,13 @@ extension GiphyError: LocalizedError {
return nil return nil
} }
guard let originalRendition = findOriginalRendition(renditions: renditions) else { guard let originalRendition = findOriginalRendition(renditions:renditions) else {
Logger.warn("\(TAG) Image has no original rendition.") Logger.warn("\(TAG) Image has no original rendition.")
return nil return nil
} }
return GiphyImageInfo(giphyId: giphyId, return GiphyImageInfo(giphyId : giphyId,
renditions: renditions, renditions : renditions,
originalRendition: originalRendition) originalRendition: originalRendition)
} }
@ -426,15 +425,15 @@ extension GiphyError: LocalizedError {
// //
// We should discard renditions which are missing or have invalid properties. // We should discard renditions which are missing or have invalid properties.
private func parseGiphyRendition(renditionName: String, private func parseGiphyRendition(renditionName: String,
renditionDict: [String: Any]) -> GiphyRendition? { renditionDict: [String:Any]) -> GiphyRendition? {
guard let width = parsePositiveUInt(dict: renditionDict, key: "width", typeName: "rendition") else { guard let width = parsePositiveUInt(dict:renditionDict, key:"width", typeName:"rendition") else {
return nil return nil
} }
guard let height = parsePositiveUInt(dict: renditionDict, key: "height", typeName: "rendition") else { guard let height = parsePositiveUInt(dict:renditionDict, key:"height", typeName:"rendition") else {
return nil return nil
} }
// Be lenient when parsing file sizes - we don't require them for stills. // Be lenient when parsing file sizes - we don't require them for stills.
let fileSize = parseLenientUInt(dict: renditionDict, key: "size") let fileSize = parseLenientUInt(dict:renditionDict, key:"size")
guard let urlString = renditionDict["url"] as? String else { guard let urlString = renditionDict["url"] as? String else {
return nil return nil
} }
@ -442,7 +441,7 @@ extension GiphyError: LocalizedError {
Logger.warn("\(TAG) Rendition has invalid url.") Logger.warn("\(TAG) Rendition has invalid url.")
return nil return nil
} }
guard let url = NSURL(string: urlString) else { guard let url = NSURL(string:urlString) else {
Logger.warn("\(TAG) Rendition url could not be parsed.") Logger.warn("\(TAG) Rendition url could not be parsed.")
return nil return nil
} }
@ -465,16 +464,16 @@ extension GiphyError: LocalizedError {
} }
return GiphyRendition( return GiphyRendition(
format: format, format : format,
name: renditionName, name : renditionName,
width: width, width : width,
height: height, height : height,
fileSize: fileSize, fileSize : fileSize,
url: url url : url
) )
} }
private func parsePositiveUInt(dict: [String: Any], key: String, typeName: String) -> UInt? { private func parsePositiveUInt(dict: [String:Any], key: String, typeName: String) -> UInt? {
guard let value = dict[key] else { guard let value = dict[key] else {
return nil return nil
} }
@ -491,7 +490,7 @@ extension GiphyError: LocalizedError {
return parsedValue return parsedValue
} }
private func parseLenientUInt(dict: [String: Any], key: String) -> UInt { private func parseLenientUInt(dict: [String:Any], key: String) -> UInt {
let defaultValue = UInt(0) let defaultValue = UInt(0)
guard let value = dict[key] else { guard let value = dict[key] else {

Loading…
Cancel
Save