diff --git a/composeApp/src/androidMain/kotlin/mg/dot/feufaro/AndroidFileRepository.kt b/composeApp/src/androidMain/kotlin/mg/dot/feufaro/AndroidFileRepository.kt index 3cce5e8..416dfbe 100644 --- a/composeApp/src/androidMain/kotlin/mg/dot/feufaro/AndroidFileRepository.kt +++ b/composeApp/src/androidMain/kotlin/mg/dot/feufaro/AndroidFileRepository.kt @@ -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,20 +117,22 @@ 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", - Toast.LENGTH_LONG + "Sauvegarde réussie : ${displayPath.substringAfterLast('/')}", + Toast.LENGTH_LONG ).show() } } diff --git a/composeApp/src/androidMain/kotlin/mg/dot/feufaro/AndroidPathValidator.kt b/composeApp/src/androidMain/kotlin/mg/dot/feufaro/AndroidPathValidator.kt index c45a59b..98d43e6 100644 --- a/composeApp/src/androidMain/kotlin/mg/dot/feufaro/AndroidPathValidator.kt +++ b/composeApp/src/androidMain/kotlin/mg/dot/feufaro/AndroidPathValidator.kt @@ -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() diff --git a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/TimeUnitObject.kt b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/TimeUnitObject.kt index 065f9ec..136f553 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/TimeUnitObject.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/TimeUnitObject.kt @@ -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) { diff --git a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ui/DrawerUI.kt b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ui/DrawerUI.kt index 93dd620..80a19fc 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ui/DrawerUI.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ui/DrawerUI.kt @@ -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,