Skip to content

Commit

Permalink
TokenInformationDialogFragment refactor more logic into common-jvm, a…
Browse files Browse the repository at this point in the history
…dd copy nft hash #9
  • Loading branch information
MrStahlfelge committed Mar 1, 2022
1 parent 71bfd25 commit a6e29db
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ import androidx.fragment.app.viewModels
import androidx.navigation.fragment.navArgs
import androidx.transition.TransitionManager
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import org.ergoplatform.TokenAmount
import org.ergoplatform.WalletStateSyncManager
import org.ergoplatform.android.Preferences
import org.ergoplatform.android.R
import org.ergoplatform.android.databinding.FragmentTokenInformationBinding
import org.ergoplatform.android.ui.AndroidStringProvider
import org.ergoplatform.android.ui.copyStringToClipboard
import org.ergoplatform.android.ui.openUrlWithBrowser
import org.ergoplatform.getExplorerTokenUrl
import org.ergoplatform.getExplorerTxUrl
import org.ergoplatform.tokens.isSingularToken
import org.ergoplatform.utils.formatTokenPriceToString
import org.ergoplatform.tokens.getHttpContentLink

class TokenInformationDialogFragment : BottomSheetDialogFragment() {
private var _binding: FragmentTokenInformationBinding? = null
Expand Down Expand Up @@ -49,59 +46,15 @@ class TokenInformationDialogFragment : BottomSheetDialogFragment() {
binding.progressCircular.visibility = View.GONE
token?.apply {
binding.mainLayout.visibility = View.VISIBLE
binding.labelTokenName.text =
if (displayName.isBlank()) getString(R.string.label_unnamed_token) else displayName

binding.labelTokenName.setCompoundDrawablesRelativeWithIntrinsicBounds(
0, 0, getGenuineDrawableId(), 0
)

binding.labelTokenId.text = tokenId
binding.labelTokenDescription.text =
if (description.isNotBlank()) description else getString(R.string.label_no_description)
binding.labelSupplyAmount.text =
TokenAmount(fullSupply, decimals).toStringUsFormatted(false)
val balanceAmount = TokenAmount(args.amount, decimals)
binding.labelBalanceAmount.text =
balanceAmount.toStringUsFormatted(false)

val showBalance = args.amount > 0 && !isSingularToken()
val walletSyncManager = WalletStateSyncManager.getInstance()
val tokenPrice = walletSyncManager.tokenPrices[tokenId]

if (showBalance) tokenPrice?.let {
binding.labelBalanceValue.text = formatTokenPriceToString(
balanceAmount,
it.ergValue,
walletSyncManager,
AndroidStringProvider(requireContext())
) + " [${it.priceSource}]"
}

binding.labelBalanceAmount.visibility = if (showBalance) View.VISIBLE else View.GONE
binding.titleBalanceAmount.visibility = binding.labelBalanceAmount.visibility
binding.labelBalanceValue.visibility =
if (showBalance && tokenPrice != null) View.VISIBLE else View.GONE
binding.labelSupplyAmount.visibility =
if (isSingularToken()) View.GONE else View.VISIBLE
binding.titleSupplyAmount.visibility = binding.labelSupplyAmount.visibility

updateLayout(viewModel)
binding.labelMintingTxId.setOnClickListener {
openUrlWithBrowser(requireContext(), getExplorerTxUrl(mintingTxId))
}

updateNftLayout(viewModel)

binding.buttonDownloadContent.setOnClickListener {
viewModel.uiLogic.downloadContent(
Preferences(requireContext())
)
}
} ?: run { binding.tvError.visibility = View.VISIBLE }
}

viewModel.downloadState.observe(viewLifecycleOwner) {
updateNftLayout(viewModel, true)
updateLayout(viewModel, true)
}

binding.labelTokenId.setOnClickListener {
Expand All @@ -116,22 +69,41 @@ class TokenInformationDialogFragment : BottomSheetDialogFragment() {
binding.labelTokenDescription.maxLines =
if (binding.labelTokenDescription.maxLines == 5) 1000 else 5
}
binding.buttonDownloadContent.setOnClickListener {
viewModel.uiLogic.downloadContent(
Preferences(requireContext())
)
}
binding.labelContentLink.setOnClickListener {
viewModel.uiLogic.eip4Token?.nftContentLink?.let { contentLink ->
val context = requireContext()
val success = openUrlWithBrowser(context, contentLink)

if (!success) {
viewModel.uiLogic.eip4Token!!.getHttpContentLink(Preferences(requireContext()))
?.let { openUrlWithBrowser(context, it) }
}
}
}
binding.labelContentHash.setOnClickListener {
copyStringToClipboard(binding.labelContentHash.text.toString(), requireContext(), null)
}
}

private fun updateNftLayout(
private fun updateLayout(
viewModel: TokenInformationViewModel,
onlyPreview: Boolean = false
) {
val context = requireContext()
val tokenInformationNftLayoutView = TokenInformationNftLayoutView(binding)
val tokenInformationLayoutView = TokenInformationLayoutView(binding)
if (onlyPreview) {
tokenInformationNftLayoutView.updatePreview(viewModel.uiLogic)
tokenInformationLayoutView.updateNftPreview(viewModel.uiLogic)
} else {
tokenInformationNftLayoutView.update(
tokenInformationLayoutView.updateLayout(
viewModel.uiLogic,
AndroidStringProvider(context),
Preferences(context)
) { openUrlWithBrowser(context, it) }
args.amount
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.ergoplatform.android.tokens

import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.view.View
import org.ergoplatform.android.R
import org.ergoplatform.android.databinding.FragmentTokenInformationBinding
import org.ergoplatform.uilogic.tokens.TokenInformationLayoutLogic
import org.ergoplatform.uilogic.tokens.TokenInformationModelLogic


class TokenInformationLayoutView(private val binding: FragmentTokenInformationBinding) :
TokenInformationLayoutLogic() {

override fun setTokenTextFields(displayName: String, tokenId: String, description: String) {
binding.labelTokenName.text = displayName
binding.labelTokenId.text = tokenId
binding.labelTokenDescription.text = description
}

override fun setLabelSupplyAmountText(supplyAmount: String?) {
binding.labelSupplyAmount.visibility =
if (supplyAmount == null) View.GONE else View.VISIBLE
binding.labelSupplyAmount.text = supplyAmount
binding.titleSupplyAmount.visibility = binding.labelSupplyAmount.visibility
}

override fun setBalanceAmountAndValue(amount: String?, balanceValue: String?) {
binding.labelBalanceAmount.text = amount
binding.labelBalanceAmount.visibility = if (amount != null) View.VISIBLE else View.GONE
binding.titleBalanceAmount.visibility = binding.labelBalanceAmount.visibility
binding.labelBalanceValue.visibility =
if (balanceValue != null) View.VISIBLE else View.GONE
binding.labelBalanceValue.text = balanceValue

}

override fun setTokenGenuine(genuineFlag: Int) {
binding.labelTokenName.setCompoundDrawablesRelativeWithIntrinsicBounds(
0, 0, getGenuineDrawableId(genuineFlag), 0
)
}

override fun setNftLayoutVisibility(visible: Boolean) {
binding.layoutNft.visibility = if (visible) View.VISIBLE else View.GONE
}

override fun setContentLinkText(linkText: String) {
binding.labelContentLink.text = linkText
}

override fun setContentHashText(hashText: String) {
binding.labelContentHash.text = hashText
}

override fun setThumbnail(thumbnailType: Int) {
val thumbnailDrawable = getThumbnailDrawableId(thumbnailType)
binding.layoutThumbnail.visibility =
if (thumbnailDrawable == 0) View.GONE else View.VISIBLE
binding.imgThumbnail.setImageResource(thumbnailDrawable)
}

override fun showNftPreview(
downloadState: TokenInformationModelLogic.StateDownload,
downloadPercent: Float,
content: ByteArray?
) {
binding.layoutPreview.visibility = when (downloadState) {
TokenInformationModelLogic.StateDownload.NOT_AVAILABLE -> View.GONE
TokenInformationModelLogic.StateDownload.NOT_STARTED -> View.GONE
TokenInformationModelLogic.StateDownload.RUNNING -> View.VISIBLE
TokenInformationModelLogic.StateDownload.DONE -> View.VISIBLE
TokenInformationModelLogic.StateDownload.ERROR -> View.VISIBLE
}

binding.descDownloadContent.visibility =
if (downloadState == TokenInformationModelLogic.StateDownload.NOT_STARTED) View.VISIBLE else View.GONE
binding.buttonDownloadContent.visibility = binding.descDownloadContent.visibility
binding.previewProgress.visibility =
if (downloadState == TokenInformationModelLogic.StateDownload.RUNNING) View.VISIBLE else View.GONE

val hasError = content?.let {
try {
val image: Drawable =
BitmapDrawable(
binding.root.resources,
BitmapFactory.decodeByteArray(it, 0, it.size)
)
binding.imgPreview.setImageDrawable(image)
false
} catch (t: Throwable) {
true
}
} ?: false

if (hasError || downloadState == TokenInformationModelLogic.StateDownload.ERROR) {
binding.imgPreview.setImageResource(R.drawable.ic_warning_amber_24)
}

}

override fun showNftHashValidation(hashValid: Boolean?) {
binding.labelContentHash.setCompoundDrawablesRelativeWithIntrinsicBounds(
0, 0, when (hashValid) {
true -> R.drawable.ic_verified_18
false -> R.drawable.ic_suspicious_18
null -> 0
}, 0
)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import kotlinx.coroutines.CoroutineScope
import org.ergoplatform.android.AppDatabase
import org.ergoplatform.android.Preferences
import org.ergoplatform.persistance.TokenInformation
import org.ergoplatform.uilogic.tokens.TokenInformationUiLogic
import org.ergoplatform.uilogic.tokens.TokenInformationModelLogic

class TokenInformationViewModel : ViewModel() {
val uiLogic = AndroidUiLogic()

private var tokenId: String? = null
private val _tokenInfo = MutableLiveData<TokenInformation?>()
val tokenInfo: LiveData<TokenInformation?> = _tokenInfo
private val _downloadState = MutableLiveData<TokenInformationUiLogic.StateDownload>()
val downloadState: LiveData<TokenInformationUiLogic.StateDownload> get() = _downloadState
private val _downloadState = MutableLiveData<TokenInformationModelLogic.StateDownload>()
val downloadState: LiveData<TokenInformationModelLogic.StateDownload> get() = _downloadState

fun init(tokenId: String, ctx: Context) {
if (this.tokenId != null)
Expand All @@ -29,7 +29,7 @@ class TokenInformationViewModel : ViewModel() {
uiLogic.init(tokenId, AppDatabase.getInstance(ctx), Preferences(ctx))
}

inner class AndroidUiLogic : TokenInformationUiLogic() {
inner class AndroidUiLogic : TokenInformationModelLogic() {
override val coroutineScope: CoroutineScope
get() = viewModelScope

Expand Down
4 changes: 3 additions & 1 deletion android/src/main/res/layout/fragment_token_information.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,11 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="?selectableItemBackground"
android:drawablePadding="@dimen/small_padding"
android:ellipsize="end"
android:ellipsize="middle"
android:gravity="center"
android:maxLines="1"
app:drawableTint="@color/primary"
tools:drawableEnd="@drawable/ic_verified_18"
tools:text="hash" />
Expand Down
Loading

0 comments on commit a6e29db

Please sign in to comment.