android, desktop: better handling of URI's (#3450)

This commit is contained in:
Stanislav Dmitrenko
2023-11-25 00:19:31 +08:00
committed by GitHub
parent 8ce9dd7ab6
commit f9b5c673c5
15 changed files with 47 additions and 39 deletions
@@ -27,7 +27,6 @@ import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.*
import java.io.File
import java.net.URI
import java.net.URLDecoder
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.util.*
@@ -2363,7 +2362,7 @@ data class CryptoFile(
companion object {
fun plain(f: String): CryptoFile = CryptoFile(f, null)
fun desktopPlain(f: URI): CryptoFile = CryptoFile(URLDecoder.decode(f.rawPath, "UTF-8"), null)
fun desktopPlain(f: URI): CryptoFile = CryptoFile(f.toFile().absolutePath, null)
}
}
@@ -7,6 +7,8 @@ import chat.simplex.common.views.helpers.generalGetString
import chat.simplex.res.MR
import java.io.*
import java.net.URI
import java.net.URLDecoder
import java.net.URLEncoder
expect val dataDir: File
expect val tmpDir: File
@@ -28,6 +30,10 @@ expect val remoteHostsDir: File
expect fun desktopOpenDatabaseDir()
fun createURIFromPath(absolutePath: String): URI = URI.create(URLEncoder.encode(absolutePath, "UTF-8"))
fun URI.toFile(): File = File(URLDecoder.decode(rawPath, "UTF-8").removePrefix("file:"))
fun copyFileToFile(from: File, to: URI, finally: () -> Unit) {
try {
to.outputStream().use { stream ->
@@ -3,6 +3,7 @@ package chat.simplex.common.platform
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import java.io.File
expect fun Modifier.navigationBarsWithImePadding(): Modifier
@@ -16,7 +17,7 @@ expect fun ProvideWindowInsets(
@Composable
expect fun Modifier.desktopOnExternalDrag(
enabled: Boolean = true,
onFiles: (List<String>) -> Unit = {},
onFiles: (List<File>) -> Unit = {},
onImage: (Painter) -> Unit = {},
onText: (String) -> Unit = {}
): Modifier
@@ -43,16 +43,13 @@ object VideoPlayerHolder {
): VideoPlayer =
players.getOrPut(uri to gallery) { VideoPlayer(uri, gallery, defaultPreview, defaultDuration, soundEnabled) }
fun enableSound(enable: Boolean, fileName: String?, gallery: Boolean): Boolean =
player(fileName, gallery)?.enableSound(enable) == true
private fun player(fileName: String?, gallery: Boolean): VideoPlayer? {
fileName ?: return null
return players.values.firstOrNull { player -> player.uri.path?.endsWith(fileName) == true && player.gallery == gallery }
private fun player(uri: URI?, gallery: Boolean): VideoPlayer? {
uri ?: return null
return players.values.firstOrNull { player -> player.uri == uri && player.gallery == gallery }
}
fun release(uri: URI, gallery: Boolean, remove: Boolean) =
player(uri.path, gallery)?.release(remove).run { }
player(uri, gallery)?.release(remove).run { }
fun stopAll() {
players.values.forEach { it.stop() }
@@ -501,7 +501,7 @@ fun ChatLayout(
.fillMaxWidth()
.desktopOnExternalDrag(
enabled = !attachmentDisabled.value && rememberUpdatedState(chat.userCanSend).value,
onFiles = { paths -> composeState.onFilesAttached(paths.map { URI.create(it) }) },
onFiles = { paths -> composeState.onFilesAttached(paths.map { it.toURI() }) },
onImage = {
// TODO: file is not saved anywhere?!
val tmpFile = File.createTempFile("image", ".bmp", tmpDir)