-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from amank22/rel_1.0.5
Rel 1.0.5
- Loading branch information
Showing
11 changed files
with
196 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
app/src/main/kotlin/com/voxfinite/logvue/ui/components/common/Images.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package com.voxfinite.logvue.ui.components.common | ||
|
||
import androidx.compose.foundation.Image | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.produceState | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.ExperimentalComposeUiApi | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.painter.BitmapPainter | ||
import androidx.compose.ui.graphics.painter.Painter | ||
import androidx.compose.ui.graphics.vector.ImageVector | ||
import androidx.compose.ui.layout.ContentScale | ||
import androidx.compose.ui.res.loadImageBitmap | ||
import androidx.compose.ui.res.loadSvgPainter | ||
import androidx.compose.ui.res.loadXmlImageVector | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.unit.Density | ||
import com.voxfinite.logvue.utils.plugins.PluginsResourceLoader | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import org.xml.sax.InputSource | ||
import java.io.File | ||
import java.io.IOException | ||
import java.net.URL | ||
|
||
@OptIn(ExperimentalComposeUiApi::class) | ||
@Composable | ||
fun CustomImage( | ||
url: String?, | ||
contentDescription: String, | ||
modifier: Modifier = Modifier, | ||
contentScale: ContentScale = ContentScale.Fit | ||
) { | ||
if (url.isNullOrBlank()) return | ||
if (url.startsWith("http://") || url.startsWith("https://")) { | ||
NetworkImage(url, contentDescription, modifier, contentScale) | ||
} else { | ||
Image(painterResource(url, PluginsResourceLoader()), contentDescription, modifier, contentScale = contentScale) | ||
} | ||
} | ||
|
||
@Composable | ||
fun NetworkImage( | ||
url: String?, | ||
contentDescription: String, | ||
modifier: Modifier = Modifier, | ||
contentScale: ContentScale = ContentScale.Fit | ||
) { | ||
if (url.isNullOrBlank()) return | ||
AsyncImage( | ||
load = { loadImageBitmap(url) }, | ||
painterFor = { remember { BitmapPainter(it) } }, | ||
contentDescription, modifier, contentScale | ||
) | ||
} | ||
|
||
@Composable | ||
fun <T> AsyncImage( | ||
load: suspend () -> T, | ||
painterFor: @Composable (T) -> Painter, | ||
contentDescription: String, | ||
modifier: Modifier = Modifier, | ||
contentScale: ContentScale = ContentScale.Fit, | ||
) { | ||
// should cache image | ||
val image: T? by produceState<T?>(null) { | ||
value = withContext(Dispatchers.IO) { | ||
try { | ||
load() | ||
} catch (e: IOException) { | ||
// instead of printing to console, you can also write this to log, | ||
// or show some error placeholder | ||
e.printStackTrace() | ||
null | ||
} | ||
} | ||
} | ||
|
||
if (image != null) { | ||
Image( | ||
painter = painterFor(image!!), | ||
contentDescription = contentDescription, | ||
contentScale = contentScale, | ||
modifier = modifier | ||
) | ||
} | ||
} | ||
|
||
|
||
/* Loading from file with java.io API */ | ||
|
||
fun loadImageBitmap(file: File): ImageBitmap = | ||
file.inputStream().buffered().use(::loadImageBitmap) | ||
|
||
fun loadSvgPainter(file: File, density: Density): Painter = | ||
file.inputStream().buffered().use { loadSvgPainter(it, density) } | ||
|
||
fun loadXmlImageVector(file: File, density: Density): ImageVector = | ||
file.inputStream().buffered().use { loadXmlImageVector(InputSource(it), density) } | ||
|
||
/* Loading from network with java.net API */ | ||
|
||
fun loadImageBitmap(url: String): ImageBitmap = | ||
URL(url).openStream().buffered().use(::loadImageBitmap) | ||
|
||
fun loadSvgPainter(url: String, density: Density): Painter = | ||
URL(url).openStream().buffered().use { loadSvgPainter(it, density) } | ||
|
||
fun loadXmlImageVector(url: String, density: Density): ImageVector = | ||
URL(url).openStream().buffered().use { loadXmlImageVector(InputSource(it), density) } | ||
|
||
/* Loading from network with Ktor client API (https://ktor.io/docs/client.html). */ | ||
|
||
/* | ||
suspend fun loadImageBitmap(url: String): ImageBitmap = | ||
urlStream(url).use(::loadImageBitmap) | ||
suspend fun loadSvgPainter(url: String, density: Density): Painter = | ||
urlStream(url).use { loadSvgPainter(it, density) } | ||
suspend fun loadXmlImageVector(url: String, density: Density): ImageVector = | ||
urlStream(url).use { loadXmlImageVector(InputSource(it), density) } | ||
@OptIn(KtorExperimentalAPI::class) | ||
private suspend fun urlStream(url: String) = HttpClient(CIO).use { | ||
ByteArrayInputStream(it.get(url)) | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
app/src/main/kotlin/com/voxfinite/logvue/utils/plugins/PluginsResourceLoader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.voxfinite.logvue.utils.plugins | ||
|
||
import androidx.compose.ui.ExperimentalComposeUiApi | ||
import androidx.compose.ui.res.ResourceLoader | ||
import java.io.InputStream | ||
|
||
@OptIn(ExperimentalComposeUiApi::class) | ||
class PluginsResourceLoader : ResourceLoader { | ||
override fun load(resourcePath: String): InputStream { | ||
PluginsHelper.pluginManager.resolvedPlugins.forEach { | ||
val classLoader = it.pluginClassLoader | ||
val resource = try { | ||
classLoader.getResourceAsStream(resourcePath) | ||
} catch (ignore : Exception) { | ||
null | ||
} | ||
if (resource != null) { | ||
return resource | ||
} | ||
} | ||
// TODO(https://github.com/JetBrains/compose-jb/issues/618): probably we shouldn't use | ||
// contextClassLoader here, as it is not defined in threads created by non-JVM | ||
val contextClassLoader = Thread.currentThread().contextClassLoader!! | ||
val resource = contextClassLoader.getResourceAsStream(resourcePath) | ||
?: (::PluginsResourceLoader.javaClass).getResourceAsStream(resourcePath) | ||
return requireNotNull(resource) { "Resource $resourcePath not found" } | ||
} | ||
} |