Skip to content

Commit

Permalink
Switch all coin page usages to new CoinPage module
Browse files Browse the repository at this point in the history
  • Loading branch information
ealymbaev committed Jun 13, 2024
1 parent b347967 commit c3afe9f
Show file tree
Hide file tree
Showing 19 changed files with 90 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import HsExtensions
import MarketKit

class CoinAnalyticsViewModelNew: ObservableObject {
private let fullCoin: FullCoin
let coin: Coin
private let marketKit = App.shared.marketKit
private let currencyManager = App.shared.currencyManager
private var tasks = Set<AnyTask>()
Expand All @@ -32,8 +32,8 @@ class CoinAnalyticsViewModelNew: ObservableObject {
return formatter
}()

init(fullCoin: FullCoin) {
self.fullCoin = fullCoin
init(coin: Coin) {
self.coin = coin
}

private func handle(analytics: Analytics) async {
Expand Down Expand Up @@ -140,7 +140,7 @@ class CoinAnalyticsViewModelNew: ObservableObject {
reports: data.reports ? .preview : nil,
investors: data.fundsInvested ? .preview : nil,
treasuries: data.treasuries ? .preview : nil,
audits: auditAddresses != nil ? .preview : nil,
audits: nil,
issueBlockchains: nil
)
}
Expand Down Expand Up @@ -186,7 +186,7 @@ class CoinAnalyticsViewModelNew: ObservableObject {
if let value {
switch postfix {
case .currency: valueString = ValueFormatter.instance.formatShort(currency: currency, value: value)
case .coin: valueString = ValueFormatter.instance.formatShort(value: value).map { [$0, fullCoin.coin.code].joined(separator: " ") }
case .coin: valueString = ValueFormatter.instance.formatShort(value: value).map { [$0, coin.code].joined(separator: " ") }
case .noPostfix: valueString = ValueFormatter.instance.formatShort(value: value)
}
}
Expand Down Expand Up @@ -218,7 +218,7 @@ class CoinAnalyticsViewModelNew: ObservableObject {

return TransactionCountViewItem(
chart: .regular(value: chartViewItem),
volume: volume.flatMap { ValueFormatter.instance.formatShort(value: $0) }.map { .regular(value: [$0, fullCoin.coin.code].joined(separator: " ")) },
volume: volume.flatMap { ValueFormatter.instance.formatShort(value: $0) }.map { .regular(value: [$0, coin.code].joined(separator: " ")) },
rank: rank.map { .regular(value: rankString(value: $0)) },
rating: rating.flatMap { CoinAnalyticsModule.Rating(rawValue: $0) }.map { .regular(value: $0) }
)
Expand Down Expand Up @@ -340,41 +340,25 @@ class CoinAnalyticsViewModelNew: ObservableObject {
return []
}
}

private var auditAddresses: [String]? {
let addresses = fullCoin.tokens.compactMap { token in
switch (token.blockchainType, token.type) {
case let (.ethereum, .eip20(address)): return address
case let (.binanceSmartChain, .eip20(address)): return address
default: return nil
}
}

return addresses.isEmpty ? nil : addresses
}
}

extension CoinAnalyticsViewModelNew {
var currency: Currency {
currencyManager.baseCurrency
}

var coin: Coin {
fullCoin.coin
}

func load() {
tasks = Set()

state = .loading

Task { [weak self, isPurchased, marketKit, fullCoin, currency] in
Task { [weak self, isPurchased, marketKit, coin, currency] in
do {
if isPurchased {
let analytics = try await marketKit.analytics(coinUid: fullCoin.coin.uid, currencyCode: currency.code)
let analytics = try await marketKit.analytics(coinUid: coin.uid, currencyCode: currency.code)
await self?.handle(analytics: analytics)
} else {
let analyticsPreview = try await marketKit.analyticsPreview(coinUid: fullCoin.coin.uid)
let analyticsPreview = try await marketKit.analyticsPreview(coinUid: coin.uid)
await self?.handle(analyticsPreview: analyticsPreview)
}
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import HsExtensions
import MarketKit

class CoinMarketsViewModel: ObservableObject {
private let coin: Coin
private let coinUid: String
private let marketKit = App.shared.marketKit
private let currency = App.shared.currencyManager.baseCurrency
private var tasks = Set<AnyTask>()
Expand All @@ -27,8 +27,8 @@ class CoinMarketsViewModel: ObservableObject {

@Published var filterTypeInfo = SelectorButtonInfo(text: "", count: 0, selectedIndex: 0)

init(coin: Coin) {
self.coin = coin
init(coinUid: String) {
self.coinUid = coinUid

syncFilterTypeInfo()
}
Expand All @@ -40,9 +40,9 @@ class CoinMarketsViewModel: ObservableObject {
state = .loading
}

Task { [weak self, marketKit, coin, currency] in
Task { [weak self, marketKit, coinUid, currency] in
do {
let tickers = try await marketKit.marketTickers(coinUid: coin.uid, currencyCode: currency.code)
let tickers = try await marketKit.marketTickers(coinUid: coinUid, currencyCode: currency.code)
self?.tickers = tickers
self?.syncState()
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,3 @@ extension CoinPageModule {
}
}
}

struct CoinPageViewNew: UIViewControllerRepresentable {
typealias UIViewControllerType = UIViewController

let coinUid: String

func makeUIViewController(context _: Context) -> UIViewController {
CoinPageModule.viewController(coinUid: coinUid) ?? UIViewController()
}

func updateUIViewController(_: UIViewController, context _: Context) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ struct CoinPageView: View {
@State private var currentTab: Tab = .overview
@State private var loadedTabs = [Tab]()

init(fullCoin: FullCoin) {
_viewModel = StateObject(wrappedValue: CoinPageViewModelNew(fullCoin: fullCoin))
_overviewViewModel = StateObject(wrappedValue: CoinOverviewViewModelNew(coinUid: fullCoin.coin.uid))
_chartViewModel = StateObject(wrappedValue: CoinChartViewModel.instance(coinUid: fullCoin.coin.uid))
_analyticsViewModel = StateObject(wrappedValue: CoinAnalyticsViewModelNew(fullCoin: fullCoin))
_marketsViewModel = StateObject(wrappedValue: CoinMarketsViewModel(coin: fullCoin.coin))
init(coin: Coin) {
_viewModel = StateObject(wrappedValue: CoinPageViewModelNew(coin: coin))
_overviewViewModel = StateObject(wrappedValue: CoinOverviewViewModelNew(coinUid: coin.uid))
_chartViewModel = StateObject(wrappedValue: CoinChartViewModel.instance(coinUid: coin.uid))
_analyticsViewModel = StateObject(wrappedValue: CoinAnalyticsViewModelNew(coin: coin))
_marketsViewModel = StateObject(wrappedValue: CoinMarketsViewModel(coinUid: coin.uid))
}

var body: some View {
Expand Down Expand Up @@ -54,7 +54,7 @@ struct CoinPageView: View {
}
}
}
.navigationTitle(viewModel.fullCoin.coin.code)
.navigationTitle(viewModel.coin.code)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ import ComponentKit
import MarketKit

class CoinPageViewModelNew: ObservableObject {
let fullCoin: FullCoin
let coin: Coin
private let watchlistManager = App.shared.watchlistManager

@Published var isFavorite: Bool {
didSet {
if isFavorite {
watchlistManager.add(coinUid: fullCoin.coin.uid)
watchlistManager.add(coinUid: coin.uid)
HudHelper.instance.show(banner: .addedToWatchlist)
} else {
watchlistManager.remove(coinUid: fullCoin.coin.uid)
watchlistManager.remove(coinUid: coin.uid)
HudHelper.instance.show(banner: .removedFromWatchlist)
}
}
}

init(fullCoin: FullCoin) {
self.fullCoin = fullCoin
init(coin: Coin) {
self.coin = coin

isFavorite = watchlistManager.isWatched(coinUid: fullCoin.coin.uid)
isFavorite = watchlistManager.isWatched(coinUid: coin.uid)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct RankView: View {
}
}
.sheet(item: $presentedCoin) { coin in
CoinPageViewNew(coinUid: coin.uid).ignoresSafeArea()
CoinPageView(coin: coin)
.onFirstAppear { stat(page: viewModel.type.statRankType, event: .openCoin(coinUid: coin.uid)) }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import UIKit

class WidgetCoinAppShowModule {
private let parentViewController: UIViewController?
private let marketKit = App.shared.marketKit

init(parentViewController: UIViewController?) {
self.parentViewController = parentViewController
Expand All @@ -16,6 +17,7 @@ extension WidgetCoinAppShowModule: IEventHandler {
}

var coinUid: String?

switch event {
case let event as String:
coinUid = event
Expand All @@ -26,11 +28,13 @@ extension WidgetCoinAppShowModule: IEventHandler {
default: ()
}

guard let coinUid, let viewController = CoinPageModule.viewController(coinUid: coinUid) else {
guard let coinUid, let coin = try? marketKit.fullCoins(coinUids: [coinUid]).first?.coin else {
throw EventHandler.HandleError.noSuitableHandler
}

let viewController = CoinPageView(coin: coin).toViewController()
parentViewController?.visibleController.present(viewController, animated: true)

stat(page: .widget, event: .openCoin(coinUid: coinUid))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct MarketAdvancedSearchResultsView: View {
@Binding var isParentPresented: Bool

@State private var sortBySelectorPresented = false
@State private var presentedFullCoin: FullCoin?
@State private var presentedCoin: Coin?

init(marketInfos: [MarketInfo], timePeriod: HsTimePeriod, isParentPresented: Binding<Bool>) {
_viewModel = StateObject(wrappedValue: MarketAdvancedSearchResultsViewModel(marketInfos: marketInfos, timePeriod: timePeriod))
Expand All @@ -26,7 +26,7 @@ struct MarketAdvancedSearchResultsView: View {
let coin = marketInfo.fullCoin.coin

ClickableRow(action: {
presentedFullCoin = marketInfo.fullCoin
presentedCoin = coin
}) {
itemContent(
coin: coin,
Expand All @@ -51,9 +51,9 @@ struct MarketAdvancedSearchResultsView: View {
}
}
}
.sheet(item: $presentedFullCoin) { fullCoin in
CoinPageViewNew(coinUid: fullCoin.coin.uid).ignoresSafeArea()
.onFirstAppear { stat(page: .advancedSearchResults, event: .openCoin(coinUid: fullCoin.coin.uid)) }
.sheet(item: $presentedCoin) { coin in
CoinPageView(coin: coin).ignoresSafeArea()
.onFirstAppear { stat(page: .advancedSearchResults, event: .openCoin(coinUid: coin.uid)) }
}
.alert(
isPresented: $sortBySelectorPresented,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct MarketCoinsView: View {
@State private var topSelectorPresented = false
@State private var timePeriodSelectorPresented = false

@State private var presentedFullCoin: FullCoin?
@State private var presentedCoin: Coin?

var body: some View {
ThemeView {
Expand All @@ -33,9 +33,9 @@ struct MarketCoinsView: View {
}
}
}
.sheet(item: $presentedFullCoin) { fullCoin in
CoinPageView(fullCoin: fullCoin)
.onFirstAppear { stat(page: .markets, section: .coins, event: .openCoin(coinUid: fullCoin.coin.uid)) }
.sheet(item: $presentedCoin) { coin in
CoinPageView(coin: coin)
.onFirstAppear { stat(page: .markets, section: .coins, event: .openCoin(coinUid: coin.uid)) }
}
}

Expand Down Expand Up @@ -113,7 +113,7 @@ struct MarketCoinsView: View {
let coin = marketInfo.fullCoin.coin

ClickableRow(action: {
presentedFullCoin = marketInfo.fullCoin
presentedCoin = coin
}) {
itemContent(
coin: coin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct MarketMarketCapView: View {
@StateObject var watchlistViewModel: WatchlistViewModel
@Binding var isPresented: Bool

@State private var presentedFullCoin: FullCoin?
@State private var presentedCoin: Coin?

init(isPresented: Binding<Bool>) {
_viewModel = StateObject(wrappedValue: MarketMarketCapViewModel())
Expand Down Expand Up @@ -63,9 +63,9 @@ struct MarketMarketCapView: View {
}
}
}
.sheet(item: $presentedFullCoin) { fullCoin in
CoinPageViewNew(coinUid: fullCoin.coin.uid).ignoresSafeArea()
.onFirstAppear { stat(page: .globalMetricsMarketCap, event: .openCoin(coinUid: fullCoin.coin.uid)) }
.sheet(item: $presentedCoin) { coin in
CoinPageView(coin: coin)
.onFirstAppear { stat(page: .globalMetricsMarketCap, event: .openCoin(coinUid: coin.uid)) }
}
}
}
Expand Down Expand Up @@ -115,7 +115,7 @@ struct MarketMarketCapView: View {
let coin = marketInfo.fullCoin.coin

ClickableRow(action: {
presentedFullCoin = marketInfo.fullCoin
presentedCoin = coin
}) {
itemContent(
coin: coin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct MarketPlatformViewNew: View {
@Binding var isPresented: Bool

@State private var sortBySelectorPresented = false
@State private var presentedFullCoin: FullCoin?
@State private var presentedCoin: Coin?

init(isPresented: Binding<Bool>, platform: TopPlatform) {
_viewModel = StateObject(wrappedValue: MarketPlatformViewModel(platform: platform))
Expand Down Expand Up @@ -63,9 +63,9 @@ struct MarketPlatformViewNew: View {
}
}
}
.sheet(item: $presentedFullCoin) { fullCoin in
CoinPageViewNew(coinUid: fullCoin.coin.uid).ignoresSafeArea()
.onFirstAppear { stat(page: .globalMetricsTvlInDefi, event: .openCoin(coinUid: fullCoin.coin.uid)) }
.sheet(item: $presentedCoin) { coin in
CoinPageView(coin: coin)
.onFirstAppear { stat(page: .globalMetricsTvlInDefi, event: .openCoin(coinUid: coin.uid)) }
}
}
}
Expand Down Expand Up @@ -128,7 +128,7 @@ struct MarketPlatformViewNew: View {
let coin = marketInfo.fullCoin.coin

ClickableRow(action: {
presentedFullCoin = marketInfo.fullCoin
presentedCoin = coin
}) {
itemContent(
coin: coin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct MarketSearchView: View {
@ObservedObject var viewModel: MarketSearchViewModel
@ObservedObject var watchlistViewModel: WatchlistViewModel

@State private var presentedFullCoin: FullCoin?
@State private var presentedCoin: Coin?

var body: some View {
ThemeView {
Expand Down Expand Up @@ -40,8 +40,8 @@ struct MarketSearchView: View {
}
}
}
.sheet(item: $presentedFullCoin) { fullCoin in
CoinPageViewNew(coinUid: fullCoin.coin.uid).ignoresSafeArea()
.sheet(item: $presentedCoin) { coin in
CoinPageView(coin: coin)
}
}

Expand All @@ -50,7 +50,7 @@ struct MarketSearchView: View {

ClickableRow(action: {
viewModel.handleOpen(coinUid: coin.uid)
presentedFullCoin = fullCoin
presentedCoin = coin
}) {
CoinIconView(coin: coin)

Expand Down
Loading

0 comments on commit c3afe9f

Please sign in to comment.