Skip to content

Commit

Permalink
Several adjusts
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Ziliao committed Aug 3, 2020
1 parent 9433464 commit 1264f49
Show file tree
Hide file tree
Showing 36 changed files with 538 additions and 92 deletions.
22 changes: 11 additions & 11 deletions Modules/Commons/Extensions/NSLayoutConstraint+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,16 @@ public extension NSLayoutConstraint {
static func centerX(
firstView: UIView,
secondView: UIView,
constant: Float = 0)
-> NSLayoutConstraint {
return NSLayoutConstraint(
item: firstView,
attribute: NSLayoutConstraint.Attribute.centerX,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: secondView,
attribute: NSLayoutConstraint.Attribute.centerX,
multiplier: 1,
constant: CGFloat(constant)
)
constant: Float = 0
) -> NSLayoutConstraint {
return NSLayoutConstraint(
item: firstView,
attribute: NSLayoutConstraint.Attribute.centerX,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: secondView,
attribute: NSLayoutConstraint.Attribute.centerX,
multiplier: 1,
constant: CGFloat(constant)
)
}
}
4 changes: 2 additions & 2 deletions Modules/Commons/Protocols/Alertable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ extension Alertable {

func alertWithError(_ error: NetworkError) -> UIAlertController {
let title = R.string.localizable.errorAlertDefaultTitle()
let message = error.messageToPresentToUser()
let message = error.localizedDescription
let actionButton = R.string.localizable.errorAlertDefaultAction()

return alertWithActions(
Expand All @@ -120,7 +120,7 @@ extension Alertable {
]
let alert = alertWithActions(
title: R.string.localizable.somethingWrong(),
message: error.messageToPresentToUser(),
message: error.localizedDescription,
actions: actions,
handlers: [
retry,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import UIKit

public protocol ViewCodable {
public protocol ViewCoding {
func buildViewHierarchy()
func setupConstraints()
func additionalSetup()
func buildView()
}

extension ViewCodable {
extension ViewCoding {
public func buildView() {
buildViewHierarchy()
setupConstraints()
additionalSetup()
}

public func additionalSetup() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import UIKit
public typealias ResulImageHandler = (Result<UIImage, Error>) -> Void

public protocol ImageLoaderServiceProtocol {
func loadImage(with url: URL, result: @escaping ResulImageHandler)
func downloadImage(with url: URL, result: @escaping ResulImageHandler)
func cancelDownload()
func starPrefetchDataSourceOperation(with urls: [URL])
func cancelPrefethcDataSoucerOperation(with urls: [URL])
}
Expand Down
10 changes: 8 additions & 2 deletions Modules/ImageLoader/Providers/NukeImageLoaderProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import Nuke

public final class NukeImageLoaderProvider: ImageLoaderServiceProtocol {
private let preheater = ImagePreheater()
private let pipeLine = ImagePipeline.shared
private var task: ImageTask!

public init () {
configure()
}

public func loadImage(with url: URL, result: @escaping ResulImageHandler) {
public func downloadImage(with url: URL, result: @escaping ResulImageHandler) {
let request = ImageRequest(url: url)

ImagePipeline.shared.loadImage(with: request, queue: nil, progress: nil) { response in
task = pipeLine.loadImage(with: request, queue: nil, progress: nil) { response in
DispatchQueue.main.async {
switch response {
case let .success(imageResponse):
Expand All @@ -23,6 +25,10 @@ public final class NukeImageLoaderProvider: ImageLoaderServiceProtocol {
}
}

public func cancelDownload() {
task.cancel()
}

private func configure() {
let contentModes = ImageLoadingOptions.ContentModes(
success: .scaleAspectFill,
Expand Down
12 changes: 5 additions & 7 deletions Modules/Network/Enums/NetworkError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ public enum NetworkError: Error {
case badRequest
case outDated

public init(description: String) {
self = .custom(description: description)
}
}

extension NetworkError {
func messageToPresentToUser() -> String {
var localizedDescription: String {
switch self {
case let .custom(description):
return description
Expand All @@ -29,4 +23,8 @@ extension NetworkError {
return R.string.localizable.invalidInput()
}
}

public init(description: String) {
self = .custom(description: description)
}
}
2 changes: 1 addition & 1 deletion Modules/Network/Extensions/URLComponents+Endpoint.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

extension URLComponents {
init?(endpoint: EndpointProtocol) {
init?(endpoint: EndpointDescriptor) {
self.init()
scheme = endpoint.scheme
host = endpoint.host
Expand Down
2 changes: 1 addition & 1 deletion Modules/Network/Extensions/URLRequest+Endpoint.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

extension URLRequest {
init?(endpoint: EndpointProtocol) {
init?(endpoint: EndpointDescriptor) {
guard
let urlComponents = URLComponents(endpoint: endpoint),
let url = urlComponents.url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public typealias Header = [String: String]
public typealias Parameters = [String: Any]
public typealias Body = [String: Any]

public protocol EndpointProtocol {
public protocol EndpointDescriptor {
var scheme: String { get }
var host: String { get }
var path: String { get }
Expand Down
2 changes: 1 addition & 1 deletion Modules/Network/Protocols/NetworkProviderProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import Foundation
public typealias ResultHandler<T> = (Result<T, NetworkError>) -> Void

public protocol NetworkProviderProtocol {
func performRequest<T: Decodable>(endpoint: EndpointProtocol, result: @escaping ResultHandler<T>)
func performRequest<T: Decodable>(endpoint: EndpointDescriptor, result: @escaping ResultHandler<T>)
}
23 changes: 9 additions & 14 deletions Modules/Network/Protocols/TreatJSONDecode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,17 @@ public extension TreatJSONDecode {
let decodable = try decoder.decode(T.self, from: data)
result(.success(decodable))
} catch DecodingError.keyNotFound(let key, let context) {
result(.failure(
NetworkError(description:
"Failed to decode missing key '\(key.stringValue)' not found – \(context.debugDescription)")
)
)
let description = """
Failed to decode missing key '\(key.stringValue)'
not found – \(context.debugDescription)
"""
result(.failure(NetworkError(description: description)))
} catch DecodingError.typeMismatch(_, let context) {
result(.failure(
NetworkError(description: "Failed to decode to type mismatch – \(context.debugDescription)")
)
)
let description = "Failed to decode to type mismatch – \(context.debugDescription)"
result(.failure(NetworkError(description: description)))
} catch DecodingError.valueNotFound(let type, let context) {
result(.failure(
NetworkError(
description: "Failed to decode to missing \(type) value – \(context.debugDescription)")
)
)
let descritpion = "Failed to decode to missing \(type) value – \(context.debugDescription)"
result(.failure(NetworkError(description: descritpion)))
} catch DecodingError.dataCorrupted(_) {
result(.failure(.noJSONData))
} catch {
Expand Down
5 changes: 1 addition & 4 deletions Modules/Network/Providers/URLSessionProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ public final class URLSessionProvider: NetworkProviderProtocol, RequestHandleRes
session = URLSession(configuration: URLSession.shared.configuration)
}

public func performRequest<T: Decodable>(
endpoint: EndpointProtocol,
result: @escaping ResultHandler<T>
) {
public func performRequest<T: Decodable>(endpoint: EndpointDescriptor, result: @escaping ResultHandler<T>) {
guard let request = URLRequest(endpoint: endpoint) else { preconditionFailure("Fail on create request") }

let task = session.dataTask(with: request) { data, response, error in
Expand Down
Loading

0 comments on commit 1264f49

Please sign in to comment.