Fix new chapters counter in details screen
This commit is contained in:
@@ -39,7 +39,6 @@ import org.koitharu.kotatsu.core.os.AppShortcutManager
|
|||||||
import org.koitharu.kotatsu.core.parser.MangaIntent
|
import org.koitharu.kotatsu.core.parser.MangaIntent
|
||||||
import org.koitharu.kotatsu.core.ui.BaseActivity
|
import org.koitharu.kotatsu.core.ui.BaseActivity
|
||||||
import org.koitharu.kotatsu.core.ui.util.MenuInvalidator
|
import org.koitharu.kotatsu.core.ui.util.MenuInvalidator
|
||||||
import org.koitharu.kotatsu.core.util.ViewBadge
|
|
||||||
import org.koitharu.kotatsu.core.util.ext.doOnExpansionsChanged
|
import org.koitharu.kotatsu.core.util.ext.doOnExpansionsChanged
|
||||||
import org.koitharu.kotatsu.core.util.ext.getAnimationDuration
|
import org.koitharu.kotatsu.core.util.ext.getAnimationDuration
|
||||||
import org.koitharu.kotatsu.core.util.ext.getThemeColor
|
import org.koitharu.kotatsu.core.util.ext.getThemeColor
|
||||||
@@ -75,7 +74,6 @@ class DetailsActivity :
|
|||||||
@Inject
|
@Inject
|
||||||
lateinit var appShortcutManager: AppShortcutManager
|
lateinit var appShortcutManager: AppShortcutManager
|
||||||
|
|
||||||
private lateinit var viewBadge: ViewBadge
|
|
||||||
private var buttonTip: WeakReference<ButtonTip>? = null
|
private var buttonTip: WeakReference<ButtonTip>? = null
|
||||||
|
|
||||||
private val viewModel: DetailsViewModel by viewModels()
|
private val viewModel: DetailsViewModel by viewModels()
|
||||||
@@ -92,8 +90,6 @@ class DetailsActivity :
|
|||||||
viewBinding.buttonRead.setOnLongClickListener(this)
|
viewBinding.buttonRead.setOnLongClickListener(this)
|
||||||
viewBinding.buttonRead.setOnContextClickListenerCompat(this)
|
viewBinding.buttonRead.setOnContextClickListenerCompat(this)
|
||||||
viewBinding.buttonDropdown.setOnClickListener(this)
|
viewBinding.buttonDropdown.setOnClickListener(this)
|
||||||
viewBadge = ViewBadge(viewBinding.buttonRead, this)
|
|
||||||
viewBadge.setMaxCharacterCount(1)
|
|
||||||
|
|
||||||
if (viewBinding.layoutBottom != null) {
|
if (viewBinding.layoutBottom != null) {
|
||||||
val behavior = BottomSheetBehavior.from(checkNotNull(viewBinding.layoutBottom))
|
val behavior = BottomSheetBehavior.from(checkNotNull(viewBinding.layoutBottom))
|
||||||
@@ -114,7 +110,6 @@ class DetailsActivity :
|
|||||||
onBackPressedDispatcher.addCallback(chaptersMenuProvider)
|
onBackPressedDispatcher.addCallback(chaptersMenuProvider)
|
||||||
|
|
||||||
viewModel.manga.filterNotNull().observe(this, ::onMangaUpdated)
|
viewModel.manga.filterNotNull().observe(this, ::onMangaUpdated)
|
||||||
viewModel.newChaptersCount.observe(this, ::onNewChaptersChanged)
|
|
||||||
viewModel.onMangaRemoved.observeEvent(this, ::onMangaRemoved)
|
viewModel.onMangaRemoved.observeEvent(this, ::onMangaRemoved)
|
||||||
viewModel.onError.observeEvent(
|
viewModel.onError.observeEvent(
|
||||||
this,
|
this,
|
||||||
@@ -296,10 +291,6 @@ class DetailsActivity :
|
|||||||
viewBinding.textViewTitle?.text = text
|
viewBinding.textViewTitle?.text = text
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onNewChaptersChanged(newChapters: Int) {
|
|
||||||
viewBadge.counter = newChapters
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun showBranchPopupMenu(v: View) {
|
private fun showBranchPopupMenu(v: View) {
|
||||||
val menu = PopupMenu(v.context, v)
|
val menu = PopupMenu(v.context, v)
|
||||||
val branches = viewModel.branches.value
|
val branches = viewModel.branches.value
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import android.widget.Toast
|
|||||||
import androidx.appcompat.widget.PopupMenu
|
import androidx.appcompat.widget.PopupMenu
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.core.graphics.Insets
|
import androidx.core.graphics.Insets
|
||||||
|
import androidx.core.text.buildSpannedString
|
||||||
|
import androidx.core.text.color
|
||||||
import androidx.core.view.isGone
|
import androidx.core.view.isGone
|
||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
import androidx.core.view.updatePadding
|
import androidx.core.view.updatePadding
|
||||||
@@ -21,6 +23,7 @@ import coil.request.SuccessResult
|
|||||||
import coil.util.CoilUtils
|
import coil.util.CoilUtils
|
||||||
import com.google.android.material.chip.Chip
|
import com.google.android.material.chip.Chip
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
|
import kotlinx.coroutines.flow.combine
|
||||||
import kotlinx.coroutines.flow.filterNotNull
|
import kotlinx.coroutines.flow.filterNotNull
|
||||||
import org.koitharu.kotatsu.R
|
import org.koitharu.kotatsu.R
|
||||||
import org.koitharu.kotatsu.bookmarks.domain.Bookmark
|
import org.koitharu.kotatsu.bookmarks.domain.Bookmark
|
||||||
@@ -37,6 +40,7 @@ import org.koitharu.kotatsu.core.util.FileSize
|
|||||||
import org.koitharu.kotatsu.core.util.ext.crossfade
|
import org.koitharu.kotatsu.core.util.ext.crossfade
|
||||||
import org.koitharu.kotatsu.core.util.ext.drawableTop
|
import org.koitharu.kotatsu.core.util.ext.drawableTop
|
||||||
import org.koitharu.kotatsu.core.util.ext.enqueueWith
|
import org.koitharu.kotatsu.core.util.ext.enqueueWith
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.getThemeColor
|
||||||
import org.koitharu.kotatsu.core.util.ext.ifNullOrEmpty
|
import org.koitharu.kotatsu.core.util.ext.ifNullOrEmpty
|
||||||
import org.koitharu.kotatsu.core.util.ext.isTextTruncated
|
import org.koitharu.kotatsu.core.util.ext.isTextTruncated
|
||||||
import org.koitharu.kotatsu.core.util.ext.observe
|
import org.koitharu.kotatsu.core.util.ext.observe
|
||||||
@@ -70,6 +74,7 @@ import org.koitharu.kotatsu.scrobbling.common.ui.selector.ScrobblingSelectorShee
|
|||||||
import org.koitharu.kotatsu.search.ui.MangaListActivity
|
import org.koitharu.kotatsu.search.ui.MangaListActivity
|
||||||
import org.koitharu.kotatsu.search.ui.SearchActivity
|
import org.koitharu.kotatsu.search.ui.SearchActivity
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
import com.google.android.material.R as materialR
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class DetailsFragment :
|
class DetailsFragment :
|
||||||
@@ -113,9 +118,9 @@ class DetailsFragment :
|
|||||||
viewModel.bookmarks.observe(viewLifecycleOwner, ::onBookmarksChanged)
|
viewModel.bookmarks.observe(viewLifecycleOwner, ::onBookmarksChanged)
|
||||||
viewModel.scrobblingInfo.observe(viewLifecycleOwner, ::onScrobblingInfoChanged)
|
viewModel.scrobblingInfo.observe(viewLifecycleOwner, ::onScrobblingInfoChanged)
|
||||||
viewModel.description.observe(viewLifecycleOwner, ::onDescriptionChanged)
|
viewModel.description.observe(viewLifecycleOwner, ::onDescriptionChanged)
|
||||||
viewModel.chapters.observe(viewLifecycleOwner, ::onChaptersChanged)
|
|
||||||
viewModel.localSize.observe(viewLifecycleOwner, ::onLocalSizeChanged)
|
viewModel.localSize.observe(viewLifecycleOwner, ::onLocalSizeChanged)
|
||||||
viewModel.relatedManga.observe(viewLifecycleOwner, ::onRelatedMangaChanged)
|
viewModel.relatedManga.observe(viewLifecycleOwner, ::onRelatedMangaChanged)
|
||||||
|
combine(viewModel.chapters, viewModel.newChaptersCount, ::Pair).observe(viewLifecycleOwner, ::onChaptersChanged)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onItemClick(item: Bookmark, view: View) {
|
override fun onItemClick(item: Bookmark, view: View) {
|
||||||
@@ -191,14 +196,28 @@ class DetailsFragment :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onChaptersChanged(chapters: List<ChapterListItem>?) {
|
private fun onChaptersChanged(data: Pair<List<ChapterListItem>?, Int>) {
|
||||||
|
val (chapters, newChapters) = data
|
||||||
val infoLayout = requireViewBinding().infoLayout
|
val infoLayout = requireViewBinding().infoLayout
|
||||||
if (chapters.isNullOrEmpty()) {
|
if (chapters.isNullOrEmpty()) {
|
||||||
infoLayout.textViewChapters.isVisible = false
|
infoLayout.textViewChapters.isVisible = false
|
||||||
} else {
|
} else {
|
||||||
val count = chapters.countChaptersByBranch()
|
val count = chapters.countChaptersByBranch()
|
||||||
infoLayout.textViewChapters.isVisible = true
|
infoLayout.textViewChapters.isVisible = true
|
||||||
infoLayout.textViewChapters.text = resources.getQuantityString(R.plurals.chapters, count, count)
|
val chaptersText = resources.getQuantityString(R.plurals.chapters, count, count)
|
||||||
|
infoLayout.textViewChapters.text = if (newChapters == 0) {
|
||||||
|
chaptersText
|
||||||
|
} else {
|
||||||
|
buildSpannedString {
|
||||||
|
append(chaptersText)
|
||||||
|
append(' ')
|
||||||
|
color(infoLayout.textViewChapters.context.getThemeColor(materialR.attr.colorError)) {
|
||||||
|
append("(+")
|
||||||
|
append(newChapters.toString())
|
||||||
|
append(')')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ import org.koitharu.kotatsu.R
|
|||||||
import org.koitharu.kotatsu.bookmarks.domain.Bookmark
|
import org.koitharu.kotatsu.bookmarks.domain.Bookmark
|
||||||
import org.koitharu.kotatsu.bookmarks.domain.BookmarksRepository
|
import org.koitharu.kotatsu.bookmarks.domain.BookmarksRepository
|
||||||
import org.koitharu.kotatsu.core.model.getPreferredBranch
|
import org.koitharu.kotatsu.core.model.getPreferredBranch
|
||||||
import org.koitharu.kotatsu.core.os.NetworkState
|
|
||||||
import org.koitharu.kotatsu.core.parser.MangaIntent
|
import org.koitharu.kotatsu.core.parser.MangaIntent
|
||||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||||
import org.koitharu.kotatsu.core.prefs.ListMode
|
import org.koitharu.kotatsu.core.prefs.ListMode
|
||||||
@@ -71,7 +70,6 @@ class DetailsViewModel @Inject constructor(
|
|||||||
private val relatedMangaUseCase: RelatedMangaUseCase,
|
private val relatedMangaUseCase: RelatedMangaUseCase,
|
||||||
private val extraProvider: ListExtraProvider,
|
private val extraProvider: ListExtraProvider,
|
||||||
private val detailsLoadUseCase: DetailsLoadUseCase,
|
private val detailsLoadUseCase: DetailsLoadUseCase,
|
||||||
networkState: NetworkState,
|
|
||||||
) : BaseViewModel() {
|
) : BaseViewModel() {
|
||||||
|
|
||||||
private val intent = MangaIntent(savedStateHandle)
|
private val intent = MangaIntent(savedStateHandle)
|
||||||
@@ -93,8 +91,13 @@ class DetailsViewModel @Inject constructor(
|
|||||||
val favouriteCategories = interactor.observeIsFavourite(mangaId)
|
val favouriteCategories = interactor.observeIsFavourite(mangaId)
|
||||||
.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, false)
|
.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, false)
|
||||||
|
|
||||||
val newChaptersCount = interactor.observeNewChapters(mangaId)
|
val newChaptersCount = details.flatMapLatest { d ->
|
||||||
.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, 0)
|
if (d?.isLocal == false) {
|
||||||
|
interactor.observeNewChapters(mangaId)
|
||||||
|
} else {
|
||||||
|
flowOf(0)
|
||||||
|
}
|
||||||
|
}.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, 0)
|
||||||
|
|
||||||
private val chaptersQuery = MutableStateFlow("")
|
private val chaptersQuery = MutableStateFlow("")
|
||||||
val selectedBranch = MutableStateFlow<String?>(null)
|
val selectedBranch = MutableStateFlow<String?>(null)
|
||||||
|
|||||||
Reference in New Issue
Block a user