delete and rename files
This commit is contained in:
@@ -501,6 +501,40 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
}
|
||||
}
|
||||
|
||||
fun renameFile(file: IsoFile, newName: String) {
|
||||
viewModelScope.launch {
|
||||
val oldPath = file.path
|
||||
val newPath = "${oldPath.substringBeforeLast("/")}/$newName"
|
||||
|
||||
// Check if new file already exists
|
||||
val checkResult = RootManager.executeCommand("test -f \"$newPath\" && echo exists")
|
||||
if (checkResult.output.trim() == "exists") {
|
||||
_uiState.update { it.copy(errorMessage = "File already exists: $newName") }
|
||||
return@launch
|
||||
}
|
||||
|
||||
val result = RootManager.executeCommand("mv \"$oldPath\" \"$newPath\"")
|
||||
if (result.success) {
|
||||
_uiState.update { it.copy(successMessage = "Renamed to $newName") }
|
||||
loadFiles()
|
||||
} else {
|
||||
_uiState.update { it.copy(errorMessage = "Failed to rename file") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteFile(file: IsoFile) {
|
||||
viewModelScope.launch {
|
||||
val result = RootManager.executeCommand("rm -f \"${file.path}\"")
|
||||
if (result.success) {
|
||||
_uiState.update { it.copy(successMessage = "Deleted ${file.name}") }
|
||||
loadFiles()
|
||||
} else {
|
||||
_uiState.update { it.copy(errorMessage = "Failed to delete file") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun clearError() {
|
||||
_uiState.update { it.copy(errorMessage = null) }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user