Skip to content

Commit

Permalink
Remove old classes in Market module
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelekol committed May 22, 2024
1 parent 0ed164f commit e6bd075
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 492 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import io.horizontalsystems.bankwallet.core.stats.statTab
import io.horizontalsystems.bankwallet.modules.balance.ui.BalanceScreen
import io.horizontalsystems.bankwallet.modules.main.MainModule.MainNavigation
import io.horizontalsystems.bankwallet.modules.manageaccount.dialogs.BackupRequiredDialog
import io.horizontalsystems.bankwallet.modules.market.overviewxxx.MarketScreenXxx
import io.horizontalsystems.bankwallet.modules.market.MarketScreen
import io.horizontalsystems.bankwallet.modules.rateapp.RateApp
import io.horizontalsystems.bankwallet.modules.releasenotes.ReleaseNotesFragment
import io.horizontalsystems.bankwallet.modules.rooteddevice.RootedDeviceModule
Expand Down Expand Up @@ -217,7 +217,7 @@ private fun MainScreen(
verticalAlignment = Alignment.Top
) { page ->
when (uiState.mainNavItems[page].mainNavItem) {
MainNavigation.Market -> MarketScreenXxx(fragmentNavController)
MainNavigation.Market -> MarketScreen(fragmentNavController)
MainNavigation.Balance -> BalanceScreen(fragmentNavController)
MainNavigation.Transactions -> TransactionsScreen(
fragmentNavController,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.horizontalsystems.bankwallet.core.App
import io.horizontalsystems.bankwallet.entities.Currency
import io.horizontalsystems.bankwallet.entities.CurrencyValue
import io.horizontalsystems.bankwallet.modules.market.filters.TimePeriod
import io.horizontalsystems.bankwallet.modules.metricchart.MetricsType
import io.horizontalsystems.bankwallet.ui.compose.TranslatableString
import io.horizontalsystems.bankwallet.ui.compose.WithTranslatableTitle
import io.horizontalsystems.marketkit.models.FullCoin
Expand All @@ -28,12 +29,29 @@ object MarketModule {

@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T {
val service = MarketService(App.marketStorage, App.localStorage)
return MarketViewModel(service) as T
return MarketViewModel(
App.marketStorage,
App.marketKit,
App.currencyManager,
App.localStorage
) as T
}

}

data class UiState(
val selectedTab: Tab,
val marketOverviewItems: List<MarketOverviewViewItem>
)

data class MarketOverviewViewItem(
val title: String,
val value: String,
val change: String,
val changePositive: Boolean,
val metricsType: MetricsType,
)

enum class Tab(@StringRes val titleResId: Int) {
Coins(R.string.Market_Tab_Coins),
Watchlist(R.string.Market_Tab_Watchlist),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,209 @@ package io.horizontalsystems.bankwallet.modules.market

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.material.Divider
import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import io.horizontalsystems.bankwallet.R
import io.horizontalsystems.bankwallet.core.slideFromBottom
import io.horizontalsystems.bankwallet.core.slideFromRight
import io.horizontalsystems.bankwallet.core.stats.StatEvent
import io.horizontalsystems.bankwallet.core.stats.StatPage
import io.horizontalsystems.bankwallet.core.stats.StatSection
import io.horizontalsystems.bankwallet.core.stats.stat
import io.horizontalsystems.bankwallet.core.stats.statTab
import io.horizontalsystems.bankwallet.core.stats.statPage
import io.horizontalsystems.bankwallet.modules.coin.CoinFragment
import io.horizontalsystems.bankwallet.modules.market.MarketModule.Tab
import io.horizontalsystems.bankwallet.modules.market.favorites.MarketFavoritesScreen
import io.horizontalsystems.bankwallet.modules.market.posts.MarketPostsScreen
import io.horizontalsystems.bankwallet.modules.market.topcoins.TopCoins
import io.horizontalsystems.bankwallet.modules.market.topplatforms.TopPlatforms
import io.horizontalsystems.bankwallet.modules.metricchart.MetricsType
import io.horizontalsystems.bankwallet.ui.compose.ComposeAppTheme
import io.horizontalsystems.bankwallet.ui.compose.TranslatableString
import io.horizontalsystems.bankwallet.ui.compose.components.AppBar
import io.horizontalsystems.bankwallet.ui.compose.components.HSpacer
import io.horizontalsystems.bankwallet.ui.compose.components.MenuItem
import io.horizontalsystems.bankwallet.ui.compose.components.ScrollableTabs
import io.horizontalsystems.bankwallet.ui.compose.components.TabItem
import io.horizontalsystems.bankwallet.ui.compose.components.Tabs
import io.horizontalsystems.bankwallet.ui.compose.components.caption_bran
import io.horizontalsystems.bankwallet.ui.compose.components.caption_grey
import io.horizontalsystems.bankwallet.ui.compose.components.caption_lucian
import io.horizontalsystems.bankwallet.ui.compose.components.caption_remus

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun MarketScreen(navController: NavController) {
val marketViewModel = viewModel<MarketViewModel>(factory = MarketModule.Factory())
val tabs = marketViewModel.tabs
val selectedTab = marketViewModel.selectedTab
val viewModel = viewModel<MarketViewModel>(factory = MarketModule.Factory())
val uiState = viewModel.uiState
val tabs = viewModel.tabs

val pagerState = rememberPagerState(initialPage = selectedTab.ordinal) { tabs.size }
Scaffold(
backgroundColor = ComposeAppTheme.colors.tyler,
topBar = {
AppBar(
title = stringResource(R.string.Market_Title),
menuItems = listOf(
MenuItem(
title = TranslatableString.ResString(R.string.Market_Search),
icon = R.drawable.icon_search,
tint = ComposeAppTheme.colors.jacob,
onClick = {
navController.slideFromRight(R.id.marketSearchFragment)

Column(modifier = Modifier.background(color = ComposeAppTheme.colors.tyler)) {
AppBar(
title = stringResource(R.string.Market_Title),
menuItems = listOf(
MenuItem(
title = TranslatableString.ResString(R.string.Market_Search),
icon = R.drawable.icon_search,
onClick = {
navController.slideFromRight(R.id.marketSearchFragment)

stat(page = StatPage.Markets, event = StatEvent.Open(StatPage.MarketSearch))
}
),
MenuItem(
title = TranslatableString.ResString(R.string.Market_Filters),
icon = R.drawable.ic_manage_2_24,
onClick = {
navController.slideFromRight(R.id.marketAdvancedSearchFragment)

stat(page = StatPage.Markets, event = StatEvent.Open(StatPage.AdvancedSearch))
}
),
stat(
page = StatPage.Markets,
event = StatEvent.Open(StatPage.MarketSearch)
)
},
),
MenuItem(
title = TranslatableString.ResString(R.string.Market_Filters),
icon = R.drawable.ic_manage_2_24,
onClick = {
navController.slideFromRight(R.id.marketAdvancedSearchFragment)

stat(
page = StatPage.Markets,
event = StatEvent.Open(StatPage.AdvancedSearch)
)
},
),
)
)
)
}
) {
Column(
Modifier
.padding(it)
.background(ComposeAppTheme.colors.tyler)
) {
MetricsBoard(navController, uiState.marketOverviewItems)
Divider(
color = ComposeAppTheme.colors.steel10,
thickness = 1.dp
)
TabsSection(navController, tabs, uiState.selectedTab) { tab ->
viewModel.onSelect(tab)
}
}
}
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun TabsSection(
navController: NavController,
tabs: Array<Tab>,
selectedTab: Tab,
onTabClick: (Tab) -> Unit
) {
val pagerState = rememberPagerState(initialPage = selectedTab.ordinal) { tabs.size }

LaunchedEffect(key1 = selectedTab, block = {
pagerState.scrollToPage(selectedTab.ordinal)
})
val tabItems = tabs.map {
TabItem(stringResource(id = it.titleResId), it == selectedTab, it)
}

ScrollableTabs(tabItems) {
onTabClick(it)
}

HorizontalPager(
state = pagerState,
userScrollEnabled = false
) { page ->
when (tabs[page]) {
Tab.Coins -> {
TopCoins(onCoinClick = { onCoinClick(it, navController) })
stat(page = StatPage.MarketOverview, section = StatSection.TopGainers, event = StatEvent.Open(StatPage.TopCoins))
}

Tab.Watchlist -> MarketFavoritesScreen(navController)
Tab.Posts -> MarketPostsScreen()
Tab.Platform -> {
TopPlatforms(navController)
stat(page = StatPage.MarketOverview, event = StatEvent.Open(StatPage.TopPlatforms))
}
Tab.Pairs -> {
}

LaunchedEffect(key1 = selectedTab, block = {
pagerState.scrollToPage(selectedTab.ordinal)
})
val tabItems = tabs.map {
TabItem(stringResource(id = it.titleResId), it == selectedTab, it)
Tab.Sectors -> {
}
}
}
}

@Composable
fun MetricsBoard(
navController: NavController,
marketOverviewItems: List<MarketModule.MarketOverviewViewItem>
) {
Row(
modifier = Modifier
.fillMaxWidth()
.height(40.dp)
.background(ComposeAppTheme.colors.tyler)
.horizontalScroll(rememberScrollState()),
verticalAlignment = androidx.compose.ui.Alignment.CenterVertically
) {
HSpacer(4.dp)
marketOverviewItems.forEach { item ->
Row(
modifier = Modifier
.clickable {
openMetricsPage(item.metricsType, navController)
}
.padding(8.dp)
) {
HSpacer(12.dp)
caption_grey(text = item.title)
HSpacer(4.dp)
caption_bran(text = item.value)
HSpacer(4.dp)
if (item.changePositive) {
caption_remus(text = item.change)
} else {
caption_lucian(text = item.change)
}
HSpacer(12.dp)
}
}
Tabs(tabItems, onClick = {
marketViewModel.onSelect(it)

stat(page = StatPage.Markets, event = StatEvent.SwitchTab(it.statTab))
})

// HorizontalPager(
// state = pagerState,
// userScrollEnabled = false
// ) { page ->
// when (tabs[page]) {
// MarketModule.Tab.Overview -> MarketOverviewScreen(navController)
// MarketModule.Tab.Posts -> MarketPostsScreen()
// MarketModule.Tab.Watchlist -> MarketFavoritesScreen(navController)
// }
// }
HSpacer(4.dp)
}
}

private fun openMetricsPage(metricsType: MetricsType, navController: NavController) {
if (metricsType == MetricsType.TvlInDefi) {
navController.slideFromBottom(R.id.tvlFragment)
} else {
navController.slideFromBottom(R.id.metricsPageFragment, metricsType)
}

stat(page = StatPage.MarketOverview, event = StatEvent.Open(metricsType.statPage))
}

private fun onCoinClick(coinUid: String, navController: NavController) {
val arguments = CoinFragment.Input(coinUid)

navController.slideFromRight(R.id.coinFragment, arguments)

stat(page = StatPage.TopCoins, event = StatEvent.OpenCoin(coinUid))
}

This file was deleted.

Loading

0 comments on commit e6bd075

Please sign in to comment.