Suggestions
parent
0e74d6e017
commit
f0e56c4b6a
@ -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,28 @@
|
|||||||
|
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>>
|
||||||
|
|
||||||
|
@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,38 @@
|
|||||||
|
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 replace(suggestions: Iterable<MangaSuggestion>) {
|
||||||
|
db.withTransaction {
|
||||||
|
db.suggestionDao.deleteAll()
|
||||||
|
suggestions.forEach { x ->
|
||||||
|
db.mangaDao.upsert(MangaEntity.from(x.manga))
|
||||||
|
db.suggestionDao.upsert(
|
||||||
|
SuggestionEntity(
|
||||||
|
mangaId = x.manga.id,
|
||||||
|
relevance = x.relevance,
|
||||||
|
createdAt = System.currentTimeMillis(),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,28 @@
|
|||||||
|
package org.koitharu.kotatsu.suggestions.ui
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.View
|
||||||
|
import org.koin.androidx.viewmodel.ext.android.viewModel
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.list.ui.MangaListFragment
|
||||||
|
|
||||||
|
class SuggestionsFragment : MangaListFragment() {
|
||||||
|
|
||||||
|
override val viewModel by viewModel<SuggestionsViewModel>(mode = LazyThreadSafetyMode.NONE)
|
||||||
|
override val isSwipeRefreshEnabled = false
|
||||||
|
|
||||||
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onScrolledToEnd() = Unit
|
||||||
|
|
||||||
|
override fun getTitle(): CharSequence? {
|
||||||
|
return context?.getString(R.string.suggestions)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
fun newInstance() = SuggestionsFragment()
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,53 @@
|
|||||||
|
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.model.Manga
|
||||||
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||||
|
import org.koitharu.kotatsu.core.prefs.ListMode
|
||||||
|
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) {
|
||||||
|
|
||||||
|
override val content = combine(
|
||||||
|
repository.observeAll(),
|
||||||
|
createListModeFlow()
|
||||||
|
) { list, mode ->
|
||||||
|
when {
|
||||||
|
list.isEmpty() -> listOf(EmptyState(R.string.text_suggestion_holder))
|
||||||
|
else -> mapList(list, 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
|
||||||
|
|
||||||
|
private fun mapList(
|
||||||
|
list: List<Manga>,
|
||||||
|
mode: ListMode,
|
||||||
|
): List<ListModel> = list.map { manga ->
|
||||||
|
when (mode) {
|
||||||
|
ListMode.LIST -> manga.toListModel()
|
||||||
|
ListMode.DETAILED_LIST -> manga.toListDetailedModel()
|
||||||
|
ListMode.GRID -> manga.toGridModel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,68 @@
|
|||||||
|
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.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>()
|
||||||
|
|
||||||
|
override suspend fun doWork(): Result {
|
||||||
|
val rawResults = ArrayList<Manga>()
|
||||||
|
val allTags = historyRepository.getAllTags()
|
||||||
|
val tagsBySources = allTags.groupBy { x -> x.source }
|
||||||
|
for ((source, tags) in tagsBySources) {
|
||||||
|
val repo = mangaRepositoryOf(source)
|
||||||
|
tags.flatMapTo(rawResults) { tag ->
|
||||||
|
repo.getList(
|
||||||
|
offset = 0,
|
||||||
|
sortOrder = SortOrder.UPDATED,
|
||||||
|
tag = tag,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
suggestionRepository.replace(
|
||||||
|
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(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return Result.success()
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
private const val TAG = "suggestions"
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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>
|
||||||
Loading…
Reference in New Issue