diff --git a/app/src/main/java/eu/kanade/tachiyomi/App.kt b/app/src/main/java/eu/kanade/tachiyomi/App.kt index 67ed7146e..27235ba64 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/App.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/App.kt @@ -87,8 +87,8 @@ class App : Application(), DefaultLifecycleObserver, ImageLoaderFactory { if (packageName != process) WebView.setDataDirectorySuffix(process) } - Injekt.importModule(AppModule(this)) Injekt.importModule(PreferenceModule(this)) + Injekt.importModule(AppModule(this)) Injekt.importModule(DomainModule()) setupAcra() diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt index 407d2d505..cfcf1fe85 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt @@ -297,8 +297,11 @@ class DownloadCache( * Returns the downloads directory from the user's preferences. */ private fun getDirectoryFromPreference(): UniFile { - return UniFile.fromUri(context, storagePreferences.baseStorageDirectory().get().toUri()) - .createDirectory(StoragePreferences.DOWNLOADS_DIR) + return storagePreferences.baseStorageDirectory().get().let { + UniFile.fromUri(context, it.toUri()).also { + it?.createDirectory(StoragePreferences.DOWNLOADS_DIR) + } + } } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt index eb444398a..2f40ab78c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt @@ -5,9 +5,6 @@ import androidx.core.net.toUri import com.hippo.unifile.UniFile import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.util.storage.DiskUtil -import kotlinx.coroutines.MainScope -import kotlinx.coroutines.flow.launchIn -import kotlinx.coroutines.flow.onEach import logcat.LogPriority import tachiyomi.core.i18n.stringResource import tachiyomi.core.util.system.logcat @@ -29,27 +26,14 @@ class DownloadProvider( private val storagePreferences: StoragePreferences = Injekt.get(), ) { - private val scope = MainScope() - - /** - * The root directory for downloads. - */ - private var downloadsDir = setDownloadsLocation() - - init { - 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) - dir + private val downloadsDir: UniFile? + get() = storagePreferences.baseStorageDirectory().get().let { + UniFile.fromUri(context, it.toUri()) + ?.createDirectory(StoragePreferences.DOWNLOADS_DIR) + ?.also { dir -> + DiskUtil.createNoMediaFile(dir, context) + } } - } /** * Returns the download directory for a manga. For internal use only. @@ -59,12 +43,12 @@ class DownloadProvider( */ internal fun getMangaDir(mangaTitle: String, source: Source): UniFile { try { - return downloadsDir + return downloadsDir!! .createDirectory(getSourceDirName(source)) .createDirectory(getMangaDirName(mangaTitle)) } catch (e: Throwable) { logcat(LogPriority.ERROR, e) { "Invalid download directory" } - throw Exception(context.stringResource(MR.strings.invalid_location, downloadsDir)) + throw Exception(context.stringResource(MR.strings.invalid_location, downloadsDir ?: "")) } } @@ -74,7 +58,7 @@ class DownloadProvider( * @param source the source to query. */ fun findSourceDir(source: Source): UniFile? { - return downloadsDir.findFile(getSourceDirName(source), true) + return downloadsDir?.findFile(getSourceDirName(source), true) } /**