|
| 1 | +package one.mixin.android.ui.wallet |
| 2 | + |
| 3 | +import androidx.compose.foundation.LocalIndication |
| 4 | +import androidx.compose.foundation.background |
| 5 | +import androidx.compose.foundation.clickable |
| 6 | +import androidx.compose.foundation.interaction.MutableInteractionSource |
| 7 | +import androidx.compose.foundation.layout.Box |
| 8 | +import androidx.compose.foundation.layout.BoxWithConstraints |
| 9 | +import androidx.compose.foundation.layout.Row |
| 10 | +import androidx.compose.foundation.layout.Spacer |
| 11 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 12 | +import androidx.compose.foundation.layout.offset |
| 13 | +import androidx.compose.foundation.layout.padding |
| 14 | +import androidx.compose.foundation.layout.size |
| 15 | +import androidx.compose.foundation.layout.width |
| 16 | +import androidx.compose.foundation.layout.wrapContentHeight |
| 17 | +import androidx.compose.material.Icon |
| 18 | +import androidx.compose.material.Text |
| 19 | +import androidx.compose.runtime.Composable |
| 20 | +import androidx.compose.runtime.remember |
| 21 | +import androidx.compose.ui.Alignment |
| 22 | +import androidx.compose.ui.Modifier |
| 23 | +import androidx.compose.ui.draw.clip |
| 24 | +import androidx.compose.ui.geometry.Size |
| 25 | +import androidx.compose.ui.graphics.Color |
| 26 | +import androidx.compose.ui.graphics.Outline |
| 27 | +import androidx.compose.ui.graphics.Path |
| 28 | +import androidx.compose.ui.graphics.Shape |
| 29 | +import androidx.compose.ui.platform.LocalContext |
| 30 | +import androidx.compose.ui.res.painterResource |
| 31 | +import androidx.compose.ui.res.stringResource |
| 32 | +import androidx.compose.ui.text.font.FontWeight |
| 33 | +import androidx.compose.ui.unit.Density |
| 34 | +import androidx.compose.ui.unit.LayoutDirection |
| 35 | +import androidx.compose.ui.unit.dp |
| 36 | +import androidx.compose.ui.unit.sp |
| 37 | +import one.mixin.android.Constants |
| 38 | +import one.mixin.android.R |
| 39 | +import one.mixin.android.compose.theme.MixinAppTheme |
| 40 | +import one.mixin.android.extension.defaultSharedPreferences |
| 41 | +import kotlin.math.hypot |
| 42 | + |
| 43 | +private val MarketPerpsButtonOverlap = 13.dp |
| 44 | +private val MarketPerpsButtonSlant = 27.dp |
| 45 | +private val MarketPerpsButtonContentOffset = 6.dp |
| 46 | +private val MarketPerpsButtonVerticalPadding = 7.dp |
| 47 | +private val MarketPerpsButtonGreenBackground = Color(0xFFDCF2DE) |
| 48 | +private val MarketPerpsButtonRedBackground = Color(0xFFFAE4E3) |
| 49 | + |
| 50 | +@Composable |
| 51 | +fun MarketPerpsAction( |
| 52 | + onLongClick: () -> Unit, |
| 53 | + onShortClick: () -> Unit, |
| 54 | + modifier: Modifier = Modifier, |
| 55 | +) { |
| 56 | + val quoteColorReversed = LocalContext.current.defaultSharedPreferences |
| 57 | + .getBoolean(Constants.Account.PREF_QUOTE_COLOR, false) |
| 58 | + val longColor = if (quoteColorReversed) MixinAppTheme.colors.marketRed else MixinAppTheme.colors.marketGreen |
| 59 | + val shortColor = if (quoteColorReversed) MixinAppTheme.colors.marketGreen else MixinAppTheme.colors.marketRed |
| 60 | + val longBackgroundColor = if (quoteColorReversed) MarketPerpsButtonRedBackground else MarketPerpsButtonGreenBackground |
| 61 | + val shortBackgroundColor = if (quoteColorReversed) MarketPerpsButtonGreenBackground else MarketPerpsButtonRedBackground |
| 62 | + |
| 63 | + BoxWithConstraints( |
| 64 | + modifier = modifier |
| 65 | + .fillMaxWidth() |
| 66 | + .wrapContentHeight(), |
| 67 | + ) { |
| 68 | + val buttonWidth = (maxWidth + MarketPerpsButtonOverlap) / 2f |
| 69 | + MarketPerpsButton( |
| 70 | + text = stringResource(R.string.Long), |
| 71 | + color = longColor, |
| 72 | + backgroundColor = longBackgroundColor, |
| 73 | + side = MarketPerpsButtonSide.LONG, |
| 74 | + onClick = onLongClick, |
| 75 | + modifier = Modifier |
| 76 | + .align(Alignment.CenterStart) |
| 77 | + .width(buttonWidth), |
| 78 | + ) |
| 79 | + MarketPerpsButton( |
| 80 | + text = stringResource(R.string.Short), |
| 81 | + color = shortColor, |
| 82 | + backgroundColor = shortBackgroundColor, |
| 83 | + side = MarketPerpsButtonSide.SHORT, |
| 84 | + onClick = onShortClick, |
| 85 | + modifier = Modifier |
| 86 | + .align(Alignment.CenterEnd) |
| 87 | + .width(buttonWidth), |
| 88 | + ) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +@Composable |
| 93 | +private fun MarketPerpsButton( |
| 94 | + text: String, |
| 95 | + color: Color, |
| 96 | + backgroundColor: Color, |
| 97 | + side: MarketPerpsButtonSide, |
| 98 | + onClick: () -> Unit, |
| 99 | + modifier: Modifier = Modifier, |
| 100 | +) { |
| 101 | + val shape = remember(side) { MarketPerpsButtonShape(side) } |
| 102 | + Box( |
| 103 | + modifier = modifier |
| 104 | + .clip(shape) |
| 105 | + .background(backgroundColor) |
| 106 | + .clickable( |
| 107 | + interactionSource = remember { MutableInteractionSource() }, |
| 108 | + indication = LocalIndication.current, |
| 109 | + onClick = onClick, |
| 110 | + ), |
| 111 | + contentAlignment = Alignment.Center, |
| 112 | + ) { |
| 113 | + Row( |
| 114 | + modifier = Modifier |
| 115 | + .padding( |
| 116 | + start = if (side == MarketPerpsButtonSide.SHORT) MarketPerpsButtonSlant else 0.dp, |
| 117 | + top = MarketPerpsButtonVerticalPadding, |
| 118 | + end = if (side == MarketPerpsButtonSide.LONG) MarketPerpsButtonSlant else 0.dp, |
| 119 | + bottom = MarketPerpsButtonVerticalPadding, |
| 120 | + ) |
| 121 | + .offset( |
| 122 | + x = if (side == MarketPerpsButtonSide.LONG) { |
| 123 | + MarketPerpsButtonContentOffset |
| 124 | + } else { |
| 125 | + -MarketPerpsButtonContentOffset |
| 126 | + }, |
| 127 | + ), |
| 128 | + verticalAlignment = Alignment.CenterVertically, |
| 129 | + ) { |
| 130 | + Icon( |
| 131 | + painter = painterResource( |
| 132 | + if (side == MarketPerpsButtonSide.LONG) { |
| 133 | + R.drawable.ic_market_perps_long |
| 134 | + } else { |
| 135 | + R.drawable.ic_market_perps_short |
| 136 | + } |
| 137 | + ), |
| 138 | + contentDescription = null, |
| 139 | + tint = color, |
| 140 | + modifier = Modifier.size(24.dp), |
| 141 | + ) |
| 142 | + Spacer(modifier = Modifier.width(8.dp)) |
| 143 | + Text( |
| 144 | + text = text, |
| 145 | + color = color, |
| 146 | + fontSize = 14.sp, |
| 147 | + lineHeight = 16.sp, |
| 148 | + fontWeight = FontWeight.Medium, |
| 149 | + ) |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +private enum class MarketPerpsButtonSide { |
| 155 | + LONG, |
| 156 | + SHORT, |
| 157 | +} |
| 158 | + |
| 159 | +private class MarketPerpsButtonShape( |
| 160 | + private val side: MarketPerpsButtonSide, |
| 161 | +) : Shape { |
| 162 | + override fun createOutline( |
| 163 | + size: Size, |
| 164 | + layoutDirection: LayoutDirection, |
| 165 | + density: Density, |
| 166 | + ): Outline { |
| 167 | + val radius = with(density) { 8.dp.toPx() }.coerceAtMost(size.height / 2) |
| 168 | + val slant = with(density) { MarketPerpsButtonSlant.toPx() }.coerceAtMost(size.width / 3) |
| 169 | + val slantRadius = radius.coerceAtMost(slant / 2) |
| 170 | + val diagonalLength = hypot(slant.toDouble(), size.height.toDouble()).toFloat() |
| 171 | + val diagonalInsetX = slantRadius * slant / diagonalLength |
| 172 | + val diagonalInsetY = slantRadius * size.height / diagonalLength |
| 173 | + val path = Path().apply { |
| 174 | + if (side == MarketPerpsButtonSide.LONG) { |
| 175 | + moveTo(radius, 0f) |
| 176 | + lineTo(size.width - slantRadius, 0f) |
| 177 | + quadraticTo(size.width, 0f, size.width - diagonalInsetX, diagonalInsetY) |
| 178 | + lineTo(size.width - slant + diagonalInsetX, size.height - diagonalInsetY) |
| 179 | + quadraticTo(size.width - slant, size.height, size.width - slant - slantRadius, size.height) |
| 180 | + lineTo(radius, size.height) |
| 181 | + quadraticTo(0f, size.height, 0f, size.height - radius) |
| 182 | + lineTo(0f, radius) |
| 183 | + quadraticTo(0f, 0f, radius, 0f) |
| 184 | + } else { |
| 185 | + moveTo(slant + slantRadius, 0f) |
| 186 | + lineTo(size.width - radius, 0f) |
| 187 | + quadraticTo(size.width, 0f, size.width, radius) |
| 188 | + lineTo(size.width, size.height - radius) |
| 189 | + quadraticTo(size.width, size.height, size.width - radius, size.height) |
| 190 | + lineTo(slantRadius, size.height) |
| 191 | + quadraticTo(0f, size.height, diagonalInsetX, size.height - diagonalInsetY) |
| 192 | + lineTo(slant - diagonalInsetX, diagonalInsetY) |
| 193 | + quadraticTo(slant, 0f, slant + slantRadius, 0f) |
| 194 | + } |
| 195 | + close() |
| 196 | + } |
| 197 | + return Outline.Generic(path) |
| 198 | + } |
| 199 | +} |
0 commit comments