From 07ea0ff5ff9a713189a01f4b6ee50cc5977b68d2 Mon Sep 17 00:00:00 2001 From: nkhanh44 Date: Thu, 28 Sep 2023 22:03:40 +0700 Subject: [PATCH 1/4] [#515] Remove all Rx things --- Tuist/Interfaces/UIKit/Project/Podfile | 10 ++----- .../NetworkAPI/Core/NetworkAPIProtocol.swift | 30 +++++++------------ .../Sources/Data/NetworkAPI/NetworkAPI.swift | 8 +++-- 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/Tuist/Interfaces/UIKit/Project/Podfile b/Tuist/Interfaces/UIKit/Project/Podfile index dee619b6..cf4fbd43 100644 --- a/Tuist/Interfaces/UIKit/Project/Podfile +++ b/Tuist/Interfaces/UIKit/Project/Podfile @@ -5,8 +5,6 @@ inhibit_all_warnings! def testing_pods pod 'Quick' pod 'Nimble' - pod 'RxNimble', subspecs: ['RxBlocking', 'RxTest'] - pod 'RxSwift' pod 'Sourcery' pod 'SwiftFormat/CLI' end @@ -16,11 +14,9 @@ target '{PROJECT_NAME}' do pod 'Kingfisher' pod 'SnapKit' - # Rx - pod 'RxAlamofire' - pod 'RxCocoa' - pod 'RxDataSources' - pod 'RxSwift' + # Backend + pod 'Alamofire' + pod 'JSONAPIMapper', :git => 'https://github.com/nimblehq/JSONMapper', :tag => '1.1.1' # Storage pod 'KeychainAccess' diff --git a/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift b/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift index c8be3e5b..fb6a161d 100644 --- a/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift +++ b/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift @@ -3,12 +3,14 @@ // import Alamofire -import RxAlamofire -import RxSwift +import Combine protocol NetworkAPIProtocol { - func performRequest(_ configuration: RequestConfiguration, for type: T.Type) -> Single + func performRequest( + _ configuration: RequestConfiguration, + for type: T.Type + ) -> AnyPublisher } extension NetworkAPIProtocol { @@ -17,28 +19,16 @@ extension NetworkAPIProtocol { session: Session, configuration: RequestConfiguration, decoder: JSONDecoder - ) -> Single { - return session.rx.request( - configuration.method, + ) -> AnyPublisher { + return session.request( configuration.url, + method: configuration.method, parameters: configuration.parameters, encoding: configuration.encoding, headers: configuration.headers, interceptor: configuration.interceptor ) - .responseData() - .flatMap { _, data -> Observable in - Observable.create { observer in - do { - let decodable = try decoder.decode(T.self, from: data) - observer.on(.next(decodable)) - } catch { - observer.on(.error(error)) - } - observer.on(.completed) - return Disposables.create() - } - } - .asSingle() + .publishDecodable(type: T.self, decoder: decoder) + .value() } } diff --git a/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/NetworkAPI.swift b/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/NetworkAPI.swift index a713490e..413a003c 100644 --- a/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/NetworkAPI.swift +++ b/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/NetworkAPI.swift @@ -3,8 +3,7 @@ // import Alamofire -import Foundation -import RxSwift +import Combine final class NetworkAPI: NetworkAPIProtocol { @@ -14,7 +13,10 @@ final class NetworkAPI: NetworkAPIProtocol { self.decoder = decoder } - func performRequest(_ configuration: RequestConfiguration, for type: T.Type) -> Single { + func performRequest( + _ configuration: RequestConfiguration, + for type: T.Type + ) -> AnyPublisher { request( session: Session(), configuration: configuration, From 78dafeba3c5991d20e34f6c0ed2ffb737c84c5c2 Mon Sep 17 00:00:00 2001 From: nkhanh44 Date: Mon, 2 Oct 2023 17:01:11 +0700 Subject: [PATCH 2/4] [#515] Add asyn await to network layer --- .../NetworkAPI/Core/NetworkAPIProtocol.swift | 38 ++++++++++++------- .../Sources/Data/NetworkAPI/NetworkAPI.swift | 5 +-- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift b/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift index fb6a161d..ff17c8a9 100644 --- a/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift +++ b/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift @@ -3,14 +3,13 @@ // import Alamofire -import Combine protocol NetworkAPIProtocol { func performRequest( _ configuration: RequestConfiguration, for type: T.Type - ) -> AnyPublisher + ) async throws -> T } extension NetworkAPIProtocol { @@ -19,16 +18,29 @@ extension NetworkAPIProtocol { session: Session, configuration: RequestConfiguration, decoder: JSONDecoder - ) -> AnyPublisher { - return session.request( - configuration.url, - method: configuration.method, - parameters: configuration.parameters, - encoding: configuration.encoding, - headers: configuration.headers, - interceptor: configuration.interceptor - ) - .publishDecodable(type: T.self, decoder: decoder) - .value() + ) async throws -> T { + try await withCheckedThrowingContinuation { continuation in + session.request( + configuration.url, + method: configuration.method, + parameters: configuration.parameters, + encoding: configuration.encoding, + headers: configuration.headers, + interceptor: configuration.interceptor + ) + .response { response in + switch response.result { + case let .success(data): + do { + let decodedData = try decoder.decode(T.self, from: data ?? Data()) + continuation.resume(returning: decodedData) + } catch { + continuation.resume(throwing: error) + } + case let .failure(error): + continuation.resume(throwing: error) + } + } + } } } diff --git a/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/NetworkAPI.swift b/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/NetworkAPI.swift index 413a003c..d52c2081 100644 --- a/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/NetworkAPI.swift +++ b/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/NetworkAPI.swift @@ -3,7 +3,6 @@ // import Alamofire -import Combine final class NetworkAPI: NetworkAPIProtocol { @@ -16,8 +15,8 @@ final class NetworkAPI: NetworkAPIProtocol { func performRequest( _ configuration: RequestConfiguration, for type: T.Type - ) -> AnyPublisher { - request( + ) async throws -> T { + try await request( session: Session(), configuration: configuration, decoder: decoder From 1f226eb6adf157bf7d277339fab759c07956d629 Mon Sep 17 00:00:00 2001 From: nkhanh44 Date: Wed, 4 Oct 2023 15:15:39 +0700 Subject: [PATCH 3/4] [#515] Refactor NetworkAPIProtocol --- .../NetworkAPI/Core/NetworkAPIProtocol.swift | 33 ++++++------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift b/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift index ff17c8a9..c2ee0030 100644 --- a/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift +++ b/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift @@ -19,28 +19,15 @@ extension NetworkAPIProtocol { configuration: RequestConfiguration, decoder: JSONDecoder ) async throws -> T { - try await withCheckedThrowingContinuation { continuation in - session.request( - configuration.url, - method: configuration.method, - parameters: configuration.parameters, - encoding: configuration.encoding, - headers: configuration.headers, - interceptor: configuration.interceptor - ) - .response { response in - switch response.result { - case let .success(data): - do { - let decodedData = try decoder.decode(T.self, from: data ?? Data()) - continuation.resume(returning: decodedData) - } catch { - continuation.resume(throwing: error) - } - case let .failure(error): - continuation.resume(throwing: error) - } - } - } + try await session.request( + configuration.url, + method: configuration.method, + parameters: configuration.parameters, + encoding: configuration.encoding, + headers: configuration.headers, + interceptor: configuration.interceptor + ) + .serializingDecodable(T.self) + .value } } From 034edaea3ac6fdc2e0990fa29d7a62a9622525a4 Mon Sep 17 00:00:00 2001 From: nkhanh44 Date: Wed, 4 Oct 2023 22:14:28 +0700 Subject: [PATCH 4/4] [#515] Refactor NetworkAPIProtocol and move them to shared folder --- .../NetworkAPI/Core/NetworkAPIProtocol.swift | 34 ---------------- .../Sources/Data/NetworkAPI/NetworkAPI.swift | 26 ------------- .../NetworkAPI/Core/NetworkAPIError.swift | 11 ------ .../Core/RequestConfiguration.swift | 39 ------------------- .../Data/NetworkAPI/Interceptors/.gitkeep | 0 .../Sources/Data/NetworkAPI/Models/.gitkeep | 0 .../NetworkAPI/RequestConfigurations/.gitkeep | 0 .../UIKit/Sources/Data/Repositories/.gitkeep | 0 .../NetworkAPI/Core/NetworkAPIError.swift | 0 .../NetworkAPI/Core/NetworkAPIProtocol.swift | 0 .../Core/RequestConfiguration.swift | 0 .../Data/NetworkAPI/Interceptors/.gitkeep | 0 .../Sources/Data/NetworkAPI/Models/.gitkeep | 0 .../Sources/Data/NetworkAPI/NetworkAPI.swift | 0 .../NetworkAPI/RequestConfigurations/.gitkeep | 0 .../Sources/Data/Repositories/.gitkeep | 0 16 files changed, 110 deletions(-) delete mode 100644 Tuist/Interfaces/SwiftUI/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift delete mode 100644 Tuist/Interfaces/SwiftUI/Sources/Data/NetworkAPI/NetworkAPI.swift delete mode 100644 Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/NetworkAPIError.swift delete mode 100644 Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift delete mode 100644 Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Interceptors/.gitkeep delete mode 100644 Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Models/.gitkeep delete mode 100644 Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/RequestConfigurations/.gitkeep delete mode 100644 Tuist/Interfaces/UIKit/Sources/Data/Repositories/.gitkeep rename {Tuist/Interfaces/SwiftUI => {PROJECT_NAME}}/Sources/Data/NetworkAPI/Core/NetworkAPIError.swift (100%) rename {Tuist/Interfaces/UIKit => {PROJECT_NAME}}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift (100%) rename {Tuist/Interfaces/SwiftUI => {PROJECT_NAME}}/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift (100%) rename {Tuist/Interfaces/SwiftUI => {PROJECT_NAME}}/Sources/Data/NetworkAPI/Interceptors/.gitkeep (100%) rename {Tuist/Interfaces/SwiftUI => {PROJECT_NAME}}/Sources/Data/NetworkAPI/Models/.gitkeep (100%) rename {Tuist/Interfaces/UIKit => {PROJECT_NAME}}/Sources/Data/NetworkAPI/NetworkAPI.swift (100%) rename {Tuist/Interfaces/SwiftUI => {PROJECT_NAME}}/Sources/Data/NetworkAPI/RequestConfigurations/.gitkeep (100%) rename {Tuist/Interfaces/SwiftUI => {PROJECT_NAME}}/Sources/Data/Repositories/.gitkeep (100%) diff --git a/Tuist/Interfaces/SwiftUI/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift b/Tuist/Interfaces/SwiftUI/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift deleted file mode 100644 index fb6a161d..00000000 --- a/Tuist/Interfaces/SwiftUI/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift +++ /dev/null @@ -1,34 +0,0 @@ -// -// NetworkAPIProtocol.swift -// - -import Alamofire -import Combine - -protocol NetworkAPIProtocol { - - func performRequest( - _ configuration: RequestConfiguration, - for type: T.Type - ) -> AnyPublisher -} - -extension NetworkAPIProtocol { - - func request( - session: Session, - configuration: RequestConfiguration, - decoder: JSONDecoder - ) -> AnyPublisher { - return session.request( - configuration.url, - method: configuration.method, - parameters: configuration.parameters, - encoding: configuration.encoding, - headers: configuration.headers, - interceptor: configuration.interceptor - ) - .publishDecodable(type: T.self, decoder: decoder) - .value() - } -} diff --git a/Tuist/Interfaces/SwiftUI/Sources/Data/NetworkAPI/NetworkAPI.swift b/Tuist/Interfaces/SwiftUI/Sources/Data/NetworkAPI/NetworkAPI.swift deleted file mode 100644 index 413a003c..00000000 --- a/Tuist/Interfaces/SwiftUI/Sources/Data/NetworkAPI/NetworkAPI.swift +++ /dev/null @@ -1,26 +0,0 @@ -// -// NetworkAPI.swift -// - -import Alamofire -import Combine - -final class NetworkAPI: NetworkAPIProtocol { - - private let decoder: JSONDecoder - - init(decoder: JSONDecoder = JSONDecoder()) { - self.decoder = decoder - } - - func performRequest( - _ configuration: RequestConfiguration, - for type: T.Type - ) -> AnyPublisher { - request( - session: Session(), - configuration: configuration, - decoder: decoder - ) - } -} diff --git a/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/NetworkAPIError.swift b/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/NetworkAPIError.swift deleted file mode 100644 index 78c72199..00000000 --- a/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/NetworkAPIError.swift +++ /dev/null @@ -1,11 +0,0 @@ -// -// NetworkAPIError.swift -// - -import Foundation - -enum NetworkAPIError: Error { - - case generic - case dataNotFound -} diff --git a/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift b/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift deleted file mode 100644 index 32ddb9d3..00000000 --- a/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift +++ /dev/null @@ -1,39 +0,0 @@ -// -// RequestConfiguration.swift -// - -import Alamofire -import Foundation - -protocol RequestConfiguration { - - var baseURL: String { get } - - var endpoint: String { get } - - var method: HTTPMethod { get } - - var url: URLConvertible { get } - - var parameters: Parameters? { get } - - var encoding: ParameterEncoding { get } - - var headers: HTTPHeaders? { get } - - var interceptor: RequestInterceptor? { get } -} - -extension RequestConfiguration { - - var url: URLConvertible { - let url = URL(string: baseURL)?.appendingPathComponent(endpoint) - return url?.absoluteString ?? "\(baseURL)\(endpoint)" - } - - var parameters: Parameters? { nil } - - var headers: HTTPHeaders? { nil } - - var interceptor: RequestInterceptor? { nil } -} diff --git a/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Interceptors/.gitkeep b/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Interceptors/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Models/.gitkeep b/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Models/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/RequestConfigurations/.gitkeep b/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/RequestConfigurations/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/Tuist/Interfaces/UIKit/Sources/Data/Repositories/.gitkeep b/Tuist/Interfaces/UIKit/Sources/Data/Repositories/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/Tuist/Interfaces/SwiftUI/Sources/Data/NetworkAPI/Core/NetworkAPIError.swift b/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIError.swift similarity index 100% rename from Tuist/Interfaces/SwiftUI/Sources/Data/NetworkAPI/Core/NetworkAPIError.swift rename to {PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIError.swift diff --git a/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift b/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift similarity index 100% rename from Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift rename to {PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift diff --git a/Tuist/Interfaces/SwiftUI/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift b/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift similarity index 100% rename from Tuist/Interfaces/SwiftUI/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift rename to {PROJECT_NAME}/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift diff --git a/Tuist/Interfaces/SwiftUI/Sources/Data/NetworkAPI/Interceptors/.gitkeep b/{PROJECT_NAME}/Sources/Data/NetworkAPI/Interceptors/.gitkeep similarity index 100% rename from Tuist/Interfaces/SwiftUI/Sources/Data/NetworkAPI/Interceptors/.gitkeep rename to {PROJECT_NAME}/Sources/Data/NetworkAPI/Interceptors/.gitkeep diff --git a/Tuist/Interfaces/SwiftUI/Sources/Data/NetworkAPI/Models/.gitkeep b/{PROJECT_NAME}/Sources/Data/NetworkAPI/Models/.gitkeep similarity index 100% rename from Tuist/Interfaces/SwiftUI/Sources/Data/NetworkAPI/Models/.gitkeep rename to {PROJECT_NAME}/Sources/Data/NetworkAPI/Models/.gitkeep diff --git a/Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/NetworkAPI.swift b/{PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift similarity index 100% rename from Tuist/Interfaces/UIKit/Sources/Data/NetworkAPI/NetworkAPI.swift rename to {PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift diff --git a/Tuist/Interfaces/SwiftUI/Sources/Data/NetworkAPI/RequestConfigurations/.gitkeep b/{PROJECT_NAME}/Sources/Data/NetworkAPI/RequestConfigurations/.gitkeep similarity index 100% rename from Tuist/Interfaces/SwiftUI/Sources/Data/NetworkAPI/RequestConfigurations/.gitkeep rename to {PROJECT_NAME}/Sources/Data/NetworkAPI/RequestConfigurations/.gitkeep diff --git a/Tuist/Interfaces/SwiftUI/Sources/Data/Repositories/.gitkeep b/{PROJECT_NAME}/Sources/Data/Repositories/.gitkeep similarity index 100% rename from Tuist/Interfaces/SwiftUI/Sources/Data/Repositories/.gitkeep rename to {PROJECT_NAME}/Sources/Data/Repositories/.gitkeep