Remove from favourites via popup menu

pull/26/head
Koitharu 6 years ago
parent d135898b49
commit eaac271143

@ -37,6 +37,9 @@ abstract class FavouritesDao {
@Update @Update
abstract suspend fun update(favourite: FavouriteEntity): Int 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") @Query("DELETE FROM favourites WHERE manga_id = :mangaId AND category_id = :categoryId")
abstract suspend fun delete(categoryId: Long, mangaId: Long) abstract suspend fun delete(categoryId: Long, mangaId: Long)

@ -93,6 +93,11 @@ class FavouritesRepository(private val db: MangaDatabase) {
notifyFavouritesChanged(manga.id) notifyFavouritesChanged(manga.id)
} }
suspend fun removeFromFavourites(manga: Manga) {
db.favouritesDao.delete(manga.id)
notifyFavouritesChanged(manga.id)
}
companion object { companion object {
private val listeners = ArraySet<OnFavouritesChangeListener>() private val listeners = ArraySet<OnFavouritesChangeListener>()

@ -28,7 +28,7 @@ class MangaListDetailsHolder(
imageRequest?.dispose() imageRequest?.dispose()
textView_title.text = data.title textView_title.text = data.title
textView_subtitle.textAndVisible = data.altTitle textView_subtitle.textAndVisible = data.altTitle
imageView_cover.newImageRequest(data.coverUrl) imageRequest = imageView_cover.newImageRequest(data.coverUrl)
.placeholder(R.drawable.ic_placeholder) .placeholder(R.drawable.ic_placeholder)
.fallback(R.drawable.ic_placeholder) .fallback(R.drawable.ic_placeholder)
.error(R.drawable.ic_placeholder) .error(R.drawable.ic_placeholder)

@ -1,33 +1,54 @@
package org.koitharu.kotatsu.ui.list.favourites 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 kotlinx.android.synthetic.main.fragment_list.*
import moxy.ktx.moxyPresenter import moxy.ktx.moxyPresenter
import org.koin.android.ext.android.get
import org.koitharu.kotatsu.R 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.MangaListFragment
import org.koitharu.kotatsu.ui.list.MangaListView import org.koitharu.kotatsu.ui.list.MangaListView
import org.koitharu.kotatsu.utils.ext.withArgs import org.koitharu.kotatsu.utils.ext.withArgs
class FavouritesListFragment : MangaListFragment<Unit>(), class FavouritesListFragment : MangaListFragment<Unit>(), MangaListView<Unit> {
MangaListView<Unit> {
private val presenter by moxyPresenter(factory = ::FavouritesListPresenter) private val presenter by moxyPresenter {
FavouritesListPresenter(categoryId, get())
}
private val categoryId: Long private val categoryId: Long
get() = arguments?.getLong(ARG_CATEGORY_ID) ?: 0L get() = arguments?.getLong(ARG_CATEGORY_ID) ?: 0L
override fun onRequestMoreItems(offset: Int) { override fun onRequestMoreItems(offset: Int) {
presenter.loadList(categoryId, offset) presenter.loadList(offset)
} }
override fun setUpEmptyListHolder() { override fun setUpEmptyListHolder() {
textView_holder.setText(if (categoryId == 0L) { textView_holder.setText(
R.string.you_have_not_favourites_yet if (categoryId == 0L) {
} else { R.string.you_have_not_favourites_yet
R.string.favourites_category_empty } else {
}) R.string.favourites_category_empty
}
)
textView_holder.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0) 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 { companion object {
private const val ARG_CATEGORY_ID = "category_id" private const val ARG_CATEGORY_ID = "category_id"

@ -4,18 +4,19 @@ import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import moxy.InjectViewState import moxy.InjectViewState
import moxy.presenterScope import moxy.presenterScope
import org.koin.core.component.get
import org.koitharu.kotatsu.BuildConfig import org.koitharu.kotatsu.BuildConfig
import org.koitharu.kotatsu.core.model.Manga
import org.koitharu.kotatsu.domain.favourites.FavouritesRepository import org.koitharu.kotatsu.domain.favourites.FavouritesRepository
import org.koitharu.kotatsu.ui.base.BasePresenter import org.koitharu.kotatsu.ui.base.BasePresenter
import org.koitharu.kotatsu.ui.list.MangaListView import org.koitharu.kotatsu.ui.list.MangaListView
@InjectViewState @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(offset: Int) {
fun loadList(categoryId: Long, offset: Int) {
presenterScope.launch { presenterScope.launch {
viewState.onLoadingStateChanged(true) viewState.onLoadingStateChanged(true)
try { 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> { inline fun <T, R> Collection<T>.mapToSet(transform: (T) -> R): Set<R> {
val destination = ArraySet<R>(size) return mapTo(ArraySet(size), transform)
for (item in this) {
destination.add(transform(item))
}
return destination
} }
inline fun <T, R> Collection<T>.mapNotNullToSet(transform: (T) -> R?): Set<R> { inline fun <T, R> Collection<T>.mapNotNullToSet(transform: (T) -> R?): Set<R> {

@ -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>
Loading…
Cancel
Save