Fix the archive filter in the main DB query to handle "all"

This commit is contained in:
Jackson Harper
2023-05-05 12:47:45 +08:00
parent 1a813a42b4
commit 2a0f114231
2 changed files with 10 additions and 31 deletions

View File

@ -191,7 +191,7 @@ interface SavedItemDao {
"LEFT OUTER JOIN Highlight on highlight.highlightId = SavedItemAndHighlightCrossRef.highlightId " +
"WHERE SavedItem.serverSyncStatus != 2 " +
"AND SavedItem.isArchived != :archiveFilter " +
"AND SavedItem.isArchived IN (:allowedArchiveStates) " +
"AND SavedItem.contentReader IN (:allowedContentReaders) " +
"AND CASE WHEN :hasRequiredLabels THEN SavedItemLabel.name in (:requiredLabels) ELSE 1 END " +
"AND CASE WHEN :hasExcludedLabels THEN SavedItemLabel.name is NULL OR SavedItemLabel.name not in (:excludedLabels) ELSE 1 END " +
@ -205,11 +205,11 @@ interface SavedItemDao {
"CASE WHEN :sortKey = 'recentlyRead' THEN SavedItem.readAt END DESC,\n" +
"CASE WHEN :sortKey = 'recentlyPublished' THEN SavedItem.publishDate END DESC"
)
fun _filteredLibraryData(archiveFilter: Int, sortKey: String, hasRequiredLabels: Int, hasExcludedLabels: Int, requiredLabels: List<String>, excludedLabels: List<String>, allowedContentReaders: List<String>): LiveData<List<SavedItemWithLabelsAndHighlights>>
fun _filteredLibraryData(allowedArchiveStates: List<Int>, sortKey: String, hasRequiredLabels: Int, hasExcludedLabels: Int, requiredLabels: List<String>, excludedLabels: List<String>, allowedContentReaders: List<String>): LiveData<List<SavedItemWithLabelsAndHighlights>>
fun filteredLibraryData(archiveFilter: Int, sortKey: String, requiredLabels: List<String>, excludedLabels: List<String>, allowedContentReaders: List<String>): LiveData<List<SavedItemWithLabelsAndHighlights>> {
fun filteredLibraryData(allowedArchiveStates: List<Int>, sortKey: String, requiredLabels: List<String>, excludedLabels: List<String>, allowedContentReaders: List<String>): LiveData<List<SavedItemWithLabelsAndHighlights>> {
return _filteredLibraryData(
archiveFilter = archiveFilter,
allowedArchiveStates = allowedArchiveStates,
sortKey = sortKey,
hasRequiredLabels = requiredLabels.size,
hasExcludedLabels = excludedLabels.size,

View File

@ -36,7 +36,7 @@ class LibraryViewModel @Inject constructor(
// Live Data
private var itemsLiveDataInternal = dataService.db.savedItemDao().filteredLibraryData(
archiveFilter = 1,
allowedArchiveStates = listOf(0),
sortKey = "newest",
requiredLabels = listOf(),
excludedLabels = listOf(),
@ -65,28 +65,6 @@ class LibraryViewModel @Inject constructor(
}
}
// runBlocking {
// datastoreRepo.getString(DatastoreKeys.lastUsedSavedItemFilter)?.let { str ->
// try {
// val filter = SavedItemFilter.values().first { it.rawValue == str }
// appliedFilterLiveData.postValue(filter)
// } catch (e: Exception) {
// Log.d("error", "invalid filter value stored in datastore repo: $e")
// }
//
// datastoreRepo.getString(DatastoreKeys.lastUsedSavedItemSortFilter)?.let { str ->
// try {
// val filter = SavedItemSortFilter.values().first { it.rawValue == str }
// appliedSortFilterLiveData.postValue(filter)
// } catch (e: Exception) {
// Log.d("error", "invalid sort filter value stored in datastore repo: $e")
// }
//
// handleFilterChanges()
// }
// }
// }
viewModelScope.launch {
handleFilterChanges()
for (slug in contentRequestChannel) {
@ -196,9 +174,10 @@ class LibraryViewModel @Inject constructor(
else -> "newest"
}
val archiveFilter = when (appliedFilterLiveData.value) {
SavedItemFilter.ARCHIVED -> 0
else -> 1
val allowedArchiveStates = when (appliedFilterLiveData.value) {
SavedItemFilter.ALL -> listOf(0, 1)
SavedItemFilter.ARCHIVED -> listOf(1)
else -> listOf(0)
}
val allowedContentReaders = when(appliedFilterLiveData.value) {
@ -221,7 +200,7 @@ class LibraryViewModel @Inject constructor(
}
val newData = dataService.db.savedItemDao().filteredLibraryData(
archiveFilter = archiveFilter,
allowedArchiveStates = allowedArchiveStates,
sortKey = sortKey,
requiredLabels = requiredLabels,
excludedLabels = excludeLabels,