Skip to content

Commit

Permalink
Minor UI fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
angelix committed Nov 5, 2024
1 parent 5c0e75e commit d72024e
Show file tree
Hide file tree
Showing 10 changed files with 324 additions and 272 deletions.
2 changes: 1 addition & 1 deletion common/src/commonMain/composeResources/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1563,5 +1563,5 @@
<string name="id_asset_to_sell">Asset to sell</string>
<string name="id_provided_by_">Provided by %1$s</string>
<string name="id_type_an_amount_between_s_and_s">Type an amount between %1$s and %2$s. A minimum setup fee of %3$s will be applied to the received amount.</string>
<string name="id_finalize_on_browser">Finalize on Browser</string>
<string name="id_buy_with_s">Buy with %1$s</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.withContext
import org.koin.mp.KoinPlatformTools
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext


// Log and handle the exception. Prevent unhanded exception crash
suspend fun <T> tryCatch(block: suspend () -> T): T? = withContext(context = logException()) {
suspend fun <T> tryCatch(context: CoroutineContext = EmptyCoroutineContext, block: suspend () -> T): T? = withContext(context = context + logException()) {
block()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.blockstream.common.models.exchange

import blockstream_green.common.generated.resources.Res
import blockstream_green.common.generated.resources.id_buy_sell
import blockstream_green.common.generated.resources.id_buy
import blockstream_green.common.generated.resources.id_type_an_amount_between_s_and_s
import breez_sdk.SwapInfo
import com.blockstream.common.data.DenominatedValue
Expand All @@ -17,6 +17,7 @@ import com.blockstream.common.extensions.isPolicyAsset
import com.blockstream.common.extensions.launchIn
import com.blockstream.common.extensions.previewAccountAssetBalance
import com.blockstream.common.extensions.previewWallet
import com.blockstream.common.extensions.tryCatch
import com.blockstream.common.gdk.data.AccountAssetBalance
import com.blockstream.common.gdk.data.AssetBalance
import com.blockstream.common.lightning.satoshi
Expand All @@ -28,12 +29,10 @@ import com.blockstream.common.utils.toAmountLook
import com.blockstream.common.utils.toAmountLookOrNa
import com.rickclephas.kmp.observableviewmodel.launch
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.withContext
import org.jetbrains.compose.resources.getString

abstract class OnOffRampsViewModelAbstract(
Expand Down Expand Up @@ -64,7 +63,8 @@ abstract class OnOffRampsViewModelAbstract(
class OnOffRampsViewModel(greenWallet: GreenWallet) :
OnOffRampsViewModelAbstract(greenWallet = greenWallet) {

override val isSandboxEnvironment: MutableStateFlow<Boolean> = MutableStateFlow(appInfo.isDevelopment)
override val isSandboxEnvironment: MutableStateFlow<Boolean> =
MutableStateFlow(appInfo.isDevelopment)

private val availableBuyAssets = session.ifConnected {
listOfNotNull(
Expand All @@ -88,7 +88,7 @@ class OnOffRampsViewModel(greenWallet: GreenWallet) :

override val buyAccount: MutableStateFlow<AccountAssetBalance?> =
MutableStateFlow(session.ifConnected {
session.accounts.value.firstOrNull { it.network.policyAsset == buyAsset.value?.assetId }?.accountAssetBalance
(listOfNotNull(session.activeAccount.value) + session.accounts.value).firstOrNull { it.network.policyAsset == buyAsset.value?.assetId }?.accountAssetBalance
})

override val amount: MutableStateFlow<String> = MutableStateFlow("")
Expand All @@ -102,19 +102,21 @@ class OnOffRampsViewModel(greenWallet: GreenWallet) :
private val _amountHint = MutableStateFlow<String?>(null)
override val amountHint = _amountHint

private var swapInfo : SwapInfo? = null
private var swapInfo: SwapInfo? = null

init {
viewModelScope.launch {
_navData.value = NavData(title = getString(Res.string.id_buy_sell))
_navData.value = NavData(title = getString(Res.string.id_buy))
}

session.ifConnected {

buyAsset.onEach { buyAsset ->
_buyAccounts.value = session.accounts.value.filter { it.network.policyAsset == buyAsset?.assetId }.map {
it.accountAssetBalance
}
_buyAccounts.value =
session.accounts.value.filter { it.network.policyAsset == buyAsset?.assetId }
.map {
it.accountAssetBalance
}

if (buyAccount.value?.account?.network?.policyAsset != buyAsset?.assetId) {
buyAccount.value = _buyAccounts.value.firstOrNull()
Expand Down Expand Up @@ -159,10 +161,10 @@ class OnOffRampsViewModel(greenWallet: GreenWallet) :
var isError = false
var hintOrError: String? = null

if(isLightning){
if (isLightning) {
// Cache SwapInfo
if(swapInfo == null){
swapInfo = withContext(context = Dispatchers.IO) {
if (swapInfo == null) {
swapInfo = tryCatch(context = Dispatchers.Main) {
session.receiveOnchain()
}
}
Expand Down Expand Up @@ -193,7 +195,9 @@ class OnOffRampsViewModel(greenWallet: GreenWallet) :
)
}

isError = (balance?.satoshi?.let { it >= (minValue ?: 0) && it < (maxValue ?: 0) } ?: true).not()
isError =
(balance?.satoshi?.let { it >= (minValue ?: 0) && it < (maxValue ?: 0) }
?: true).not()
}

if (isError) {
Expand Down Expand Up @@ -224,7 +228,8 @@ class OnOffRampsViewModel(greenWallet: GreenWallet) :
private fun redirectToRamps() {
doAsync({

val meldKey = if(isSandboxEnvironment.value) MELD_DEVELOPMENT_KEY else MELD_PRODUCTION_KEY
val meldKey =
if (isSandboxEnvironment.value) MELD_DEVELOPMENT_KEY else MELD_PRODUCTION_KEY

if (isBuy.value) {
val address = session.getReceiveAddressAsString(buyAccount.value!!.account)
Expand Down Expand Up @@ -277,14 +282,17 @@ class OnOffRampsViewModel(greenWallet: GreenWallet) :
amount.value = denominatedValue.asInput ?: ""
}

companion object: Loggable() {
companion object : Loggable() {
private const val MELD_PRODUCTION = "https://meldcrypto.com"
private const val MELD_SANDBOX = "https://sb.meldcrypto.com"

private const val MELD_PRODUCTION_KEY = "WXDhJPMkahPCQ9AjtjS4Mi:49txEyv53WtUvXfVg1FNYvQdzWJc"
private const val MELD_DEVELOPMENT_KEY = "WQ59eghSwdJxyfaKbk87Cm:D2J97iJX5XjutTot6PAcxNnt4NTuCSWaH"
private const val MELD_PRODUCTION_KEY =
"WXDhJPMkahPCQ9AjtjS4Mi:49txEyv53WtUvXfVg1FNYvQdzWJc"
private const val MELD_DEVELOPMENT_KEY =
"WQ59eghSwdJxyfaKbk87Cm:D2J97iJX5XjutTot6PAcxNnt4NTuCSWaH"

fun meldUrl(isSandboxEnvironment: Boolean) = if(isSandboxEnvironment) MELD_SANDBOX else MELD_PRODUCTION
fun meldUrl(isSandboxEnvironment: Boolean) =
if (isSandboxEnvironment) MELD_SANDBOX else MELD_PRODUCTION
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
package com.blockstream.compose.components

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import blockstream_green.common.generated.resources.Res
import blockstream_green.common.generated.resources.share_network
import com.blockstream.compose.theme.GreenThemePreview
import org.jetbrains.compose.resources.painterResource

@Composable
@Preview
fun GreenButtonPreview() {
GreenThemePreview {
GreenColumn(
space = 6,
modifier = Modifier.verticalScroll(rememberScrollState())
) {
Text("Specific")

GreenRow(padding = 0) {
ScanQrButton {

}

LearnMoreButton {

}
}

GreenRow(padding = 0) {
AboutButton { }
AppSettingsButton { }
HelpButton { }
}

GreenRow(padding = 0) {
ZoomButton { }
}

HorizontalDivider()
Text("Normal")
GreenButton(
text = "Normal Loading",
enabled = false,
onProgress = true,
modifier = Modifier.fillMaxWidth()
) { }
GreenRow(padding = 0) {
GreenButton(text = "Normal Enabled", icon = painterResource(Res.drawable.share_network)) { }
GreenButton(text = "Norma Disabled", enabled = false) { }
}
GreenRow(padding = 0) {
GreenButton(text = "Big Enabled", size = GreenButtonSize.BIG) { }
GreenButton(text = "Big Disabled", size = GreenButtonSize.BIG, enabled = false) { }
}
GreenRow(padding = 0) {
GreenButton(text = "S", size = GreenButtonSize.SMALL) { }
GreenButton(text = "Small Enabled", size = GreenButtonSize.SMALL) { }
GreenButton(
text = "Small Disabled",
size = GreenButtonSize.SMALL,
enabled = false
) { }
}
GreenRow(padding = 0) {
GreenButton(text = "T", size = GreenButtonSize.TINY) { }
GreenButton(text = "Tiny Enabled", size = GreenButtonSize.TINY) { }
GreenButton(
text = "Tiny Disabled",
size = GreenButtonSize.TINY,
enabled = false
) { }
}
GreenRow(padding = 0) {
GreenButton(text = "Greener", color = GreenButtonColor.RED) { }
GreenButton(text = "Red", color = GreenButtonColor.RED) { }
GreenButton(text = "White", color = GreenButtonColor.WHITE) { }
}


HorizontalDivider()
Text("Outline")
GreenRow(padding = 0, space = 4) {
GreenButton(text = "Normal Enabled", type = GreenButtonType.OUTLINE) { }
GreenButton(
text = "Norma Disabled",
type = GreenButtonType.OUTLINE,
enabled = false
) { }
}

GreenRow(padding = 0, space = 4) {
GreenButton(
text = "Big Enabled",
type = GreenButtonType.OUTLINE,
size = GreenButtonSize.BIG
) { }
GreenButton(
text = "Big Disabled",
type = GreenButtonType.OUTLINE,
size = GreenButtonSize.BIG,
enabled = false
) { }
}

GreenRow(padding = 0, space = 4) {
GreenButton(
text = "S",
type = GreenButtonType.OUTLINE,
size = GreenButtonSize.SMALL
) { }
GreenButton(
text = "Small Enabled",
type = GreenButtonType.OUTLINE,
size = GreenButtonSize.SMALL
) { }
GreenButton(
text = "Small Disabled",
type = GreenButtonType.OUTLINE,
size = GreenButtonSize.SMALL,
enabled = false
) { }
}

GreenRow(padding = 0, space = 4) {
GreenButton(
text = "T",
type = GreenButtonType.OUTLINE,
size = GreenButtonSize.TINY
) { }
GreenButton(
text = "Tiny Enabled",
type = GreenButtonType.OUTLINE,
size = GreenButtonSize.TINY
) { }
GreenButton(
text = "Tiny Disabled",
type = GreenButtonType.OUTLINE,
size = GreenButtonSize.TINY,
enabled = false
) { }
}

GreenRow(padding = 0, space = 4) {
GreenButton(
text = "Green",
type = GreenButtonType.OUTLINE,
color = GreenButtonColor.GREENER
) { }
GreenButton(
text = "Green",
type = GreenButtonType.OUTLINE,
color = GreenButtonColor.RED
) { }
GreenButton(
text = "Green",
type = GreenButtonType.OUTLINE,
color = GreenButtonColor.WHITE
) { }
}

HorizontalDivider()
Text("Text")
GreenRow(padding = 0, space = 4) {
GreenButton(text = "Normal Enabled", type = GreenButtonType.TEXT) { }
GreenButton(
text = "Norma Disabled",
type = GreenButtonType.TEXT,
enabled = false
) { }
}

GreenRow(padding = 0, space = 4) {
GreenButton(
text = "Big Enabled",
type = GreenButtonType.TEXT,
size = GreenButtonSize.BIG
) { }
GreenButton(
text = "Big Disabled",
type = GreenButtonType.TEXT,
size = GreenButtonSize.BIG,
enabled = false
) { }
}

GreenRow(padding = 0, space = 4) {
GreenButton(
text = "S",
type = GreenButtonType.TEXT,
size = GreenButtonSize.SMALL
) { }
GreenButton(
text = "Small Enabled",
type = GreenButtonType.TEXT,
size = GreenButtonSize.SMALL
) { }
GreenButton(
text = "Small Disabled",
type = GreenButtonType.TEXT,
size = GreenButtonSize.SMALL,
enabled = false
) { }
}

GreenRow(padding = 0, space = 4) {
GreenButton(
text = "T",
type = GreenButtonType.TEXT,
size = GreenButtonSize.TINY
) { }
GreenButton(
text = "Tiny Enabled",
type = GreenButtonType.TEXT,
size = GreenButtonSize.TINY
) { }
GreenButton(
text = "Tiny Disabled",
type = GreenButtonType.TEXT,
size = GreenButtonSize.TINY,
enabled = false
) { }
}

GreenRow(padding = 0, space = 4) {
GreenButton(
text = "Green",
type = GreenButtonType.TEXT,
color = GreenButtonColor.RED
) { }
GreenButton(
text = "Green",
type = GreenButtonType.TEXT,
color = GreenButtonColor.WHITE
) { }
}
}
}
}

Loading

0 comments on commit d72024e

Please sign in to comment.