Update suggestions

This commit is contained in:
Koitharu
2023-07-19 15:10:11 +03:00
parent e174bc68af
commit dafca9e1e1
5 changed files with 35 additions and 12 deletions

View File

@@ -98,7 +98,7 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-process:2.6.1' implementation 'androidx.lifecycle:lifecycle-process:2.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0' implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
implementation 'androidx.recyclerview:recyclerview:1.3.0' implementation 'androidx.recyclerview:recyclerview:1.3.1-rc1'
implementation 'androidx.viewpager2:viewpager2:1.1.0-beta02' implementation 'androidx.viewpager2:viewpager2:1.1.0-beta02'
implementation 'androidx.preference:preference-ktx:1.2.0' implementation 'androidx.preference:preference-ktx:1.2.0'
implementation 'androidx.biometric:biometric-ktx:1.2.0-alpha05' implementation 'androidx.biometric:biometric-ktx:1.2.0-alpha05'

View File

@@ -277,11 +277,14 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
get() = prefs.getBoolean(KEY_SUGGESTIONS, false) get() = prefs.getBoolean(KEY_SUGGESTIONS, false)
set(value) = prefs.edit { putBoolean(KEY_SUGGESTIONS, value) } set(value) = prefs.edit { putBoolean(KEY_SUGGESTIONS, value) }
val isSuggestionsWiFiOnly: Boolean
get() = prefs.getBoolean(KEY_SUGGESTIONS_WIFI_ONLY, false)
val isSuggestionsExcludeNsfw: Boolean val isSuggestionsExcludeNsfw: Boolean
get() = prefs.getBoolean(KEY_SUGGESTIONS_EXCLUDE_NSFW, false) get() = prefs.getBoolean(KEY_SUGGESTIONS_EXCLUDE_NSFW, false)
val isSuggestionsNotificationAvailable: Boolean val isSuggestionsNotificationAvailable: Boolean
get() = prefs.getBoolean(KEY_SUGGESTIONS_NOTIFICATIONS, true) get() = prefs.getBoolean(KEY_SUGGESTIONS_NOTIFICATIONS, false)
val suggestionsTagsBlacklist: Set<String> val suggestionsTagsBlacklist: Set<String>
get() { get() {
@@ -460,6 +463,7 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
const val KEY_SCREENSHOTS_POLICY = "screenshots_policy" const val KEY_SCREENSHOTS_POLICY = "screenshots_policy"
const val KEY_PAGES_PRELOAD = "pages_preload" const val KEY_PAGES_PRELOAD = "pages_preload"
const val KEY_SUGGESTIONS = "suggestions" const val KEY_SUGGESTIONS = "suggestions"
const val KEY_SUGGESTIONS_WIFI_ONLY = "suggestions_wifi"
const val KEY_SUGGESTIONS_EXCLUDE_NSFW = "suggestions_exclude_nsfw" const val KEY_SUGGESTIONS_EXCLUDE_NSFW = "suggestions_exclude_nsfw"
const val KEY_SUGGESTIONS_EXCLUDE_TAGS = "suggestions_exclude_tags" const val KEY_SUGGESTIONS_EXCLUDE_TAGS = "suggestions_exclude_tags"
const val KEY_SUGGESTIONS_NOTIFICATIONS = "suggestions_notifications" const val KEY_SUGGESTIONS_NOTIFICATIONS = "suggestions_notifications"

View File

@@ -3,7 +3,6 @@ package org.koitharu.kotatsu.explore.ui
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
@@ -13,11 +12,13 @@ import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.plus import kotlinx.coroutines.plus
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.core.prefs.observeAsFlow
import org.koitharu.kotatsu.core.prefs.observeAsStateFlow import org.koitharu.kotatsu.core.prefs.observeAsStateFlow
import org.koitharu.kotatsu.core.ui.BaseViewModel import org.koitharu.kotatsu.core.ui.BaseViewModel
import org.koitharu.kotatsu.core.ui.util.ReversibleAction import org.koitharu.kotatsu.core.ui.util.ReversibleAction
@@ -52,15 +53,15 @@ class ExploreViewModel @Inject constructor(
valueProducer = { isSourcesGridMode }, valueProducer = { isSourcesGridMode },
) )
val isSuggestionsEnabled = settings.observeAsFlow(
key = AppSettings.KEY_SUGGESTIONS,
valueProducer = { isSuggestionsEnabled },
)
val onOpenManga = MutableEventFlow<Manga>() val onOpenManga = MutableEventFlow<Manga>()
val onActionDone = MutableEventFlow<ReversibleAction>() val onActionDone = MutableEventFlow<ReversibleAction>()
val onShowSuggestionsTip = MutableEventFlow<Unit>() val onShowSuggestionsTip = MutableEventFlow<Unit>()
private val isRandomLoading = MutableStateFlow(false) private val isRandomLoading = MutableStateFlow(false)
private val recommendationDeferred = viewModelScope.async(Dispatchers.Default) {
runCatchingCancellable {
suggestionRepository.getRandom()
}.getOrNull()
}
val content: StateFlow<List<ListModel>> = isLoading.flatMapLatest { loading -> val content: StateFlow<List<ListModel>> = isLoading.flatMapLatest { loading ->
if (loading) { if (loading) {
@@ -114,12 +115,12 @@ class ExploreViewModel @Inject constructor(
private fun createContentFlow() = combine( private fun createContentFlow() = combine(
observeSources(), observeSources(),
getSuggestionFlow(),
isGrid, isGrid,
isRandomLoading, isRandomLoading,
observeNewSources(), observeNewSources(),
) { content, grid, randomLoading, newSources -> ) { content, suggestions, grid, randomLoading, newSources ->
val recommendation = recommendationDeferred.await() buildList(content, suggestions, grid, randomLoading, newSources)
buildList(content, recommendation, grid, randomLoading, newSources)
} }
private fun buildList( private fun buildList(
@@ -173,6 +174,16 @@ class ExploreViewModel @Inject constructor(
LoadingState, LoadingState,
) )
private fun getSuggestionFlow() = isSuggestionsEnabled.mapLatest { isEnabled ->
if (isEnabled) {
runCatchingCancellable {
suggestionRepository.getRandom()
}.getOrNull()
} else {
null
}
}
private fun observeNewSources() = settings.observe() private fun observeNewSources() = settings.observe()
.filter { it == AppSettings.KEY_SOURCES_ORDER || it == AppSettings.KEY_SOURCES_HIDDEN } .filter { it == AppSettings.KEY_SOURCES_ORDER || it == AppSettings.KEY_SOURCES_HIDDEN }
.onStart { emit("") } .onStart { emit("") }

View File

@@ -461,4 +461,5 @@
<string name="data_not_restored">Data was not restored</string> <string name="data_not_restored">Data was not restored</string>
<string name="data_not_restored_text">Make sure you have selected the correct backup file</string> <string name="data_not_restored_text">Make sure you have selected the correct backup file</string>
<string name="manage_favourites">Manage favourites</string> <string name="manage_favourites">Manage favourites</string>
<string name="suggestions_wifi_only_summary">Do not update suggestions using metered network connections</string>
</resources> </resources>

View File

@@ -10,7 +10,14 @@
android:title="@string/suggestions_enable" /> android:title="@string/suggestions_enable" />
<SwitchPreferenceCompat <SwitchPreferenceCompat
android:defaultValue="true" android:defaultValue="false"
android:key="suggestions_wifi"
android:summary="@string/suggestions_wifi_only_summary"
android:title="@string/only_using_wifi"
app:allowDividerAbove="true" />
<SwitchPreferenceCompat
android:defaultValue="false"
android:dependency="suggestions" android:dependency="suggestions"
android:key="suggestions_notifications" android:key="suggestions_notifications"
android:summary="@string/suggestions_notifications_summary" android:summary="@string/suggestions_notifications_summary"