Skip to content

Commit

Permalink
readded monocolor + added button scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
LagradOst committed Aug 10, 2023
1 parent e25ecaa commit 30175b3
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 41 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/lagradost/quicknovel/DataStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const val EPUB_TWELVE_HOUR_TIME: String = "reader_epub_twelve_hour_time"
const val EPUB_FONT: String = "reader_epub_font"
const val EPUB_LANG: String = "reader_epub_lang"
const val EPUB_VOICE: String = "reader_epub_voice"
const val EPUB_READER_TYPE: String = "reader_reader_type"
const val EPUB_CURRENT_POSITION: String = "reader_epub_position"
const val EPUB_CURRENT_POSITION_SCROLL: String = "reader_epub_position_scroll"
const val EPUB_CURRENT_POSITION_SCROLL_CHAR: String = "reader_epub_position_scroll_char"
Expand Down
28 changes: 27 additions & 1 deletion app/src/main/java/com/lagradost/quicknovel/ReadActivity2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import com.lagradost.quicknovel.ui.CONFIG_COLOR
import com.lagradost.quicknovel.ui.CONFIG_FONT
import com.lagradost.quicknovel.ui.CONFIG_FONT_BOLD
import com.lagradost.quicknovel.ui.OrientationType
import com.lagradost.quicknovel.ui.ReadingType
import com.lagradost.quicknovel.ui.ScrollIndex
import com.lagradost.quicknovel.ui.ScrollVisibilityIndex
import com.lagradost.quicknovel.ui.ScrollVisibilityItem
Expand Down Expand Up @@ -656,7 +657,8 @@ class ReadActivity2 : AppCompatActivity(), ColorPickerDialogListener {
defaultFont = binding.readText.typeface,
textColor = viewModel.textColor,
textSize = viewModel.textSize,
textFont = viewModel.textFont
textFont = viewModel.textFont,
backgroundColor = viewModel.backgroundColor
).also { config ->
updateOtherTextConfig(config)
}
Expand All @@ -683,6 +685,9 @@ class ReadActivity2 : AppCompatActivity(), ColorPickerDialogListener {
observe(viewModel.backgroundColorLive) { color ->
binding.root.setBackgroundColor(color)
binding.readOverlay.setBackgroundColor(color)
if (textAdapter.changeBackgroundColor(color)) {
updateTextAdapterConfig()
}
}

observe(viewModel.showBatteryLive) { show ->
Expand Down Expand Up @@ -930,6 +935,27 @@ class ReadActivity2 : AppCompatActivity(), ColorPickerDialogListener {

val fontSizeProgressOffset = 10

binding.readReadingType.setText(viewModel.readerType.stringRes)
binding.readReadingType.setOnLongClickListener {
it.popupMenu(items = listOf(Pair(1, R.string.reset_value)), selectedItemId = null) {
if (itemId == 1) {
binding.readReadingType.setText(ReadingType.DEFAULT.stringRes)
viewModel.readerType = ReadingType.DEFAULT
}
}
return@setOnLongClickListener true
}
binding.readReadingType.setOnClickListener {
it.popupMenu(
items = ReadingType.values().map { v -> v.prefValue to v.stringRes },
selectedItemId = viewModel.readerType.prefValue
) {
val set = ReadingType.fromSpinner(itemId)
binding.readReadingType.setText(set.stringRes)
viewModel.readerType = set
}
}

binding.readSettingsTextSizeText.setOnClickListener {
it.popupMenu(items = listOf(Pair(1, R.string.reset_value)), selectedItemId = null) {
if (itemId == 1) {
Expand Down
95 changes: 73 additions & 22 deletions app/src/main/java/com/lagradost/quicknovel/ReadActivityViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.lagradost.quicknovel.mvvm.map
import com.lagradost.quicknovel.mvvm.safeApiCall
import com.lagradost.quicknovel.providers.RedditProvider
import com.lagradost.quicknovel.ui.OrientationType
import com.lagradost.quicknovel.ui.ReadingType
import com.lagradost.quicknovel.ui.ScrollIndex
import com.lagradost.quicknovel.ui.ScrollVisibilityIndex
import com.lagradost.quicknovel.ui.UiText
Expand Down Expand Up @@ -426,33 +427,69 @@ class ReadActivityViewModel : ViewModel() {
}
}

private fun chapterIdxToSpanDisplay(index: Int): List<SpanDisplay> {
return when (val data = chapterData[index]) {
null -> emptyList()
is Resource.Loading -> {
listOf<SpanDisplay>(LoadingSpanned(data.url, index))
}

is Resource.Success -> {
data.value.spans
}

is Resource.Failure -> listOf<SpanDisplay>(
FailedSpanned(
reason = data.errorString.toUiText(),
index = index,
canReload = data.isNetworkError
)
)
}
}

private fun chapterIdxToSpanDisplayNext(index: Int, fromIndex : Int) : SpanDisplay? {
return when (val data = chapterData[index]) {
is Resource.Loading -> LoadingSpanned(data.url, index)
is Resource.Failure ->
FailedSpanned(
reason = data.errorString.toUiText(),
index = index,
canReload = data.isNetworkError
)
else -> chaptersTitlesInternal.getOrNull(index)?.let { text -> ChapterLoadSpanned(fromIndex, 0, index, text) }
}
}

private fun updateReadArea(seekToDesired: Boolean = false) {
val cIndex = currentIndex
val chapters = ArrayList<SpanDisplay>()
for (idx in cIndex - chapterPaddingBottom..cIndex + chapterPaddingTop) {
val append: List<SpanDisplay> = when (val data = chapterData[idx]) {
null -> emptyList()
is Resource.Loading -> {
listOf<SpanDisplay>(LoadingSpanned(data.url, idx))
when (readerType) {
ReadingType.DEFAULT, ReadingType.INF_SCROLL -> {
for (idx in cIndex - chapterPaddingBottom..cIndex + chapterPaddingTop) {
if (idx < chaptersTitlesInternal.size && idx >= 0)
chapters.add(ChapterStartSpanned(idx, 0, chaptersTitlesInternal[idx]))
chapters.addAll(chapterIdxToSpanDisplay(idx))
}
}

is Resource.Success -> {
data.value.spans
ReadingType.BTT_SCROLL -> {
chapterIdxToSpanDisplayNext(cIndex - 1, cIndex)?.let {
chapters.add(it)
}

is Resource.Failure -> listOf<SpanDisplay>(
FailedSpanned(
reason = data.errorString.toUiText(),
index = idx,
canReload = data.isNetworkError
)
)
}
chaptersTitlesInternal.getOrNull(cIndex)?.let { text ->
chapters.add(ChapterStartSpanned(cIndex, 0, text))
}

if (idx < chaptersTitlesInternal.size && idx >= 0)
chapters.add(ChapterStartSpanned(idx, 0, chaptersTitlesInternal[idx]))
chapters.addAll(append)
chapters.addAll(chapterIdxToSpanDisplay(cIndex))

chapterIdxToSpanDisplayNext(cIndex + 1, cIndex)?.let {
chapters.add(it)
}
}
}

_chapterData.postValue(ChapterUpdate(data = chapters, seekToDesired = seekToDesired))
}

Expand Down Expand Up @@ -538,11 +575,11 @@ class ReadActivityViewModel : ViewModel() {
book.getChapterData(index, reload)
}.map { text ->
val rawText = preParseHtml(text)
// val renderedBuilder = SpannableStringBuilder()
// val lengths : IntArray
// val renderedBuilder = SpannableStringBuilder()
// val lengths : IntArray
//val nodes : Array<Node>
val rendered : Spanned
val parsed : Node
val rendered: Spanned
val parsed: Node
markwonMutex.withLock {
parsed = markwon.parse(rawText)
rendered = markwon.render(parsed)
Expand Down Expand Up @@ -1058,6 +1095,20 @@ class ReadActivityViewModel : ViewModel() {
}


private var readerTypeInternal by PreferenceDelegate(
EPUB_READER_TYPE,
ReadingType.DEFAULT.prefValue,
Int::class
)

var readerType
get() = ReadingType.fromSpinner(readerTypeInternal)
set(value) {
readerTypeInternal = value.prefValue
updateReadArea(seekToDesired = true)
}


var scrollWithVolume by PreferenceDelegate(EPUB_SCROLL_VOL, true, Boolean::class)
var ttsLock by PreferenceDelegate(EPUB_TTS_LOCK, true, Boolean::class)
val textFontLive: MutableLiveData<String> = MutableLiveData(null)
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/lagradost/quicknovel/TTSHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,17 @@ data class FailedSpanned(val reason: UiText, override val index: Int, val canRel
}
}

data class ChapterLoadSpanned(
override val index: Int,
override val innerIndex: Int,
val loadIndex : Int,
val name: UiText
) : SpanDisplay() {
override fun id(): Long {
return generateId(4, index, 0, 0)
}
}

object TTSHelper {
data class TTSLine(
val speakOutMsg: String,
Expand Down
47 changes: 44 additions & 3 deletions app/src/main/java/com/lagradost/quicknovel/ui/TextAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.lagradost.quicknovel.ChapterLoadSpanned
import com.lagradost.quicknovel.ChapterStartSpanned
import com.lagradost.quicknovel.CommonActivity.showToast
import com.lagradost.quicknovel.FailedSpanned
Expand All @@ -27,6 +28,7 @@ import com.lagradost.quicknovel.TextSpan
import com.lagradost.quicknovel.databinding.SingleFailedBinding
import com.lagradost.quicknovel.databinding.SingleFinishedChapterBinding
import com.lagradost.quicknovel.databinding.SingleImageBinding
import com.lagradost.quicknovel.databinding.SingleLoadBinding
import com.lagradost.quicknovel.databinding.SingleLoadingBinding
import com.lagradost.quicknovel.databinding.SingleTextBinding
import com.lagradost.quicknovel.mvvm.logError
Expand All @@ -41,6 +43,7 @@ const val DRAW_TEXT = 0
const val DRAW_LOADING = 2
const val DRAW_FAILED = 3
const val DRAW_CHAPTER = 4
const val DRAW_LOAD = 5


data class ScrollVisibilityItem(
Expand Down Expand Up @@ -154,14 +157,16 @@ const val CONFIG_FONT = 1 shl 1
const val CONFIG_SIZE = 1 shl 2
const val CONFIG_FONT_BOLD = 1 shl 3
const val CONFIG_FONT_ITALIC = 1 shl 4
const val CONFIG_BG_COLOR = 1 shl 5

// this uses val to make it explicit copy because of lazy properties
data class TextConfig(
val toolbarHeight: Int,
val textColor: Int,
val textSize: Int,
val textFont: String,
val defaultFont: Typeface
val defaultFont: Typeface,
val backgroundColor : Int,
) {
private val fontFile: File? by lazy {
if (textFont == "") null else systemFonts.firstOrNull { it.name == textFont }
Expand Down Expand Up @@ -194,7 +199,9 @@ data class TextConfig(
private fun setTextColor(textView: TextView) {
textView.setTextColor(textColor)
}

private fun setBgTextColor(textView: TextView) {
textView.setTextColor(backgroundColor)
}
fun setArgs(progressBar: ProgressBar) {
progressBar.progressTintList = ColorStateList.valueOf(textColor)
progressBar.indeterminateTintList = ColorStateList.valueOf(textColor)
Expand All @@ -204,6 +211,9 @@ data class TextConfig(
if ((args and CONFIG_COLOR) != 0) {
setTextColor(textView)
}
if ((args and CONFIG_BG_COLOR) != 0) {
setBgTextColor(textView)
}
if ((args and CONFIG_FONT) != 0) {
val bold = (args and CONFIG_FONT_BOLD) != 0
val italic = (args and CONFIG_FONT_ITALIC) != 0
Expand Down Expand Up @@ -251,7 +261,11 @@ class TextAdapter(private val viewModel: ReadActivityViewModel, var config: Text
config = config.copy(textFont = font)
return true
}

fun changeBackgroundColor(color: Int): Boolean {
if (config.backgroundColor == color) return false
config = config.copy(backgroundColor = color)
return true
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TextAdapterHolder {
val inflater = LayoutInflater.from(parent.context)
val binding: ViewBinding = when (viewType) {
Expand All @@ -260,6 +274,7 @@ class TextAdapter(private val viewModel: ReadActivityViewModel, var config: Text
DRAW_LOADING -> SingleLoadingBinding.inflate(inflater, parent, false)
DRAW_FAILED -> SingleFailedBinding.inflate(inflater, parent, false)
DRAW_CHAPTER -> SingleFinishedChapterBinding.inflate(inflater, parent, false)
DRAW_LOAD -> SingleLoadBinding.inflate(inflater, parent, false)
else -> throw NotImplementedError()
}

Expand Down Expand Up @@ -432,6 +447,10 @@ class TextAdapter(private val viewModel: ReadActivityViewModel, var config: Text
DRAW_CHAPTER
}

is ChapterLoadSpanned -> {
DRAW_LOAD
}

else -> throw NotImplementedError()
}
}
Expand Down Expand Up @@ -470,6 +489,11 @@ class TextAdapter(private val viewModel: ReadActivityViewModel, var config: Text
binding.root.minHeight = config.toolbarHeight
}

is SingleLoadBinding -> {
config.setArgs(binding.root, CONFIG_BG_COLOR or CONFIG_FONT or CONFIG_FONT_BOLD)
binding.root.backgroundTintList = ColorStateList.valueOf(config.textColor)
}

else -> {}
}
}
Expand Down Expand Up @@ -557,6 +581,14 @@ class TextAdapter(private val viewModel: ReadActivityViewModel, var config: Text
}
}

private fun bindLoadChapter(obj: ChapterLoadSpanned) {
if (binding !is SingleLoadBinding) throw NotImplementedError()
binding.root.setText(obj.name)
binding.root.setOnClickListener {
viewModel.seekToChapter(obj.loadIndex)
}
}

private fun bindChapter(obj: ChapterStartSpanned) {
if (binding !is SingleFinishedChapterBinding) throw NotImplementedError()
binding.root.setText(obj.name)
Expand Down Expand Up @@ -602,6 +634,10 @@ class TextAdapter(private val viewModel: ReadActivityViewModel, var config: Text
this.bindChapter(obj)
}

is ChapterLoadSpanned -> {
this.bindLoadChapter(obj)
}

else -> throw NotImplementedError()
}
setConfig(config)
Expand Down Expand Up @@ -638,6 +674,11 @@ class TextAdapter(private val viewModel: ReadActivityViewModel, var config: Text
newItem.id == oldItem.id && oldItem.name == newItem.name
}

is ChapterLoadSpanned -> {
if (newItem !is ChapterLoadSpanned) return false
newItem.id == oldItem.id && oldItem.name == newItem.name
}

else -> throw NotImplementedError()
}
}
Expand Down
Loading

0 comments on commit 30175b3

Please sign in to comment.