Skip to content

Commit

Permalink
Integrate api tags for HS backend queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ealymbaev committed Nov 28, 2023
1 parent 6cfebf8 commit 405b3d2
Show file tree
Hide file tree
Showing 45 changed files with 1,301 additions and 1,371 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12698,7 +12698,7 @@
repositoryURL = "https://github.com/horizontalsystems/MarketKit.Swift";
requirement = {
kind = exactVersion;
version = 2.2.8;
version = 2.3.0;
};
};
D3604E7728F03B9F0066C366 /* XCRemoteSwiftPackageReference "ModuleKit.Swift" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,16 @@ class WalletConnectSessionManager {
let activeSessions = storage.sessions(accountId: account.id)

guard activeSessions.first(where: { session in session.topic == request.topic }) != nil,
let session = allSessions.first(where: { session in session.topic == request.topic }) else {
let session = allSessions.first(where: { session in session.topic == request.topic })
else {
return
}

let request = requestHandler.handle(session: session, request: request)
switch request {
case .request(let request): sessionRequestReceivedRelay.accept(request)
case let .request(request): sessionRequestReceivedRelay.accept(request)
case .handled: ()
case .unsuccessful(error: let error): print("Error while parsing request: \(error)")
case let .unsuccessful(error): print("Error while parsing request: \(error?.localizedDescription ?? "nil")")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import ThemeKit
import UIKit

struct CoinAnalyticsModule {
static func view(fullCoin: FullCoin) -> some View {
CoinAnalyticsView(fullCoin: fullCoin)
static func view(fullCoin: FullCoin, apiTag: String) -> some View {
CoinAnalyticsView(fullCoin: fullCoin, apiTag: apiTag)
}

static func viewController(fullCoin: FullCoin) -> CoinAnalyticsViewController {
static func viewController(fullCoin: FullCoin, apiTag: String) -> CoinAnalyticsViewController {
let service = CoinAnalyticsService(
fullCoin: fullCoin,
marketKit: App.shared.marketKit,
currencyManager: App.shared.currencyManager,
subscriptionManager: App.shared.subscriptionManager
subscriptionManager: App.shared.subscriptionManager,
apiTag: apiTag
)
let technicalIndicatorService = TechnicalIndicatorService(
coinUid: fullCoin.coin.uid,
Expand Down Expand Up @@ -61,9 +62,10 @@ struct CoinAnalyticsView: UIViewControllerRepresentable {
typealias UIViewControllerType = UIViewController

let fullCoin: FullCoin
let apiTag: String

func makeUIViewController(context _: Context) -> UIViewController {
CoinAnalyticsModule.viewController(fullCoin: fullCoin)
CoinAnalyticsModule.viewController(fullCoin: fullCoin, apiTag: apiTag)
}

func updateUIViewController(_: UIViewController, context _: Context) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
import Combine
import EvmKit
import MarketKit
import HsToolKit
import HsExtensions
import HsToolKit
import MarketKit

class CoinAnalyticsService {
private let fullCoin: FullCoin
private let marketKit: MarketKit.Kit
private let currencyManager: CurrencyManager
private let subscriptionManager: SubscriptionManager
private let apiTag: String
private var tasks = Set<AnyTask>()
private var cancellables = Set<AnyCancellable>()

@PostPublished private(set) var state: State = .loading

init(fullCoin: FullCoin, marketKit: MarketKit.Kit, currencyManager: CurrencyManager, subscriptionManager: SubscriptionManager) {
init(fullCoin: FullCoin, marketKit: MarketKit.Kit, currencyManager: CurrencyManager, subscriptionManager: SubscriptionManager, apiTag: String) {
self.fullCoin = fullCoin
self.marketKit = marketKit
self.currencyManager = currencyManager
self.subscriptionManager = subscriptionManager
self.apiTag = apiTag

subscriptionManager.$isAuthenticated
.sink { [weak self] isAuthenticated in
if isAuthenticated {
self?.sync()
}
.sink { [weak self] isAuthenticated in
if isAuthenticated {
self?.sync()
}
.store(in: &cancellables)
}
.store(in: &cancellables)
}

private func loadPreview() {
Task { [weak self, marketKit, fullCoin] in
Task { [weak self, marketKit, fullCoin, apiTag] in
do {
let analyticsPreview = try await marketKit.analyticsPreview(coinUid: fullCoin.coin.uid)
let analyticsPreview = try await marketKit.analyticsPreview(coinUid: fullCoin.coin.uid, apiTag: apiTag)
self?.state = .preview(analyticsPreview: analyticsPreview)
} catch {
self?.state = .failed(error)
}
}.store(in: &tasks)
}

}

extension CoinAnalyticsService {

var currency: Currency {
currencyManager.baseCurrency
}
Expand All @@ -55,8 +55,8 @@ extension CoinAnalyticsService {
var auditAddresses: [String]? {
let addresses = fullCoin.tokens.compactMap { token in
switch (token.blockchainType, token.type) {
case (.ethereum, .eip20(let address)): return address
case (.binanceSmartChain, .eip20(let address)): return address
case let (.ethereum, .eip20(address)): return address
case let (.binanceSmartChain, .eip20(address)): return address
default: return nil
}
}
Expand All @@ -78,36 +78,33 @@ extension CoinAnalyticsService {
state = .loading

if subscriptionManager.isAuthenticated {
Task { [weak self, subscriptionManager, marketKit, fullCoin, currency] in
Task { [weak self, subscriptionManager, marketKit, fullCoin, currency, apiTag] in
try await subscriptionManager.fetch(
request: {
try await marketKit.analytics(coinUid: fullCoin.coin.uid, currencyCode: currency.code)
},
onSuccess: { [weak self] analytics in
self?.state = .success(analytics: analytics)
},
onInvalidAuthToken: { [weak self] in
self?.loadPreview()
},
onFailure: { [weak self] error in
self?.state = .failed(error)
}
request: {
try await marketKit.analytics(coinUid: fullCoin.coin.uid, currencyCode: currency.code, apiTag: apiTag)
},
onSuccess: { [weak self] analytics in
self?.state = .success(analytics: analytics)
},
onInvalidAuthToken: { [weak self] in
self?.loadPreview()
},
onFailure: { [weak self] error in
self?.state = .failed(error)
}
)
}.store(in: &tasks)
} else {
loadPreview()
}
}

}

extension CoinAnalyticsService {

enum State {
case loading
case failed(Error)
case preview(analyticsPreview: AnalyticsPreview)
case success(analytics: Analytics)
}

}
Loading

0 comments on commit 405b3d2

Please sign in to comment.