Skip to content

Commit

Permalink
Added URL session. Added sorts. Added transforms.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvorobei committed Jan 5, 2024
1 parent c7dc54f commit 324f9fe
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,17 @@ public extension CGAffineTransform {
init(scale: CGFloat) {
self.init(scaleX: scale, y: scale)
}

init(rotationDegress: CGFloat) {
self.init(rotationAngle: CGFloat(rotationDegress * .pi / 180))
}

func scaledBy(xy: CGFloat) -> CGAffineTransform {
return self.scaledBy(x: xy, y: xy)
}

func rotated(degress: CGFloat) -> CGAffineTransform {
return self.rotated(by: CGFloat(degress * .pi / 180))
}
}
#endif
95 changes: 0 additions & 95 deletions Sources/SwiftBoost/Foundation/Extensions/File.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Foundation

public extension MutableCollection where Self: RandomAccessCollection {

func sorted<Value>(
by keyPath: KeyPath<Element, Value>,
using valuesAreInIncreasingOrder: (Value, Value) throws -> Bool
) rethrows -> [Element] {
try sorted {
try valuesAreInIncreasingOrder($0[keyPath: keyPath], $1[keyPath: keyPath])
}
}

func sorted<Value: Comparable>(
by keyPath: KeyPath<Element, Value>,
order: MutableCollectionOrder<Value>
) -> [Element] {
sorted(by: keyPath, using: order.operator)
}
}

public enum MutableCollectionOrder<Value: Comparable> {

// MARK: - Cases

/// Represents ascending order. In this case, the associated operator function is `<`.
case ascending
/// Represents descending order. In this case, the associated operator function is `>`.
case descending

// MARK: - Properties

public var `operator`: (Value, Value) -> Bool {
switch self {
case .ascending:
return (<)
case .descending:
return (>)
}
}
}
8 changes: 8 additions & 0 deletions Sources/SwiftBoost/Foundation/Extensions/URLExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation

public extension URL {

static var empty: URL {
return URL(string: "https://apple.com")!
}
}
62 changes: 62 additions & 0 deletions Sources/SwiftBoost/Foundation/Extensions/URLSessionExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Foundation

public extension URLSession {

enum AppError: Error {

case invalidURL(String)
case networkError(Error)
case noResponse
case decodingError(Error)

public func errorMessage() -> String {
switch self {
case .invalidURL(let str):
return "badURL: \(str)"
case .networkError(let error):
return "networkError: \(error)"
case .noResponse:
return "no network response"
case .decodingError(let error):
return "decoding error: \(error)"
}
}
}

enum HTTPMethod {

case get

var id: String {
switch self {
case .get: return "get"
}
}
}

static func request(
url: String,
method: HTTPMethod,
completion: @escaping (AppError?, Data?, HTTPURLResponse?) ->Void)
{
guard let url = URL(string: url) else {
completion(AppError.invalidURL(url), nil, nil)
return
}

var request = URLRequest(url: url)
request.httpMethod = method.id
URLSession.shared.dataTask(with: request) { (data, response, error) in
guard let response = response as? HTTPURLResponse else {
completion(AppError.noResponse, nil, nil)
return
}

if let error = error {
completion(AppError.networkError(error), nil, response)
} else if let data = data {
completion(nil, data, response)
}
}.resume()
}
}

0 comments on commit 324f9fe

Please sign in to comment.