Created
March 17, 2022 04:50
-
-
Save KrauserHuang/a012212333439ba51746883e93799c18 to your computer and use it in GitHub Desktop.
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 | |
import UIKit | |
protocol BotanicalModelProtocol { | |
func botanicalResultsRetrieved(_ results: [ResultsDetail]) | |
} | |
enum BotanicalError: Error { | |
case invalidURL | |
case noDataAvailable | |
case canNotProcessData | |
} | |
enum imageLoadingError: Error { | |
case purelyNil(Error) | |
case invalidData | |
} | |
struct BotanicalModel { | |
var delegate: BotanicalModelProtocol? | |
let imageCache = NSCache<NSURL, UIImage>() | |
// get data | |
func getBotanicalResults(completionHandler: @escaping (Result<[ResultsDetail], BotanicalError>) -> Void) { | |
// let urlString = "https://data.taipei/opendata/datalist/apiAccess?scope=resourceAquire&rid=f18de02f-b6c9-47c0-8cda-50efad621c14" | |
let urlString = "https://data.taipei/opendata/datalist/apiAccess?scope=resourceAquire&rid=f18de02f-b6c9-47c0-8cda-50efad621c14&limit=20&offset=0" | |
guard let url = URL(string: urlString) else { | |
completionHandler(.failure(.invalidURL)) | |
return | |
} | |
let decoder = JSONDecoder() | |
URLSession.shared.dataTask(with: url) { data, response, error in | |
guard let jsonData = data else { | |
completionHandler(.failure(.noDataAvailable)) | |
return | |
} | |
do { | |
let botanicalResponse = try decoder.decode(BotanicalMain.self, from: jsonData) | |
let botanicalResults = botanicalResponse.result.results | |
print("success getting results!") | |
completionHandler(.success(botanicalResults)) | |
} catch { | |
completionHandler(.failure(.canNotProcessData)) | |
} | |
}.resume() | |
} | |
func loadmore(offset: Int, completionHandler: @escaping (Result<[ResultsDetail], BotanicalError>) -> Void) { | |
let urlString = "https://data.taipei/opendata/datalist/apiAccess?scope=resourceAquire&rid=f18de02f-b6c9-47c0-8cda-50efad621c14&limit=20&offset=\(offset)" | |
guard let url = URL(string: urlString) else { | |
completionHandler(.failure(.invalidURL)) | |
return | |
} | |
let decoder = JSONDecoder() | |
URLSession.shared.dataTask(with: url) { data, response, error in | |
guard let jsonData = data else { | |
completionHandler(.failure(.noDataAvailable)) | |
return | |
} | |
do { | |
let botanicalResponse = try decoder.decode(BotanicalMain.self, from: jsonData) | |
let botanicalResults = botanicalResponse.result.results | |
print("success getting another 20 results!") | |
completionHandler(.success(botanicalResults)) | |
} catch { | |
completionHandler(.failure(.canNotProcessData)) | |
} | |
}.resume() | |
} | |
// get image | |
func getBotanicalImage(pagination: Bool = false, url: URL, completionHandler: @escaping (Result<UIImage, imageLoadingError>) -> Void) { | |
if let image = imageCache.object(forKey: url as NSURL) { | |
completionHandler(.success(image)) | |
return | |
} | |
URLSession.shared.dataTask(with: url) { data, response, error in | |
if let error = error { | |
completionHandler(.failure(.purelyNil(error))) | |
} | |
if let jsonData = data, | |
let image = UIImage(data: jsonData) { | |
imageCache.setObject(image, forKey: url as NSURL) | |
completionHandler(.success(image)) | |
} else { | |
completionHandler(.failure(.invalidData)) | |
} | |
}.resume() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment