Skip to content

Commit

Permalink
Initial implementation of analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
ealymbaev committed Mar 15, 2024
1 parent e0e99e4 commit 70ab378
Show file tree
Hide file tree
Showing 27 changed files with 164 additions and 65 deletions.
12 changes: 12 additions & 0 deletions UnstoppableWallet/UnstoppableWallet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@
11B355F32686B8689B4EC105 /* WalletConnectRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35CD5EBBB403D46BDEF0B /* WalletConnectRequest.swift */; };
11B355FAD0E7823AF5F8EC83 /* SendEvmTransactionService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35C7F043B6C41E53D43BC /* SendEvmTransactionService.swift */; };
11B355FC0E3DE029EB3F95D5 /* TransactionTokenSelectView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35DDC338BFE2832C07360 /* TransactionTokenSelectView.swift */; };
11B355FCAF8DC88550CE2DB3 /* StatsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B359980AA45D6B44151D7A /* StatsManager.swift */; };
11B3560586CBAB617211F003 /* Caution.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35D96CF03878016FC38FD /* Caution.swift */; };
11B356075F51B38338958A4A /* MultiSwapSettingStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B350910284BA2BF694FA17 /* MultiSwapSettingStorage.swift */; };
11B35608F7D19B3E6318CB22 /* Text.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B352972B14FA6EBEFD6904 /* Text.swift */; };
Expand Down Expand Up @@ -793,6 +794,7 @@
11B3586BF6AC0538272E71A4 /* NftCollectionModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35708A630D70385F34A8B /* NftCollectionModule.swift */; };
11B3586F6BFCA16BDFD5921D /* DuressModeModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35A81FB3D4C06BBFEE7E7 /* DuressModeModule.swift */; };
11B35871BA700133050E9241 /* CexWithdrawViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35B2465CB748311AF03D5 /* CexWithdrawViewModel.swift */; };
11B3587280273E0804CA84F2 /* StatPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35743A66A4653A3C2FDBF /* StatPage.swift */; };
11B358781EBEFCE7CED000F0 /* SearchBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35A6BEF8299304625D6D0 /* SearchBar.swift */; };
11B3587D9E89A97F63CD0C5A /* EditPasscodeModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B3529CF33E51DA1C872106 /* EditPasscodeModule.swift */; };
11B3587EF674C1E8EEE61DE7 /* MarkdownBlockQuoteCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B3552D3F84BA594EFE964C /* MarkdownBlockQuoteCell.swift */; };
Expand Down Expand Up @@ -1078,6 +1080,7 @@
11B35B15B1815F7EEB128C5F /* BinanceAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35AAAC675987369F2DA1B /* BinanceAdapter.swift */; };
11B35B172B09C2017B389C07 /* AlertTitleCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35BF766EAC97E74CD620D /* AlertTitleCell.swift */; };
11B35B177527D30099B67C91 /* FeeData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B3542B6FE4B4F0C0B65369 /* FeeData.swift */; };
11B35B1875DF852DE471C511 /* StatPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35743A66A4653A3C2FDBF /* StatPage.swift */; };
11B35B1F09AE8EBC7E594974 /* ScanQrView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35FE66467E3FA27809733 /* ScanQrView.swift */; };
11B35B1F63DBDEEF57043C97 /* PublicKeysService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B358A294479046C42D2E6B /* PublicKeysService.swift */; };
11B35B26C32F47DE6E83FC82 /* BlockchainTokensModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35BC10B98A0770A2AC342 /* BlockchainTokensModule.swift */; };
Expand Down Expand Up @@ -1526,6 +1529,7 @@
11B35FE603192CF3195115D0 /* LanguageSettingsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35920D0AD623C5E4A4460 /* LanguageSettingsViewModel.swift */; };
11B35FE8D60BFF31C3104484 /* SwitchAccountViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B350BC3E707879846AC0AA /* SwitchAccountViewModel.swift */; };
11B35FEB268E7C6B085B56C9 /* MarketOverviewTopPairsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35AB1D0CE5D8ECE7DDF65 /* MarketOverviewTopPairsViewModel.swift */; };
11B35FEC7AA2A8887FCF0AE6 /* StatsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B359980AA45D6B44151D7A /* StatsManager.swift */; };
11B35FF02BADC6D832836C44 /* EvmPrivateKeyViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B353262E45560C91FD6B65 /* EvmPrivateKeyViewModel.swift */; };
11B35FF072E32C5D4028D0F6 /* TextInputCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35968F5DE9FDA6EC26FCD /* TextInputCell.swift */; };
11B35FF10EF7FC47869EF295 /* CexWithdrawViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35B2465CB748311AF03D5 /* CexWithdrawViewModel.swift */; };
Expand Down Expand Up @@ -3560,6 +3564,7 @@
11B3573B3FE1FD8B476375E6 /* NftCollectionAssetsViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NftCollectionAssetsViewModel.swift; sourceTree = "<group>"; };
11B357426B767AA64ED8E7A2 /* WalletViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WalletViewModel.swift; sourceTree = "<group>"; };
11B35743158763BD8E336770 /* InputPrefixWrapperView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InputPrefixWrapperView.swift; sourceTree = "<group>"; };
11B35743A66A4653A3C2FDBF /* StatPage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatPage.swift; sourceTree = "<group>"; };
11B35747FAD8381F2AD48276 /* MainViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainViewModel.swift; sourceTree = "<group>"; };
11B357511F8F17D8221B64E2 /* AppearanceView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppearanceView.swift; sourceTree = "<group>"; };
11B3575530EE722514F89A61 /* PasteInputView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PasteInputView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3690,6 +3695,7 @@
11B35995E0D358AC4DA2FA74 /* RestoreBinanceModule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RestoreBinanceModule.swift; sourceTree = "<group>"; };
11B35996D668B9ADC60E6B9B /* CoinAnalyticsService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoinAnalyticsService.swift; sourceTree = "<group>"; };
11B35997A9E413878F48313B /* ActivateSubscriptionModule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ActivateSubscriptionModule.swift; sourceTree = "<group>"; };
11B359980AA45D6B44151D7A /* StatsManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatsManager.swift; sourceTree = "<group>"; };
11B3599833F166185872A3AC /* RecipientAndSlippageMultiSwapSettingsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RecipientAndSlippageMultiSwapSettingsView.swift; sourceTree = "<group>"; };
11B35999E6C5518115365410 /* EvmAccountRestoreStateStorage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EvmAccountRestoreStateStorage.swift; sourceTree = "<group>"; };
11B359A35AEB7964A94AFFC0 /* BiometryType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BiometryType.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -5509,6 +5515,7 @@
11B35B5570E7513DF2A455BB /* PasscodeManager.swift */,
11B35196B818E6069195BAF1 /* KeychainManager.swift */,
11B3505A43D9C2787B3BD153 /* PasscodeLockManager.swift */,
11B359980AA45D6B44151D7A /* StatsManager.swift */,
);
path = Managers;
sourceTree = "<group>";
Expand Down Expand Up @@ -5893,6 +5900,7 @@
ABC9A68AFE3CF24D2B88808F /* EnabledWalletCache_v_0_36.swift */,
11B35E41142BD3D2FF59BAE7 /* AutoLockPeriod.swift */,
11B35B6F5261FF3F9ECBC02E /* PasscodeLockState.swift */,
11B35743A66A4653A3C2FDBF /* StatPage.swift */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -10872,6 +10880,8 @@
ABC9A6C591067D34C6DF2673 /* SlideButtonStyling.swift in Sources */,
11B35A32114022EF422E6602 /* MultiSwapRevokeView.swift in Sources */,
11B357DCC0BA9E888DE64CB1 /* MultiSwapRevokeViewModel.swift in Sources */,
11B35FEC7AA2A8887FCF0AE6 /* StatsManager.swift in Sources */,
11B3587280273E0804CA84F2 /* StatPage.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -12358,6 +12368,8 @@
11B35A920F2DB5784F178BDA /* EvmFeeEstimator.swift in Sources */,
11B3545369350E4253688D91 /* MultiSwapRevokeView.swift in Sources */,
11B35CBF59F85C3DAB5FE751 /* MultiSwapRevokeViewModel.swift in Sources */,
11B355FCAF8DC88550CE2DB3 /* StatsManager.swift in Sources */,
11B35B1875DF852DE471C511 /* StatPage.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Alamofire
import Foundation
import HsToolKit

func stat(page: StatPage, section: StatSection? = nil, event: StatEvent, params: [StatParam: Any]? = nil) {
StatsManager.instance.sendStat(page: page, section: section, event: event, params: params)
}

class StatsManager {
static let instance = StatsManager(networkManager: NetworkManager(logger: Logger(minLogLevel: .debug)))

private let networkManager: NetworkManager

init(networkManager: NetworkManager) {
self.networkManager = networkManager
}

func sendStat(page: StatPage, section: StatSection? = nil, event: StatEvent, params: [StatParam: Any]? = nil) {
Task {
var parameters: Parameters = [
"page": page.rawValue,
"event": event.rawValue,
]

if let section {
parameters["section"] = section.rawValue
}

if let params {
for (key, value) in params {
parameters[key.rawValue] = value
}
}

_ = try await networkManager.fetchJson(url: "\(AppConfig.marketApiUrl)/v1/stats", method: .head, parameters: parameters)
}
}
}
34 changes: 34 additions & 0 deletions UnstoppableWallet/UnstoppableWallet/Models/StatPage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
enum StatPage: String {
case advancedSearchResults = "advanced_search_results"
case coinPage = "coin_page"
case coinCategory = "coin_category"
case coinRank = "coin_rank"
case globalMetricsMarketCap = "global_metrics_market_cap"
case globalMetricsVolume = "global_metrics_volume"
case globalMetricsDefiCap = "global_metrics_defi_cap"
case globalMetricsTvlInDefi = "global_metrics_tvl_in_defi"
case marketOverview = "market_overview"
case marketSearch = "market_search"
case topCoins = "top_coins"
case topMarketPairs = "top_market_pairs"
case topNftCollections = "top_nft_collections"
case topPlatform = "top_platform"
case topPlatforms = "top_platforms"
case tokenPage = "token_page"
case transactionInfo = "transaction_info"
case watchlist
case widget
}

enum StatEvent: String {
case coinOpen = "coin_open"
}

enum StatParam: String {
case coinUid = "coin_uid"
}

enum StatSection: String {
case topGainers = "top_gainers"
case topLosers = "top_losers"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import ThemeKit
import UIKit

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

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

let fullCoin: FullCoin
let apiTag: String

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

func updateUIViewController(_: UIViewController, context _: Context) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ class CoinAnalyticsService {
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, apiTag: String) {
init(fullCoin: FullCoin, marketKit: MarketKit.Kit, currencyManager: CurrencyManager, subscriptionManager: SubscriptionManager) {
self.fullCoin = fullCoin
self.marketKit = marketKit
self.currencyManager = currencyManager
self.subscriptionManager = subscriptionManager
self.apiTag = apiTag

subscriptionManager.$isAuthenticated
.sink { [weak self] isAuthenticated in
Expand All @@ -32,9 +30,9 @@ class CoinAnalyticsService {
}

private func loadPreview() {
Task { [weak self, marketKit, fullCoin, apiTag] in
Task { [weak self, marketKit, fullCoin] in
do {
let analyticsPreview = try await marketKit.analyticsPreview(coinUid: fullCoin.coin.uid, apiTag: apiTag)
let analyticsPreview = try await marketKit.analyticsPreview(coinUid: fullCoin.coin.uid, apiTag: "")
self?.state = .preview(analyticsPreview: analyticsPreview)
} catch {
self?.state = .failed(error)
Expand Down Expand Up @@ -78,10 +76,10 @@ extension CoinAnalyticsService {
state = .loading

if subscriptionManager.isAuthenticated {
Task { [weak self, subscriptionManager, marketKit, fullCoin, currency, apiTag] in
Task { [weak self, subscriptionManager, marketKit, fullCoin, currency] in
try await subscriptionManager.fetch(
request: {
try await marketKit.analytics(coinUid: fullCoin.coin.uid, currencyCode: currency.code, apiTag: apiTag)
try await marketKit.analytics(coinUid: fullCoin.coin.uid, currencyCode: currency.code, apiTag: "")
},
onSuccess: { [weak self] analytics in
self?.state = .success(analytics: analytics)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import MarketKit
import SwiftUI

enum CoinOverviewModule {
static func view(coinUid: String, apiTag: String) -> some View {
static func view(coinUid: String) -> some View {
let repository = ChartIndicatorsRepository(
localStorage: App.shared.localStorage,
subscriptionManager: App.shared.subscriptionManager
Expand All @@ -24,8 +24,7 @@ enum CoinOverviewModule {
currencyManager: App.shared.currencyManager,
languageManager: LanguageManager.shared,
accountManager: App.shared.accountManager,
walletManager: App.shared.walletManager,
apiTag: apiTag
walletManager: App.shared.walletManager
)

return CoinOverviewView(
Expand All @@ -36,15 +35,14 @@ enum CoinOverviewModule {
)
}

static func viewController(coinUid: String, apiTag: String) -> CoinOverviewViewController {
static func viewController(coinUid: String) -> CoinOverviewViewController {
let service = CoinOverviewService(
coinUid: coinUid,
marketKit: App.shared.marketKit,
currencyManager: App.shared.currencyManager,
languageManager: LanguageManager.shared,
accountManager: App.shared.accountManager,
walletManager: App.shared.walletManager,
apiTag: apiTag
walletManager: App.shared.walletManager
)

let repository = ChartIndicatorsRepository(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class CoinOverviewService {
private let languageManager: LanguageManager
private let accountManager: AccountManager
private let walletManager: WalletManager
private let apiTag: String

private let stateRelay = PublishRelay<DataStatus<Item>>()
private(set) var state: DataStatus<Item> = .loading {
Expand All @@ -22,14 +21,13 @@ class CoinOverviewService {
}
}

init(coinUid: String, marketKit: MarketKit.Kit, currencyManager: CurrencyManager, languageManager: LanguageManager, accountManager: AccountManager, walletManager: WalletManager, apiTag: String) {
init(coinUid: String, marketKit: MarketKit.Kit, currencyManager: CurrencyManager, languageManager: LanguageManager, accountManager: AccountManager, walletManager: WalletManager) {
self.coinUid = coinUid
self.marketKit = marketKit
self.currencyManager = currencyManager
self.languageManager = languageManager
self.accountManager = accountManager
self.walletManager = walletManager
self.apiTag = apiTag
}

private func sync(info: MarketInfoOverview) {
Expand Down Expand Up @@ -120,13 +118,13 @@ extension CoinOverviewService {

state = .loading

Task { [weak self, marketKit, coinUid, currencyManager, languageManager, apiTag] in
Task { [weak self, marketKit, coinUid, currencyManager, languageManager] in
do {
let info = try await marketKit.marketInfoOverview(
coinUid: coinUid,
currencyCode: currencyManager.baseCurrency.code,
languageCode: languageManager.currentLanguage,
apiTag: apiTag
apiTag: ""
)
self?.sync(info: info)
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,19 @@ class CoinOverviewViewModelNew: ObservableObject {
private let languageManager: LanguageManager
private let accountManager: AccountManager
private let walletManager: WalletManager
private let apiTag: String
private let viewItemFactory = CoinOverviewViewItemFactory()

let currency: Currency

@Published private(set) var state: DataStatus<Item> = .loading

init(coinUid: String, marketKit: MarketKit.Kit, currencyManager: CurrencyManager, languageManager: LanguageManager, accountManager: AccountManager, walletManager: WalletManager, apiTag: String) {
init(coinUid: String, marketKit: MarketKit.Kit, currencyManager: CurrencyManager, languageManager: LanguageManager, accountManager: AccountManager, walletManager: WalletManager) {
self.coinUid = coinUid
self.marketKit = marketKit
self.currencyManager = currencyManager
self.languageManager = languageManager
self.accountManager = accountManager
self.walletManager = walletManager
self.apiTag = apiTag

currency = currencyManager.baseCurrency
}
Expand Down Expand Up @@ -119,13 +117,13 @@ extension CoinOverviewViewModelNew {

state = .loading

Task { [weak self, marketKit, coinUid, currencyManager, languageManager, apiTag] in
Task { [weak self, marketKit, coinUid, currencyManager, languageManager] in
do {
let info = try await marketKit.marketInfoOverview(
coinUid: coinUid,
currencyCode: currencyManager.baseCurrency.code,
languageCode: languageManager.currentLanguage,
apiTag: apiTag
apiTag: ""
)
self?.handleSuccess(info: info)
} catch {
Expand Down
Loading

0 comments on commit 70ab378

Please sign in to comment.