Skip to content

Commit 71af030

Browse files
committed
feat: polish qr transitions
1 parent a616a91 commit 71af030

File tree

1 file changed

+50
-40
lines changed

1 file changed

+50
-40
lines changed

app/src/main/java/to/bitkit/ui/components/QrCodeImage.kt

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package to.bitkit.ui.components
22

33
import android.graphics.Bitmap
4+
import androidx.compose.animation.Crossfade
5+
import androidx.compose.animation.core.tween
46
import androidx.compose.foundation.Image
57
import androidx.compose.foundation.background
68
import androidx.compose.foundation.clickable
@@ -67,7 +69,7 @@ fun QrCodeImage(
6769
val coroutineScope = rememberCoroutineScope()
6870

6971
Box(
70-
contentAlignment = Alignment.TopCenter,
72+
contentAlignment = Alignment.Center,
7173
modifier = modifier
7274
.aspectRatio(1f)
7375
.clip(AppShapes.small)
@@ -79,53 +81,61 @@ fun QrCodeImage(
7981
onBitmapGenerated(bitmap)
8082
}
8183

82-
if (bitmap != null) {
83-
val imageComposable = @Composable {
84-
Image(
85-
painter = remember(bitmap) { BitmapPainter(bitmap.asImageBitmap()) },
86-
contentDescription = content,
87-
contentScale = ContentScale.Inside,
88-
modifier = Modifier
89-
.clickable(enabled = tipMessage.isNotBlank()) {
90-
coroutineScope.launch {
91-
context.setClipboardText(content)
92-
tooltipState.show()
84+
Crossfade(
85+
targetState = bitmap,
86+
animationSpec = tween(durationMillis = 200),
87+
label = "QR Code Crossfade"
88+
) { currentBitmap ->
89+
if (currentBitmap != null) {
90+
val imageComposable = @Composable {
91+
Image(
92+
painter = remember(currentBitmap) { BitmapPainter(currentBitmap.asImageBitmap()) },
93+
contentDescription = content,
94+
contentScale = ContentScale.Inside,
95+
modifier = Modifier
96+
.clickable(enabled = tipMessage.isNotBlank()) {
97+
coroutineScope.launch {
98+
context.setClipboardText(content)
99+
tooltipState.show()
100+
}
93101
}
94-
}
95-
.then(testTag?.let { Modifier.testTag(it) } ?: Modifier)
96-
)
102+
.then(testTag?.let { Modifier.testTag(it) } ?: Modifier)
103+
)
104+
}
105+
106+
if (tipMessage.isNotBlank()) {
107+
Tooltip(
108+
text = tipMessage,
109+
tooltipState = tooltipState,
110+
content = imageComposable,
111+
)
112+
} else {
113+
imageComposable()
114+
}
97115
}
116+
}
98117

99-
if (tipMessage.isNotBlank()) {
100-
Tooltip(
101-
text = tipMessage,
102-
tooltipState = tooltipState,
103-
content = imageComposable,
118+
logoPainter?.let {
119+
Box(
120+
contentAlignment = Alignment.Center,
121+
modifier = Modifier
122+
.size(68.dp)
123+
.background(Color.White, shape = CircleShape)
124+
.align(Alignment.Center)
125+
) {
126+
Image(
127+
painter = it,
128+
contentDescription = null,
129+
modifier = Modifier.size(50.dp)
104130
)
105-
} else {
106-
imageComposable()
107131
}
132+
}
108133

109-
logoPainter?.let {
110-
Box(
111-
contentAlignment = Alignment.Center,
112-
modifier = Modifier
113-
.size(68.dp)
114-
.background(Color.White, shape = CircleShape)
115-
.align(Alignment.Center)
116-
) {
117-
Image(
118-
painter = it,
119-
contentDescription = null,
120-
modifier = Modifier.size(50.dp)
121-
)
122-
}
123-
}
124-
} else {
134+
if (bitmap == null) {
125135
CircularProgressIndicator(
126136
color = Colors.Black,
127-
strokeWidth = 2.dp,
128-
modifier = Modifier.align(Alignment.Center)
137+
strokeWidth = 4.dp,
138+
modifier = Modifier.size(68.dp)
129139
)
130140
}
131141
}

0 commit comments

Comments
 (0)