Skip to content

Commit ce19173

Browse files
committed
Update loader
1 parent aa1889e commit ce19173

File tree

6 files changed

+37
-38
lines changed

6 files changed

+37
-38
lines changed

ApexyLoaderExample/ApexyLoaderExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ApexyLoaderExample/ApexyLoaderExample/Sources/Business Logic/Loaders/OrganizationLoader.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ protocol OrganizationLoading: ContentLoading {
1212
var state: LoadingState<Organization> { get }
1313
}
1414

15+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
1516
final class OrganizationLoader: WebLoader<Organization>, OrganizationLoading {
1617
func load() {
1718
guard startLoading() else { return }
18-
request(OrganizationEndpoint()) { result in
19-
// imitation of waiting for the request for 3 seconds
20-
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
21-
self.finishLoading(result)
22-
}
19+
Task {
20+
await request(OrganizationEndpoint())
2321
}
2422
}
2523
}

ApexyLoaderExample/ApexyLoaderExample/Sources/Business Logic/Loaders/RepositoriesLoader.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ protocol RepoLoading: ContentLoading {
1212
var state: LoadingState<[Repository]> { get }
1313
}
1414

15+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
1516
final class RepositoriesLoader: WebLoader<[Repository]>, RepoLoading {
1617
func load() {
1718
guard startLoading() else { return }
18-
request(RepositoriesEndpoint()) { result in
19-
// imitation of waiting for the request for 5 seconds
20-
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
21-
self.finishLoading(result)
22-
}
19+
Task {
20+
await request(RepositoriesEndpoint())
2321
}
2422
}
2523
}

Documentation/loader.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ protocol UserProfileLoading: ContentLoading {
2222
var state: LoadingState<UserProfile> { get }
2323
}
2424

25+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
2526
final class UserProfileLoader: WebLoader<UserProfile>, UserProfileLoading {
2627
func load() {
2728
guard startLoading() else { return }
28-
request(UserProfileEndpoint())
29+
Task {
30+
await request(UserProfileEndpoint())
31+
}
2932
}
3033
}
3134
```

Documentation/loader_ru.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ protocol UserProfileLoading: ContentLoading {
2121
var state: LoadingState<UserProfile> { get }
2222
}
2323

24+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
2425
final class UserProfileLoader: WebLoader<UserProfile>, UserProfileLoading {
2526
func load() {
2627
guard startLoading() else { return }
27-
request(UserProfileEndpoint())
28+
Task {
29+
await request(UserProfileEndpoint())
30+
}
2831
}
2932
}
3033
```

Sources/ApexyLoader/WebLoader.swift

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,27 @@ import Apexy
22
import Foundation
33

44
/// Loads content by network.
5+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
56
open class WebLoader<Content>: ContentLoader<Content> {
67
private let apiClient: Client
7-
public private(set) var progress: Progress?
88

99
/// Creates an instance of `WebLoader` to load content by network using specified `Client`.
1010
/// - Parameter apiClient: An instance of the `Client` protocol. Use `AlamofireClient` or `URLSessionClient`.
1111
public init(apiClient: Client) {
1212
self.apiClient = apiClient
1313
}
1414

15-
deinit {
16-
progress?.cancel()
17-
}
18-
1915
/// Sends requests to the network.
2016
///
2117
/// - Warning: You must call `startLoading` before calling this method!
2218
/// - Parameter endpoint: An object representing request.
23-
public func request<T>(_ endpoint: T) where T: Endpoint, T.Content == Content {
24-
progress = apiClient.request(endpoint) { [weak self] result in
25-
self?.progress = nil
26-
self?.finishLoading(result)
19+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
20+
public func request<T>(_ endpoint: T) async where T: Endpoint, T.Content == Content {
21+
do {
22+
let content = try await apiClient.request(endpoint)
23+
finishLoading(.success(content))
24+
} catch {
25+
finishLoading(.failure(error))
2726
}
2827
}
2928

@@ -32,21 +31,28 @@ open class WebLoader<Content>: ContentLoader<Content> {
3231
/// - Parameters:
3332
/// - endpoint: An object representing request.
3433
/// - transform: A closure that transforms successfull result.
35-
public func request<T>(_ endpoint: T, transform: @escaping (T.Content) -> Content) where T: Endpoint {
36-
progress = apiClient.request(endpoint) { [weak self] result in
37-
self?.progress = nil
38-
self?.finishLoading(result.map(transform))
34+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
35+
public func request<T>(_ endpoint: T, transform: @escaping (T.Content) -> Content) async where T: Endpoint {
36+
do {
37+
let content = try await apiClient.request(endpoint)
38+
finishLoading(.success(transform(content)))
39+
} catch {
40+
finishLoading(.failure(error))
3941
}
4042
}
4143

4244
/// Sends requests to the network and calls completion handler.
4345
/// - Parameters:
4446
/// - endpoint: An object representing request.
4547
/// - completion: A completion handler.
46-
public func request<T>(_ endpoint: T, completion: @escaping (Result<T.Content, Error>) -> Void) where T: Endpoint {
47-
progress = apiClient.request(endpoint) { [weak self] result in
48-
self?.progress = nil
49-
completion(result)
48+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
49+
public func request<T>(_ endpoint: T, completion: @escaping (Result<T.Content, Error>) -> Void) async where T: Endpoint {
50+
do {
51+
let content = try await apiClient.request(endpoint)
52+
completion(.success(content))
53+
} catch {
54+
completion(.failure(error))
5055
}
5156
}
57+
5258
}

0 commit comments

Comments
 (0)