Optimize chapters list

This commit is contained in:
Koitharu
2021-10-20 18:00:43 +03:00
parent 78f2a13761
commit 977da5b1b4
8 changed files with 34 additions and 41 deletions

1
.gitignore vendored
View File

@@ -3,6 +3,7 @@
/local.properties
/.idea/caches
/.idea/libraries
/.idea/dictionaries
/.idea/modules.xml
/.idea/misc.xml
/.idea/workspace.xml

View File

@@ -1,16 +0,0 @@
<component name="ProjectDictionaryState">
<dictionary name="admin">
<words>
<w>amoled</w>
<w>chucker</w>
<w>desu</w>
<w>failsafe</w>
<w>koin</w>
<w>kotatsu</w>
<w>manga</w>
<w>snackbar</w>
<w>upsert</w>
<w>webtoon</w>
</words>
</dictionary>
</component>

View File

@@ -171,6 +171,7 @@ class DetailsViewModel(
branch: String?,
): List<ChapterListItem> {
val result = ArrayList<ChapterListItem>(chapters.size)
val dateFormat = settings.dateFormat()
val currentIndex = chapters.indexOfFirst { it.id == currentId }
val firstNewIndex = chapters.size - newCount
for (i in chapters.indices) {
@@ -185,7 +186,8 @@ class DetailsViewModel(
i < currentIndex -> ChapterExtra.READ
else -> ChapterExtra.UNREAD
},
isMissing = false
isMissing = false,
dateFormat = dateFormat,
)
}
return result
@@ -202,6 +204,7 @@ class DetailsViewModel(
val result = ArrayList<ChapterListItem>(sourceChapters.size)
val currentIndex = sourceChapters.indexOfFirst { it.id == currentId }
val firstNewIndex = sourceChapters.size - newCount
val dateFormat = settings.dateFormat()
for (i in sourceChapters.indices) {
val chapter = sourceChapters[i]
if (chapter.branch != branch) {
@@ -215,7 +218,8 @@ class DetailsViewModel(
i < currentIndex -> ChapterExtra.READ
else -> ChapterExtra.UNREAD
},
isMissing = false
isMissing = false,
dateFormat = dateFormat,
) ?: chapter.toListItem(
extra = when {
i >= firstNewIndex -> ChapterExtra.NEW
@@ -223,13 +227,14 @@ class DetailsViewModel(
i < currentIndex -> ChapterExtra.READ
else -> ChapterExtra.UNREAD
},
isMissing = true
isMissing = true,
dateFormat = dateFormat,
)
}
if (chaptersMap.isNotEmpty()) { // some chapters on device but not online source
result.ensureCapacity(result.size + chaptersMap.size)
chaptersMap.values.mapTo(result) {
it.toListItem(ChapterExtra.UNREAD, false)
it.toListItem(ChapterExtra.UNREAD, false, dateFormat)
}
result.sortBy { it.chapter.number }
}

View File

@@ -1,16 +1,13 @@
package org.koitharu.kotatsu.details.ui.adapter
import android.text.SpannableStringBuilder
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
import org.koin.core.context.GlobalContext
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.base.ui.list.OnListItemClickListener
import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.databinding.ItemChapterBinding
import org.koitharu.kotatsu.details.ui.model.ChapterListItem
import org.koitharu.kotatsu.history.domain.ChapterExtra
import org.koitharu.kotatsu.utils.ext.getThemeColor
import java.util.*
import org.koitharu.kotatsu.utils.ext.textAndVisible
fun chapterListItemAD(
clickListener: OnListItemClickListener<ChapterListItem>,
@@ -25,23 +22,10 @@ fun chapterListItemAD(
clickListener.onItemLongClick(item, it)
}
bind { payload ->
bind {
binding.textViewTitle.text = item.chapter.name
binding.textViewNumber.text = item.chapter.number.toString()
val settings = GlobalContext.get().get<AppSettings>()
val descriptions = mutableListOf<CharSequence>()
val dateFormat = settings.dateFormat()
if (item.chapter.uploadDate > 0) {
descriptions.add(dateFormat.format(Date(item.chapter.uploadDate)))
}
if (!item.chapter.scanlator.isNullOrBlank()) {
descriptions.add(item.chapter.scanlator!!)
}
if (descriptions.isNotEmpty()) {
binding.textViewDescription.text = descriptions.joinTo(SpannableStringBuilder(), "")
} else {
binding.textViewDescription.text = ""
}
binding.textViewDescription.textAndVisible = item.description()
when (item.extra) {
ChapterExtra.UNREAD -> {
binding.textViewNumber.setBackgroundResource(R.drawable.bg_badge_default)

View File

@@ -7,4 +7,15 @@ data class ChapterListItem(
val chapter: MangaChapter,
val extra: ChapterExtra,
val isMissing: Boolean,
)
val uploadDate: String?,
) {
fun description(): CharSequence? {
val scanlator = chapter.scanlator?.takeUnless { it.isBlank() }
return when {
uploadDate != null && scanlator != null -> "$uploadDate$scanlator"
scanlator != null -> scanlator
else -> uploadDate
}
}
}

View File

@@ -2,12 +2,15 @@ package org.koitharu.kotatsu.details.ui.model
import org.koitharu.kotatsu.core.model.MangaChapter
import org.koitharu.kotatsu.history.domain.ChapterExtra
import java.text.DateFormat
fun MangaChapter.toListItem(
extra: ChapterExtra,
isMissing: Boolean,
dateFormat: DateFormat,
) = ChapterListItem(
chapter = this,
extra = extra,
isMissing = isMissing,
uploadDate = if (uploadDate != 0L) dateFormat.format(uploadDate) else null
)

View File

@@ -9,10 +9,12 @@ import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import org.koin.android.ext.android.get
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.base.ui.AlertDialogFragment
import org.koitharu.kotatsu.base.ui.list.OnListItemClickListener
import org.koitharu.kotatsu.core.model.MangaChapter
import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.databinding.DialogChaptersBinding
import org.koitharu.kotatsu.details.ui.adapter.ChaptersAdapter
import org.koitharu.kotatsu.details.ui.model.ChapterListItem
@@ -45,6 +47,7 @@ class ChaptersDialog : AlertDialogFragment<DialogChaptersBinding>(),
}
val currentId = arguments?.getLong(ARG_CURRENT_ID, 0L) ?: 0L
val currentPosition = chapters.indexOfFirst { it.id == currentId }
val dateFormat = get<AppSettings>().dateFormat()
binding.recyclerViewChapters.adapter = ChaptersAdapter(this).apply {
setItems(chapters.mapIndexed { index, chapter ->
chapter.toListItem(
@@ -53,7 +56,8 @@ class ChaptersDialog : AlertDialogFragment<DialogChaptersBinding>(),
index == currentPosition -> ChapterExtra.CURRENT
else -> ChapterExtra.UNREAD
},
isMissing = false
isMissing = false,
dateFormat = dateFormat,
)
}) {
if (currentPosition >= 0) {

View File

@@ -40,6 +40,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView_number"
app:layout_constraintTop_toTopOf="parent"
app:layout_goneMarginBottom="8dp"
tools:text="@tools:sample/lorem[15]" />
<TextView