Remove from favourites via popup menu
This commit is contained in:
@@ -37,6 +37,9 @@ abstract class FavouritesDao {
|
||||
@Update
|
||||
abstract suspend fun update(favourite: FavouriteEntity): Int
|
||||
|
||||
@Query("DELETE FROM favourites WHERE manga_id = :mangaId")
|
||||
abstract suspend fun delete(mangaId: Long)
|
||||
|
||||
@Query("DELETE FROM favourites WHERE manga_id = :mangaId AND category_id = :categoryId")
|
||||
abstract suspend fun delete(categoryId: Long, mangaId: Long)
|
||||
|
||||
|
||||
@@ -93,6 +93,11 @@ class FavouritesRepository(private val db: MangaDatabase) {
|
||||
notifyFavouritesChanged(manga.id)
|
||||
}
|
||||
|
||||
suspend fun removeFromFavourites(manga: Manga) {
|
||||
db.favouritesDao.delete(manga.id)
|
||||
notifyFavouritesChanged(manga.id)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private val listeners = ArraySet<OnFavouritesChangeListener>()
|
||||
|
||||
@@ -28,7 +28,7 @@ class MangaListDetailsHolder(
|
||||
imageRequest?.dispose()
|
||||
textView_title.text = data.title
|
||||
textView_subtitle.textAndVisible = data.altTitle
|
||||
imageView_cover.newImageRequest(data.coverUrl)
|
||||
imageRequest = imageView_cover.newImageRequest(data.coverUrl)
|
||||
.placeholder(R.drawable.ic_placeholder)
|
||||
.fallback(R.drawable.ic_placeholder)
|
||||
.error(R.drawable.ic_placeholder)
|
||||
|
||||
@@ -1,33 +1,54 @@
|
||||
package org.koitharu.kotatsu.ui.list.favourites
|
||||
|
||||
import android.view.Menu
|
||||
import android.view.MenuInflater
|
||||
import android.view.MenuItem
|
||||
import kotlinx.android.synthetic.main.fragment_list.*
|
||||
import moxy.ktx.moxyPresenter
|
||||
import org.koin.android.ext.android.get
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.model.Manga
|
||||
import org.koitharu.kotatsu.ui.list.MangaListFragment
|
||||
import org.koitharu.kotatsu.ui.list.MangaListView
|
||||
import org.koitharu.kotatsu.utils.ext.withArgs
|
||||
|
||||
class FavouritesListFragment : MangaListFragment<Unit>(),
|
||||
MangaListView<Unit> {
|
||||
class FavouritesListFragment : MangaListFragment<Unit>(), MangaListView<Unit> {
|
||||
|
||||
private val presenter by moxyPresenter(factory = ::FavouritesListPresenter)
|
||||
private val presenter by moxyPresenter {
|
||||
FavouritesListPresenter(categoryId, get())
|
||||
}
|
||||
|
||||
private val categoryId: Long
|
||||
get() = arguments?.getLong(ARG_CATEGORY_ID) ?: 0L
|
||||
|
||||
override fun onRequestMoreItems(offset: Int) {
|
||||
presenter.loadList(categoryId, offset)
|
||||
presenter.loadList(offset)
|
||||
}
|
||||
|
||||
override fun setUpEmptyListHolder() {
|
||||
textView_holder.setText(if (categoryId == 0L) {
|
||||
R.string.you_have_not_favourites_yet
|
||||
} else {
|
||||
R.string.favourites_category_empty
|
||||
})
|
||||
textView_holder.setText(
|
||||
if (categoryId == 0L) {
|
||||
R.string.you_have_not_favourites_yet
|
||||
} else {
|
||||
R.string.favourites_category_empty
|
||||
}
|
||||
)
|
||||
textView_holder.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0)
|
||||
}
|
||||
|
||||
override fun onCreatePopupMenu(inflater: MenuInflater, menu: Menu, data: Manga) {
|
||||
super.onCreatePopupMenu(inflater, menu, data)
|
||||
inflater.inflate(R.menu.popup_favourites, menu)
|
||||
}
|
||||
|
||||
override fun onPopupMenuItemSelected(item: MenuItem, data: Manga) = when(item.itemId) {
|
||||
R.id.action_remove -> {
|
||||
presenter.removeFromFavourites(data)
|
||||
true
|
||||
}
|
||||
else -> super.onPopupMenuItemSelected(item, data)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private const val ARG_CATEGORY_ID = "category_id"
|
||||
|
||||
@@ -4,18 +4,19 @@ import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.launch
|
||||
import moxy.InjectViewState
|
||||
import moxy.presenterScope
|
||||
import org.koin.core.component.get
|
||||
import org.koitharu.kotatsu.BuildConfig
|
||||
import org.koitharu.kotatsu.core.model.Manga
|
||||
import org.koitharu.kotatsu.domain.favourites.FavouritesRepository
|
||||
import org.koitharu.kotatsu.ui.base.BasePresenter
|
||||
import org.koitharu.kotatsu.ui.list.MangaListView
|
||||
|
||||
@InjectViewState
|
||||
class FavouritesListPresenter : BasePresenter<MangaListView<Unit>>() {
|
||||
class FavouritesListPresenter(
|
||||
private val categoryId: Long,
|
||||
private val repository: FavouritesRepository
|
||||
) : BasePresenter<MangaListView<Unit>>() {
|
||||
|
||||
private val repository = get<FavouritesRepository>()
|
||||
|
||||
fun loadList(categoryId: Long, offset: Int) {
|
||||
fun loadList(offset: Int) {
|
||||
presenterScope.launch {
|
||||
viewState.onLoadingStateChanged(true)
|
||||
try {
|
||||
@@ -44,4 +45,15 @@ class FavouritesListPresenter : BasePresenter<MangaListView<Unit>>() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun removeFromFavourites(manga: Manga) {
|
||||
launchJob {
|
||||
if (categoryId == 0L) {
|
||||
repository.removeFromFavourites(manga)
|
||||
} else {
|
||||
repository.removeFromCategory(manga, categoryId)
|
||||
}
|
||||
viewState.onItemRemoved(manga)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,11 +31,7 @@ fun <T> List<T>.medianOrNull(): T? = when {
|
||||
}
|
||||
|
||||
inline fun <T, R> Collection<T>.mapToSet(transform: (T) -> R): Set<R> {
|
||||
val destination = ArraySet<R>(size)
|
||||
for (item in this) {
|
||||
destination.add(transform(item))
|
||||
}
|
||||
return destination
|
||||
return mapTo(ArraySet(size), transform)
|
||||
}
|
||||
|
||||
inline fun <T, R> Collection<T>.mapNotNullToSet(transform: (T) -> R?): Set<R> {
|
||||
|
||||
9
app/src/main/res/menu/popup_favourites.xml
Normal file
9
app/src/main/res/menu/popup_favourites.xml
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<item
|
||||
android:id="@+id/action_remove"
|
||||
android:title="@string/remove" />
|
||||
|
||||
</menu>
|
||||
Reference in New Issue
Block a user