Skip to content

Commit

Permalink
fix(globalsearch): Update favorite state in real time
Browse files Browse the repository at this point in the history
  • Loading branch information
null2264 committed Dec 16, 2024
1 parent 3651c2a commit 8ec29bf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is simplified version of [Keep a Changelog](https://keepachangelog.co
### Fixes
- Fix new chapters not showing up in `Recents > Grouped`
- Add potential workaround for duplicate chapter bug
- Fix favorite state is not being updated when browsing source

### Other
- Update dependency androidx.compose:compose-bom to v2024.12.01
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,25 @@ import eu.davidea.flexibleadapter.items.AbstractFlexibleItem
import eu.davidea.flexibleadapter.items.IFlexible
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.domain.manga.models.Manga
import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import uy.kohesive.injekt.injectLazy
import yokai.domain.manga.interactor.GetManga

class GlobalSearchMangaItem(val manga: Manga) : AbstractFlexibleItem<GlobalSearchMangaHolder>() {
// FIXME: Migrate to compose
class GlobalSearchMangaItem(
initialManga: Manga
) : AbstractFlexibleItem<GlobalSearchMangaHolder>() {

private val getManga: GetManga by injectLazy()

var manga: Manga = initialManga
private set
// TODO: Could potentially cause memleak, test it with leakcanary before deploying to stable!
private val scope = MainScope()
private var job: Job? = null

override fun getLayoutRes(): Int {
return R.layout.source_global_search_controller_card_item
Expand All @@ -24,7 +41,23 @@ class GlobalSearchMangaItem(val manga: Manga) : AbstractFlexibleItem<GlobalSearc
position: Int,
payloads: MutableList<Any?>?,
) {
holder.bind(manga)
if (job == null) holder.bind(manga)
job?.cancel()
job = scope.launch {
getManga.subscribeByUrlAndSource(manga.url, manga.source).collectLatest {
manga = it ?: return@collectLatest
holder.bind(manga)
}
}
}

override fun unbindViewHolder(
adapter: FlexibleAdapter<IFlexible<RecyclerView.ViewHolder>>?,
holder: GlobalSearchMangaHolder?,
position: Int
) {
job?.cancel()
job = null
}

override fun equals(other: Any?): Boolean {
Expand Down

0 comments on commit 8ec29bf

Please sign in to comment.