diff --git a/app/build.gradle b/app/build.gradle index b0aea1542..f104d4b6b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -98,7 +98,7 @@ dependencies { implementation 'androidx.lifecycle:lifecycle-process:2.6.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' 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.preference:preference-ktx:1.2.0' implementation 'androidx.biometric:biometric-ktx:1.2.0-alpha05' diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt index 081d3ec0e..32093ed71 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt @@ -277,11 +277,14 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) { get() = prefs.getBoolean(KEY_SUGGESTIONS, false) set(value) = prefs.edit { putBoolean(KEY_SUGGESTIONS, value) } + val isSuggestionsWiFiOnly: Boolean + get() = prefs.getBoolean(KEY_SUGGESTIONS_WIFI_ONLY, false) + val isSuggestionsExcludeNsfw: Boolean get() = prefs.getBoolean(KEY_SUGGESTIONS_EXCLUDE_NSFW, false) val isSuggestionsNotificationAvailable: Boolean - get() = prefs.getBoolean(KEY_SUGGESTIONS_NOTIFICATIONS, true) + get() = prefs.getBoolean(KEY_SUGGESTIONS_NOTIFICATIONS, false) val suggestionsTagsBlacklist: Set get() { @@ -460,6 +463,7 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) { const val KEY_SCREENSHOTS_POLICY = "screenshots_policy" const val KEY_PAGES_PRELOAD = "pages_preload" 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_TAGS = "suggestions_exclude_tags" const val KEY_SUGGESTIONS_NOTIFICATIONS = "suggestions_notifications" diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/explore/ui/ExploreViewModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/explore/ui/ExploreViewModel.kt index d9c10142c..15946493d 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/explore/ui/ExploreViewModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/explore/ui/ExploreViewModel.kt @@ -3,7 +3,6 @@ package org.koitharu.kotatsu.explore.ui import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.async import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow @@ -13,11 +12,13 @@ import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.plus import org.koitharu.kotatsu.R 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.ui.BaseViewModel import org.koitharu.kotatsu.core.ui.util.ReversibleAction @@ -52,15 +53,15 @@ class ExploreViewModel @Inject constructor( valueProducer = { isSourcesGridMode }, ) + val isSuggestionsEnabled = settings.observeAsFlow( + key = AppSettings.KEY_SUGGESTIONS, + valueProducer = { isSuggestionsEnabled }, + ) + val onOpenManga = MutableEventFlow() val onActionDone = MutableEventFlow() val onShowSuggestionsTip = MutableEventFlow() private val isRandomLoading = MutableStateFlow(false) - private val recommendationDeferred = viewModelScope.async(Dispatchers.Default) { - runCatchingCancellable { - suggestionRepository.getRandom() - }.getOrNull() - } val content: StateFlow> = isLoading.flatMapLatest { loading -> if (loading) { @@ -114,12 +115,12 @@ class ExploreViewModel @Inject constructor( private fun createContentFlow() = combine( observeSources(), + getSuggestionFlow(), isGrid, isRandomLoading, observeNewSources(), - ) { content, grid, randomLoading, newSources -> - val recommendation = recommendationDeferred.await() - buildList(content, recommendation, grid, randomLoading, newSources) + ) { content, suggestions, grid, randomLoading, newSources -> + buildList(content, suggestions, grid, randomLoading, newSources) } private fun buildList( @@ -173,6 +174,16 @@ class ExploreViewModel @Inject constructor( LoadingState, ) + private fun getSuggestionFlow() = isSuggestionsEnabled.mapLatest { isEnabled -> + if (isEnabled) { + runCatchingCancellable { + suggestionRepository.getRandom() + }.getOrNull() + } else { + null + } + } + private fun observeNewSources() = settings.observe() .filter { it == AppSettings.KEY_SOURCES_ORDER || it == AppSettings.KEY_SOURCES_HIDDEN } .onStart { emit("") } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e27cdda2f..c87cea4bf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -461,4 +461,5 @@ Data was not restored Make sure you have selected the correct backup file Manage favourites + Do not update suggestions using metered network connections diff --git a/app/src/main/res/xml/pref_suggestions.xml b/app/src/main/res/xml/pref_suggestions.xml index c754b220f..9c9353e3c 100644 --- a/app/src/main/res/xml/pref_suggestions.xml +++ b/app/src/main/res/xml/pref_suggestions.xml @@ -10,7 +10,14 @@ android:title="@string/suggestions_enable" /> + +