Skip to content

Commit

Permalink
sa-234 minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
arvifox committed Oct 17, 2023
1 parent 46b6875 commit 4daeddb
Show file tree
Hide file tree
Showing 11 changed files with 495 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,62 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package jp.co.soramitsu.common.domain

import kotlinx.coroutines.delay

interface RepeatStrategy {
suspend fun repeat(block: suspend () -> Unit)
}

interface RetryStrategy {
@Throws(RuntimeException::class)
suspend fun <T> retryIf(
retries: Int,
predicate: suspend (cause: Throwable) -> Boolean,
block: suspend () -> T,
): T
}

object RepeatStrategyBuilder {
fun infinite(): RepeatStrategy = InfiniteRepeatStrategy()
}

object RetryStrategyBuilder {
fun build(): RetryStrategy = RetryStrategyImpl()
}

private class InfiniteRepeatStrategy : RepeatStrategy {
override suspend fun repeat(block: suspend () -> Unit) {
while (true) {
block.invoke()
}
}
}

private class RetryStrategyImpl : RetryStrategy {

override suspend fun <T> retryIf(
retries: Int,
predicate: suspend (cause: Throwable) -> Boolean,
block: suspend () -> T,
): T {
var attempt = 0
var shallRetry: Boolean
do {
shallRetry = false
runCatching {
block.invoke()
}.onSuccess {
return it
}.onFailure { t ->
if (predicate(t) && attempt < retries) {
shallRetry = true
attempt++
delay(500)
} else {
throw t
}
}
} while (shallRetry)
throw RuntimeException("RetryStrategy")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
This file is part of the SORA network and Polkaswap app.
Copyright (c) 2020, 2021, Polka Biome Ltd. All rights reserved.
SPDX-License-Identifier: BSD-4-Clause
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
All advertising materials mentioning features or use of this software must display
the following acknowledgement: This product includes software developed by Polka Biome
Ltd., SORA, and Polkaswap.
Neither the name of the Polka Biome Ltd. nor the names of its contributors may be used
to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY Polka Biome Ltd. AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Polka Biome Ltd. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package jp.co.soramitsu.common.domain

import jp.co.soramitsu.test_shared.MainCoroutineRule
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test

class RetryStrategyTests {
@get:Rule
var mainCoroutineRule = MainCoroutineRule()

@Test
fun `retry test 01`() = runTest {
val retry = RetryStrategyBuilder.build()
val res = retry.retryIf(
3,
{ t -> t is IllegalArgumentException },
{ okTime1() },
)
assertEquals(12, res)
}

@Test
fun `retry test 02`() = runTest {
val retry = RetryStrategyBuilder.build()
val res = retry.retryIf(
3,
{ t -> t is IllegalArgumentException },
{ okTime2() },
)
assertEquals(2, res)
}

@Test(expected = IllegalArgumentException::class)
fun `retry test 03`() = runTest {
val retry = RetryStrategyBuilder.build()
retry.retryIf(
3,
{ t -> t is IllegalArgumentException },
{ okTime3() },
)
}

private fun okTime1(): Int {
return 12
}

private var count2 = 0
private fun okTime2(): Int {
count2++
if (count2 == 1) throw IllegalArgumentException("count2=$count2")
return count2
}

private var count3 = 0
private fun okTime3(): Int {
count3++
if (count3 < 5) throw IllegalArgumentException("count3=$count3")
return count3
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ package jp.co.soramitsu.common_wallet.presentation.compose.states

import jp.co.soramitsu.common.domain.AssetHolder
import jp.co.soramitsu.common.domain.formatFiat
import jp.co.soramitsu.common.domain.formatFiatChange
import jp.co.soramitsu.common.domain.iconUri
import jp.co.soramitsu.common.util.NumbersFormatter
import jp.co.soramitsu.common.util.StringPair
import jp.co.soramitsu.common.util.ext.isNanZero
import jp.co.soramitsu.common_wallet.domain.model.CommonUserPoolData

class PoolsListState(
Expand Down Expand Up @@ -86,12 +84,13 @@ fun mapPoolsData(
token2Icon = poolData.basic.targetToken.iconUri(),
fiat = formatted[i]?.first?.let { poolData.basic.baseToken.formatFiat(it, numbersFormatter) }
.orEmpty(),
fiatChange = formatted[i]?.second?.let {
formatFiatChange(
it.isNanZero(),
numbersFormatter
)
}.orEmpty(),
fiatChange = "",
// fiatChange = formatted[i]?.second?.let {
// formatFiatChange(
// it.isNanZero(),
// numbersFormatter
// )
// }.orEmpty(),
tokenIds = poolData.basic.baseToken.id to poolData.basic.targetToken.id,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ internal fun AssetDetailsBalanceCard(
frozenAmount: String? = null,
frozenAmountFiat: String? = null,
isTransferableAmountAvailable: Boolean = false,
hasHistory: Boolean = false,
buyCryptoAvailable: Boolean = false,
onSendClick: () -> Unit,
onReceiveClick: () -> Unit,
Expand All @@ -96,20 +95,59 @@ internal fun AssetDetailsBalanceCard(
.fillMaxWidth()
.wrapContentHeight()
) {
if (hasHistory) {
Row(
modifier = Modifier
.padding(horizontal = Dimens.x3)
.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween
) {
Text(
text = stringResource(id = R.string.asset_details_liquid_balance),
color = MaterialTheme.customColors.fgPrimary,
style = MaterialTheme.customTypography.headline2
)
Text(
text = amountFiat,
color = MaterialTheme.customColors.fgPrimary,
style = MaterialTheme.customTypography.headline2
)
}
Text(
modifier = Modifier
.padding(horizontal = Dimens.x3)
.fillMaxWidth(),
text = amount,
style = MaterialTheme.customTypography.textXSBold,
color = MaterialTheme.customColors.fgSecondary
)

Spacer(modifier = Modifier.height(Dimens.x2))

if (frozenAmount != null && frozenAmountFiat != null) {
Row(
modifier = Modifier
.padding(horizontal = Dimens.x3)
.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween
) {
Row {
Text(
text = stringResource(id = R.string.details_frozen),
style = MaterialTheme.customTypography.headline2,
color = MaterialTheme.customColors.fgSecondary
)
Icon(
modifier = Modifier
.padding(start = Dimens.x1)
.align(Alignment.CenterVertically)
.size(Dimens.x2),
painter = painterResource(id = R.drawable.ic_neu_lock),
tint = MaterialTheme.customColors.fgSecondary,
contentDescription = "",
)
}
Text(
text = stringResource(id = R.string.asset_details_liquid_balance),
color = MaterialTheme.customColors.fgPrimary,
style = MaterialTheme.customTypography.headline2
)
Text(
text = amountFiat,
text = frozenAmountFiat,
color = MaterialTheme.customColors.fgPrimary,
style = MaterialTheme.customTypography.headline2
)
Expand All @@ -118,60 +156,19 @@ internal fun AssetDetailsBalanceCard(
modifier = Modifier
.padding(horizontal = Dimens.x3)
.fillMaxWidth(),
text = amount,
text = frozenAmount,
style = MaterialTheme.customTypography.textXSBold,
color = MaterialTheme.customColors.fgSecondary
)

Spacer(modifier = Modifier.height(Dimens.x2))

if (frozenAmount != null && frozenAmountFiat != null) {
Row(
modifier = Modifier
.padding(horizontal = Dimens.x3)
.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween
) {
Row {
Text(
text = stringResource(id = R.string.details_frozen),
style = MaterialTheme.customTypography.headline2,
color = MaterialTheme.customColors.fgSecondary
)
Icon(
modifier = Modifier
.padding(start = Dimens.x1)
.align(Alignment.CenterVertically)
.size(Dimens.x2),
painter = painterResource(id = R.drawable.ic_neu_lock),
tint = MaterialTheme.customColors.fgSecondary,
contentDescription = "",
)
}
Text(
text = frozenAmountFiat,
color = MaterialTheme.customColors.fgPrimary,
style = MaterialTheme.customTypography.headline2
)
}
Text(
modifier = Modifier
.padding(horizontal = Dimens.x3)
.fillMaxWidth(),
text = frozenAmount,
style = MaterialTheme.customTypography.textXSBold,
color = MaterialTheme.customColors.fgSecondary
)
}
Divider(
color = MaterialTheme.customColors.fgOutline,
thickness = 1.dp,
modifier = Modifier
.padding(horizontal = Dimens.x3, vertical = Dimens.x2)
.height(1.dp)
.fillMaxWidth()
)
}
Divider(
color = MaterialTheme.customColors.fgOutline,
thickness = 1.dp,
modifier = Modifier
.padding(horizontal = Dimens.x3, vertical = Dimens.x2)
.height(1.dp)
.fillMaxWidth()
)
Row(
modifier = Modifier
.wrapContentHeight()
Expand Down Expand Up @@ -255,7 +252,6 @@ private fun PreviewAssetDetailsBalanceCard() {
frozenAmount = "12.3 XOR",
frozenAmountFiat = "$ 1000.1",
buyCryptoAvailable = true,
hasHistory = true,
isTransferableAmountAvailable = true,
onSendClick = { },
onReceiveClick = { },
Expand Down
Loading

0 comments on commit 4daeddb

Please sign in to comment.