From ad3e1d833e8ee1f894e63cf3fbdaf7f39e0b999f Mon Sep 17 00:00:00 2001 From: Rudrank Date: Sun, 3 Oct 2021 16:09:32 +0530 Subject: [PATCH] Add support for iOS 13, tvOS 13, watchOS 6, and macOS 11 --- .../xcshareddata/xcschemes/QuoteKit.xcscheme | 10 +++ Package.swift | 2 +- Sources/QuoteKit/QuoteKit.swift | 63 +++++++++++++++++-- 3 files changed, 68 insertions(+), 7 deletions(-) diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/QuoteKit.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/QuoteKit.xcscheme index ace7a34..ec7ef37 100644 --- a/.swiftpm/xcode/xcshareddata/xcschemes/QuoteKit.xcscheme +++ b/.swiftpm/xcode/xcshareddata/xcschemes/QuoteKit.xcscheme @@ -28,6 +28,16 @@ selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" shouldUseLaunchSchemeArgsEnv = "YES"> + + + + (with endpoint: QuotableEndpoint) async throws -> Model { let url = endpoint.url let (data, _) = try await URLSession.shared.data(from: url) return try JSONDecoder().decode(Model.self, from: data) } + + static func execute(with endpoint: QuotableEndpoint, completion: @escaping (Result) -> ()) { + let url = endpoint.url + + URLSession.shared.dataTask(with: url) { data, _, error in + if let error = error { + completion(.failure(error)) + return + } + + guard let data = data else { + completion(.failure(QuoteFetchError.missingData)) + return + } + + do { + let model = try JSONDecoder().decode(Model.self, from: data) + completion(.success(model)) + } catch { + completion(.failure(error)) + } + }.resume() + } } // MARK: - QUOTES APIS public extension QuoteKit { + @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) static func quote(id: String) async throws -> Quote? { try await execute(with: QuotableEndpoint(.quote(id))) } + static func quote(id: String, completion: @escaping (Result) -> ()) { + execute(with: QuotableEndpoint(.quote(id)), completion: completion) + } + + @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) static func quotes(minLength: Int? = nil, maxLength: Int? = nil, tags: [String]? = nil, @@ -33,7 +68,7 @@ public extension QuoteKit { page: Int = 1) async throws -> Quotes? { var queryItems: [URLQueryItem] = [] - + queryItems.append(.limit(limit)) queryItems.append(.page(page)) @@ -64,10 +99,16 @@ public extension QuoteKit { return try await execute(with: QuotableEndpoint(.quotes, queryItems: queryItems)) } + @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) static func quotes() async throws -> Quotes? { try await execute(with: QuotableEndpoint(.quotes)) } + static func quotes(completion: @escaping (Result) -> ()) { + execute(with: QuotableEndpoint(.quotes), completion: completion) + } + + @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) static func randomQuote(minLength: Int? = nil, maxLength: Int? = nil, tags: [String]? = nil, @@ -75,7 +116,7 @@ public extension QuoteKit { authors: [String]? = nil) async throws -> Quote? { var queryItems: [URLQueryItem] = [] - + if let minLength = minLength { queryItems.append(.minLength(minLength)) } @@ -102,10 +143,16 @@ public extension QuoteKit { QuotableEndpoint(.authorProfile(size, slug), host: .images).url } + @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) static func author(id: String) async throws -> Author? { try await execute(with: QuotableEndpoint(.author(id))) } + static func author(id: String, completion: @escaping (Result) -> ()) { + execute(with: QuotableEndpoint(.author(id)), completion: completion) + } + + @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) static func authors(slugs: [String]? = nil, sortBy: AuthorsAndTagsSortType? = nil, order: QuotableListOrder? = nil, @@ -113,7 +160,7 @@ public extension QuoteKit { page: Int = 1) async throws -> Authors? { var queryItems: [URLQueryItem] = [] - + queryItems.append(.limit(limit)) queryItems.append(.page(page)) @@ -135,11 +182,12 @@ public extension QuoteKit { // MARK: - TAGS APIS public extension QuoteKit { + @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) static func tags(sortBy: AuthorsAndTagsSortType? = nil, order: QuotableListOrder? = nil) async throws -> Tags? { var queryItems: [URLQueryItem] = [] - + if let sortBy = sortBy { queryItems.append(.sortBy(sortBy)) } @@ -154,21 +202,24 @@ public extension QuoteKit { // MARK: - SEARCH APIS public extension QuoteKit { + @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) static func searchQuotes(for query: String, limit: Int = 20, page: Int = 1) async throws -> Quotes? { try await search(path: .searchQuotes, query: query, limit: limit, page: page) } - + + @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) static func searchAuthors(for query: String, limit: Int = 20, page: Int = 1) async throws -> Authors? { try await search(path: .searchAuthors, query: query, limit: limit, page: page) } + @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) private static func search(path: QuotableEndpointPath, query: String, limit: Int = 20, page: Int = 1) async throws -> Model { var queryItems: [URLQueryItem] = [] queryItems.append(.search(query)) queryItems.append(.limit(limit)) queryItems.append(.page(page)) - + return try await execute(with: QuotableEndpoint(path, queryItems: queryItems)) } }