Skip to content

Commit

Permalink
bug fixeds
Browse files Browse the repository at this point in the history
  • Loading branch information
polstianka committed Oct 8, 2024
1 parent e72e783 commit f732194
Show file tree
Hide file tree
Showing 20 changed files with 230 additions and 71 deletions.
14 changes: 10 additions & 4 deletions apps/wallet/api/src/main/java/com/tonapps/wallet/api/API.kt
Original file line number Diff line number Diff line change
Expand Up @@ -630,10 +630,16 @@ class API(
accountId: String,
testnet: Boolean
): Account? {
val normalizedAccountId = if (accountId.endsWith(".ton")) {
accountId.lowercase().trim().unicodeToPunycode()
} else {
accountId
var normalizedAccountId = accountId
if (normalizedAccountId.startsWith("https://")) {
normalizedAccountId = normalizedAccountId.replace("https://", "")
}
if (normalizedAccountId.startsWith("t.me/")) {
normalizedAccountId = normalizedAccountId.replace("t.me/", "")
normalizedAccountId = "$normalizedAccountId.t.me"
}
if (!normalizedAccountId.isValidTonAddress()) {
normalizedAccountId = normalizedAccountId.lowercase().trim().unicodeToPunycode()
}
return withRetry { accounts(testnet).getAccount(normalizedAccountId) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ fun TokenRates.to(toCurrency: String, value: Float): Float {
return price.toFloat() * value
}

val MessageConsequences.totalFees: Long
get() = trace.transaction.totalFees
/*val MessageConsequences.totalFees: Long
get() {
// = event.extra // trace.transaction.totalFees
val extra = event.extra
if (0 > extra) {
return abs(extra)
}
return 0
}*/

val AccountEvent.withTON: Boolean
get() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
package com.tonapps.tonkeeper.core.history

import android.content.Context
import android.util.Log
import androidx.collection.arrayMapOf
import com.tonapps.blockchain.ton.extensions.equalsAddress
import com.tonapps.blockchain.ton.extensions.toRawAddress
import com.tonapps.icu.Coins
import com.tonapps.blockchain.ton.extensions.toUserFriendly
import com.tonapps.extensions.max24
import com.tonapps.extensions.short4
import com.tonapps.extensions.short8
import com.tonapps.extensions.withMinus
import com.tonapps.extensions.withPlus
import com.tonapps.icu.CurrencyFormatter
import com.tonapps.tonkeeper.api.amount
import com.tonapps.tonkeeper.api.fee
import com.tonapps.tonkeeper.api.getNameOrAddress
import com.tonapps.tonkeeper.api.getWalletAddress
import com.tonapps.tonkeeper.api.iconURL
import com.tonapps.tonkeeper.api.jettonPreview
import com.tonapps.tonkeeper.api.parsedAmount
import com.tonapps.tonkeeper.api.refund
import com.tonapps.tonkeeper.api.shortAddress
import com.tonapps.tonkeeper.api.title
import com.tonapps.tonkeeper.api.ton
import com.tonapps.tonkeeper.core.history.list.item.HistoryItem
import io.tonapi.models.AccountAddress
import io.tonapi.models.AccountEvent
import io.tonapi.models.Action
import io.tonapi.models.ActionSimplePreview
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import com.tonapps.tonkeeper.api.totalFees
import com.tonapps.tonkeeper.extensions.with
import com.tonapps.tonkeeper.helper.DateHelper
import com.tonapps.tonkeeper.ui.screen.dialog.encrypted.EncryptedCommentScreen
Expand All @@ -50,25 +43,16 @@ import com.tonapps.wallet.data.rates.RatesRepository
import com.tonapps.wallet.data.rates.entity.RatesEntity
import com.tonapps.wallet.data.settings.SettingsRepository
import com.tonapps.wallet.localization.Localization
import io.tonapi.models.Account
import io.tonapi.models.JettonVerificationType
import io.tonapi.models.MessageConsequences
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.take
import kotlinx.datetime.Clock
import kotlinx.datetime.DateTimeUnit
import kotlinx.datetime.Instant
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toJavaLocalDate
import kotlinx.datetime.toLocalDateTime
import kotlinx.datetime.until
import java.text.SimpleDateFormat
import java.time.format.DateTimeFormatterBuilder
import java.util.Calendar
import java.util.Date
import java.util.Locale
import kotlin.math.abs

// TODO request refactoring
class HistoryHelper(
Expand Down Expand Up @@ -208,23 +192,32 @@ class HistoryHelper(
isBattery: Boolean = false,
): Details {
val items = mapping(wallet, response.event, true, positionExtra = 1).toMutableList()
val extra = response.event.extra

val fee = Coins.of(response.totalFees)
val fee = if (0 > extra) Coins.of(abs(extra)) else Coins.ZERO
val feeFormat = "≈ " + CurrencyFormatter.format("TON", fee)
val feeFiat = rates.convert("TON", fee)
val feeFiatFormat = CurrencyFormatter.formatFiat(rates.currency.code, feeFiat)

val refund = if (extra > 0) Coins.of(extra) else Coins.ZERO
val refundFormat = "≈ " + CurrencyFormatter.format("TON", refund)
val refundFiat = rates.convert("TON", refund)
val refundFiatFormat = CurrencyFormatter.formatFiat(rates.currency.code, refundFiat)

val isRefund = extra > 0


items.add(
HistoryItem.Event(
index = items.lastIndex + 1,
position = ListCell.Position.LAST,
txId = "fee",
iconURL = "",
action = ActionType.Fee,
action = if (isRefund) ActionType.Refund else ActionType.Fee,
title = "",
subtitle = if (isBattery) context.getString(Localization.will_be_paid_with_battery) else "",
value = feeFormat,
date = feeFiatFormat.toString(),
value = if (isRefund) refundFormat else feeFormat,
date = if (isRefund) refundFiatFormat.toString() else feeFiatFormat.toString(),
isOut = true,
sender = null,
recipient = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.tonapps.tonkeeper.koin

import com.tonapps.tonkeeper.worker.DAppPushToggleWorker
import com.tonapps.tonkeeper.worker.PushToggleWorker
import org.koin.androidx.workmanager.dsl.workerOf
import org.koin.dsl.module

val workerModule = module {
workerOf(::DAppPushToggleWorker)
workerOf(::PushToggleWorker)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MainRecyclerView @JvmOverloads constructor(
private var topOffset = 0
private var bottomOffset = 0

private val topPadding: Int
val topPadding: Int
get() = topOffset + barSize

private val bottomPadding: Int
Expand All @@ -39,7 +39,6 @@ class MainRecyclerView @JvmOverloads constructor(
if (bottomBlur?.hasBlur == true) {
overScrollMode = OVER_SCROLL_NEVER
}
context.isLowDevice
}

override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.tonapps.tonkeeper.ui.component

import android.content.Context
import android.util.AttributeSet
import androidx.core.view.doOnLayout
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.tonapps.uikit.color.accentBlueColor
import uikit.extensions.dp
import uikit.extensions.findViewByClass

class MainSwipeRefreshLayout @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null
) : SwipeRefreshLayout(context, attrs) {

private val recyclerView: MainRecyclerView? by lazy { findViewByClass() }

private val topPadding: Int
get() = recyclerView?.topPadding ?: 0

init {
setColorSchemeColors(context.accentBlueColor)
}

override fun setRefreshing(refreshing: Boolean) {
if (refreshing) {
doOnLayout { postOnAnimation { super.setRefreshing(true) } }
} else {
super.setRefreshing(false)
}
}

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
setProgressViewOffset(true, topPadding, topPadding + 42.dp)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.view.View
import androidx.core.view.updatePadding
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.tonapps.tonkeeper.koin.walletViewModel
import com.tonapps.tonkeeper.ui.base.UiListState
import com.tonapps.tonkeeper.ui.screen.collectibles.list.Adapter
Expand All @@ -30,6 +31,7 @@ class CollectiblesScreen(wallet: WalletEntity): MainScreen.Child(R.layout.fragme
private val adapter = Adapter()

private lateinit var headerView: HeaderView
private lateinit var refreshView: SwipeRefreshLayout
private lateinit var listView: RecyclerView
private lateinit var emptyView: EmptyLayout

Expand All @@ -39,6 +41,9 @@ class CollectiblesScreen(wallet: WalletEntity): MainScreen.Child(R.layout.fragme
headerView.title = getString(Localization.collectibles)
headerView.setColor(requireContext().backgroundTransparentColor)

refreshView = view.findViewById(R.id.refresh)
refreshView.setOnRefreshListener { viewModel.refresh() }

listView = view.findViewById(R.id.list)
listView.updatePadding(top = 0)
listView.layoutManager = object : GridLayoutManager(context, 3) {
Expand All @@ -54,13 +59,16 @@ class CollectiblesScreen(wallet: WalletEntity): MainScreen.Child(R.layout.fragme
adapter.applySkeleton()
headerView.setSubtitle(Localization.updating)
} else if (state is UiListState.Empty) {
refreshView.isRefreshing = false
setEmptyState()
headerView.setSubtitle(null)
} else if (state is UiListState.Items) {
setListState()
adapter.submitList(state.items)
if (!state.cache) {
headerView.setSubtitle(null)
adapter.submitList(state.items) {
if (!state.cache) {
headerView.setSubtitle(null)
refreshView.isRefreshing = false
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import com.tonapps.wallet.data.settings.SettingsRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.cancellable
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.emitAll
Expand All @@ -37,7 +39,8 @@ class CollectiblesViewModel(
private val transactionManager: TransactionManager,
): BaseWalletVM(app) {

private val ltFlow = transactionManager.eventsFlow(wallet).stateIn(viewModelScope, SharingStarted.Eagerly, 0L)
private val _ltFlow = MutableStateFlow(0L)
private val ltFlow = _ltFlow.asStateFlow()

val uiListStateFlow = combine(
networkMonitor.isOnlineFlow,
Expand All @@ -52,6 +55,12 @@ class CollectiblesViewModel(
)
}.stateIn(viewModelScope, SharingStarted.Eagerly, null).filterNotNull().flattenFirst()

init {
transactionManager.eventsFlow(wallet).collectFlow {
_ltFlow.value = it.lt
}
}

private fun stateFlow(
wallet: WalletEntity,
hiddenBalances: Boolean,
Expand Down Expand Up @@ -92,4 +101,8 @@ class CollectiblesViewModel(
}
}.flowOn(Dispatchers.IO)

fun refresh() {
_ltFlow.value += 1
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.tonapps.tonkeeper.core.history.list.HistoryAdapter
import com.tonapps.tonkeeper.core.history.list.HistoryItemDecoration
import com.tonapps.tonkeeper.core.history.list.item.HistoryItem
Expand Down Expand Up @@ -40,6 +41,7 @@ class EventsScreen(wallet: WalletEntity) : MainScreen.Child(R.layout.fragment_ma

private lateinit var headerView: HeaderView
private lateinit var containerView: View
private lateinit var refreshView: SwipeRefreshLayout
private lateinit var listView: RecyclerView
private lateinit var filtersView: RecyclerView
private lateinit var emptyView: EmptyLayout
Expand All @@ -52,6 +54,11 @@ class EventsScreen(wallet: WalletEntity) : MainScreen.Child(R.layout.fragment_ma
headerView.setColor(requireContext().backgroundTransparentColor)

containerView = view.findViewById(R.id.container)
refreshView = view.findViewById(R.id.refresh)
refreshView.setOnRefreshListener {
viewModel.refresh()
}

filtersView = view.findViewById(R.id.filters)

listView = view.findViewById(R.id.list)
Expand All @@ -74,19 +81,31 @@ class EventsScreen(wallet: WalletEntity) : MainScreen.Child(R.layout.fragment_ma
private suspend fun applyState(state: EventsUiState) = withContext(Dispatchers.Main) {
if (state.isEmpty) {
setEmptyState()
setLoading(false)
} else {
setListState()
if (state.isLoading) {
headerView.setSubtitle(Localization.updating)
setLoading(true)
}
legacyAdapter.submitList(state.items) {
if (!state.isLoading) {
headerView.setSubtitle(null)
setLoading(false)
}
}
}
}

private fun setLoading(loading: Boolean) {
if (refreshView.isRefreshing) {
refreshView.isRefreshing = loading
}
if (loading) {
headerView.setSubtitle(Localization.updating)
} else {
headerView.setSubtitle(null)
}
}

override fun scrollUp() {
super.scrollUp()
listView.scrollToPosition(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.time.Duration.Companion.seconds

// TODO: Refactor this class
class EventsViewModel(
Expand Down Expand Up @@ -61,16 +62,16 @@ class EventsViewModel(
}

transactionManager.eventsFlow(wallet).collectFlow { event ->
if (event.pending) {
/*if (event.pending) {
appendEvent(AccountEventWrap(event.body))
}
}*/
refresh()
}

autoRefreshJob = viewModelScope.launch(Dispatchers.IO) {
while (true) {
checkAutoRefresh()
delay(25000)
delay(35.seconds)
}
}
}
Expand Down Expand Up @@ -124,7 +125,7 @@ class EventsViewModel(
private fun setUiItems(uiItems: List<HistoryItem>) {
val loading = isLoading.get()
_uiStateFlow.value = EventsUiState(
items = if (loading) {
items = if (loading && uiItems.isNotEmpty()) {
historyHelper.withLoadingItem(uiItems)
} else {
uiItems
Expand Down
Loading

0 comments on commit f732194

Please sign in to comment.