Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sa-234 minor fixes #235

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}
2 changes: 1 addition & 1 deletion common/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@
<string name="edit_view">Режим редактирования</string>
<string name="empty_recent_recipients">Адрес получателя будет отображен здесь.</string>
<string name="empty_recent_recipients_2">Получатели появятся здесь</string>
<string name="enter_backup_password_title">Ввдите пароль резервной копии</string>
<string name="enter_backup_password_title">Введите пароль резервной копии</string>
<string name="enter_password_description">Введите пароль резервной копии для выбранного аккаунта</string>
<string name="enter_password_incorect_title">Неверный пароль</string>
<string name="enter_password_title">Введите пароль</string>
Expand Down
14 changes: 7 additions & 7 deletions common/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<string name="asset_to_send">待发送的资产</string>
<string name="backup_account_already_exist">备份已存在。继续覆盖它。</string>
<string name="backup_not_enough_space_error">备份失败。您的Google Drive空间不足,请释放一些空间并重试。</string>
<string name="backup_now">Backup now</string>
<string name="backup_now">立即备份</string>
<string name="backup_password_mandatory_reqs_fulfilled">密码的强制要求已满足</string>
<string name="backup_password_requirments">至少六个字符。为了更好的安全性,可以包含大写字母、数字和非字母字符。</string>
<string name="backup_password_title">设置密码将加密您的Google备份。在恢复钱包时,您需要输入此密码。\n最小密码长度为6个字符。</string>
Expand All @@ -51,7 +51,7 @@
<string name="buy_crypto_buy_xor_banner_title">购买XOR代币</string>
<string name="buy_crypto_buy_xor_with_fiat_subtitle">用欧元购买或出售XOR代币</string>
<string name="buy_crypto_buy_xor_with_fiat_title">用法币购买XOR</string>
<string name="cant_load_balance">Can\'t load balance</string>
<string name="cant_load_balance">无法加载余额</string>
<string name="change_language">更改语言</string>
<string name="change_pin">更改PIN码</string>
<string name="choose_token">选择代币</string>
Expand Down Expand Up @@ -238,7 +238,7 @@
<string name="explore_create_pool">创建池子</string>
<string name="explore_provide_and_earn">提供流动性并获得奖励</string>
<string name="explore_swap_tokens_on_sora">在SORA网络上进行代币兑换</string>
<string name="export_account_details_backup_description">如果您无法访问此设备,您的资金将会丢失,除非您备份!</string>
<string name="export_account_details_backup_description">如果你无法访问这台设备,除非你进行备份,否则你的资金将会丢失!</string>
<string name="export_account_details_backup_options">备份选项</string>
<string name="export_account_details_show_passphrase">显示密码短语</string>
<string name="export_account_details_show_raw_seed">显示原始种子</string>
Expand Down Expand Up @@ -386,7 +386,7 @@
<item quantity="other"> %s 秒剩余</item>
</plurals>
<string name="project_ended_template">结束%s</string>
<string name="protect_loss_access_funds">Protect yourself from losing access to your funds</string>
<string name="protect_loss_access_funds">保护自己免于失去对资金的访问权限</string>
<string name="qr_code">QR 代码</string>
<string name="qr_upload">从相册</string>
<string name="receive_tokens">接收代币</string>
Expand Down Expand Up @@ -491,7 +491,7 @@
<string name="sora_card_iban_headline">IBAN账户详情</string>
<string name="sora_card_log_in_or_sign_up">登录或注册</string>
<string name="sora_card_no_more_free_tries">没有更多的免费尝试</string>
<string name="sora_card_option_logout">退出 SORA 卡</string>
<string name="sora_card_option_logout">退出SORA卡</string>
<string name="sora_card_option_logout_description">您即将退出 SORA Card。您仍然可以使用 SORA Card 独立应用程序,但余额将不再在 SORA 钱包中可用。</string>
<string name="sora_card_refer_and_earn_action">推荐并赚取</string>
<string name="sora_card_referral_headline">推荐SORA卡。\n获得20欧元奖金。</string>
Expand All @@ -512,7 +512,7 @@
<string name="status_successful">成功的</string>
<string name="status_title">状态</string>
<string name="succesfully_imported_account_title">您已成功导入一个账户</string>
<string name="support_chat">Support chat</string>
<string name="support_chat">支持聊天</string>
<string name="swap_confirmation_screen_warning_balance_afterwards_transaction_is_too_small">确保在此事务之后还剩下 %s。否则,您将无法进行其他交易。</string>
<string name="switch_node">切换节点</string>
<string name="system_appearance">系统外观</string>
Expand All @@ -530,7 +530,7 @@
<string name="unexpected_error">发生了意外错误。\n\n您可以在SORA社区的@SORAhappiness Telegram聊天中解决此问题。</string>
<string name="update_downloaded">SORA 应用程序的更新已下载并可以安装。 \n\n使用最新版本的应用程序很重要。 \n\n为了更新应用程序,需要重新启动。</string>
<string name="update_install_now">您想现在安装更新吗?</string>
<string name="wallet_backup">Wallet backup</string>
<string name="wallet_backup">钱包备份</string>
<string name="wallet_balance_locked">已被锁定</string>
<string name="wallet_balance_reserved">已保留</string>
<string name="wallet_bonded">已绑定</string>
Expand Down
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
Loading