This file contains hidden or 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
protocol RemoteContent : ObservableObject { | |
associatedtype Value | |
var loadingState: RemoteContentLoadingState<Value> { get } | |
func load() | |
func cancel() | |
} |
This file contains hidden or 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
enum RemoteContentLoadingState<Value> { | |
case initial | |
case inProgress | |
case success(_ value: Value) | |
case failure(_ error: Error) | |
} |
This file contains hidden or 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
let view: PostView | |
if useNetworkingA { | |
let postsDataProviderA = PostsDataProviderA() | |
view = PostView(dataProvider: postsDataProviderA) | |
} | |
else { | |
let postsDataProviderB = PostsDataProviderB() | |
view = PostView(dataProvider: postsDataProviderB) | |
} |
This file contains hidden or 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
struct PostsDataProviderA: DataProvider { | |
let networkService: NetworkingA | |
let userService: UserService | |
func fetchPosts(withURL url: URL, completion: @escaping (Result<[Post], Error>) -> Void) { | |
networkService.loadJSON(withURL: url, type: [Post].self, completion: completion) | |
} | |
func fetchUser(withId id: Int, completion: @escaping (User?) -> Void) { |
This file contains hidden or 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
protocol DataProvider { | |
func fetchPosts(withURL url: URL, completion: @escaping (Result<[Post], Error>) -> Void) | |
func fetchUser(withId id: Int, completion: @escaping (User?) -> Void) | |
} | |
struct PostView: View { |
This file contains hidden or 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
let userService: UserService | |
if useNetworkingA { | |
let networkServiceA = NetworkServiceA() | |
userService = UserService(networkService: networkServiceA) | |
} | |
else { | |
let networkServiceB = NetworkServiceB() | |
userService = UserService(networkService: networkServiceB) | |
} |
This file contains hidden or 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
extension NetworkingA: UserNetworkService { | |
func loadUsers(withURL url: URL, completion: @escaping (Result<[User], Error>) -> Void) { | |
loadJSON(withURL: url, type: [User].self, completion: completion) | |
} | |
} | |
extension NetworkingB: UserNetworkService { | |
This file contains hidden or 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
protocol UserNetworkService { | |
func loadUsers(withURL url: URL, completion: @escaping (Result<[User], Error>) -> Void) | |
} | |
class UserService { | |
let networkService: UserNetworkService |
This file contains hidden or 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
import Foundation | |
class NetworkingA { | |
func loadJSON<T: Decodable>(withURL url: URL, type: T.Type = T.self, completion: @escaping (Result<T, Error>) -> Void) { | |
urlSession.dataTask(with: url) { data, response, error in | |
// ... | |
}.resume() | |
} | |
} |
This file contains hidden or 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
extension ImageDecoder { | |
/// Creates static or animated image depending on `frameCount`. | |
var uiImage: UIImage? | |
/// Creates animated image if there is more than one frame. | |
var animatedUIImage: UIImage? | |
/// Creates static image from the first frame. | |
var staticUIImage: UIImage? |