Fix path! for android & loadItems when drawer is opened
This commit is contained in:
parent
d67d54db24
commit
21b14b00a1
4 changed files with 35 additions and 4 deletions
|
|
@ -13,6 +13,8 @@ import java.io.File
|
|||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import java.io.OutputStream
|
||||
import java.net.URLDecoder
|
||||
import java.nio.charset.StandardCharsets
|
||||
|
||||
class AndroidFileRepository(private val context: Context) : FileRepository {
|
||||
|
||||
|
|
@ -84,11 +86,30 @@ class AndroidFileRepository(private val context: Context) : FileRepository {
|
|||
if (!file.exists()) throw IOException("File not found: ${file.absolutePath}")
|
||||
file.readBytes()
|
||||
}*/
|
||||
|
||||
private fun String.parseSafToRelativePath(): String {
|
||||
if (!this.startsWith("content://")) return this
|
||||
|
||||
return try {
|
||||
val decodedUri = URLDecoder.decode(this, StandardCharsets.UTF_8.name())
|
||||
val documentPath = decodedUri.substringAfter("/document/", decodedUri)
|
||||
|
||||
when {
|
||||
documentPath.startsWith("home:") -> "Documents/" + documentPath.substringAfter("home:")
|
||||
documentPath.startsWith("primary:") -> documentPath.substringAfter("primary:")
|
||||
documentPath.contains(":") -> documentPath.substringAfter(":")
|
||||
else -> documentPath
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
this
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun saveLocalFile(filePath: String, data: ByteArray) = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
if (filePath.startsWith("content://") || filePath.startsWith("file://")) {
|
||||
val uri = Uri.parse(filePath)
|
||||
context.contentResolver.openOutputStream(uri)?.use { outputStream ->
|
||||
context.contentResolver.openOutputStream(uri, "rwt")?.use { outputStream ->
|
||||
outputStream.write(data)
|
||||
outputStream.flush()
|
||||
} ?: throw Exception("Impossible d'ouvrir l'Uri pour écriture")
|
||||
|
|
@ -96,19 +117,21 @@ class AndroidFileRepository(private val context: Context) : FileRepository {
|
|||
val file = if (filePath.contains("/")) {
|
||||
File(filePath)
|
||||
} else {
|
||||
File(getPAppPublicFolder(), filePath)
|
||||
File(getAppPublicFolder(), filePath)
|
||||
}
|
||||
file.parentFile?.mkdirs()
|
||||
file.writeBytes(data)
|
||||
}
|
||||
|
||||
val displayPath = filePath.parseSafToRelativePath()
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
if (filePath.endsWith(".pdf", ignoreCase = true)) {
|
||||
openPdfFile(context = context, filePath = filePath)
|
||||
} else {
|
||||
Toast.makeText(
|
||||
context.applicationContext,
|
||||
"Sauvegarde réussie",
|
||||
"Sauvegarde réussie : ${displayPath.substringAfterLast('/')}",
|
||||
Toast.LENGTH_LONG
|
||||
).show()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ package mg.dot.feufaro
|
|||
class AndroidPathValidator : PathValidator {
|
||||
override fun isRestrictedPath(path: String): Boolean {
|
||||
if (path.isEmpty()) return true
|
||||
if (path.startsWith("content://")) return false
|
||||
|
||||
val lowerPath = path.lowercase()
|
||||
|
||||
|
|
|
|||
|
|
@ -1441,6 +1441,7 @@ fun EditSourceCompose(
|
|||
solfaScreenModel.fileRepository.saveLocalFile(chosenPath, data)
|
||||
withContext(Dispatchers.Main) {
|
||||
println("Fichier sauvegardé avec succès")
|
||||
sharedScreenModel.setFileContent(codeContent, chosenPath)
|
||||
solfaScreenModel.loadExternalFile(chosenPath)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
|
|
|
|||
|
|
@ -182,6 +182,12 @@ fun MainScreenWithDrawer(
|
|||
|
||||
val isQrVisible = sharedScreenModel.isQRCodeVisible.value
|
||||
|
||||
LaunchedEffect(drawerState.isOpen) {
|
||||
if (drawerState.isOpen) {
|
||||
sharedScreenModel.loadItems()
|
||||
}
|
||||
}
|
||||
|
||||
ModalNavigationDrawer(drawerState = drawerState, drawerContent = {
|
||||
SimpleDrawerContent(
|
||||
items,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue