Skip to content

Commit

Permalink
removed live boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
LagradOst committed Jul 11, 2023
1 parent ee6c0b5 commit 5cfb1b9
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 96 deletions.
12 changes: 12 additions & 0 deletions app/src/main/java/com/lagradost/quicknovel/BaseApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ class BaseApplication : Application() {
return context?.getKey(path)
}

fun <T : Any> getKeyClass(path: String, valueType: Class<T>): T? {
return context?.getKey(path, valueType)
}

fun <T : Any> setKeyClass(path: String, value: T) {
context?.setKey(path, value)
}

inline fun <reified T : Any> getKey(folder: String, path: String): T? {
return context?.getKey(folder, path)
}
Expand All @@ -65,6 +73,10 @@ class BaseApplication : Application() {
context?.removeKey(folder, path)
}

fun removeKeyClass(path: String) {
context?.removeKey(path)
}

fun removeKey(path: String) {
context?.removeKey(path)
}
Expand Down
19 changes: 16 additions & 3 deletions app/src/main/java/com/lagradost/quicknovel/DataStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ const val EPUB_VOICE: String = "reader_epub_voice"
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"
const val RESULT_BOOKMARK : String = "result_bookmarked"
const val RESULT_BOOKMARK_STATE : String = "result_bookmarked_state"
const val HISTORY_FOLDER : String = "result_history"
const val RESULT_BOOKMARK: String = "result_bookmarked"
const val RESULT_BOOKMARK_STATE: String = "result_bookmarked_state"
const val HISTORY_FOLDER: String = "result_history"

object DataStore {
val mapper: JsonMapper = JsonMapper.builder().addModule(
Expand Down Expand Up @@ -143,6 +143,10 @@ object DataStore {
return mapper.readValue(this, T::class.java)
}

fun <T> String.toKotlinObject(valueType: Class<T>): T {
return mapper.readValue(this, valueType)
}

// GET KEY GIVEN PATH AND DEFAULT VALUE, NULL IF ERROR
inline fun <reified T : Any> Context.getKey(path: String, defVal: T?): T? {
try {
Expand All @@ -153,6 +157,15 @@ object DataStore {
}
}

fun <T> Context.getKey(path: String, valueType: Class<T>): T? {
try {
val json: String = getSharedPrefs().getString(path, null) ?: return null
return json.toKotlinObject(valueType)
} catch (e: Exception) {
return null
}
}

inline fun <reified T : Any> Context.getKey(path: String): T? {
return getKey(path, null)
}
Expand Down
70 changes: 54 additions & 16 deletions app/src/main/java/com/lagradost/quicknovel/ReadActivity2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.jaredrummler.android.colorpicker.ColorPickerDialogListener
import com.lagradost.quicknovel.CommonActivity.showToast
import com.lagradost.quicknovel.DataStore.getKey
import com.lagradost.quicknovel.DataStore.setKey
import com.lagradost.quicknovel.databinding.ReadBottomSettingsBinding
import com.lagradost.quicknovel.databinding.ReadMainBinding
import com.lagradost.quicknovel.mvvm.Resource
Expand All @@ -48,7 +48,6 @@ import com.lagradost.quicknovel.ui.TextAdapter
import com.lagradost.quicknovel.ui.TextVisualLine
import com.lagradost.quicknovel.util.Coroutines.ioSafe
import com.lagradost.quicknovel.util.SingleSelectionHelper.showBottomDialog
import com.lagradost.quicknovel.util.UIHelper
import com.lagradost.quicknovel.util.UIHelper.fixPaddingStatusbar
import com.lagradost.quicknovel.util.UIHelper.getStatusBarHeight
import com.lagradost.quicknovel.util.UIHelper.popupMenu
Expand Down Expand Up @@ -145,11 +144,11 @@ class ReadActivity2 : AppCompatActivity(), ColorPickerDialogListener {
}

private fun setBackgroundColor(color: Int) {
viewModel.setBackgroundColor(color)
viewModel.backgroundColor = color
}

private fun setTextColor(color: Int) {
viewModel.setTextColor(color)
viewModel.textColor = color
}

override fun onDialogDismissed(dialog: Int) {
Expand Down Expand Up @@ -368,7 +367,7 @@ class ReadActivity2 : AppCompatActivity(), ColorPickerDialogListener {
var lockTop: Int? = null
var lockBottom: Int? = null
var currentScroll: Int = 0
var lockTTS: Boolean = true

private fun updateTTSLine(line: TTSHelper.TTSLine?) {
// update the visual component
textAdapter.updateTTSLine(line)
Expand All @@ -379,7 +378,7 @@ class ReadActivity2 : AppCompatActivity(), ColorPickerDialogListener {
}

// update the lock area
if (line == null || !lockTTS) {
if (line == null || !viewModel.ttsLock) {
lockTop = null
lockBottom = null
return
Expand Down Expand Up @@ -504,7 +503,7 @@ class ReadActivity2 : AppCompatActivity(), ColorPickerDialogListener {

res.adapter = adapter
res.setOnItemClickListener { _, _, which, _ ->
viewModel.setTextFont(items[which]?.name ?: "")
viewModel.textFont = items[which]?.name ?: ""
bottomSheetDialog.dismiss()
}
bottomSheetDialog.show()
Expand Down Expand Up @@ -539,20 +538,20 @@ class ReadActivity2 : AppCompatActivity(), ColorPickerDialogListener {
//pendingPost()


observe(viewModel.textSize) { size ->
observe(viewModel.textSizeLive) { size ->
if (textAdapter.changeSize(size)) {
updateTextAdapterConfig()
postDesired(binding.realText)
}
}

observe(viewModel.textColor) { color ->
observe(viewModel.textColorLive) { color ->
if (textAdapter.changeColor(color)) {
updateTextAdapterConfig()
}
}

observe(viewModel.textFont) { font ->
observe(viewModel.textFontLive) { font ->
if (textAdapter.changeFont(font)) {
updateTextAdapterConfig()
}
Expand Down Expand Up @@ -581,7 +580,8 @@ class ReadActivity2 : AppCompatActivity(), ColorPickerDialogListener {
viewModel.backwardsTTS()
}

observe(viewModel.orientation) { org ->
observe(viewModel.orientationLive) { position ->
val org = OrientationType.fromSpinner(position)
requestedOrientation = org.flag
binding.readActionRotate.setImageResource(org.iconRes)

Expand All @@ -591,7 +591,7 @@ class ReadActivity2 : AppCompatActivity(), ColorPickerDialogListener {
items = OrientationType.values().map { it.prefValue to it.stringRes },
selectedItemId = org.prefValue
) {
viewModel.setOrientation(OrientationType.fromSpinner(itemId))
viewModel.orientation = itemId
}
}
}
Expand Down Expand Up @@ -768,7 +768,7 @@ class ReadActivity2 : AppCompatActivity(), ColorPickerDialogListener {
binding.readSettingsTextSizeText.setOnClickListener {
it.popupMenu(items = listOf(Pair(1, R.string.reset_value)), selectedItemId = null) {
if (itemId == 1) {
viewModel.setTextSize(DEF_FONT_SIZE)
viewModel.textSize = DEF_FONT_SIZE
binding.readSettingsTextSize.progress =
DEF_FONT_SIZE - fontSizeProgressOffset
}
Expand All @@ -777,15 +777,15 @@ class ReadActivity2 : AppCompatActivity(), ColorPickerDialogListener {

binding.readSettingsTextSize.apply {
max = 20
progress = (viewModel.textSize.value ?: DEF_FONT_SIZE) - fontSizeProgressOffset
progress = viewModel.textSize - fontSizeProgressOffset
setOnSeekBarChangeListener(object :
SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(
seekBar: SeekBar?,
progress: Int,
fromUser: Boolean
) {
viewModel.setTextSize(progress + fontSizeProgressOffset)
viewModel.textSize = progress + fontSizeProgressOffset
}

override fun onStartTrackingTouch(seekBar: SeekBar?) {}
Expand All @@ -804,7 +804,7 @@ class ReadActivity2 : AppCompatActivity(), ColorPickerDialogListener {
binding.readSettingsTextFontText.setOnClickListener {
it.popupMenu(items = listOf(Pair(1, R.string.reset_value)), selectedItemId = null) {
if (itemId == 1) {
viewModel.setTextFont("")
viewModel.textFont = ""
}
}
}
Expand Down Expand Up @@ -873,6 +873,44 @@ class ReadActivity2 : AppCompatActivity(), ColorPickerDialogListener {
}
}

binding.apply {
hardResetStream.isVisible = viewModel.canReload()
hardResetStream.setOnClickListener {
showToast(getString(R.string.reload_chapter_format).format(""))
viewModel.reloadChapter()
}

readSettingsScrollVol.isChecked = viewModel.scrollWithVolume
readSettingsScrollVol.setOnCheckedChangeListener { _, isChecked ->
viewModel.scrollWithVolume = isChecked
}

readSettingsLockTts.isChecked = viewModel.ttsLock
readSettingsLockTts.setOnCheckedChangeListener { _, isChecked ->
viewModel.ttsLock = isChecked
}

readSettingsShowTime.isChecked = viewModel.showTime
readSettingsShowTime.setOnCheckedChangeListener { _, isChecked ->
viewModel.showTime = isChecked
}

readSettingsTwelveHourTime.isChecked = viewModel.time12H
readSettingsTwelveHourTime.setOnCheckedChangeListener { _, isChecked ->
viewModel.time12H = isChecked
}

readSettingsShowBattery.isChecked = viewModel.showBattery
readSettingsShowBattery.setOnCheckedChangeListener { _, isChecked ->
viewModel.showBattery = isChecked
}

readSettingsKeepScreenActive.isChecked = viewModel.screenAwake
readSettingsKeepScreenActive.setOnCheckedChangeListener { _, isChecked ->
viewModel.screenAwake = isChecked
}
}

/*binding.readLanguage.setOnClickListener { view ->
view?.context?.let { ctx ->
requireTTS { tts ->
Expand Down

0 comments on commit 5cfb1b9

Please sign in to comment.