Skip to content

Commit

Permalink
#14 Update Unsplash API response to fix image not showing
Browse files Browse the repository at this point in the history
  • Loading branch information
ayltai committed Mar 30, 2021
1 parent 3aebede commit ec3c725
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
56 changes: 28 additions & 28 deletions DevExcuses/Sources/DevExcusesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class DevExcusesView: ScreenSaverView {
return
}

client.random(size: self.frame.size, query: self.configs.imageTopics)
client.random(query: self.configs.imageTopics)
.subscribe { event in
if let error = event.error {
self.update(
Expand All @@ -125,34 +125,34 @@ class DevExcusesView: ScreenSaverView {

self.setNeedsDisplay(self.frame)
} else if let photo = event.element {
photo.download()
.observeOn(MainScheduler.instance)
.subscribeOn(CurrentThreadScheduler.instance)
.subscribe { event in
if let error = event.error {
self.update(
excuse : error.localizedDescription,
background: nil,
userName : nil,
profileUrl: nil
)
} else if
let data = event.element,
let user = photo.user,
let userName = user.name,
let links = user.links,
let profileUrl = links.html {
self.update(
excuse : self.configs.quotes[self.configs.quotes.count.random()],
background: data,
userName : userName,
profileUrl: profileUrl
)
photo.download(size: self.frame.size)
.observeOn(MainScheduler.instance)
.subscribeOn(CurrentThreadScheduler.instance)
.subscribe { event in
if let error = event.error {
self.update(
excuse : error.localizedDescription,
background: nil,
userName : nil,
profileUrl: nil
)
} else if
let data = event.element,
let user = photo.user,
let userName = user.name,
let links = user.links,
let profileUrl = links.html {
self.update(
excuse : self.configs.quotes[self.configs.quotes.count.random()],
background: data,
userName : userName,
profileUrl: profileUrl
)
}

self.setNeedsDisplay(self.frame)
}

self.setNeedsDisplay(self.frame)
}
.disposed(by: self.disposeBag)
.disposed(by: self.disposeBag)
}
}
.disposed(by: self.disposeBag)
Expand Down
8 changes: 4 additions & 4 deletions DevExcuses/Sources/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ extension String {
}

extension Photo {
func download() -> Observable<Data> {
func download(size: CGSize) -> Observable<Data> {
return Observable.create { observer in
if
let urls = self.urls,
let custom = urls.custom,
let url = URL(string: custom) {
let urls = self.urls,
let raw = urls.raw,
let url = URL(string: raw + "&w=" + String(Int(size.width)) + "&h=" + String(Int(size.height))) {
URLSession.shared.dataTask(with: url) { (data, response, error) in
if let error = error {
observer.onError(error as NSError)
Expand Down
6 changes: 3 additions & 3 deletions DevExcuses/Sources/Models/PhotoUrls.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
struct PhotoUrls: Codable {
enum CodingKeys: CodingKey {
case custom
case raw
}

let custom: String?
let raw: String?

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.custom = try container.decodeIfPresent(String.self, forKey: .custom)
self.raw = try container.decodeIfPresent(String.self, forKey: .raw)
}
}
16 changes: 8 additions & 8 deletions DevExcuses/Sources/UnsplashClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ import RxSwift
final class UnsplashClient {
private static let endPoint = "https://unsplash-api-proxy.appspot.com/"

func random(size: CGSize, query: [String]?) -> Observable<Photo> {
func random(query: [String]?) -> Observable<Photo> {
return Observable.create { observer in
var queryItems: [URLQueryItem] = [
URLQueryItem(name: "w", value: String(Int(size.width))),
URLQueryItem(name: "h", value: String(Int(size.height)))
]

var queryItems: [URLQueryItem]?

if let query = query {
queryItems.append(URLQueryItem(name: "query", value: query[query.count.random()]))
queryItems = [
URLQueryItem(name: "query", value: query[query.count.random()]),
]
}

var mutable = URLComponents(url: URL(string: UnsplashClient.endPoint)!, resolvingAgainstBaseURL: true)!
mutable.path = "/photos/random"
mutable.queryItems = queryItems

URLSession.shared.dataTask(with: mutable.url!) { (data, response, error) in
if let error = error {
observer.onError(error as NSError)
Expand All @@ -27,6 +26,7 @@ final class UnsplashClient {
if response.isSuccess {
do {
let photo: Photo = try JSONDecoder().decode(Photo.self, from: data)

observer.onNext(photo)
} catch let error as NSError {
observer.onError(error)
Expand Down

0 comments on commit ec3c725

Please sign in to comment.