Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #275 from SuhasDissa/files-dir
Browse files Browse the repository at this point in the history
fix: Changing save directory makes the old recordings inaccessible
  • Loading branch information
SuhasDissa authored Mar 15, 2024
2 parents e4df6fb + 5cd85df commit 51ec9fa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
28 changes: 18 additions & 10 deletions app/src/main/java/com/bnyro/recorder/util/FileRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface FileRepository {
suspend fun deleteAllFiles()
fun getOutputFile(extension: String, prefix: String = ""): DocumentFile?
fun getOutputDir(): DocumentFile
fun getOutputDirs(): List<DocumentFile>
}

class FileRepositoryImpl(val context: Context) : FileRepository {
Expand All @@ -44,13 +45,17 @@ class FileRepositoryImpl(val context: Context) : FileRepository {
)

private fun getVideoFiles(): List<DocumentFile> =
getOutputDir().listFiles().filter { file ->
getOutputDirs().flatMap {
it.listFiles().filter { file ->
file.isFile && commonVideoExtensions.any { file.name?.endsWith(it) ?: false }
}
}

private fun getAudioFiles(): List<DocumentFile> =
getOutputDir().listFiles().filter { file ->
getOutputDirs().flatMap {
it.listFiles().filter { file ->
file.isFile && commonAudioExtensions.any { file.name?.endsWith(it) ?: false }
}
}

override suspend fun getVideoRecordingItems(sortOrder: SortOrder): List<RecordingItemData> {
Expand Down Expand Up @@ -85,9 +90,11 @@ class FileRepositoryImpl(val context: Context) : FileRepository {

override suspend fun deleteAllFiles() {
withContext(Dispatchers.IO) {
getOutputDir().listFiles().forEach {
getOutputDirs().map { files ->
files.listFiles().forEach {
if (it.isFile) it.delete()
}
}
}
}

Expand Down Expand Up @@ -117,15 +124,16 @@ class FileRepositoryImpl(val context: Context) : FileRepository {
return existingFile ?: outputDir.createFile("audio/*", fullFileName)
}

override fun getOutputDir(): DocumentFile {
override fun getOutputDir(): DocumentFile = getOutputDirs().last()
override fun getOutputDirs(): List<DocumentFile> {
val prefDir = Preferences.prefs.getString(Preferences.targetFolderKey, "")
val externalFilesDir = run {
val dir = context.getExternalFilesDir(null) ?: context.filesDir
DocumentFile.fromFile(dir)
}
return when {
prefDir.isNullOrBlank() -> {
val dir = context.getExternalFilesDir(null) ?: context.filesDir
DocumentFile.fromFile(dir)
}

else -> DocumentFile.fromTreeUri(context, prefDir.toUri())!!
prefDir.isNullOrBlank() -> listOf(externalFilesDir)
else -> listOf(externalFilesDir, DocumentFile.fromTreeUri(context, prefDir.toUri())!!)
}
}

Expand Down
11 changes: 7 additions & 4 deletions app/src/main/java/com/bnyro/recorder/util/IntentHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@ object IntentHelper {
}

private fun getFileUri(context: Context, file: DocumentFile): Uri {
return when (Preferences.prefs.getString(Preferences.targetFolderKey, "")) {
"", null -> {
return if (file.uri.path!!.startsWith(
(context.getExternalFilesDir(null) ?: context.filesDir).path
)
) {
val rawFile = File(
context.getExternalFilesDir(null) ?: context.filesDir,
file.name.orEmpty()
)
Log.e("using raw", rawFile.absolutePath)
FileProvider.getUriForFile(context, context.packageName + ".provider", rawFile)
} else {
file.uri
}
else -> file.uri
}

}

fun openHref(context: Context, url: String) {
Expand Down

0 comments on commit 51ec9fa

Please sign in to comment.