Migrate PageIndicatorTextView to Compose

Probably closes #7798

(cherry picked from commit 3c79777e66d701958c2a20dfb5ccbdfef6e5c294)

# Conflicts:
#	app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt
#	app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt
This commit is contained in:
arkon 2023-05-03 16:18:25 -04:00 committed by Jobobby04
parent 18f65d4ca4
commit c320daf832
7 changed files with 83 additions and 70 deletions

View File

@ -0,0 +1,46 @@
package eu.kanade.presentation.reader
import androidx.compose.foundation.layout.Box
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.text.ExperimentalTextApi
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
@OptIn(ExperimentalTextApi::class)
@Composable
fun PageIndicatorText(
// SY -->
currentPage: String,
// SY <--
totalPages: Int,
) {
if (currentPage.isEmpty() || totalPages <= 0) return
val text = "$currentPage / $totalPages"
Box {
Text(
text = text,
color = Color(45, 45, 45),
fontSize = MaterialTheme.typography.bodySmall.fontSize,
fontWeight = FontWeight.Bold,
letterSpacing = 1.sp,
style = TextStyle.Default.copy(
drawStyle = Stroke(width = 4f),
),
)
Text(
text = text,
color = Color(235, 235, 235),
fontSize = MaterialTheme.typography.bodySmall.fontSize,
fontWeight = FontWeight.Bold,
letterSpacing = 1.sp,
)
}
}

View File

@ -339,6 +339,7 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
else -> {
try {
val loggedServices = trackManager.services.filter { it.isLogged }
val newChapters = updateManga(manga, loggedServices)
.sortedByDescending { it.sourceOrder }

View File

@ -1,52 +0,0 @@
package eu.kanade.tachiyomi.ui.reader
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ScaleXSpan
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import eu.kanade.tachiyomi.widget.OutlineSpan
/**
* Page indicator found at the bottom of the reader
*/
class PageIndicatorTextView(
context: Context,
attrs: AttributeSet? = null,
) : AppCompatTextView(context, attrs) {
init {
setTextColor(fillColor)
}
@SuppressLint("SetTextI18n")
override fun setText(text: CharSequence?, type: BufferType?) {
// Add spaces at the start & end of the text, otherwise the stroke is cut-off because it's
// not taken into account when measuring the text (view's padding doesn't help).
val currText = " $text "
// Also add a bit of spacing between each character, as the stroke overlaps them
val finalText = SpannableString(currText.asIterable().joinToString("\u00A0")).apply {
// Apply text outline
setSpan(spanOutline, 1, length - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
for (i in 1..lastIndex step 2) {
setSpan(ScaleXSpan(0.2f), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
}
super.setText(finalText, BufferType.SPANNABLE)
}
}
private val fillColor = Color.rgb(235, 235, 235)
private val strokeColor = Color.rgb(45, 45, 45)
// A span object with text outlining properties
private val spanOutline = OutlineSpan(
strokeColor = strokeColor,
strokeWidth = 4f,
)

View File

@ -35,6 +35,8 @@ import android.widget.TextView
import android.widget.Toast
import androidx.activity.viewModels
import androidx.annotation.ColorInt
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.core.graphics.ColorUtils
import androidx.core.net.toUri
import androidx.core.transition.doOnEnd
@ -56,6 +58,7 @@ import dev.chrisbanes.insetter.applyInsetter
import eu.kanade.domain.base.BasePreferences
import eu.kanade.domain.manga.model.orientationType
import eu.kanade.domain.manga.model.readingModeType
import eu.kanade.presentation.reader.PageIndicatorText
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.notification.NotificationReceiver
import eu.kanade.tachiyomi.data.notification.Notifications
@ -96,6 +99,7 @@ import eu.kanade.tachiyomi.util.system.toShareIntent
import eu.kanade.tachiyomi.util.system.toast
import eu.kanade.tachiyomi.util.view.copy
import eu.kanade.tachiyomi.util.view.popupMenu
import eu.kanade.tachiyomi.util.view.setComposeContent
import eu.kanade.tachiyomi.util.view.setTooltip
import eu.kanade.tachiyomi.widget.listener.SimpleAnimationListener
import exh.log.xLogE
@ -527,6 +531,15 @@ class ReaderActivity : BaseActivity() {
}
}
binding.pageNumber.setComposeContent {
val state by viewModel.state.collectAsState()
PageIndicatorText(
currentPage = state.currentPage,
totalPages = state.viewerChapters?.currChapter?.pages?.size ?: -1,
)
}
// SY -->
// Init listeners on bottom menu
val listener = object : Slider.OnSliderTouchListener {
@ -1311,7 +1324,7 @@ class ReaderActivity : BaseActivity() {
* other cases are handled with chapter transitions on the viewers and chapter preloading.
*/
@Suppress("DEPRECATION")
fun setProgressDialog(show: Boolean) {
private fun setProgressDialog(show: Boolean) {
progressDialog?.dismiss()
progressDialog = if (show) {
ProgressDialog.show(this, null, getString(R.string.loading), true)
@ -1359,19 +1372,16 @@ class ReaderActivity : BaseActivity() {
*/
@SuppressLint("SetTextI18n")
fun onPageSelected(page: ReaderPage, hasExtraPage: Boolean = false) {
val newChapter = viewModel.onPageSelected(page, hasExtraPage)
val pages = page.chapter.pages ?: return
// SY -->
val currentPage = if (hasExtraPage) {
val invertDoublePage = (viewer as? PagerViewer)?.config?.invertDoublePages ?: false
if (resources.isLTR xor invertDoublePage) "${page.number}-${page.number + 1}" else "${page.number + 1}-${page.number}"
} else {
"${page.number}"
}
// Set bottom page number
binding.pageNumber.text = "$currentPage/${pages.size}"
// binding.pageText.text = "${page.number}/${pages.size}"
viewModel.onPageSelected(page, hasExtraPage, currentPage)
// SY <--
val pages = page.chapter.pages ?: return
// Set page numbers
if (viewer !is R2LPagerViewer) {

View File

@ -485,7 +485,7 @@ class ReaderViewModel(
* read, update tracking services, enqueue downloaded chapter deletion, and updating the active chapter if this
* [page]'s chapter is different from the currently active.
*/
fun onPageSelected(page: ReaderPage, hasExtraPage: Boolean) {
fun onPageSelected(page: ReaderPage, hasExtraPage: Boolean, currentPage: String) {
val currentChapters = state.value.viewerChapters ?: return
val selectedChapter = page.chapter
@ -496,6 +496,13 @@ class ReaderViewModel(
}
// Save last page read and mark as read if needed
mutableState.update {
it.copy(
// SY -->
currentPage = currentPage,
// SY <--
)
}
selectedChapter.chapter.last_page_read = page.index
val shouldTrack = !incognitoMode || hasTrackers
if (
@ -1102,6 +1109,7 @@ class ReaderViewModel(
val viewerChapters: ViewerChapters? = null,
val isLoadingAdjacentChapter: Boolean = false,
// SY -->
val currentPage: String = "",
val meta: RaisedSearchMetadata? = null,
val mergedManga: Map<Long, Manga>? = null,
// SY <--

View File

@ -15,9 +15,13 @@ object LocaleHelper {
* Sorts by display name, except keeps the "all" (displayed as "Multi") locale at the top.
*/
val comparator = { a: String, b: String ->
if (a == "all") -1
else if (b == "all") 1
else getDisplayName(a).compareTo(getDisplayName(b))
if (a == "all") {
-1
} else if (b == "all") {
1
} else {
getDisplayName(a).compareTo(getDisplayName(b))
}
}
/**

View File

@ -17,15 +17,11 @@
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants" />
<eu.kanade.tachiyomi.ui.reader.PageIndicatorTextView
<androidx.compose.ui.platform.ComposeView
android:id="@+id/page_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:padding="4dp"
android:textAppearance="?attr/textAppearanceBodySmall"
android:textDirection="ltr"
android:textStyle="bold" />
android:layout_gravity="bottom|center_horizontal" />
</FrameLayout>