fix: Synchronized

This commit is contained in:
rebelonion 2024-05-27 05:11:39 -05:00
parent 7b36cd0d29
commit 43e560a893

View file

@ -13,7 +13,6 @@ import ani.dantotsu.snackString
import ani.dantotsu.util.Logger import ani.dantotsu.util.Logger
import com.anggrayudi.storage.callback.FolderCallback import com.anggrayudi.storage.callback.FolderCallback
import com.anggrayudi.storage.file.deleteRecursively import com.anggrayudi.storage.file.deleteRecursively
import com.anggrayudi.storage.file.findFolder
import com.anggrayudi.storage.file.moveFolderTo import com.anggrayudi.storage.file.moveFolderTo
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
@ -279,6 +278,7 @@ class DownloadsManager(private val context: Context) {
* @param type the type of media * @param type the type of media
* @return the base directory * @return the base directory
*/ */
@Synchronized
private fun getBaseDirectory(context: Context, type: MediaType): DocumentFile? { private fun getBaseDirectory(context: Context, type: MediaType): DocumentFile? {
val baseDirectory = Uri.parse(PrefManager.getVal<String>(PrefName.DownloadsDir)) val baseDirectory = Uri.parse(PrefManager.getVal<String>(PrefName.DownloadsDir))
if (baseDirectory == Uri.EMPTY) return null if (baseDirectory == Uri.EMPTY) return null
@ -307,6 +307,7 @@ class DownloadsManager(private val context: Context) {
* @param chapter the chapter of the media * @param chapter the chapter of the media
* @return the subdirectory * @return the subdirectory
*/ */
@Synchronized
fun getSubDirectory( fun getSubDirectory(
context: Context, context: Context,
type: MediaType, type: MediaType,
@ -344,22 +345,32 @@ class DownloadsManager(private val context: Context) {
} }
} }
@Synchronized
private fun getBaseDirectory(context: Context): DocumentFile? { private fun getBaseDirectory(context: Context): DocumentFile? {
val baseDirectory = Uri.parse(PrefManager.getVal<String>(PrefName.DownloadsDir)) val baseDirectory = Uri.parse(PrefManager.getVal<String>(PrefName.DownloadsDir))
if (baseDirectory == Uri.EMPTY) return null if (baseDirectory == Uri.EMPTY) return null
return DocumentFile.fromTreeUri(context, baseDirectory) return DocumentFile.fromTreeUri(context, baseDirectory)
} }
private val lock = Any()
private fun DocumentFile.findOrCreateFolder( private fun DocumentFile.findOrCreateFolder(
name: String, overwrite: Boolean name: String, overwrite: Boolean
): DocumentFile? { ): DocumentFile? {
val validName = name.findValidName()
synchronized(lock) {
return if (overwrite) { return if (overwrite) {
findFolder(name.findValidName())?.delete() findFolder(validName)?.delete()
createDirectory(name.findValidName()) createDirectory(validName)
} else { } else {
findFolder(name.findValidName()) ?: createDirectory(name.findValidName()) val folder = findFolder(validName)
return folder ?: createDirectory(validName)
} }
} }
}
private fun DocumentFile.findFolder(name: String): DocumentFile? =
listFiles().find { it.name == name && it.isDirectory }
private const val RATIO_THRESHOLD = 95 private const val RATIO_THRESHOLD = 95
fun Media.compareName(name: String): Boolean { fun Media.compareName(name: String): Boolean {
@ -379,7 +390,7 @@ class DownloadsManager(private val context: Context) {
private const val RESERVED_CHARS = "|\\?*<\":>+[]/'" private const val RESERVED_CHARS = "|\\?*<\":>+[]/'"
fun String?.findValidName(): String { fun String?.findValidName(): String {
return this?.replace("/","_")?.filterNot { RESERVED_CHARS.contains(it) } ?: "" return this?.replace("/", "_")?.filterNot { RESERVED_CHARS.contains(it) } ?: ""
} }
data class DownloadedType( data class DownloadedType(