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.FileOutputStream
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
|
import java.net.URLDecoder
|
||||||
|
import java.nio.charset.StandardCharsets
|
||||||
|
|
||||||
class AndroidFileRepository(private val context: Context) : FileRepository {
|
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}")
|
if (!file.exists()) throw IOException("File not found: ${file.absolutePath}")
|
||||||
file.readBytes()
|
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) {
|
override suspend fun saveLocalFile(filePath: String, data: ByteArray) = withContext(Dispatchers.IO) {
|
||||||
try {
|
try {
|
||||||
if (filePath.startsWith("content://") || filePath.startsWith("file://")) {
|
if (filePath.startsWith("content://") || filePath.startsWith("file://")) {
|
||||||
val uri = Uri.parse(filePath)
|
val uri = Uri.parse(filePath)
|
||||||
context.contentResolver.openOutputStream(uri)?.use { outputStream ->
|
context.contentResolver.openOutputStream(uri, "rwt")?.use { outputStream ->
|
||||||
outputStream.write(data)
|
outputStream.write(data)
|
||||||
outputStream.flush()
|
outputStream.flush()
|
||||||
} ?: throw Exception("Impossible d'ouvrir l'Uri pour écriture")
|
} ?: 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("/")) {
|
val file = if (filePath.contains("/")) {
|
||||||
File(filePath)
|
File(filePath)
|
||||||
} else {
|
} else {
|
||||||
File(getPAppPublicFolder(), filePath)
|
File(getAppPublicFolder(), filePath)
|
||||||
}
|
}
|
||||||
file.parentFile?.mkdirs()
|
file.parentFile?.mkdirs()
|
||||||
file.writeBytes(data)
|
file.writeBytes(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val displayPath = filePath.parseSafToRelativePath()
|
||||||
|
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
if (filePath.endsWith(".pdf", ignoreCase = true)) {
|
if (filePath.endsWith(".pdf", ignoreCase = true)) {
|
||||||
openPdfFile(context = context, filePath = filePath)
|
openPdfFile(context = context, filePath = filePath)
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(
|
Toast.makeText(
|
||||||
context.applicationContext,
|
context.applicationContext,
|
||||||
"Sauvegarde réussie",
|
"Sauvegarde réussie : ${displayPath.substringAfterLast('/')}",
|
||||||
Toast.LENGTH_LONG
|
Toast.LENGTH_LONG
|
||||||
).show()
|
).show()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ package mg.dot.feufaro
|
||||||
class AndroidPathValidator : PathValidator {
|
class AndroidPathValidator : PathValidator {
|
||||||
override fun isRestrictedPath(path: String): Boolean {
|
override fun isRestrictedPath(path: String): Boolean {
|
||||||
if (path.isEmpty()) return true
|
if (path.isEmpty()) return true
|
||||||
|
if (path.startsWith("content://")) return false
|
||||||
|
|
||||||
val lowerPath = path.lowercase()
|
val lowerPath = path.lowercase()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1441,6 +1441,7 @@ fun EditSourceCompose(
|
||||||
solfaScreenModel.fileRepository.saveLocalFile(chosenPath, data)
|
solfaScreenModel.fileRepository.saveLocalFile(chosenPath, data)
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
println("Fichier sauvegardé avec succès")
|
println("Fichier sauvegardé avec succès")
|
||||||
|
sharedScreenModel.setFileContent(codeContent, chosenPath)
|
||||||
solfaScreenModel.loadExternalFile(chosenPath)
|
solfaScreenModel.loadExternalFile(chosenPath)
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
|
|
||||||
|
|
@ -182,6 +182,12 @@ fun MainScreenWithDrawer(
|
||||||
|
|
||||||
val isQrVisible = sharedScreenModel.isQRCodeVisible.value
|
val isQrVisible = sharedScreenModel.isQRCodeVisible.value
|
||||||
|
|
||||||
|
LaunchedEffect(drawerState.isOpen) {
|
||||||
|
if (drawerState.isOpen) {
|
||||||
|
sharedScreenModel.loadItems()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ModalNavigationDrawer(drawerState = drawerState, drawerContent = {
|
ModalNavigationDrawer(drawerState = drawerState, drawerContent = {
|
||||||
SimpleDrawerContent(
|
SimpleDrawerContent(
|
||||||
items,
|
items,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue