Merge branch 'feature/suggestions' into devel
commit
9b86052624
@ -0,0 +1,46 @@
|
|||||||
|
package org.koitharu.kotatsu.settings
|
||||||
|
|
||||||
|
import android.content.SharedPreferences
|
||||||
|
import android.os.Bundle
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import org.koin.android.ext.android.inject
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.base.ui.BasePreferenceFragment
|
||||||
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||||
|
import org.koitharu.kotatsu.suggestions.domain.SuggestionRepository
|
||||||
|
import org.koitharu.kotatsu.suggestions.ui.SuggestionsWorker
|
||||||
|
|
||||||
|
class SuggestionsSettingsFragment : BasePreferenceFragment(R.string.suggestions),
|
||||||
|
SharedPreferences.OnSharedPreferenceChangeListener {
|
||||||
|
|
||||||
|
private val repository by inject<SuggestionRepository>(mode = LazyThreadSafetyMode.NONE)
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
settings.subscribe(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||||
|
addPreferencesFromResource(R.xml.pref_suggestions)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
settings.unsubscribe(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
|
||||||
|
if (key == AppSettings.KEY_SUGGESTIONS && settings.isSuggestionsEnabled) {
|
||||||
|
onSuggestionsEnabled()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun onSuggestionsEnabled() {
|
||||||
|
lifecycleScope.launch {
|
||||||
|
if (repository.isEmpty()) {
|
||||||
|
SuggestionsWorker.startNow(context ?: return@launch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,14 @@
|
|||||||
|
package org.koitharu.kotatsu.suggestions
|
||||||
|
|
||||||
|
import org.koin.androidx.viewmodel.dsl.viewModel
|
||||||
|
import org.koin.dsl.module
|
||||||
|
import org.koitharu.kotatsu.suggestions.domain.SuggestionRepository
|
||||||
|
import org.koitharu.kotatsu.suggestions.ui.SuggestionsViewModel
|
||||||
|
|
||||||
|
val suggestionsModule
|
||||||
|
get() = module {
|
||||||
|
|
||||||
|
factory { SuggestionRepository(get()) }
|
||||||
|
|
||||||
|
viewModel { SuggestionsViewModel(get(), get()) }
|
||||||
|
}
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
package org.koitharu.kotatsu.suggestions.data
|
||||||
|
|
||||||
|
import androidx.room.*
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
|
@Dao
|
||||||
|
abstract class SuggestionDao {
|
||||||
|
|
||||||
|
@Transaction
|
||||||
|
@Query("SELECT * FROM suggestions ORDER BY relevance DESC")
|
||||||
|
abstract fun observeAll(): Flow<List<SuggestionWithManga>>
|
||||||
|
|
||||||
|
@Query("SELECT COUNT(*) FROM suggestions")
|
||||||
|
abstract suspend fun count(): Int
|
||||||
|
|
||||||
|
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||||
|
abstract suspend fun insert(entity: SuggestionEntity): Long
|
||||||
|
|
||||||
|
@Update
|
||||||
|
abstract suspend fun update(entity: SuggestionEntity): Int
|
||||||
|
|
||||||
|
@Query("DELETE FROM suggestions")
|
||||||
|
abstract suspend fun deleteAll()
|
||||||
|
|
||||||
|
@Transaction
|
||||||
|
open suspend fun upsert(entity: SuggestionEntity) {
|
||||||
|
if (update(entity) == 0) {
|
||||||
|
insert(entity)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
package org.koitharu.kotatsu.suggestions.data
|
||||||
|
|
||||||
|
import androidx.room.Embedded
|
||||||
|
import androidx.room.Junction
|
||||||
|
import androidx.room.Relation
|
||||||
|
import org.koitharu.kotatsu.core.db.entity.MangaEntity
|
||||||
|
import org.koitharu.kotatsu.core.db.entity.MangaTagsEntity
|
||||||
|
import org.koitharu.kotatsu.core.db.entity.TagEntity
|
||||||
|
|
||||||
|
data class SuggestionWithManga(
|
||||||
|
@Embedded val suggestion: SuggestionEntity,
|
||||||
|
@Relation(
|
||||||
|
parentColumn = "manga_id",
|
||||||
|
entityColumn = "manga_id"
|
||||||
|
)
|
||||||
|
val manga: MangaEntity,
|
||||||
|
@Relation(
|
||||||
|
parentColumn = "manga_id",
|
||||||
|
entityColumn = "tag_id",
|
||||||
|
associateBy = Junction(MangaTagsEntity::class)
|
||||||
|
)
|
||||||
|
val tags: List<TagEntity>
|
||||||
|
)
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
package org.koitharu.kotatsu.suggestions.domain
|
||||||
|
|
||||||
|
import androidx.annotation.FloatRange
|
||||||
|
import org.koitharu.kotatsu.core.model.Manga
|
||||||
|
|
||||||
|
data class MangaSuggestion(
|
||||||
|
val manga: Manga,
|
||||||
|
@FloatRange(from = 0.0, to = 1.0)
|
||||||
|
val relevance: Float,
|
||||||
|
)
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
package org.koitharu.kotatsu.suggestions.domain
|
||||||
|
|
||||||
|
import androidx.room.withTransaction
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import org.koitharu.kotatsu.core.db.MangaDatabase
|
||||||
|
import org.koitharu.kotatsu.core.db.entity.MangaEntity
|
||||||
|
import org.koitharu.kotatsu.core.db.entity.TagEntity
|
||||||
|
import org.koitharu.kotatsu.core.model.Manga
|
||||||
|
import org.koitharu.kotatsu.suggestions.data.SuggestionEntity
|
||||||
|
import org.koitharu.kotatsu.utils.ext.mapItems
|
||||||
|
import org.koitharu.kotatsu.utils.ext.mapToSet
|
||||||
|
|
||||||
|
class SuggestionRepository(
|
||||||
|
private val db: MangaDatabase,
|
||||||
|
) {
|
||||||
|
|
||||||
|
fun observeAll(): Flow<List<Manga>> {
|
||||||
|
return db.suggestionDao.observeAll().mapItems {
|
||||||
|
it.manga.toManga(it.tags.mapToSet(TagEntity::toMangaTag))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun clear() {
|
||||||
|
db.suggestionDao.deleteAll()
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun isEmpty(): Boolean {
|
||||||
|
return db.suggestionDao.count() == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun replace(suggestions: Iterable<MangaSuggestion>) {
|
||||||
|
db.withTransaction {
|
||||||
|
db.suggestionDao.deleteAll()
|
||||||
|
suggestions.forEach { x ->
|
||||||
|
val tags = x.manga.tags.map(TagEntity.Companion::fromMangaTag)
|
||||||
|
db.tagsDao.upsert(tags)
|
||||||
|
db.mangaDao.upsert(MangaEntity.from(x.manga), tags)
|
||||||
|
db.suggestionDao.upsert(
|
||||||
|
SuggestionEntity(
|
||||||
|
mangaId = x.manga.id,
|
||||||
|
relevance = x.relevance,
|
||||||
|
createdAt = System.currentTimeMillis(),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,57 @@
|
|||||||
|
package org.koitharu.kotatsu.suggestions.ui
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.Menu
|
||||||
|
import android.view.MenuInflater
|
||||||
|
import android.view.MenuItem
|
||||||
|
import com.google.android.material.snackbar.Snackbar
|
||||||
|
import org.koin.androidx.viewmodel.ext.android.viewModel
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.list.ui.MangaListFragment
|
||||||
|
import org.koitharu.kotatsu.reader.ui.SimpleSettingsActivity
|
||||||
|
|
||||||
|
class SuggestionsFragment : MangaListFragment() {
|
||||||
|
|
||||||
|
override val viewModel by viewModel<SuggestionsViewModel>()
|
||||||
|
override val isSwipeRefreshEnabled = false
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
setHasOptionsMenu(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
||||||
|
super.onCreateOptionsMenu(menu, inflater)
|
||||||
|
inflater.inflate(R.menu.opt_suggestions, menu)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
|
return when (item.itemId) {
|
||||||
|
R.id.action_update -> {
|
||||||
|
SuggestionsWorker.startNow(requireContext())
|
||||||
|
Snackbar.make(
|
||||||
|
binding.recyclerView,
|
||||||
|
R.string.feed_will_update_soon,
|
||||||
|
Snackbar.LENGTH_LONG,
|
||||||
|
).show()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
R.id.action_settings -> {
|
||||||
|
startActivity(SimpleSettingsActivity.newSuggestionsSettingsIntent(requireContext()))
|
||||||
|
true
|
||||||
|
}
|
||||||
|
else -> super.onOptionsItemSelected(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onScrolledToEnd() = Unit
|
||||||
|
|
||||||
|
override fun getTitle(): CharSequence? {
|
||||||
|
return context?.getString(R.string.suggestions)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
fun newInstance() = SuggestionsFragment()
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,49 @@
|
|||||||
|
package org.koitharu.kotatsu.suggestions.ui
|
||||||
|
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.flow.catch
|
||||||
|
import kotlinx.coroutines.flow.combine
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||||
|
import org.koitharu.kotatsu.list.ui.MangaListViewModel
|
||||||
|
import org.koitharu.kotatsu.list.ui.model.*
|
||||||
|
import org.koitharu.kotatsu.suggestions.domain.SuggestionRepository
|
||||||
|
import org.koitharu.kotatsu.utils.ext.asLiveDataDistinct
|
||||||
|
import org.koitharu.kotatsu.utils.ext.onFirst
|
||||||
|
|
||||||
|
class SuggestionsViewModel(
|
||||||
|
repository: SuggestionRepository,
|
||||||
|
settings: AppSettings,
|
||||||
|
) : MangaListViewModel(settings) {
|
||||||
|
|
||||||
|
private val headerModel = ListHeader(null, R.string.suggestions)
|
||||||
|
|
||||||
|
override val content = combine(
|
||||||
|
repository.observeAll(),
|
||||||
|
createListModeFlow()
|
||||||
|
) { list, mode ->
|
||||||
|
when {
|
||||||
|
list.isEmpty() -> listOf(EmptyState(
|
||||||
|
icon = R.drawable.ic_book_cross,
|
||||||
|
textPrimary = R.string.nothing_found,
|
||||||
|
textSecondary = R.string.text_suggestion_holder,
|
||||||
|
))
|
||||||
|
else -> buildList<ListModel>(list.size + 1) {
|
||||||
|
add(headerModel)
|
||||||
|
list.toUi(this, mode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.onFirst {
|
||||||
|
isLoading.postValue(false)
|
||||||
|
}.catch {
|
||||||
|
it.toErrorState(canRetry = false)
|
||||||
|
}.asLiveDataDistinct(
|
||||||
|
viewModelScope.coroutineContext + Dispatchers.Default,
|
||||||
|
listOf(LoadingState)
|
||||||
|
)
|
||||||
|
|
||||||
|
override fun onRefresh() = Unit
|
||||||
|
|
||||||
|
override fun onRetry() = Unit
|
||||||
|
}
|
||||||
@ -0,0 +1,104 @@
|
|||||||
|
package org.koitharu.kotatsu.suggestions.ui
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import androidx.work.*
|
||||||
|
import org.koin.core.component.KoinComponent
|
||||||
|
import org.koin.core.component.inject
|
||||||
|
import org.koitharu.kotatsu.core.model.Manga
|
||||||
|
import org.koitharu.kotatsu.core.model.SortOrder
|
||||||
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||||
|
import org.koitharu.kotatsu.history.domain.HistoryRepository
|
||||||
|
import org.koitharu.kotatsu.suggestions.domain.MangaSuggestion
|
||||||
|
import org.koitharu.kotatsu.suggestions.domain.SuggestionRepository
|
||||||
|
import org.koitharu.kotatsu.utils.ext.mangaRepositoryOf
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
import kotlin.math.pow
|
||||||
|
|
||||||
|
class SuggestionsWorker(appContext: Context, params: WorkerParameters) :
|
||||||
|
CoroutineWorker(appContext, params), KoinComponent {
|
||||||
|
|
||||||
|
private val suggestionRepository by inject<SuggestionRepository>()
|
||||||
|
private val historyRepository by inject<HistoryRepository>()
|
||||||
|
private val appSettings by inject<AppSettings>()
|
||||||
|
|
||||||
|
override suspend fun doWork(): Result = try {
|
||||||
|
val count = doWorkImpl()
|
||||||
|
Result.success(workDataOf(DATA_COUNT to count))
|
||||||
|
} catch (t: Throwable) {
|
||||||
|
Result.failure()
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun doWorkImpl(): Int {
|
||||||
|
if (!appSettings.isSuggestionsEnabled) {
|
||||||
|
suggestionRepository.clear()
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
val rawResults = ArrayList<Manga>()
|
||||||
|
val allTags = historyRepository.getAllTags()
|
||||||
|
if (allTags.isEmpty()) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
val tagsBySources = allTags.groupBy { x -> x.source }
|
||||||
|
for ((source, tags) in tagsBySources) {
|
||||||
|
val repo = mangaRepositoryOf(source)
|
||||||
|
tags.flatMapTo(rawResults) { tag ->
|
||||||
|
repo.getList2(
|
||||||
|
offset = 0,
|
||||||
|
sortOrder = SortOrder.UPDATED,
|
||||||
|
tags = setOf(tag),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (appSettings.isSuggestionsExcludeNsfw) {
|
||||||
|
rawResults.removeAll { it.isNsfw }
|
||||||
|
}
|
||||||
|
if (rawResults.isEmpty()) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
val suggestions = rawResults.distinctBy { manga ->
|
||||||
|
manga.id
|
||||||
|
}.map { manga ->
|
||||||
|
val jointTags = manga.tags intersect allTags
|
||||||
|
MangaSuggestion(
|
||||||
|
manga = manga,
|
||||||
|
relevance = (jointTags.size / manga.tags.size.toDouble()).pow(2.0).toFloat(),
|
||||||
|
)
|
||||||
|
}.sortedBy { it.relevance }.take(LIMIT)
|
||||||
|
suggestionRepository.replace(suggestions)
|
||||||
|
return suggestions.size
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
private const val TAG = "suggestions"
|
||||||
|
private const val TAG_ONESHOT = "suggestions_oneshot"
|
||||||
|
private const val LIMIT = 140
|
||||||
|
private const val DATA_COUNT = "count"
|
||||||
|
|
||||||
|
fun setup(context: Context) {
|
||||||
|
val constraints = Constraints.Builder()
|
||||||
|
.setRequiredNetworkType(NetworkType.UNMETERED)
|
||||||
|
.setRequiresBatteryNotLow(true)
|
||||||
|
.build()
|
||||||
|
val request = PeriodicWorkRequestBuilder<SuggestionsWorker>(6, TimeUnit.HOURS)
|
||||||
|
.setConstraints(constraints)
|
||||||
|
.addTag(TAG)
|
||||||
|
.setBackoffCriteria(BackoffPolicy.LINEAR, 30, TimeUnit.MINUTES)
|
||||||
|
.build()
|
||||||
|
WorkManager.getInstance(context)
|
||||||
|
.enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.KEEP, request)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun startNow(context: Context) {
|
||||||
|
val constraints = Constraints.Builder()
|
||||||
|
.setRequiredNetworkType(NetworkType.CONNECTED)
|
||||||
|
.build()
|
||||||
|
val request = OneTimeWorkRequestBuilder<SuggestionsWorker>()
|
||||||
|
.setConstraints(constraints)
|
||||||
|
.addTag(TAG_ONESHOT)
|
||||||
|
.build()
|
||||||
|
WorkManager.getInstance(context)
|
||||||
|
.enqueue(request)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<vector
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24dp"
|
||||||
|
android:height="24dp"
|
||||||
|
android:tint="?attr/colorControlNormal"
|
||||||
|
android:viewportWidth="24"
|
||||||
|
android:viewportHeight="24">
|
||||||
|
<path
|
||||||
|
android:fillColor="#000"
|
||||||
|
android:pathData="M12,2A7,7 0 0,1 19,9C19,11.38 17.81,13.47 16,14.74V17A1,1 0 0,1 15,18H9A1,1 0 0,1 8,17V14.74C6.19,13.47 5,11.38 5,9A7,7 0 0,1 12,2M9,21V20H15V21A1,1 0 0,1 14,22H10A1,1 0 0,1 9,21M12,4A5,5 0 0,0 7,9C7,11.05 8.23,12.81 10,13.58V16H14V13.58C15.77,12.81 17,11.05 17,9A5,5 0 0,0 12,4Z" />
|
||||||
|
</vector>
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<menu
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_update"
|
||||||
|
android:orderInCategory="50"
|
||||||
|
android:title="@string/update"
|
||||||
|
app:showAsAction="never" />
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_settings"
|
||||||
|
android:orderInCategory="90"
|
||||||
|
android:title="@string/settings"
|
||||||
|
app:showAsAction="never" />
|
||||||
|
|
||||||
|
</menu>
|
||||||
@ -0,0 +1,27 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<PreferenceScreen
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||||
|
|
||||||
|
<SwitchPreferenceCompat
|
||||||
|
android:defaultValue="false"
|
||||||
|
android:key="suggestions"
|
||||||
|
android:summary="@string/suggestions_summary"
|
||||||
|
android:title="@string/suggestions_enable"
|
||||||
|
app:iconSpaceReserved="false" />
|
||||||
|
|
||||||
|
<SwitchPreferenceCompat
|
||||||
|
android:dependency="suggestions"
|
||||||
|
android:key="suggestions_exclude_nsfw"
|
||||||
|
android:title="@string/exclude_nsfw_from_suggestions"
|
||||||
|
app:iconSpaceReserved="false" />
|
||||||
|
|
||||||
|
<Preference
|
||||||
|
android:icon="@drawable/ic_info_outline"
|
||||||
|
android:key="track_warning"
|
||||||
|
android:persistent="false"
|
||||||
|
android:selectable="false"
|
||||||
|
android:summary="@string/suggestions_info"
|
||||||
|
app:allowDividerAbove="true" />
|
||||||
|
|
||||||
|
</PreferenceScreen>
|
||||||
Loading…
Reference in New Issue