Skip to content

Commit

Permalink
Fetch body sizes, CSS fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownJoe796 committed Jun 24, 2024
1 parent fc533c9 commit 152a9ab
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,15 @@ val webSocketClient: HttpClient by lazy {
}
}
}

actual fun Blob.bytes(): Long = data.size.toLong()
actual fun FileReference.bytes(): Long {
return AndroidAppContext.applicationCtx.contentResolver
.query(uri, null, null, null, null)
?.use { cursor ->
val nameIndex = cursor.getColumnIndex(OpenableColumns.SIZE)
cursor.moveToFirst()
cursor.getLong(nameIndex)
}
?: return -1L
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,25 @@ expect class Blob
expect class FileReference

expect fun Blob.mimeType(): String
expect fun Blob.bytes(): Long
expect fun FileReference.mimeType():String
expect fun FileReference.bytes():Long
expect fun FileReference.fileName():String

sealed interface RequestBody {
val type: String
val bytes: Long
}
data class RequestBodyText(val content: String, override val type: String): RequestBody {
override val bytes: Long get() = content.encodeToByteArray().size.toLong()
}
data class RequestBodyText(val content: String, override val type: String): RequestBody
data class RequestBodyBlob(val content: Blob): RequestBody {
override val type: String get() = content.mimeType()
override val bytes: Long get() = content.bytes()
}
data class RequestBodyFile(val content: FileReference): RequestBody {
override val type: String get() = content.mimeType()
override val bytes: Long get() = content.bytes()
}

expect fun websocket(url: String): WebSocket
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,7 @@ fun NSData.toByteArray(): ByteArray = ByteArray(this@toByteArray.length.toInt())
usePinned {
memcpy(it.addressOf(0), this@toByteArray.bytes, this@toByteArray.length)
}
}
}

actual fun Blob.bytes(): Long = this.data.length.toLong()
actual fun FileReference.bytes(): Long = -1L
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,7 @@ class WebSocketWrapper(val native: org.w3c.dom.WebSocket) : WebSocket {
override fun onClose(action: (Short) -> Unit) {
native.addEventListener("close", { action((it as CloseEvent).code) })
}
}
}

actual fun Blob.bytes(): Long = size.toLong()
actual fun FileReference.bytes(): Long = size.toLong()
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ object DynamicCSS {
style(":hover.visibleOnParentHover", mapOf("visibility" to "visible"))

style(".swapImage", mapOf("overflow" to "hidden"))
style(".swapImage > img", mapOf("object-fit" to "contain"))
style(".swapImage > img", mapOf("object-fit" to "contain", "transition-duration" to "var(--transition-duration, 0.25s)"))
style(".swapImage.scaleType-Fit > img", mapOf("object-fit" to "contain"))
style(".swapImage.scaleType-Crop > img", mapOf("object-fit" to "cover"))
style(".swapImage.scaleType-Stretch > img", mapOf("object-fit" to "fill"))
Expand Down Expand Up @@ -1293,6 +1293,7 @@ object DynamicCSS {
"letter-spacing" to theme.body.additionalLetterSpacing.toString(),
"outline-color" to theme.outline.toCss(),
"transition-duration" to theme.transitionDuration.toCss(),
"--transition-duration" to theme.transitionDuration.toCss(),
) + when (val it = theme.foreground) {
is Color -> mapOf("color" to it.toCss())
is LinearGradient, is RadialGradient -> mapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ actual inline fun ViewWriter.imageActual(crossinline setup: ImageView.() -> Unit
}

actual inline var ImageView.source: ImageSource?
get() = TODO()
get() = native.asDynamic().__ROCK__source as? ImageSource
set(value) {
val last = native.asDynamic().__ROCK__source
if(refreshOnParamChange && value is ImageRemote) {
if(value.url == (last as? ImageRemote)?.url) return
} else if(value == last) return
native.asDynamic().__ROCK__source = value
when (value) {
null -> setSrc("")
is ImageRemote -> {
Expand All @@ -47,19 +52,6 @@ actual inline var ImageView.source: ImageSource?
}
}
fun ImageView.setSrc(url: String) {
if(refreshOnParamChange) {
if (((native.lastElementChild as? HTMLImageElement)?.src?.substringBefore('?')
?: "") == url.substringBefore('?')
) {
(native.lastElementChild as? HTMLImageElement)?.style?.opacity = "1"
return
}
} else {
if (((native.lastElementChild as? HTMLImageElement)?.src ?: "") == url) {
(native.lastElementChild as? HTMLImageElement)?.style?.opacity = "1"
return
}
}
if(!animationsEnabled) {
native.innerHTML = ""
}
Expand All @@ -79,7 +71,7 @@ fun ImageView.setSrc(url: String) {
}

val newElement = document.createElement("img") as HTMLImageElement
newElement.style.opacity = "0"
newElement.style.opacity = "0.01"
val now = clockMillis()
newElement.addEventListener("error", {
if(newElement.parentElement === native) {
Expand All @@ -97,10 +89,10 @@ fun ImageView.setSrc(url: String) {
if((clockMillis() - now) < 32) {
// disable animations and get it done; no reason to show the user an animation
newElement.withoutAnimation {
newElement.style.opacity = "1"
newElement.style.opacity = "0.8"
}
} else {
newElement.style.opacity = "1"
newElement.style.opacity = "0.99"
}
for(index in 0..<myIndex) {
val it = children[index] as? HTMLElement ?: continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ actual fun FileReference.fileName(): String {
return this.name
}

actual fun websocket(url: String): WebSocket = TODO()
actual fun websocket(url: String): WebSocket = TODO()
actual fun Blob.bytes(): Long = -1L
actual fun FileReference.bytes(): Long = -1L

0 comments on commit 152a9ab

Please sign in to comment.