Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
let thumbnail: UIImageView = ...
thumbnail.rx_setImage(url: URL(string: currentVideo.thumbnail)!, placeholderImage: UIImage(named: "Placeholder Video"), animated: true)
let params: QueryParameters = ["key": "value"]
let headers: Headers = ["key", "value"]
RxNetwork.request("http://www.google.com", headers: headers).asData()
.subscribe(onNext: { data in
// Use data as Data
})
let videoObservable : Observable<Video> = RxNetwork.request("http://www.google.com", parameters: params).asUnboxable()
videoObservable.subscribe(onNext: { data in
// Use data as Video
})
let videoObservable : Observable<Video> = RxNetwork.withJSON(URLRequest(url: url!))
videoObservable.subscribe(onNext: { data in
// Use data as Video
})
import Foundation
import Unbox
import RxSwift
extension RxNetworkRequest {
func asUnboxable<T: Unboxable>() -> Observable<T>{
return asData().map { (data: Data) -> T in try Unbox(data: data) }
}
}
import RxCocoa
import RxSwift
typealias QueryParameters = [String : String]
typealias Headers = [String: String]
public enum HTTPMethod: String {
case options = "OPTIONS"
case get = "GET"
case head = "HEAD"
case post = "POST"
case put = "PUT"
case patch = "PATCH"
case delete = "DELETE"
case trace = "TRACE"
case connect = "CONNECT"
}
class RxNetwork {
static func request(_ url: String, method: HTTPMethod = .get, headers: Headers = Headers(), parameters: QueryParameters = QueryParameters()) -> RxNetworkRequest {
return RxNetworkRequest(url: url, method: method, headers: headers, parameters: parameters)
}
}
struct RxNetworkRequest {
let url: String
let method: HTTPMethod
let headers: Headers
let parameters: QueryParameters
func asData() -> Observable<Data> {
guard let strongRequest = self.asURLRequest() else { return Observable.empty() }
return URLSession.shared.rx.data(strongRequest)
}
private func asURLRequest() -> URLRequest? {
guard var urlComponents = URLComponents(string: url) else { return nil }
urlComponents.queryItems = parameters.map { (key, value) -> URLQueryItem in
return URLQueryItem(name: key, value: value)
}
guard let requestURL = urlComponents.url else { return nil }
var request = URLRequest(url: requestURL)
request.httpMethod = method.rawValue
request.addHeaders(headers)
return request
}
}
extension URLRequest {
mutating func addHeaders(_ headers: [String : String]) {
headers.forEach { header in
self.addValue(header.value, forHTTPHeaderField: header.key)
}
}
}
import UIKit
import RxSwift
extension UIImageView {
struct LoaderWrapper {
static var SetImageFetcherKey = 0
let networkObservable: Disposable
}
private var rx_wrapper : LoaderWrapper? {
get {
return objc_getAssociatedObject(self, &LoaderWrapper.SetImageFetcherKey) as? LoaderWrapper
}
set(wrapper) {
objc_setAssociatedObject(self, &LoaderWrapper.SetImageFetcherKey, wrapper, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
func rx_setImage(url: String, placeholderImage: UIImage? = nil, animated: Bool = false) {
rx_cancelSetImage()
rx_displayImage(placeholderImage, false)
let imageObservable = RxNetwork.request(url).asData()
.observeOn(MainScheduler.instance)
.subscribe { [weak self] event in
guard let strongSelf = self else { return }
if let data = event.element, !event.isStopEvent {
strongSelf.rx_displayData(data, animated: animated)
}
strongSelf.rx_cancelSetImage()
}
rx_wrapper = LoaderWrapper(networkObservable: imageObservable)
}
private func rx_displayData(_ data: Data?, animated: Bool) {
if let data = data {
rx_displayImage(UIImage(data: data), animated)
}
}
private func rx_displayImage(_ image : UIImage?, _ animated: Bool) {
UIView.animate(withDuration: animated ? 0.25 : 0) {
self.image = image
}
}
private func rx_cancelSetImage() {
if let networkObservable = rx_wrapper?.networkObservable {
networkObservable.dispose()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment