-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58eaa55
commit a665ed7
Showing
1 changed file
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,72 @@ | ||
# YTools-2 | ||
# A few tools for iOS - MacOS apps and Simple Network Manager | ||
|
||
A description of this package. | ||
### UIKit Tools | ||
- Some Device info | ||
- Convert `Int` - `Double` - `Float` - `Data` to `String` eg: `SomeData.stringValue` | ||
- `UIImage`: `averageColor: UIColor?` & `resizeImage(targetSize: CGSize) -> UIImage` | ||
- `UIImageView`: `setImageColor(color: UIColor)` | ||
- `UILabel`: `calculateMaxLines() -> Int` | ||
- `UIView`: Auto loadable from xib | ||
|
||
### Combine Netorking | ||
Flexible, cancellable, error safe | ||
|
||
```swift | ||
|
||
import YNetworkKit | ||
|
||
ApiConstant.apiDefault.set(defaultDomain: "https://api.mocki.io") | ||
|
||
class Interactor: Networkable { | ||
var someDataRequests: BaseRequestModel? | ||
|
||
func getSomeData(result: @escaping (Result<TestResponse, Error>) -> Void) { | ||
someDataRequests = BaseRequestModel(path: "/v1/d0e7928e", method: .GET([])) | ||
guard let someDataRequests = someDataRequests else { return } | ||
send(requestData: someDataRequests, modelType: TestResponse.self, result: result) | ||
} | ||
|
||
func cancel() { | ||
someDataRequests?.cancel() | ||
} | ||
} | ||
``` | ||
### Networkable | ||
```swift | ||
public protocol Networkable { | ||
func send<T>(requestData: BaseRequestProtocol, | ||
modelType: T.Type, | ||
decoder: JSONDecoder, | ||
urlSession: NetworkProtocol, | ||
result: @escaping (Result<T, Error>) -> Void) where T : Decodable | ||
} | ||
``` | ||
|
||
#### For Tests | ||
|
||
When you used MockURLSession, json file from url last path and uses | ||
|
||
```swift | ||
|
||
public class MockURLSession: NetworkProtocol { | ||
let bundle: Bundle | ||
|
||
/// MockURLSession: Request handle json file content | ||
/// - Parameter bundle: Your json file bundle | ||
public init(bundle: Bundle) { | ||
self.bundle = bundle | ||
} | ||
|
||
public func dataTaskPublisher(for request: URLRequest) -> URLSession.DataTaskPublisher { | ||
let stubReply = request.url?.lastPathComponent ?? "stub_error" | ||
return URLSession.shared.dataTaskPublisher(for: bundle.url(forResource: stubReply, withExtension: "json")!) | ||
} | ||
} | ||
|
||
|
||
// Send Request with mock URLSession | ||
send(requestData: someDataRequests, | ||
modelType: TestResponse.self, | ||
urlSession: MockURLSession.init(bundle: Bundle(for: type(of: self))), | ||
result: result) | ||
``` |