More FlowPreference migration

This commit is contained in:
arkon 2020-04-24 11:58:16 -04:00
parent 292655cbdc
commit aa1d927da6
11 changed files with 46 additions and 45 deletions

View file

@ -8,7 +8,6 @@ import androidx.work.WorkManager
import androidx.work.Worker
import androidx.work.WorkerParameters
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import java.util.concurrent.TimeUnit
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
@ -19,7 +18,7 @@ class BackupCreatorJob(private val context: Context, workerParams: WorkerParamet
override fun doWork(): Result {
val preferences = Injekt.get<PreferencesHelper>()
val backupManager = BackupManager(context)
val uri = Uri.parse(preferences.backupsDirectory().getOrDefault())
val uri = Uri.parse(preferences.backupsDirectory().get())
val flags = BackupCreateService.BACKUP_ALL
backupManager.createBackup(uri, flags, true)
return Result.success()

View file

@ -6,9 +6,9 @@ import com.hippo.unifile.UniFile
import eu.kanade.tachiyomi.data.database.models.Chapter
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.SourceManager
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.flow.onEach
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
@ -47,19 +47,18 @@ class DownloadCache(
private var rootDir = RootDirectory(getDirectoryFromPreference())
init {
preferences.downloadsDirectory().asObservable()
.skip(1)
.subscribe {
lastRenew = 0L // invalidate cache
rootDir = RootDirectory(getDirectoryFromPreference())
}
preferences.downloadsDirectory().asFlow()
.onEach {
lastRenew = 0L // invalidate cache
rootDir = RootDirectory(getDirectoryFromPreference())
}
}
/**
* Returns the downloads directory from the user's preferences.
*/
private fun getDirectoryFromPreference(): UniFile {
val dir = preferences.downloadsDirectory().getOrDefault()
val dir = preferences.downloadsDirectory().get()
return UniFile.fromUri(context, Uri.parse(dir))
}

View file

@ -7,9 +7,13 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Chapter
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.storage.DiskUtil
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import uy.kohesive.injekt.injectLazy
/**
@ -22,19 +26,21 @@ class DownloadProvider(private val context: Context) {
private val preferences: PreferencesHelper by injectLazy()
private val scope = CoroutineScope(Job() + Dispatchers.Main)
/**
* The root directory for downloads.
*/
private var downloadsDir = preferences.downloadsDirectory().getOrDefault().let {
private var downloadsDir = preferences.downloadsDirectory().get().let {
val dir = UniFile.fromUri(context, Uri.parse(it))
DiskUtil.createNoMediaFile(dir, context)
dir
}
init {
preferences.downloadsDirectory().asObservable()
.skip(1)
.subscribe { downloadsDir = UniFile.fromUri(context, Uri.parse(it)) }
preferences.downloadsDirectory().asFlow()
.onEach { downloadsDir = UniFile.fromUri(context, Uri.parse(it)) }
.launchIn(scope)
}
/**

View file

@ -153,15 +153,15 @@ class PreferencesHelper(val context: Context) {
.apply()
}
fun trackToken(sync: TrackService) = rxPrefs.getString(Keys.trackToken(sync.id), "")
fun trackToken(sync: TrackService) = flowPrefs.getString(Keys.trackToken(sync.id), "")
fun anilistScoreType() = rxPrefs.getString("anilist_score_type", Anilist.POINT_10)
fun anilistScoreType() = flowPrefs.getString("anilist_score_type", Anilist.POINT_10)
fun backupsDirectory() = rxPrefs.getString(Keys.backupDirectory, defaultBackupDir.toString())
fun backupsDirectory() = flowPrefs.getString(Keys.backupDirectory, defaultBackupDir.toString())
fun dateFormat() = rxPrefs.getObject(Keys.dateFormat, DateFormat.getDateInstance(DateFormat.SHORT), DateFormatConverter())
fun downloadsDirectory() = rxPrefs.getString(Keys.downloadsDirectory, defaultDownloadsDir.toString())
fun downloadsDirectory() = flowPrefs.getString(Keys.downloadsDirectory, defaultDownloadsDir.toString())
fun downloadOnlyOverWifi() = prefs.getBoolean(Keys.downloadOnlyOverWifi, true)

View file

@ -5,7 +5,6 @@ import android.graphics.Color
import com.google.gson.Gson
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.data.track.TrackService
import eu.kanade.tachiyomi.data.track.model.TrackSearch
import rx.Completable
@ -75,7 +74,7 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) {
override fun getCompletionStatus(): Int = COMPLETED
override fun getScoreList(): List<String> {
return when (scorePreference.getOrDefault()) {
return when (scorePreference.get()) {
// 10 point
POINT_10 -> IntRange(0, 10).map(Int::toString)
// 100 point
@ -91,7 +90,7 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) {
}
override fun indexToScore(index: Int): Float {
return when (scorePreference.getOrDefault()) {
return when (scorePreference.get()) {
// 10 point
POINT_10 -> index * 10f
// 100 point
@ -115,7 +114,7 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) {
override fun displayScore(track: Track): String {
val score = track.score
return when (scorePreference.getOrDefault()) {
return when (scorePreference.get()) {
POINT_5 -> when (score) {
0f -> "0 ★"
else -> "${((score + 10) / 20).toInt()}"
@ -193,7 +192,7 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) {
override fun logout() {
super.logout()
preferences.trackToken(this).set(null)
preferences.trackToken(this).delete()
interceptor.setAuth(null)
}

View file

@ -2,7 +2,6 @@ package eu.kanade.tachiyomi.data.track.anilist
import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.data.track.model.TrackSearch
import java.text.SimpleDateFormat
@ -80,7 +79,7 @@ fun Track.toAnilistStatus() = when (status) {
private val preferences: PreferencesHelper by injectLazy()
fun Track.toAnilistScore(): String = when (preferences.anilistScoreType().getOrDefault()) {
fun Track.toAnilistScore(): String = when (preferences.anilistScoreType().get()) {
// 10 point
"POINT_10" -> (score.toInt() / 10).toString()
// 100 point

View file

@ -126,7 +126,7 @@ class Bangumi(private val context: Context, id: Int) : TrackService(id) {
override fun logout() {
super.logout()
preferences.trackToken(this).set(null)
preferences.trackToken(this).delete()
interceptor.newAuth(null)
}

View file

@ -4,7 +4,6 @@ import android.content.Context
import android.graphics.Color
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.data.track.TrackService
import eu.kanade.tachiyomi.data.track.model.TrackSearch
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
@ -145,7 +144,7 @@ class MyAnimeList(private val context: Context, id: Int) : TrackService(id) {
getCSRF().isNotEmpty() &&
checkCookies()
fun getCSRF(): String = preferences.trackToken(this).getOrDefault()
fun getCSRF(): String = preferences.trackToken(this).get()
private fun saveCSRF(csrf: String) = preferences.trackToken(this).set(csrf)

View file

@ -131,7 +131,7 @@ class Shikimori(private val context: Context, id: Int) : TrackService(id) {
override fun logout() {
super.logout()
preferences.trackToken(this).set(null)
preferences.trackToken(this).delete()
interceptor.newAuth(null)
}
}

View file

@ -22,7 +22,6 @@ import eu.kanade.tachiyomi.data.backup.BackupCreatorJob
import eu.kanade.tachiyomi.data.backup.BackupRestoreService
import eu.kanade.tachiyomi.data.backup.models.Backup
import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.ui.base.controller.DialogController
import eu.kanade.tachiyomi.ui.base.controller.requestPermissionsSafe
import eu.kanade.tachiyomi.ui.setting.backup.BackupNotifier
@ -125,7 +124,7 @@ class SettingsBackupController : SettingsController() {
titleRes = R.string.pref_backup_directory
onClick {
val currentDir = preferences.backupsDirectory().getOrDefault()
val currentDir = preferences.backupsDirectory().get()
try {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
startActivityForResult(intent, CODE_BACKUP_DIR)
@ -135,11 +134,12 @@ class SettingsBackupController : SettingsController() {
}
}
preferences.backupsDirectory().asObservable()
.subscribeUntilDestroy { path ->
val dir = UniFile.fromUri(context, Uri.parse(path))
summary = dir.filePath + "/automatic"
}
preferences.backupsDirectory().asFlow()
.onEach { path ->
val dir = UniFile.fromUri(context, Uri.parse(path))
summary = dir.filePath + "/automatic"
}
.launchIn(scope)
}
val backupNumber = intListPreference {
key = Keys.numberOfBackups
@ -210,7 +210,7 @@ class SettingsBackupController : SettingsController() {
backupFlags = flags
// Get dirs
val currentDir = preferences.backupsDirectory().getOrDefault()
val currentDir = preferences.backupsDirectory().get()
try {
// Use Android's built-in file creator

View file

@ -17,7 +17,6 @@ import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Category
import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.ui.base.controller.DialogController
import eu.kanade.tachiyomi.util.preference.defaultValue
import eu.kanade.tachiyomi.util.preference.entriesRes
@ -52,11 +51,12 @@ class SettingsDownloadController : SettingsController() {
ctrl.showDialog(router)
}
preferences.downloadsDirectory().asObservable()
.subscribeUntilDestroy { path ->
val dir = UniFile.fromUri(context, Uri.parse(path))
summary = dir.filePath ?: path
}
preferences.downloadsDirectory().asFlow()
.onEach { path ->
val dir = UniFile.fromUri(context, Uri.parse(path))
summary = dir.filePath ?: path
}
.launchIn(scope)
}
switchPreference {
key = Keys.downloadOnlyOverWifi
@ -159,7 +159,7 @@ class SettingsDownloadController : SettingsController() {
override fun onCreateDialog(savedViewState: Bundle?): Dialog {
val activity = activity!!
val currentDir = preferences.downloadsDirectory().getOrDefault()
val currentDir = preferences.downloadsDirectory().get()
val externalDirs = (getExternalDirs() + File(activity.getString(R.string.custom_dir))).map(File::toString)
val selectedIndex = externalDirs.indexOfFirst { it in currentDir }