Skip to content

Commit

Permalink
Merge branch 'release/1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
MaikCL committed Aug 15, 2021
2 parents 7a310fb + 0d334ef commit 03f002d
Show file tree
Hide file tree
Showing 15 changed files with 374 additions and 98 deletions.
25 changes: 25 additions & 0 deletions Sources/Common/Extensions/Combine/Result+IsStatus.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Result+IsStatus.swift
//
//
// Created by Miguel Angel on 08-08-21.
//

import Combine

extension Result {

public var isSuccess: Bool {
switch self {
case .success: return true
default: return false
}
}

public var isFailure: Bool {
switch self {
case .failure: return true
default: return false
}
}
}
16 changes: 16 additions & 0 deletions Sources/Common/Extensions/Foundation/Range+NSRange.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Range+NSRange.swift
//
//
// Created by Miguel Angel on 09-08-21.
//

import Foundation

extension Range where Bound == String.Index {

public var nsRange:NSRange {
return NSRange(location: self.lowerBound.encodedOffset, length: self.upperBound.encodedOffset - self.lowerBound.encodedOffset)
}

}
21 changes: 21 additions & 0 deletions Sources/Common/Extensions/UIKit/SearchBar+Enabled.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// SearchBar+Enabled.swift
//
//
// Created by Miguel Angel on 13-08-21.
//

import UIKit

extension UISearchBar {
public func enable() {
isUserInteractionEnabled = true
alpha = 1.0
}

public func disable() {
isUserInteractionEnabled = false
alpha = 0.5
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// UITapGesture+TapAttributedText.swift
//
//
// Created by Miguel Angel on 09-08-21.
//

import UIKit

extension UITapGestureRecognizer {

public func didTapAttributedTextInLabel(label: UILabel, inRange targetRange: NSRange) -> Bool {
let layoutManager = NSLayoutManager()
let textContainer = NSTextContainer(size: CGSize.zero)
let textStorage = NSTextStorage(attributedString: label.attributedText!)

layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)

textContainer.lineFragmentPadding = 0.0
textContainer.lineBreakMode = label.lineBreakMode
textContainer.maximumNumberOfLines = label.numberOfLines
let labelSize = label.bounds.size
textContainer.size = labelSize

let locationOfTouchInLabel = self.location(in: label)
let textBoundingBox = layoutManager.usedRect(for: textContainer)

let textContainerOffset = CGPoint(
x: (labelSize.width - textBoundingBox.size.width) * 0.3 - textBoundingBox.origin.x,
y: (labelSize.height - textBoundingBox.size.height) * 0.3 - textBoundingBox.origin.y
)

let locationOfTouchInTextContainer = CGPoint(x: locationOfTouchInLabel.x - textContainerOffset.x, y: locationOfTouchInLabel.y - textContainerOffset.y)
let indexOfCharacter = layoutManager.characterIndex(for: locationOfTouchInTextContainer, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
return NSLocationInRange(indexOfCharacter, targetRange)
}

}
43 changes: 43 additions & 0 deletions Sources/Common/Extensions/UIKit/UITextField+SetupKeyboard.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// UITextField+SetupKeyboard.swift
//
//
// Created by Miguel Angel on 09-08-21.
//

import UIKit

public enum KeyboardType {
case email, password, numbers, phoneNumber, normal
}

extension UITextField {

public func setupKeyboard(_ type: KeyboardType, returnKeyType: UIReturnKeyType) {
self.autocapitalizationType = .none
self.autocorrectionType = .no
self.spellCheckingType = .no
self.keyboardAppearance = .default
self.returnKeyType = returnKeyType
self.clearButtonMode = .always

switch type {
case .email:
self.keyboardType = .emailAddress

case .password:
self.keyboardType = .default
self.isSecureTextEntry = true

case .numbers:
self.keyboardType = .numbersAndPunctuation

case .phoneNumber:
self.keyboardType = .phonePad

case .normal:
self.keyboardType = .default
}
}

}
17 changes: 17 additions & 0 deletions Sources/Common/Extensions/UIKit/UIView+SetCornerRadius.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// UIView+SetCornerRadius.swift
//
//
// Created by Miguel Angel on 03-08-21.
//

import UIKit

extension UIView {

public func set(cornerRadius: CGFloat) {
self.clipsToBounds = true
self.layer.cornerRadius = cornerRadius
}

}
22 changes: 22 additions & 0 deletions Sources/Common/Extensions/UIKit/UIView+SetSubviewAutolayout.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// UIView+SetSubviewAutolayout.swift
//
//
// Created by Miguel Angel on 03-08-21.
//

import UIKit

extension UIView {

public func setSubviewForAutoLayout(_ subview: UIView) {
subview.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(subview)
}

public func setSubviewsForAutoLayout(_ subviews: [UIView]) {
subviews.forEach(setSubviewForAutoLayout(_:))
}

}

23 changes: 23 additions & 0 deletions Sources/Common/PropertyWrappers/Localized.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Localized.swift
//
//
// Created by Miguel Angel on 03-08-21.
//

import Foundation

@propertyWrapper
public struct Localized {
private var key: String
private var stringsFileName: String

public init(_ key: String, stringsFileName: String = "Localizable") {
self.key = key
self.stringsFileName = stringsFileName
}

public var wrappedValue: String {
NSLocalizedString(key, tableName: stringsFileName, comment: "")
}
}
21 changes: 21 additions & 0 deletions Sources/Common/Protocols/Coordinator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Coordinator.swift
//
//
// Created by Miguel Angel on 03-08-21.
//

import UIKit

public protocol Coordinator {
func start()
func coordinate(to coordinator: Coordinator)
}

public extension Coordinator {

func coordinate(to coordinator: Coordinator) {
coordinator.start()
}

}
26 changes: 26 additions & 0 deletions Sources/Common/Protocols/Exception.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Foundation
public protocol Exception: LocalizedError {
var code: String { get }
var category: ExceptionCategory { get }
var errorTitle: String? { get }
var errorDescription: String? { get }
}

Expand All @@ -15,4 +16,29 @@ public enum ExceptionCategory {
case network
case storage
case mappers
case unknown
}

public enum GenericException: Exception {
case unknown(_ underlying: Error)

public var category: ExceptionCategory {
.unknown
}

public var code: String {
return "mdk.cmn.00"
}

public var errorTitle: String? {
return "An Exception ocurred"
}

public var errorDescription: String? {
switch self {
case .unknown(let error):
return "Error: \(error)"
}
}

}
4 changes: 4 additions & 0 deletions Sources/Common/Protocols/Mappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ extension MapperException: Exception {
}
}

public var errorTitle: String? {
return "An exception occurred"
}

public var errorDescription: String? {
switch self {
case .cantMapToModel:
Expand Down
12 changes: 6 additions & 6 deletions Sources/Providers/Network/Agents/NSUrlSessionAgent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ final class NSUrlSessionAgent: NetworkAgent {

public init () { }

public func run<Endpoint>(_ endpoint: Endpoint) -> AnyPublisher<Endpoint.APIResponse, NetworkException> where Endpoint: EndpointProvider {
public func run<Endpoint>(_ endpoint: Endpoint) -> AnyPublisher<Endpoint.APIResponse, Error> where Endpoint: EndpointProvider {
guard let url = URL(string: endpoint.path) else {
return AnyPublisher(Fail<Endpoint.APIResponse, NetworkException>(error: .invalidURL))
return AnyPublisher(Fail<Endpoint.APIResponse, Error>(error: NetworkException.invalidURL))
}

guard Reachability.isNetworkReachable() else {
return AnyPublisher(Fail<Endpoint.APIResponse, NetworkException>(error: .unreachable))
return AnyPublisher(Fail<Endpoint.APIResponse, Error>(error: NetworkException.unreachable))
}

var request = URLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
var request = URLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 6.0)
request.httpMethod = httpMethod(from: endpoint.method)
request.allHTTPHeaderFields = endpoint.headers

if endpoint.parameters != nil {
guard let postParams = try? JSONEncoder().encode(endpoint.parameters) else {
return AnyPublisher(Fail<Endpoint.APIResponse, NetworkException>(error: .invalidPostParams))
return AnyPublisher(Fail<Endpoint.APIResponse, Error>(error: NetworkException.invalidPostParams))
}
request.httpBody = postParams
}

return URLSession.shared
.dataTaskPublisher(for: request)
.retry(3)
.retry(1)
.tryMap { data, response in
let code = (response as? HTTPURLResponse)?.statusCode ?? -1
let statusCode = HTTPStatusCode(rawCode: code)
Expand Down
6 changes: 5 additions & 1 deletion Sources/Providers/Network/NetworkAgent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import AltairMDKCommon
import Combine

public protocol NetworkAgent: AnyObject {
func run<Endpoint: EndpointProvider>(_ endpoint: Endpoint) -> AnyPublisher<Endpoint.APIResponse, NetworkException>
func run<Endpoint: EndpointProvider>(_ endpoint: Endpoint) -> AnyPublisher<Endpoint.APIResponse, Error>
}

public enum NetworkException {
Expand Down Expand Up @@ -37,6 +37,10 @@ extension NetworkException: Exception {
}
}

public var errorTitle: String? {
return "An exception occurred"
}

public var errorDescription: String? {
switch self {
case .unknown(let error):
Expand Down
Loading

0 comments on commit 03f002d

Please sign in to comment.