Saved searches tweaks

This commit is contained in:
Jobobby04 2021-06-11 19:51:34 -04:00
parent d5638c6204
commit 1dcf49200a
4 changed files with 41 additions and 46 deletions

View File

@ -147,8 +147,8 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) {
* @return list of [BackupSavedSearch] to be backed up
*/
private fun backupSavedSearches(): List<BackupSavedSearch> {
return preferences.savedSearches().get().map {
val sourceId = it.substringBefore(':').toLong()
return preferences.savedSearches().get().mapNotNull {
val sourceId = it.substringBefore(':').toLongOrNull() ?: return@mapNotNull null
val content = Json.decodeFromString<JsonSavedSearch>(it.substringAfter(':'))
BackupSavedSearch(
content.name,
@ -414,8 +414,8 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) {
// SY -->
internal fun restoreSavedSearches(backupSavedSearches: List<BackupSavedSearch>) {
val currentSavedSearches = preferences.savedSearches().get().map {
val sourceId = it.substringBefore(':').toLong()
val currentSavedSearches = preferences.savedSearches().get().mapNotNull {
val sourceId = it.substringBefore(':').toLongOrNull() ?: return@mapNotNull null
val content = Json.decodeFromString<JsonSavedSearch>(it.substringAfter(':'))
BackupSavedSearch(
content.name,
@ -425,11 +425,9 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) {
)
}
preferences.savedSearches()
.set(
(
backupSavedSearches.filter { backupSavedSearch -> currentSavedSearches.none { it.name == backupSavedSearch.name && it.source == backupSavedSearch.source } }
.map {
val newSavedSearches = backupSavedSearches.filter { backupSavedSearch ->
currentSavedSearches.none { it.name == backupSavedSearch.name && it.source == backupSavedSearch.source }
}.map {
"${it.source}:" + Json.encodeToString(
JsonSavedSearch(
it.name,
@ -437,10 +435,9 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) {
Json.decodeFromString(it.filterList)
)
)
} + preferences.savedSearches().get()
)
.toSet()
)
}.toSet()
preferences.savedSearches().set(newSavedSearches + preferences.savedSearches().get())
}
/**

View File

@ -289,32 +289,32 @@ class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : Ab
val newSavedSearches = backupSavedSearches.mapNotNull {
runCatching {
val id = it.substringBefore(':').toLong()
val id = it.substringBefore(':').toLongOrNull() ?: return@mapNotNull null
val content = parser.decodeFromString<JsonSavedSearch>(it.substringAfter(':'))
id to content
}.getOrNull()
}.toMutableList()
}.toMutableSet()
val currentSources = newSavedSearches.map { it.first }.toSet()
val currentSources = newSavedSearches.map(Pair<Long, *>::first).toSet()
newSavedSearches += preferences.savedSearches().get().mapNotNull {
kotlin.runCatching {
val id = it.substringBefore(':').toLong()
val id = it.substringBefore(':').toLongOrNull() ?: return@mapNotNull null
val content = parser.decodeFromString<JsonSavedSearch>(it.substringAfter(':'))
id to content
}.getOrNull()
}.toMutableList()
}
val otherSerialized = preferences.savedSearches().get().mapNotNull {
val sourceId = it.split(":")[0].toLongOrNull() ?: return@mapNotNull null
val sourceId = it.substringBefore(":").toLongOrNull() ?: return@mapNotNull null
if (sourceId in currentSources) return@mapNotNull null
it
}
}.toSet()
val newSerialized = newSavedSearches.map {
"${it.first}:" + Json.encodeToString(it.second)
}
preferences.savedSearches().set((otherSerialized + newSerialized).toSet())
val newSerialized = newSavedSearches.map { (source, savedSearch) ->
"$source:" + Json.encodeToString(savedSearch)
}.toSet()
preferences.savedSearches().set(otherSerialized + newSerialized)
}
/**

View File

@ -39,6 +39,7 @@ import eu.kanade.tachiyomi.util.chapter.syncChaptersWithTrackServiceTwoWay
import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.lang.withUIContext
import eu.kanade.tachiyomi.util.removeCovers
import exh.log.xLogE
import exh.savedsearches.EXHSavedSearch
import exh.savedsearches.JsonSavedSearch
import kotlinx.coroutines.Job
@ -443,9 +444,9 @@ open class BrowseSourcePresenter(
// EXH -->
fun saveSearches(searches: List<EXHSavedSearch>) {
val otherSerialized = prefs.savedSearches().get().filter {
!it.startsWith("${source.id}:")
}
val otherSerialized = prefs.savedSearches().get().filterNot {
it.startsWith("${source.id}:")
}.toSet()
val newSerialized = searches.map {
"${source.id}:" + Json.encodeToString(
JsonSavedSearch(
@ -457,14 +458,13 @@ open class BrowseSourcePresenter(
)
)
}
prefs.savedSearches().set((otherSerialized + newSerialized).toSet())
prefs.savedSearches().set(otherSerialized + newSerialized)
}
fun loadSearches(): List<EXHSavedSearch> {
val loaded = prefs.savedSearches().get()
return loaded.map {
val id = it.substringBefore(':').toLong()
if (id != source.id) return@map null
return prefs.savedSearches().get().mapNotNull {
val id = it.substringBefore(':').toLongOrNull() ?: return@mapNotNull null
if (id != source.id) return@mapNotNull null
val content = Json.decodeFromString<JsonSavedSearch>(it.substringAfter(':'))
try {
val originalFilters = source.getFilterList()
@ -476,15 +476,14 @@ open class BrowseSourcePresenter(
)
} catch (t: RuntimeException) {
// Load failed
Timber.e(t, "Failed to load saved search!")
t.printStackTrace()
xLogE("Failed to load saved search!", t)
EXHSavedSearch(
content.name,
content.query,
null
)
}
}.filterNotNull()
}
}
// EXH <--
}

View File

@ -16,6 +16,7 @@ import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourcePresenter.Compani
import eu.kanade.tachiyomi.util.lang.awaitSingle
import eu.kanade.tachiyomi.util.lang.runAsObservable
import eu.kanade.tachiyomi.util.lang.withUIContext
import exh.log.xLogE
import exh.savedsearches.EXHSavedSearch
import exh.savedsearches.JsonSavedSearch
import kotlinx.coroutines.Dispatchers
@ -213,9 +214,8 @@ open class IndexPresenter(
private val filterSerializer = FilterSerializer()
fun loadSearches(): List<EXHSavedSearch> {
val loaded = preferences.savedSearches().get()
return loaded.mapNotNull {
val id = it.substringBefore(':').toLong()
return preferences.savedSearches().get().mapNotNull {
val id = it.substringBefore(':').toLongOrNull() ?: return@mapNotNull null
if (id != source.id) return@mapNotNull null
val content = Json.decodeFromString<JsonSavedSearch>(it.substringAfter(':'))
try {
@ -228,8 +228,7 @@ open class IndexPresenter(
)
} catch (t: RuntimeException) {
// Load failed
Timber.e(t, "Failed to load saved search!")
t.printStackTrace()
xLogE("Failed to load saved search!", t)
EXHSavedSearch(
content.name,
content.query,