Avoids the animations when loading NIP94 and NIP95 content

pull/519/head
Vitor Pamplona 2023-07-23 16:32:36 -04:00
rodzic 9d75fb7074
commit b055e520c2
1 zmienionych plików z 69 dodań i 99 usunięć

Wyświetl plik

@ -123,6 +123,7 @@ import com.vitorpamplona.amethyst.ui.actions.toImmutableListOfLists
import com.vitorpamplona.amethyst.ui.components.ClickableUrl import com.vitorpamplona.amethyst.ui.components.ClickableUrl
import com.vitorpamplona.amethyst.ui.components.CreateClickableTextWithEmoji import com.vitorpamplona.amethyst.ui.components.CreateClickableTextWithEmoji
import com.vitorpamplona.amethyst.ui.components.CreateTextWithEmoji import com.vitorpamplona.amethyst.ui.components.CreateTextWithEmoji
import com.vitorpamplona.amethyst.ui.components.LoadNote
import com.vitorpamplona.amethyst.ui.components.LoadThumbAndThenVideoView import com.vitorpamplona.amethyst.ui.components.LoadThumbAndThenVideoView
import com.vitorpamplona.amethyst.ui.components.MeasureSpaceWidth import com.vitorpamplona.amethyst.ui.components.MeasureSpaceWidth
import com.vitorpamplona.amethyst.ui.components.ObserveDisplayNip05Status import com.vitorpamplona.amethyst.ui.components.ObserveDisplayNip05Status
@ -3128,130 +3129,99 @@ fun FileHeaderDisplay(note: Note, accountViewModel: AccountViewModel) {
val event = (note.event as? FileHeaderEvent) ?: return val event = (note.event as? FileHeaderEvent) ?: return
val fullUrl = event.url() ?: return val fullUrl = event.url() ?: return
var content by remember { mutableStateOf<ZoomableContent?>(null) } val content by remember(note) {
val blurHash = event.blurhash()
val hash = event.hash()
val dimensions = event.dimensions()
val description = event.content
val isImage = imageExtensions.any { fullUrl.split("?")[0].lowercase().endsWith(it) }
val uri = note.toNostrUri()
if (content == null) { mutableStateOf<ZoomableContent>(
LaunchedEffect(key1 = event.id) { if (isImage) {
launch(Dispatchers.IO) { ZoomableUrlImage(
val blurHash = event.blurhash() url = fullUrl,
val hash = event.hash() description = description,
val dimensions = event.dimensions() hash = hash,
val description = event.content blurhash = blurHash,
val removedParamsFromUrl = fullUrl.split("?")[0].lowercase() dim = dimensions,
val isImage = imageExtensions.any { removedParamsFromUrl.endsWith(it) } uri = uri
val uri = note.toNostrUri() )
val newContent = if (isImage) { } else {
ZoomableUrlImage( ZoomableUrlVideo(
url = fullUrl, url = fullUrl,
description = description, description = description,
hash = hash, hash = hash,
blurhash = blurHash, dim = dimensions,
dim = dimensions, uri = uri,
uri = uri authorName = note.author?.toBestDisplayName()
) )
} else {
ZoomableUrlVideo(
url = fullUrl,
description = description,
hash = hash,
dim = dimensions,
uri = uri,
authorName = note.author?.toBestDisplayName()
)
}
launch(Dispatchers.Main) {
content = newContent
}
} }
} )
} }
Crossfade(targetState = content) { SensitivityWarning(note = note, accountViewModel = accountViewModel) {
if (it != null) { ZoomableContentView(content = content, accountViewModel = accountViewModel)
SensitivityWarning(note = note, accountViewModel = accountViewModel) {
ZoomableContentView(content = it, accountViewModel = accountViewModel)
}
}
} }
} }
@Composable @Composable
fun FileStorageHeaderDisplay(baseNote: Note, accountViewModel: AccountViewModel) { fun FileStorageHeaderDisplay(baseNote: Note, accountViewModel: AccountViewModel) {
val eventHeader = (baseNote.event as? FileStorageHeaderEvent) ?: return val eventHeader = (baseNote.event as? FileStorageHeaderEvent) ?: return
val dataEventId = eventHeader.dataEventId() ?: return
var fileNote by remember { mutableStateOf<Note?>(null) } LoadNote(baseNoteHex = dataEventId) { contentNote ->
if (contentNote != null) {
if (fileNote == null) { ObserverAndRenderNIP95(baseNote, contentNote, accountViewModel)
LaunchedEffect(key1 = eventHeader.id) {
launch(Dispatchers.IO) {
val newFileNote = eventHeader.dataEventId()?.let { LocalCache.checkGetOrCreateNote(it) }
launch(Dispatchers.Main) {
fileNote = newFileNote
}
}
}
}
Crossfade(targetState = fileNote) {
if (it != null) {
RenderNIP95(it, eventHeader, baseNote, accountViewModel)
} }
} }
} }
@Composable @Composable
private fun RenderNIP95( private fun ObserverAndRenderNIP95(
content: Note,
eventHeader: FileStorageHeaderEvent,
header: Note, header: Note,
content: Note,
accountViewModel: AccountViewModel accountViewModel: AccountViewModel
) { ) {
val eventHeader = (header.event as? FileStorageHeaderEvent) ?: return
val appContext = LocalContext.current.applicationContext val appContext = LocalContext.current.applicationContext
val noteState by content.live().metadata.observeAsState() val noteState by content.live().metadata.observeAsState()
val note = remember(noteState) { noteState?.note }
var content by remember { mutableStateOf<ZoomableContent?>(null) } val content by remember(noteState) {
// Creates a new object when the event arrives to force an update of the image.
val note = noteState?.note
val uri = header.toNostrUri()
val localDir = note?.idHex?.let { File(File(appContext.externalCacheDir, "NIP95"), it) }
val blurHash = eventHeader.blurhash()
val dimensions = eventHeader.dimensions()
val description = eventHeader.content
val mimeType = eventHeader.mimeType()
if (content == null) { val newContent = if (mimeType?.startsWith("image") == true) {
LaunchedEffect(key1 = eventHeader.id, key2 = noteState, key3 = note?.event) { ZoomableLocalImage(
launch(Dispatchers.IO) { localFile = localDir,
val uri = header.toNostrUri() mimeType = mimeType,
val localDir = description = description,
note?.idHex?.let { File(File(appContext.externalCacheDir, "NIP95"), it) } blurhash = blurHash,
val blurHash = eventHeader.blurhash() dim = dimensions,
val dimensions = eventHeader.dimensions() isVerified = true,
val description = eventHeader.content uri = uri
val mimeType = eventHeader.mimeType() )
} else {
val newContent = if (mimeType?.startsWith("image") == true) { ZoomableLocalVideo(
ZoomableLocalImage( localFile = localDir,
localFile = localDir, mimeType = mimeType,
mimeType = mimeType, description = description,
description = description, dim = dimensions,
blurhash = blurHash, isVerified = true,
dim = dimensions, uri = uri,
isVerified = true, authorName = header.author?.toBestDisplayName()
uri = uri )
)
} else {
ZoomableLocalVideo(
localFile = localDir,
mimeType = mimeType,
description = description,
dim = dimensions,
isVerified = true,
uri = uri,
authorName = header.author?.toBestDisplayName()
)
}
launch(Dispatchers.Main) {
content = newContent
}
}
} }
mutableStateOf<ZoomableContent?>(newContent)
} }
Crossfade(targetState = content) { Crossfade(targetState = content) {