Skip to content

Commit

Permalink
bug fixeds
Browse files Browse the repository at this point in the history
  • Loading branch information
polstianka committed Nov 21, 2024
1 parent ab24ffa commit 21e75e9
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ class AccountRepository(
}
}

suspend fun setInitialized(id: String, initialized: Boolean) {
try {
database.setInitialized(id, initialized)
} catch (e: Throwable) {
recordException(e)
}
}

suspend fun importPrivateKeysFromRNLegacy(passcode: String): Boolean = withContext(Dispatchers.IO) {
val vaultState = migrationHelper.loadSecureStore(passcode)
if (vaultState.hasError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import android.database.sqlite.SQLiteOpenHelper
import android.util.Log
import androidx.core.database.getStringOrNull
import com.tonapps.blockchain.ton.contract.walletVersion
import com.tonapps.extensions.isNullOrEmpty
import com.tonapps.extensions.toByteArray
import com.tonapps.extensions.toParcel
import com.tonapps.sqlite.withTransaction
import com.tonapps.wallet.data.account.Wallet
import com.tonapps.wallet.data.account.entities.WalletEntity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.ton.api.pub.PublicKeyEd25519

Expand Down Expand Up @@ -112,6 +114,15 @@ internal class DatabaseSource(
writableDatabase.delete(WALLET_TABLE_NAME, null, null)
}

suspend fun setInitialized(id: String, initialized: Boolean) = withContext(scope.coroutineContext) {
val values = ContentValues()
values.put(WALLET_TABLE_INITIALIZED_COLUMN, if (initialized) 1 else 0)
val count = writableDatabase.update(WALLET_TABLE_NAME, values, "$WALLET_TABLE_ID_COLUMN = ?", arrayOf(id))
if (count == 0) {
throw IllegalStateException("Account with id $id not found")
}
}

suspend fun deleteAccount(id: String) = withContext(scope.coroutineContext) {
val count = writableDatabase.delete(WALLET_TABLE_NAME, "$WALLET_TABLE_ID_COLUMN = ?", arrayOf(id))
if (count == 0) {
Expand Down Expand Up @@ -139,14 +150,22 @@ internal class DatabaseSource(
suspend fun getAccounts(): List<WalletEntity> = withContext(scope.coroutineContext) {
val query = "SELECT $walletFields FROM $WALLET_TABLE_NAME LIMIT 1000;"
val cursor = readableDatabase.rawQuery(query, null)
readAccounts(cursor)
if (cursor.isNullOrEmpty()) {
emptyList()
} else {
readAccounts(cursor)
}
}

suspend fun getAccount(id: String): WalletEntity? = withContext(scope.coroutineContext) {
if (id.isNotBlank()) {
val query = "SELECT $walletFields FROM $WALLET_TABLE_NAME WHERE $WALLET_TABLE_ID_COLUMN = ?;"
val cursor = readableDatabase.rawQuery(query, arrayOf(id))
readAccounts(cursor).firstOrNull()
if (cursor.isNullOrEmpty()) {
null
} else {
readAccounts(cursor).firstOrNull()
}
} else {
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ suspend fun <T> BillingClient.ready(block: suspend (client: BillingClient) -> T)
private suspend fun BillingClient.getReady(): BillingClient = suspendCancellableCoroutine { continuation ->
startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(result: BillingResult) {
if (!continuation.isActive) return
if (result.isSuccess) {
if (!continuation.isActive) {
return
} else if (result.isSuccess) {
continuation.resume(this@getReady)
} else {
continuation.resumeWithException(ErrorForUserException.of(result.debugMessage))
val message = result?.debugMessage ?: "unknown error"
continuation.resumeWithException(ErrorForUserException.of(message))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.tonapps.icu.Coins.Companion.sumOf
import com.tonapps.tonkeeper.core.entities.AssetsEntity
import com.tonapps.tonkeeper.core.entities.AssetsEntity.Companion.sort
import com.tonapps.tonkeeper.core.entities.StakedEntity
import com.tonapps.wallet.data.account.AccountRepository
import com.tonapps.wallet.data.account.entities.WalletEntity
import com.tonapps.wallet.data.core.WalletCurrency
import com.tonapps.wallet.data.rates.RatesRepository
Expand All @@ -15,15 +16,18 @@ import com.tonapps.wallet.data.staking.entities.StakingEntity
import com.tonapps.wallet.data.token.TokenRepository
import com.tonapps.wallet.data.token.entities.AccountTokenEntity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.withContext

class AssetsManager(
private val scope: CoroutineScope,
private val ratesRepository: RatesRepository,
private val tokenRepository: TokenRepository,
private val stakingRepository: StakingRepository,
private val settingsRepository: SettingsRepository,
private val accountRepository: AccountRepository,
) {

private val cache = TotalBalanceCache()
Expand Down Expand Up @@ -53,9 +57,14 @@ class AssetsManager(
wallet: WalletEntity,
currency: WalletCurrency = settingsRepository.currency,
refresh: Boolean,
): List<AssetsEntity.Token> {
): List<AssetsEntity.Token> {
val safeMode = settingsRepository.isSafeModeEnabled()
val tokens = tokenRepository.get(currency, wallet.accountId, wallet.testnet, refresh) ?: return emptyList()
tokens.firstOrNull()?.let {
if (wallet.initialized != it.balance.initializedAccount) {
accountRepository.setInitialized(wallet.accountId, it.balance.initializedAccount)
}
}
return if (safeMode) {
tokens.filter { it.verified }.map { AssetsEntity.Token(it) }
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,31 @@ package com.tonapps.tonkeeper.ui.base

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentFactory
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.tonapps.wallet.data.account.entities.WalletEntity

class WalletFragmentFactory: FragmentFactory() {

override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
val fragmentClass = loadFragmentClass(classLoader, className)
val constructors = fragmentClass.constructors
/*if (constructors.size > 1) {
throw IllegalStateException("$className class should have only one constructor")
}*/
val constructor = constructors.first()
val parameters = constructor.parameterTypes
/*if (parameters.size > 1) {
throw IllegalStateException("$className class should have only one constructor with one parameter")
} else if (parameters.isEmpty()) {
return fragmentClass.getConstructor().newInstance()
}*/
if (parameters.isEmpty()) {
return fragmentClass.getConstructor().newInstance()
}
val parameter = parameters.first()
if (parameter == WalletEntity::class.java) {
return fragmentClass.getDeclaredConstructor(WalletEntity::class.java).newInstance(WalletEntity.EMPTY)
try {
val fragmentClass = loadFragmentClass(classLoader, className)
val constructors = fragmentClass.constructors
val constructor = constructors.first()
val parameters = constructor.parameterTypes
if (parameters.isEmpty()) {
try {
return fragmentClass.getConstructor().newInstance()
} catch (e: Throwable) {
val walletConstructor = fragmentClass.getDeclaredConstructor(WalletEntity::class.java)
return walletConstructor.newInstance(WalletEntity.EMPTY)
}
}
val parameter = parameters.first()
if (parameter == WalletEntity::class.java) {
return fragmentClass.getDeclaredConstructor(WalletEntity::class.java).newInstance(WalletEntity.EMPTY)
}
} catch (e: Throwable) {
FirebaseCrashlytics.getInstance().recordException(e)
}
return super.instantiate(classLoader, className)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,14 @@ class RootActivity: BaseWalletActivity() {
}

override fun attachBaseContext(newBase: Context) {
super.attachBaseContext(newBase)
if (DevSettings.ignoreSystemFontSize) {
val newConfig = Configuration(newBase.resources.configuration)
newConfig.fontScale = 1.0f
if (newConfig.fontScale >= 1.0f) {
newConfig.fontScale = .9f
}
applyOverrideConfiguration(newConfig)
}
super.attachBaseContext(newBase)
}

override fun onResume() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ class RootViewModel(
crashlytics.setCustomKeys {
key("testnet", wallet.testnet)
key("walletType", wallet.type.name)
key("wallet", wallet.address)
key("installId", settingsRepository.installId)
}
}

Expand Down
5 changes: 5 additions & 0 deletions lib/extensions/src/main/java/com/tonapps/extensions/Cursor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tonapps.extensions

import android.database.Cursor

fun Cursor?.isNullOrEmpty(): Boolean = this == null || this.count == 0

0 comments on commit 21e75e9

Please sign in to comment.