Add unified storage location setting

Currently only using it as a replacement for the downloads location.
This commit is contained in:
arkon
2023-10-26 13:43:42 -04:00
parent e3b70ca08d
commit 695813ef7d
10 changed files with 99 additions and 106 deletions

View File

@@ -92,8 +92,8 @@ class BackupCreator(
file = (
if (isAutoBackup) {
// Get dir of file and create
var dir = UniFile.fromUri(context, uri)
dir = dir.createDirectory("automatic")
val dir = UniFile.fromUri(context, uri)
.createDirectory("automatic")
// Delete older backups
dir.listFiles { _, filename -> Backup.filenameRegex.matches(filename) }

View File

@@ -44,9 +44,9 @@ import tachiyomi.core.util.lang.launchIO
import tachiyomi.core.util.lang.launchNonCancellable
import tachiyomi.core.util.system.logcat
import tachiyomi.domain.chapter.model.Chapter
import tachiyomi.domain.download.service.DownloadPreferences
import tachiyomi.domain.manga.model.Manga
import tachiyomi.domain.source.service.SourceManager
import tachiyomi.domain.storage.service.StoragePreferences
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.io.File
@@ -64,7 +64,7 @@ class DownloadCache(
private val provider: DownloadProvider = Injekt.get(),
private val sourceManager: SourceManager = Injekt.get(),
private val extensionManager: ExtensionManager = Injekt.get(),
private val downloadPreferences: DownloadPreferences = Injekt.get(),
private val storagePreferences: StoragePreferences = Injekt.get(),
) {
private val scope = CoroutineScope(Dispatchers.IO)
@@ -98,7 +98,7 @@ class DownloadCache(
private var rootDownloadsDir = RootDirectory(getDirectoryFromPreference())
init {
downloadPreferences.downloadsDirectory().changes()
storagePreferences.baseStorageDirectory().changes()
.onEach {
rootDownloadsDir = RootDirectory(getDirectoryFromPreference())
invalidateCache()
@@ -297,8 +297,8 @@ class DownloadCache(
* Returns the downloads directory from the user's preferences.
*/
private fun getDirectoryFromPreference(): UniFile {
val dir = downloadPreferences.downloadsDirectory().get()
return UniFile.fromUri(context, dir.toUri())
return UniFile.fromUri(context, storagePreferences.baseStorageDirectory().get().toUri())
.createDirectory(StoragePreferences.DOWNLOADS_DIR)
}
/**

View File

@@ -12,8 +12,8 @@ import logcat.LogPriority
import tachiyomi.core.i18n.stringResource
import tachiyomi.core.util.system.logcat
import tachiyomi.domain.chapter.model.Chapter
import tachiyomi.domain.download.service.DownloadPreferences
import tachiyomi.domain.manga.model.Manga
import tachiyomi.domain.storage.service.StoragePreferences
import tachiyomi.i18n.MR
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
@@ -26,7 +26,7 @@ import uy.kohesive.injekt.api.get
*/
class DownloadProvider(
private val context: Context,
downloadPreferences: DownloadPreferences = Injekt.get(),
private val storagePreferences: StoragePreferences = Injekt.get(),
) {
private val scope = MainScope()
@@ -34,18 +34,24 @@ class DownloadProvider(
/**
* The root directory for downloads.
*/
private var downloadsDir = downloadPreferences.downloadsDirectory().get().let {
val dir = UniFile.fromUri(context, it.toUri())
DiskUtil.createNoMediaFile(dir, context)
dir
}
private var downloadsDir = setDownloadsLocation()
init {
downloadPreferences.downloadsDirectory().changes()
.onEach { downloadsDir = UniFile.fromUri(context, it.toUri()) }
storagePreferences.baseStorageDirectory().changes()
.onEach { downloadsDir = setDownloadsLocation() }
.launchIn(scope)
}
private fun setDownloadsLocation(): UniFile {
return storagePreferences.baseStorageDirectory().get().let {
val dir = UniFile.fromUri(context, it.toUri())
.createDirectory(StoragePreferences.DOWNLOADS_DIR)
DiskUtil.createNoMediaFile(dir, context)
logcat { "downloadsDir: ${dir.filePath}" }
dir
}
}
/**
* Returns the download directory for a manga. For internal use only.
*