From 9f9de27a575066d32283404e87fb4b8f0c015d4a Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 2 Feb 2020 22:04:11 -0500 Subject: [PATCH] Reorganize some util files --- .../tachiyomi/data/cache/ChapterCache.kt | 4 +- .../kanade/tachiyomi/data/cache/CoverCache.kt | 2 +- .../data/download/DownloadProvider.kt | 2 +- .../tachiyomi/data/download/Downloader.kt | 2 + .../data/notification/NotificationHandler.kt | 2 +- .../data/notification/NotificationReceiver.kt | 4 +- .../tachiyomi/data/updater/UpdaterService.kt | 4 +- .../extension/util/ExtensionInstaller.kt | 2 +- .../eu/kanade/tachiyomi/source/LocalSource.kt | 4 +- .../tachiyomi/ui/reader/ReaderActivity.kt | 1 + .../tachiyomi/ui/reader/ReaderPresenter.kt | 2 +- .../ui/reader/loader/EpubPageLoader.kt | 2 +- .../ui/setting/SettingsBackupController.kt | 1 + .../tachiyomi/util/PreferenceExtensions.kt | 42 +++++++++++++++++++ .../tachiyomi/util/{ => storage}/DiskUtil.kt | 3 +- .../tachiyomi/util/{ => storage}/EpubFile.kt | 2 +- .../util/{ => storage}/FileExtensions.kt | 2 +- .../util/{ => storage}/OkioExtensions.kt | 2 +- 18 files changed, 65 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/PreferenceExtensions.kt rename app/src/main/java/eu/kanade/tachiyomi/util/{ => storage}/DiskUtil.kt (97%) rename app/src/main/java/eu/kanade/tachiyomi/util/{ => storage}/EpubFile.kt (98%) rename app/src/main/java/eu/kanade/tachiyomi/util/{ => storage}/FileExtensions.kt (92%) rename app/src/main/java/eu/kanade/tachiyomi/util/{ => storage}/OkioExtensions.kt (95%) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt index acb7b6b7a..a60e84c33 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt @@ -7,8 +7,8 @@ import com.google.gson.Gson import com.jakewharton.disklrucache.DiskLruCache import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.source.model.Page -import eu.kanade.tachiyomi.util.DiskUtil -import eu.kanade.tachiyomi.util.saveTo +import eu.kanade.tachiyomi.util.storage.DiskUtil +import eu.kanade.tachiyomi.util.storage.saveTo import okhttp3.Response import okio.buffer import okio.sink diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt index c13492d9d..7a3d315ac 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt @@ -1,7 +1,7 @@ package eu.kanade.tachiyomi.data.cache import android.content.Context -import eu.kanade.tachiyomi.util.DiskUtil +import eu.kanade.tachiyomi.util.storage.DiskUtil import java.io.File import java.io.IOException import java.io.InputStream 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 e6c7e7ba9..9e71903a3 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 @@ -9,7 +9,7 @@ import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.source.Source -import eu.kanade.tachiyomi.util.DiskUtil +import eu.kanade.tachiyomi.util.storage.DiskUtil import uy.kohesive.injekt.injectLazy /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt index 82915d90f..a8d241a67 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt @@ -14,6 +14,8 @@ import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.source.online.fetchAllImageUrlsFromPageList import eu.kanade.tachiyomi.util.* +import eu.kanade.tachiyomi.util.storage.DiskUtil +import eu.kanade.tachiyomi.util.storage.saveTo import kotlinx.coroutines.async import okhttp3.Response import rx.Observable diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationHandler.kt b/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationHandler.kt index 4c61ca4ee..2f474f3f5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationHandler.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationHandler.kt @@ -5,7 +5,7 @@ import android.content.Context import android.content.Intent import android.net.Uri import eu.kanade.tachiyomi.ui.main.MainActivity -import eu.kanade.tachiyomi.util.getUriCompat +import eu.kanade.tachiyomi.util.storage.getUriCompat import java.io.File /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt b/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt index 11622c505..2ab3b0747 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt @@ -13,8 +13,8 @@ import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.download.DownloadService import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.ui.reader.ReaderActivity -import eu.kanade.tachiyomi.util.DiskUtil -import eu.kanade.tachiyomi.util.getUriCompat +import eu.kanade.tachiyomi.util.storage.DiskUtil +import eu.kanade.tachiyomi.util.storage.getUriCompat import eu.kanade.tachiyomi.util.notificationManager import eu.kanade.tachiyomi.util.toast import uy.kohesive.injekt.injectLazy diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterService.kt index 5dd8dee2e..7de94fd30 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterService.kt @@ -10,8 +10,8 @@ import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.NetworkHelper import eu.kanade.tachiyomi.network.ProgressListener import eu.kanade.tachiyomi.network.newCallWithProgress -import eu.kanade.tachiyomi.util.getUriCompat -import eu.kanade.tachiyomi.util.saveTo +import eu.kanade.tachiyomi.util.storage.getUriCompat +import eu.kanade.tachiyomi.util.storage.saveTo import timber.log.Timber import uy.kohesive.injekt.injectLazy import java.io.File diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstaller.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstaller.kt index f6feabcb8..7b2a15d10 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstaller.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstaller.kt @@ -10,7 +10,7 @@ import android.os.Build import com.jakewharton.rxrelay.PublishRelay import eu.kanade.tachiyomi.extension.model.Extension import eu.kanade.tachiyomi.extension.model.InstallStep -import eu.kanade.tachiyomi.util.getUriCompat +import eu.kanade.tachiyomi.util.storage.getUriCompat import rx.Observable import rx.android.schedulers.AndroidSchedulers import timber.log.Timber diff --git a/app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt b/app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt index dabadd0f7..4ac38ad99 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt @@ -7,8 +7,8 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.source.model.* import eu.kanade.tachiyomi.util.ChapterRecognition import eu.kanade.tachiyomi.util.ComparatorUtil.CaseInsensitiveNaturalComparator -import eu.kanade.tachiyomi.util.DiskUtil -import eu.kanade.tachiyomi.util.EpubFile +import eu.kanade.tachiyomi.util.storage.DiskUtil +import eu.kanade.tachiyomi.util.storage.EpubFile import eu.kanade.tachiyomi.util.ImageUtil import junrar.Archive import junrar.rarfile.FileHeader diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt index 581593ca9..b02204a87 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt @@ -30,6 +30,7 @@ import eu.kanade.tachiyomi.ui.reader.viewer.pager.R2LPagerViewer import eu.kanade.tachiyomi.ui.reader.viewer.pager.VerticalPagerViewer import eu.kanade.tachiyomi.ui.reader.viewer.webtoon.WebtoonViewer import eu.kanade.tachiyomi.util.* +import eu.kanade.tachiyomi.util.storage.getUriCompat import eu.kanade.tachiyomi.widget.SimpleAnimationListener import eu.kanade.tachiyomi.widget.SimpleSeekBarListener import kotlinx.android.synthetic.main.reader_activity.* diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt index f1b17c7e2..86e694bb4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt @@ -21,7 +21,7 @@ import eu.kanade.tachiyomi.ui.reader.loader.DownloadPageLoader import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter import eu.kanade.tachiyomi.ui.reader.model.ReaderPage import eu.kanade.tachiyomi.ui.reader.model.ViewerChapters -import eu.kanade.tachiyomi.util.DiskUtil +import eu.kanade.tachiyomi.util.storage.DiskUtil import eu.kanade.tachiyomi.util.ImageUtil import rx.Completable import rx.Observable diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/EpubPageLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/EpubPageLoader.kt index 76965b645..43e9348c3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/EpubPageLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/EpubPageLoader.kt @@ -2,7 +2,7 @@ package eu.kanade.tachiyomi.ui.reader.loader import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.ui.reader.model.ReaderPage -import eu.kanade.tachiyomi.util.EpubFile +import eu.kanade.tachiyomi.util.storage.EpubFile import rx.Observable import java.io.File diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt index 2f31da321..6f1a3ceef 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt @@ -21,6 +21,7 @@ import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.ui.base.controller.popControllerWithTag import eu.kanade.tachiyomi.ui.base.controller.requestPermissionsSafe import eu.kanade.tachiyomi.util.* +import eu.kanade.tachiyomi.util.storage.getUriCompat import java.io.File import java.util.concurrent.TimeUnit import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/PreferenceExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/PreferenceExtensions.kt new file mode 100644 index 000000000..34e1e0557 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/util/PreferenceExtensions.kt @@ -0,0 +1,42 @@ +package eu.kanade.tachiyomi.util + +import android.content.SharedPreferences +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.channels.awaitClose +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.channelFlow +import kotlinx.coroutines.flow.flowOn +import kotlin.coroutines.CoroutineContext + +@ExperimentalCoroutinesApi +inline fun SharedPreferences.getKey(key: String, default: T, dispatcher: CoroutineContext = Dispatchers.Default): Flow { + val flow: Flow = channelFlow { + offer(getItem(key, default)) + + val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, k -> + if (key == k) { + offer(getItem(key, default)!!) + } + } + + registerOnSharedPreferenceChangeListener(listener) + awaitClose { unregisterOnSharedPreferenceChangeListener(listener) } + } + return flow + .flowOn(dispatcher) +} + +inline fun SharedPreferences.getItem(key: String, default: T): T { + @Suppress("UNCHECKED_CAST") + return when (default) { + is String -> getString(key, default) as T + is Int -> getInt(key, default) as T + is Long -> getLong(key, default) as T + is Boolean -> getBoolean(key, default) as T + is Float -> getFloat(key, default) as T + is Set<*> -> getStringSet(key, default as Set) as T + is MutableSet<*> -> getStringSet(key, default as MutableSet) as T + else -> throw IllegalArgumentException("Generic type not handled: ${T::class.java.name}") + } +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/DiskUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/storage/DiskUtil.kt similarity index 97% rename from app/src/main/java/eu/kanade/tachiyomi/util/DiskUtil.kt rename to app/src/main/java/eu/kanade/tachiyomi/util/storage/DiskUtil.kt index e089c9ebc..41dbea830 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/DiskUtil.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/storage/DiskUtil.kt @@ -1,4 +1,4 @@ -package eu.kanade.tachiyomi.util +package eu.kanade.tachiyomi.util.storage import android.content.Context import android.content.Intent @@ -7,6 +7,7 @@ import android.os.Environment import androidx.core.content.ContextCompat import androidx.core.os.EnvironmentCompat import com.hippo.unifile.UniFile +import eu.kanade.tachiyomi.util.Hash import java.io.File object DiskUtil { diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/EpubFile.kt b/app/src/main/java/eu/kanade/tachiyomi/util/storage/EpubFile.kt similarity index 98% rename from app/src/main/java/eu/kanade/tachiyomi/util/EpubFile.kt rename to app/src/main/java/eu/kanade/tachiyomi/util/storage/EpubFile.kt index f1c81b5b8..4727e5635 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/EpubFile.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/storage/EpubFile.kt @@ -1,4 +1,4 @@ -package eu.kanade.tachiyomi.util +package eu.kanade.tachiyomi.util.storage import org.jsoup.Jsoup import org.jsoup.nodes.Document diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/FileExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/storage/FileExtensions.kt similarity index 92% rename from app/src/main/java/eu/kanade/tachiyomi/util/FileExtensions.kt rename to app/src/main/java/eu/kanade/tachiyomi/util/storage/FileExtensions.kt index 98fe9bfbb..2b2ad5843 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/FileExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/storage/FileExtensions.kt @@ -1,4 +1,4 @@ -package eu.kanade.tachiyomi.util +package eu.kanade.tachiyomi.util.storage import android.content.Context import android.net.Uri diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/OkioExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/storage/OkioExtensions.kt similarity index 95% rename from app/src/main/java/eu/kanade/tachiyomi/util/OkioExtensions.kt rename to app/src/main/java/eu/kanade/tachiyomi/util/storage/OkioExtensions.kt index 7556f18df..c3f72c866 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/OkioExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/storage/OkioExtensions.kt @@ -1,4 +1,4 @@ -package eu.kanade.tachiyomi.util +package eu.kanade.tachiyomi.util.storage import okio.BufferedSource import okio.buffer