-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
) * Re-implement RequestView, add Requested property wrapper * Rework RequestImage implementation * Allow nil to be passed to RequestImage init * Don't reload on every appear, only the first * Don't rely on result to perform onAppear
- Loading branch information
1 parent
83bad6e
commit 6266d88
Showing
8 changed files
with
265 additions
and
106 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
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
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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// Requested.swift | ||
// | ||
// | ||
// Created by Carson Katri on 1/17/21. | ||
// | ||
|
||
import SwiftUI | ||
import Combine | ||
|
||
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *) | ||
@propertyWrapper | ||
public struct Requested<Value: Decodable>: DynamicProperty { | ||
@StateObject private var requestStore: RequestStore | ||
|
||
final class RequestStore: ObservableObject { | ||
@Published var status: RequestStatus<Value> = .loading | ||
private var cancellable: AnyCancellable? | ||
var request: AnyRequest<Value> { | ||
didSet { | ||
call() | ||
} | ||
} | ||
|
||
init(request: AnyRequest<Value>) { | ||
self.request = request | ||
call() | ||
} | ||
|
||
func call() { | ||
print("Calling") | ||
self.status = .loading | ||
cancellable = request | ||
.objectPublisher | ||
.receive(on: DispatchQueue.main) | ||
.sink { [weak self] completion in | ||
switch completion { | ||
case let .failure(error): | ||
self?.status = .failure(error) | ||
case .finished: break | ||
} | ||
} receiveValue: { [weak self] result in | ||
self?.status = .success(result) | ||
} | ||
} | ||
} | ||
|
||
public init(wrappedValue: AnyRequest<Value>) { | ||
self._requestStore = .init(wrappedValue: .init(request: wrappedValue)) | ||
} | ||
|
||
public var wrappedValue: AnyRequest<Value> { | ||
get { | ||
requestStore.request | ||
} | ||
nonmutating set { | ||
requestStore.request = newValue | ||
} | ||
} | ||
|
||
public var projectedValue: RequestStatus<Value> { | ||
requestStore.status | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// RequestStatus.swift | ||
// | ||
// | ||
// Created by Carson Katri on 1/17/21. | ||
// | ||
|
||
public enum RequestStatus<Value: Decodable> { | ||
case loading | ||
case success(Value) | ||
case failure(Error) | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.