diff --git a/Package.swift b/Package.swift index 1041d827..0dc55fe0 100644 --- a/Package.swift +++ b/Package.swift @@ -84,7 +84,7 @@ let package = Package( .target(name: "TIAuth", dependencies: ["TIFoundationUtils"], path: "TIAuth/Sources"), //MARK: - Skolkovo - .target(name: "TIEcommerce", dependencies: ["TIFoundationUtils"], path: "TIEcommerce/Sources"), + .target(name: "TIEcommerce", dependencies: ["TIFoundationUtils", "TISwiftUtils", "TINetworking"], path: "TIEcommerce/Sources"), // MARK: - Tests diff --git a/TIEcommerce/Sources/Classes/CartRequestExecutor.swift b/TIEcommerce/Sources/Classes/CartRequestExecutor.swift index a587c7f9..8b700d84 100644 --- a/TIEcommerce/Sources/Classes/CartRequestExecutor.swift +++ b/TIEcommerce/Sources/Classes/CartRequestExecutor.swift @@ -1,8 +1,54 @@ -// -// File.swift -// -// -// Created by  Григорий Бойко on 01.08.2022. -// +import TIFoundationUtils +import TINetworking -import Foundation +open class CartRequestExecutor: Cancellable { + public typealias ExecutionCompletion = (EndpointRecoverableRequestResult) -> Void + public typealias ExecutionClosure = (ExecutionCompletion) -> Cancellable + + public typealias SuccessCompletion = (S) -> Void + + private let executionClosure: ExecutionClosure + public var successCompletion: SuccessCompletion + + private var executingRequest: Cancellable? + + public init(executionClosure: @escaping ExecutionClosure, successCompletion: @escaping SuccessCompletion) { + self.executionClosure = executionClosure + self.successCompletion = successCompletion + } + + open func execute() { + executingRequest?.cancel() + + _ = executionClosure { [weak self] in + switch $0 { + case let .success(result): + self?.handle(successResult: result) + case let .failure(errorCollection): + self?.handle(failure: errorCollection) + } + } + } + + open func handle(successResult: S) { + successCompletion(successResult) + } + + open func handle(failure: ErrorCollection>) { + if shouldRetry(failure: failure) && 0 < 3 { + execute() + } + } + + open func shouldRetry(failure: ErrorCollection>) -> Bool { + if case .networkError = failure.failures.first { + return true + } + + return false + } + + open func cancel() { + executingRequest?.cancel() + } +} diff --git a/TIEcommerce/Sources/Models/BaseErrorResponseBody.swift b/TIEcommerce/Sources/Models/BaseErrorResponseBody.swift index 0ecf98c0..79fd24cf 100644 --- a/TIEcommerce/Sources/Models/BaseErrorResponseBody.swift +++ b/TIEcommerce/Sources/Models/BaseErrorResponseBody.swift @@ -1,8 +1,8 @@ import Foundation -struct BaseErrorResponseBody: Decodable, Equatable, Hashable { +protocol BaseErrorResponseBody: Decodable, Hashable { ///Код ошибки - let errorCode: Int + var errorCode: Int { get } ///Текст сообщения об ошибке - let errorMessage: String + var errorMessage: String { get } } diff --git a/TIEcommerce/Sources/Models/CartProduct.swift b/TIEcommerce/Sources/Models/CartProduct.swift index da6d3113..06b7b976 100644 --- a/TIEcommerce/Sources/Models/CartProduct.swift +++ b/TIEcommerce/Sources/Models/CartProduct.swift @@ -1,14 +1,14 @@ import Foundation -protocol CartProduct: ProductPriceHolder, BonusesHolder { +protocol CartProduct { ///Идентификатор продукта var id: String { get } ///Цена в определённой валюте - var price: CartProductPrice { get } + var price: Price { get } ///Сколько единиц есть доступно var availableCount: Int? { get } ///Варианты товара (фасовка, цвет, размер, и т.п.) - var variants: [CartProduct?] { get } + var variants: [CartProduct] { get } ///Количество бонусов, которые будут начислены при покупке var bonuses: Int? { get } } diff --git a/TIEcommerce/Sources/Models/Promocode.swift b/TIEcommerce/Sources/Models/Promocode.swift index a587c7f9..9a0896d0 100644 --- a/TIEcommerce/Sources/Models/Promocode.swift +++ b/TIEcommerce/Sources/Models/Promocode.swift @@ -1,8 +1,5 @@ -// -// File.swift -// -// -// Created by  Григорий Бойко on 01.08.2022. -// - import Foundation + +protocol Promocode { + var discount: Price? { get } +} diff --git a/TIEcommerce/TIEcommerce.podspec b/TIEcommerce/TIEcommerce.podspec index fcbb0d88..becd7e54 100644 --- a/TIEcommerce/TIEcommerce.podspec +++ b/TIEcommerce/TIEcommerce.podspec @@ -1,13 +1,13 @@ Pod::Spec.new do |s| s.name = 'TIEcommerce' s.version = '1.25.0' - s.summary = 'Cart' + s.summary = 'Cart, products, promocodes, bonuses, receipt and other related actions' s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name s.license = { :type => 'MIT', :file => 'LICENSE' } s.author = { 'petropavel13' => 'ivan.smolin@touchin.ru' } s.source = { :git => 'https://github.com/TouchInstinct/LeadKit.git', :tag => s.version.to_s } - s.ios.deployment_target = '13.0' + s.ios.deployment_target = '10.0' s.swift_versions = ['5.3'] s.source_files = s.name + '/Sources/**/*'