-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathGenerateImageView.kt
95 lines (90 loc) · 3.45 KB
/
GenerateImageView.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.crosspaste.ui.paste
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.FixedScale
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil3.PlatformContext
import coil3.compose.AsyncImagePainter
import coil3.compose.SubcomposeAsyncImage
import coil3.compose.SubcomposeAsyncImageContent
import coil3.request.ImageRequest
import coil3.request.crossfade
import com.crosspaste.image.coil.GenerateImageItem
import com.crosspaste.image.coil.ImageLoaders
import com.crosspaste.rendering.RenderingHelper
import okio.Path
import org.koin.compose.koinInject
@Composable
fun GenerateImageView(
modifier: Modifier = Modifier,
imagePath: Path,
text: String,
preview: Boolean,
alignment: Alignment = Alignment.Center,
) {
val imageLoaders = koinInject<ImageLoaders>()
val platformContext = koinInject<PlatformContext>()
val renderingHelper = koinInject<RenderingHelper>()
val density = LocalDensity.current
SubcomposeAsyncImage(
modifier = modifier,
model =
ImageRequest.Builder(platformContext)
.data(GenerateImageItem(imagePath, preview, renderingHelper.scale))
.crossfade(true)
.build(),
imageLoader = imageLoaders.generateImageLoader,
alignment = alignment,
contentScale = FixedScale(density.density / renderingHelper.scale.toFloat()),
contentDescription = "generate Image to preview",
content = {
when (this.painter.state.collectAsState().value) {
is AsyncImagePainter.State.Loading,
is AsyncImagePainter.State.Error,
-> {
Row(
modifier =
Modifier.fillMaxSize()
.padding(10.dp),
) {
Text(
text = text,
fontFamily = FontFamily.SansSerif,
maxLines =
if (preview) {
4
} else {
Int.MAX_VALUE
},
softWrap = true,
overflow = TextOverflow.Ellipsis,
style =
TextStyle(
fontWeight = FontWeight.Normal,
color = MaterialTheme.colorScheme.onSurface,
fontSize = 16.sp,
lineHeight = 20.sp,
),
)
}
}
else -> {
SubcomposeAsyncImageContent()
}
}
},
)
}