-
Notifications
You must be signed in to change notification settings - Fork 0
/
Preview.kt
146 lines (137 loc) · 5.48 KB
/
Preview.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import androidx.compose.animation.*
import androidx.compose.animation.core.tween
import androidx.compose.foundation.*
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.produceState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.loadSvgResource
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import model.ContentReadable
import model.Node
import org.jetbrains.skija.Image
import java.io.InputStream
@Composable
fun Preview(node: Node?, modifier: Modifier = Modifier) {
Surface(color = MaterialTheme.colors.background, modifier = modifier) {
Crossfade(targetState = node, animationSpec = tween(150), modifier = Modifier.fillMaxSize()) {
Box(Modifier.fillMaxSize(), Alignment.Center) {
if (it != null) {
if (it is ContentReadable) {
@Suppress("BlockingMethodInNonBlockingContext")
when (it.contentType.split("/")[0]) {
"text" -> TextPreview(it)
"image" -> ImagePreview(it)
else -> DimmedMessage("No preview available for this file type")
}
} else {
DimmedMessage("No preview available for this file type")
}
} else {
DimmedMessage("Select a file to see a preview")
}
}
}
}
}
@OptIn(ExperimentalAnimationApi::class)
@Composable
private fun TextPreview(contentReadable: ContentReadable) {
val previewTextResult by produceState<Result<String>?>(initialValue = null, contentReadable) {
withContext(Dispatchers.IO) {
var contentInputStream: InputStream? = null
value = try {
contentInputStream = contentReadable.contentInputStream()
Result.success(contentInputStream.reader().readText())
} catch (throwable: Throwable) {
Result.failure(throwable)
} finally {
@Suppress("BlockingMethodInNonBlockingContext")
contentInputStream?.close()
}
}
}
AnimatedVisibility(
visible = previewTextResult != null,
enter = fadeIn() + slideInHorizontally(initialOffsetX = { -it / 8 })
) {
if (previewTextResult!!.isSuccess) {
val scrollState = rememberScrollState()
Box(Modifier.fillMaxSize()) {
Text(
previewTextResult!!.getOrThrow(),
fontFamily = FontFamily.Monospace,
modifier = Modifier.verticalScroll(scrollState).padding(12.dp).align(Alignment.Center),
)
VerticalScrollbar(adapter = rememberScrollbarAdapter(scrollState), Modifier.align(Alignment.CenterEnd))
}
} else {
ErrorMessage("Could not read file for preview")
}
}
}
@OptIn(ExperimentalAnimationApi::class)
@Composable
private fun ImagePreview(contentReadable: ContentReadable) {
val localDensity = LocalDensity.current
val imagePreviewPainter by produceState<Result<Painter>?>(initialValue = null, contentReadable, localDensity) {
withContext(Dispatchers.IO) {
var contentInputStream: InputStream? = null
value = try {
contentInputStream = contentReadable.contentInputStream()
@Suppress("BlockingMethodInNonBlockingContext")
val painter = when (contentReadable.contentType) {
"image/svg+xml" -> loadSvgResource(contentInputStream, localDensity)
else -> BitmapPainter(
Image.makeFromEncoded(contentInputStream.readAllBytes()).asImageBitmap()
)
}
Result.success(painter)
} catch (throwable: Throwable) {
Result.failure(throwable)
} finally {
@Suppress("BlockingMethodInNonBlockingContext")
contentInputStream?.close()
}
}
}
AnimatedVisibility(
visible = imagePreviewPainter != null,
enter = fadeIn() + slideInHorizontally(initialOffsetX = { -it / 8 }),
) {
if (imagePreviewPainter!!.isSuccess) {
Image(
painter = imagePreviewPainter!!.getOrThrow(),
contentDescription = "Image preview",
modifier = Modifier.padding(12.dp)
)
} else {
ErrorMessage("Could not read file for preview")
}
}
}
@Composable
private fun DimmedMessage(message: String) {
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {
Text(message)
}
}
@Composable
private fun ErrorMessage(message: String) {
Surface(color = MaterialTheme.colors.error) {
Text(message, Modifier.padding(12.dp))
}
}