Optimize chapters list

pull/72/head
Koitharu 5 years ago
parent 78f2a13761
commit 977da5b1b4
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

1
.gitignore vendored

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

@ -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>

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

@ -1,16 +1,13 @@
package org.koitharu.kotatsu.details.ui.adapter package org.koitharu.kotatsu.details.ui.adapter
import android.text.SpannableStringBuilder
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
import org.koin.core.context.GlobalContext
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.base.ui.list.OnListItemClickListener 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.databinding.ItemChapterBinding
import org.koitharu.kotatsu.details.ui.model.ChapterListItem import org.koitharu.kotatsu.details.ui.model.ChapterListItem
import org.koitharu.kotatsu.history.domain.ChapterExtra import org.koitharu.kotatsu.history.domain.ChapterExtra
import org.koitharu.kotatsu.utils.ext.getThemeColor import org.koitharu.kotatsu.utils.ext.getThemeColor
import java.util.* import org.koitharu.kotatsu.utils.ext.textAndVisible
fun chapterListItemAD( fun chapterListItemAD(
clickListener: OnListItemClickListener<ChapterListItem>, clickListener: OnListItemClickListener<ChapterListItem>,
@ -25,23 +22,10 @@ fun chapterListItemAD(
clickListener.onItemLongClick(item, it) clickListener.onItemLongClick(item, it)
} }
bind { payload -> bind {
binding.textViewTitle.text = item.chapter.name binding.textViewTitle.text = item.chapter.name
binding.textViewNumber.text = item.chapter.number.toString() binding.textViewNumber.text = item.chapter.number.toString()
val settings = GlobalContext.get().get<AppSettings>() binding.textViewDescription.textAndVisible = item.description()
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 = ""
}
when (item.extra) { when (item.extra) {
ChapterExtra.UNREAD -> { ChapterExtra.UNREAD -> {
binding.textViewNumber.setBackgroundResource(R.drawable.bg_badge_default) binding.textViewNumber.setBackgroundResource(R.drawable.bg_badge_default)

@ -7,4 +7,15 @@ data class ChapterListItem(
val chapter: MangaChapter, val chapter: MangaChapter,
val extra: ChapterExtra, val extra: ChapterExtra,
val isMissing: Boolean, 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
}
}
}

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

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

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

Loading…
Cancel
Save