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

AND-66 Notification bugfix #126

Merged
merged 6 commits into from
Sep 23, 2024
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
- Concordex exchange and Wert service where CCD can be purchased
- Ability to reveal the wallet private key for those
having no ability to reveal the seed phrase
- Ability to use Wallet private key to restore a wallet
- Ability to use the wallet private key to restore the wallet

### Fixed

- Inability to configure a validator closed for delegation
- Incorrect state of the account tokens page when there are no tokens

## [1.2.0] - 2024-08-27

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class FcmNotificationsService : FirebaseMessagingService() {
val existingContractToken = contractTokensRepository.find(
accountAddress = recipientAccountAddress,
contractIndex = contractAddress.index.toString(),
tokenId = tokenId,
token = tokenId,
)
val token: Token

Expand All @@ -206,7 +206,7 @@ class FcmNotificationsService : FirebaseMessagingService() {
ContractToken(
id = 0,
isNewlyReceived = true,
tokenId = tokenId,
token = tokenId,
accountAddress = recipientAccountAddress,
isFungible = !isTokenUnique,
contractIndex = contractAddress.index.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ class TransactionNotificationsManager(
Intent(context, AccountDetailsActivity::class.java)
.putExtra(AccountDetailsActivity.EXTRA_ACCOUNT, account)
.putExtra(AccountDetailsActivity.EXTRA_OPEN_TOKENS, true)
.apply {
// In case the token is already in the wallet,
// open its details page.
if (!token.isNewlyReceived) {
putExtra(AccountDetailsActivity.EXTRA_TOKEN_TO_OPEN_UID, token.uid)
}
}
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ class ContractTokensRepository(private val contractTokenDao: ContractTokenDao) {
suspend fun find(
accountAddress: String,
contractIndex: String,
tokenId: String
token: String
): ContractToken? {
return contractTokenDao.find(accountAddress, contractIndex, tokenId)
return contractTokenDao.find(accountAddress, contractIndex, token)
}

suspend fun delete(accountAddress: String, contractIndex: String, tokenId: String) {
contractTokenDao.delete(accountAddress, contractIndex, tokenId)
suspend fun delete(accountAddress: String, contractIndex: String, token: String) {
contractTokenDao.delete(accountAddress, contractIndex, token)
}

suspend fun unmarkNewlyReceived(accountAddress: String, contractIndex: String, tokenId: String) {
contractTokenDao.unmarkNewlyReceived(accountAddress, contractIndex, tokenId)
suspend fun unmarkNewlyReceived(tokenUid: String) {
contractTokenDao.unmarkNewlyReceived(tokenUid)
}
}
27 changes: 19 additions & 8 deletions app/src/main/java/com/concordium/wallet/data/model/Token.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@ package com.concordium.wallet.data.model

import com.concordium.wallet.data.room.Account
import com.concordium.wallet.data.room.ContractToken
import com.fasterxml.jackson.annotation.JsonProperty
import java.io.Serializable
import java.math.BigInteger

// The class must have default value for each field,
// otherwise Gson fails to use defaults and sets not-nullable fields to null.
data class Token(
val id: String = "",
/**
* A unique identifier of this token.
* Either a wallet-proxy id or a local [ContractToken.id] from the local DB.
* **wallet-proxy "tokenId" is [token]**
*/
@JsonProperty("id")
val uid: String = "",
/**
* An identifier of this token **within its contract**,
* either an empty string or a hex counter.
*/
var token: String = "",
var metadata: TokenMetadata? = null,
var contractIndex: String = "",
Expand All @@ -21,25 +32,25 @@ data class Token(

val symbol: String
get() = metadata?.symbol ?: ""

val name: String?
get() = metadata?.name

val decimals: Int
get() = metadata?.decimals ?: 0

val isUnique: Boolean
get() = metadata?.unique == true

val isCcd: Boolean
get() = id == "CCD"
get() = uid == "CCD"

constructor(
contractToken: ContractToken,
isSelected: Boolean = false,
) : this(
id = contractToken.tokenId,
token = contractToken.tokenId,
uid = contractToken.id.toString(),
token = contractToken.token,
metadata = contractToken.tokenMetadata,
contractIndex = contractToken.contractIndex,
contractName = contractToken.contractName,
Expand All @@ -57,7 +68,7 @@ data class Token(
val atDisposal = account.getAtDisposalWithoutStakedOrScheduled(totalUnshieldedBalance)

return Token(
id = "CCD",
uid = "CCD",
metadata = TokenMetadata(
symbol = "CCD",
decimals = 6,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ data class ContractToken(
@ColumnInfo(name = "contract_name", defaultValue = "")
val contractName: String,
@ColumnInfo(name = "token_id")
val tokenId: String,
val token: String,
@ColumnInfo(name = "account_address")
val accountAddress: String?,
@ColumnInfo(name = "is_fungible")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ interface ContractTokenDao {
@Update(onConflict = OnConflictStrategy.REPLACE)
suspend fun update(vararg contractToken: ContractToken)

@Query("DELETE FROM contract_token_table WHERE contract_index = :contractIndex AND token_id = :tokenId AND account_address = :accountAddress")
suspend fun delete(accountAddress: String, contractIndex: String, tokenId: String)
@Query("DELETE FROM contract_token_table WHERE contract_index = :contractIndex AND token_id = :token AND account_address = :accountAddress")
suspend fun delete(accountAddress: String, contractIndex: String, token: String)

@Query("SELECT * FROM contract_token_table WHERE contract_index = :contractIndex AND token_id = :tokenId AND account_address = :accountAddress")
suspend fun find(accountAddress: String, contractIndex: String, tokenId: String): ContractToken?
@Query("SELECT * FROM contract_token_table WHERE contract_index = :contractIndex AND token_id = :token AND account_address = :accountAddress")
suspend fun find(accountAddress: String, contractIndex: String, token: String): ContractToken?

@Query("UPDATE contract_token_table SET is_newly_received = 0 WHERE contract_index = :contractIndex AND token_id = :tokenId AND account_address = :accountAddress")
suspend fun unmarkNewlyReceived(accountAddress: String, contractIndex: String, tokenId: String)
@Query("UPDATE contract_token_table SET is_newly_received = 0 WHERE id = :tokenUid")
suspend fun unmarkNewlyReceived(tokenUid: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.concordium.wallet.R
import com.concordium.wallet.core.arch.EventObserver
Expand Down Expand Up @@ -45,9 +46,13 @@ class AccountDetailsActivity : BaseActivity(
private val shouldOpenTokens: Boolean by lazy {
intent.getBooleanExtra(EXTRA_OPEN_TOKENS, false)
}
private val tokenToOpenUid: String? by lazy {
intent.getStringExtra(EXTRA_TOKEN_TO_OPEN_UID)
}

companion object {
const val EXTRA_ACCOUNT = "EXTRA_ACCOUNT"
const val EXTRA_TOKEN_TO_OPEN_UID = "EXTRA_TOKEN_TO_OPEN_UID"
const val EXTRA_OPEN_TOKENS = "EXTRA_OPEN_TOKENS"
const val RESULT_RETRY_ACCOUNT_CREATION = 2
}
Expand Down Expand Up @@ -129,6 +134,18 @@ class AccountDetailsActivity : BaseActivity(
viewModelTokens.chooseToken.observe(this) { token ->
showTokenDetailsDialog(token)
}

viewModelTokens.tokenBalances.observe(this, object : Observer<Boolean> {
override fun onChanged(t: Boolean) {
// Open the requested token once, when balances are loaded.
if (tokenToOpenUid != null) {
viewModelTokens.tokenBalances.removeObserver(this)
viewModelTokens.tokens
.find { it.uid == tokenToOpenUid }
?.also(viewModelTokens.chooseToken::postValue)
}
}
})
}

private fun initViews() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,10 @@ class TokenDetailsActivity : BaseActivity(R.layout.activity_token_details) {
}

private fun handleNewlyReceivedToken(token: Token) {
viewModel.unmarkNewlyReceivedSelectedToken()
showNewlyReceivedNotice(
tokenName = token.name ?: token.symbol
)
viewModel.unmarkNewlyReceivedToken(
viewModel.tokenData.account!!.address,
viewModel.tokenData.selectedToken!!.contractIndex,
viewModel.tokenData.selectedToken!!.id
)
setResult(
Activity.RESULT_OK,
Intent().putExtra(CHANGED, true)
Expand All @@ -124,11 +120,7 @@ class TokenDetailsActivity : BaseActivity(R.layout.activity_token_details) {
builder.setMessage(getString(R.string.cis_delete_dialog_content))
builder.setPositiveButton(getString(R.string.cis_delete_dialog_confirm)) { dialog, _ ->
dialog.dismiss()
viewModel.deleteSingleToken(
viewModel.tokenData.account!!.address,
viewModel.tokenData.selectedToken!!.contractIndex,
viewModel.tokenData.selectedToken!!.id
)
viewModel.deleteSelectedToken()
setResult(
Activity.RESULT_OK,
Intent().putExtra(CHANGED, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,23 @@ class TokensAccountDetailsAdapter(

@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
if (position == itemCount - 1 && showManageButton) {
holder.binding.manageTokens.visibility = View.VISIBLE
holder.binding.content.visibility = View.GONE
return
} else {
holder.binding.manageTokens.visibility = View.GONE
holder.binding.content.visibility = View.VISIBLE
when {
dataSet.isEmpty() -> {
holder.binding.manageTokens.visibility = View.GONE
holder.binding.content.visibility = View.GONE
return
}

(position == itemCount - 1 && showManageButton) -> {
holder.binding.manageTokens.visibility = View.VISIBLE
holder.binding.content.visibility = View.GONE
return
}

else -> {
holder.binding.manageTokens.visibility = View.GONE
holder.binding.content.visibility = View.VISIBLE
}
}

val token = dataSet[position]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,14 @@ class TokensFragment : Fragment() {
tokensAccountDetailsAdapter.setManageButtonClickListener {
showFindTokensDialog()
}
binding.noItemsManageTokens.setOnClickListener {
showFindTokensDialog()
}
}

private fun initObservers() {
viewModel.waiting.observe(viewLifecycleOwner) {
binding.noItems.isVisible = viewModel.tokens.isEmpty()
binding.noItemsLayout.isVisible = viewModel.tokens.isEmpty()
tokensAccountDetailsAdapter.dataSet = viewModel.tokens.toTypedArray()
tokensAccountDetailsAdapter.notifyDataSetChanged()
viewModel.loadTokensBalances()
Expand Down
34 changes: 19 additions & 15 deletions app/src/main/java/com/concordium/wallet/ui/cis2/TokensViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class TokensViewModel(application: Application) : AndroidViewModel(application)
val existingContractTokens =
contractTokensRepository.getTokens(accountAddress, tokenData.contractIndex)
val selectedTokenIds =
existingContractTokens.mapTo(mutableSetOf(), ContractToken::tokenId) +
existingContractTokens.mapTo(mutableSetOf(), ContractToken::token) +
everFoundExactTokens.asSequence()
.filter(Token::isSelected)
.mapTo(mutableSetOf(), Token::token)
Expand Down Expand Up @@ -173,7 +173,7 @@ class TokensViewModel(application: Application) : AndroidViewModel(application)
}

isLastTokenPage = pageTokens.size < limit
tokenPageCursor = pageTokens.lastOrNull()?.id
tokenPageCursor = pageTokens.lastOrNull()?.uid

loadTokensMetadata(pageTokens)
val tokensWithMetadata = pageTokens.filter { it.metadata != null }
Expand Down Expand Up @@ -310,7 +310,7 @@ class TokensViewModel(application: Application) : AndroidViewModel(application)
val existingContractTokens =
contractTokensRepository.getTokens(accountAddress, tokenData.contractIndex)
val selectedTokenIds =
existingContractTokens.mapTo(mutableSetOf(), ContractToken::tokenId) +
existingContractTokens.mapTo(mutableSetOf(), ContractToken::token) +
(tokens.asSequence() + everFoundExactTokens.asSequence())
.filter(Token::isSelected)
.mapTo(mutableSetOf(), Token::token)
Expand Down Expand Up @@ -402,7 +402,7 @@ class TokensViewModel(application: Application) : AndroidViewModel(application)
contractTokensRepository.insert(
ContractToken(
id = 0,
tokenId = selectedToken.token,
token = selectedToken.token,
contractIndex = selectedToken.contractIndex,
contractName = selectedToken.contractName,
accountAddress = account.address,
Expand All @@ -420,16 +420,16 @@ class TokensViewModel(application: Application) : AndroidViewModel(application)
// we must only delete the unselected ones among loaded.
// Therefore, if there is an existing token but the user haven't scrolled to it
// in the search results, it won't be deleted.
val loadedNotSelectedTokenIds = loadedTokens
val loadedNotSelectedTokens = loadedTokens
.filterNot(Token::isSelected)
.mapTo(mutableSetOf(), Token::token)

// Delete each loaded not selected token.
loadedNotSelectedTokenIds.forEach { loadedNotSelectedTokenId ->
loadedNotSelectedTokens.forEach { loadedNotSelectedToken ->
contractTokensRepository.delete(
accountAddress = account.address,
contractIndex = tokenData.contractIndex,
tokenId = loadedNotSelectedTokenId
token = loadedNotSelectedToken,
)
anyChanges = true
}
Expand Down Expand Up @@ -468,15 +468,19 @@ class TokensViewModel(application: Application) : AndroidViewModel(application)
}
}

fun deleteSingleToken(accountAddress: String, contractIndex: String, tokenId: String) =
viewModelScope.launch(Dispatchers.IO) {
contractTokensRepository.delete(accountAddress, contractIndex, tokenId)
}
fun deleteSelectedToken() = viewModelScope.launch {
contractTokensRepository.delete(
accountAddress = tokenData.account!!.address,
contractIndex = tokenData.selectedToken!!.contractIndex,
token = tokenData.selectedToken!!.token,
)
}

fun unmarkNewlyReceivedToken(accountAddress: String, contractIndex: String, tokenId: String) =
viewModelScope.launch {
contractTokensRepository.unmarkNewlyReceived(accountAddress, contractIndex, tokenId)
}
fun unmarkNewlyReceivedSelectedToken() = viewModelScope.launch {
contractTokensRepository.unmarkNewlyReceived(
tokenUid = tokenData.selectedToken!!.uid,
)
}

fun onFindTokensDialogDismissed() {
resetLookForTokens()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import com.concordium.wallet.data.room.ContractToken
class DefaultFungibleToken(
val symbol: String,
val contractIndex: String,
val tokenId: String,
val contractName: String,
val token: String,
val name: String,
val description: String,
val thumbnailUrl: String?,
Expand All @@ -17,7 +17,7 @@ class DefaultFungibleToken(
fun toContractToken(accountAddress: String) = ContractToken(
id = 0,
contractIndex = contractIndex,
tokenId = tokenId,
token = token,
accountAddress = accountAddress,
isFungible = true,
contractName = contractName,
Expand Down
Loading