kopia lustrzana https://github.com/vitorpamplona/amethyst
Caching URL Infos
rodzic
fd1f8663e5
commit
582b55c6be
|
@ -2,6 +2,7 @@ package com.vitorpamplona.amethyst.model
|
||||||
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
|
import com.baha.url.preview.UrlInfoItem
|
||||||
import com.fasterxml.jackson.databind.DeserializationFeature
|
import com.fasterxml.jackson.databind.DeserializationFeature
|
||||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||||
import com.vitorpamplona.amethyst.service.model.ReactionEvent
|
import com.vitorpamplona.amethyst.service.model.ReactionEvent
|
||||||
|
@ -103,6 +104,8 @@ object LocalCache {
|
||||||
it.addReply(note)
|
it.addReply(note)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UrlCachedPreviewer.preloadPreviewsFor(note)
|
||||||
|
|
||||||
refreshObservers()
|
refreshObservers()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
package com.vitorpamplona.amethyst.model
|
||||||
|
|
||||||
|
import com.baha.url.preview.BahaUrlPreview
|
||||||
|
import com.baha.url.preview.IUrlPreviewCallback
|
||||||
|
import com.baha.url.preview.UrlInfoItem
|
||||||
|
import com.vitorpamplona.amethyst.ui.components.imageExtension
|
||||||
|
import com.vitorpamplona.amethyst.ui.components.isValidURL
|
||||||
|
import com.vitorpamplona.amethyst.ui.components.noProtocolUrlValidator
|
||||||
|
import com.vitorpamplona.amethyst.ui.components.videoExtension
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
|
|
||||||
|
object UrlCachedPreviewer {
|
||||||
|
val cache = ConcurrentHashMap<String, UrlInfoItem>()
|
||||||
|
|
||||||
|
fun previewInfo(url: String, callback: IUrlPreviewCallback? = null) {
|
||||||
|
cache[url]?.let {
|
||||||
|
callback?.onComplete(it)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
BahaUrlPreview(url, object : IUrlPreviewCallback {
|
||||||
|
override fun onComplete(urlInfo: UrlInfoItem) {
|
||||||
|
cache.put(url, urlInfo)
|
||||||
|
callback?.onComplete(urlInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onFailed(throwable: Throwable) {
|
||||||
|
callback?.onFailed(throwable)
|
||||||
|
}
|
||||||
|
}).fetchUrlPreview()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun findUrlsInMessage(message: String): List<String> {
|
||||||
|
return message.split('\n').map { paragraph ->
|
||||||
|
paragraph.split(' ').filter { word: String ->
|
||||||
|
isValidURL(word) || noProtocolUrlValidator.matcher(word).matches()
|
||||||
|
}
|
||||||
|
}.flatten()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun preloadPreviewsFor(note: Note) {
|
||||||
|
note.event?.content?.let {
|
||||||
|
findUrlsInMessage(it).forEach {
|
||||||
|
val removedParamsFromUrl = it.split("?")[0].toLowerCase()
|
||||||
|
if (imageExtension.matcher(removedParamsFromUrl).matches()) {
|
||||||
|
// Preload Images? Isn't this too heavy?
|
||||||
|
} else if (videoExtension.matcher(removedParamsFromUrl).matches()) {
|
||||||
|
// Do nothing for now.
|
||||||
|
} else {
|
||||||
|
previewInfo(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -31,6 +31,7 @@ import coil.compose.AsyncImage
|
||||||
import com.baha.url.preview.BahaUrlPreview
|
import com.baha.url.preview.BahaUrlPreview
|
||||||
import com.baha.url.preview.IUrlPreviewCallback
|
import com.baha.url.preview.IUrlPreviewCallback
|
||||||
import com.baha.url.preview.UrlInfoItem
|
import com.baha.url.preview.UrlInfoItem
|
||||||
|
import com.vitorpamplona.amethyst.model.UrlCachedPreviewer
|
||||||
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
|
@ -41,7 +42,7 @@ fun UrlPreview(url: String, urlText: String, showUrlIfError: Boolean = true) {
|
||||||
|
|
||||||
// Doesn't use a viewModel because of viewModel reusing issues (too many UrlPreview are created).
|
// Doesn't use a viewModel because of viewModel reusing issues (too many UrlPreview are created).
|
||||||
LaunchedEffect(url) {
|
LaunchedEffect(url) {
|
||||||
BahaUrlPreview(url, object : IUrlPreviewCallback {
|
UrlCachedPreviewer.previewInfo(url, object : IUrlPreviewCallback {
|
||||||
override fun onComplete(urlInfo: UrlInfoItem) {
|
override fun onComplete(urlInfo: UrlInfoItem) {
|
||||||
if (urlInfo.allFetchComplete() && urlInfo.url == url)
|
if (urlInfo.allFetchComplete() && urlInfo.url == url)
|
||||||
urlPreviewState = UrlPreviewState.Loaded(urlInfo)
|
urlPreviewState = UrlPreviewState.Loaded(urlInfo)
|
||||||
|
@ -52,7 +53,7 @@ fun UrlPreview(url: String, urlText: String, showUrlIfError: Boolean = true) {
|
||||||
override fun onFailed(throwable: Throwable) {
|
override fun onFailed(throwable: Throwable) {
|
||||||
urlPreviewState = UrlPreviewState.Error("Error parsing preview for ${url}: ${throwable.message}")
|
urlPreviewState = UrlPreviewState.Error("Error parsing preview for ${url}: ${throwable.message}")
|
||||||
}
|
}
|
||||||
}).fetchUrlPreview()
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
Crossfade(targetState = urlPreviewState) { state ->
|
Crossfade(targetState = urlPreviewState) { state ->
|
||||||
|
|
|
@ -40,7 +40,6 @@ fun VideoView(videoUri: String) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println("CCC"+exoPlayer.videoFormat?.width)
|
|
||||||
AndroidView(
|
AndroidView(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth(),
|
.fillMaxWidth(),
|
||||||
|
|
Ładowanie…
Reference in New Issue