Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiments on webview renderer #766

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import java.util.Properties
import java.io.FileInputStream

plugins {
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.android.application)
alias(libs.plugins.ksp)
alias(libs.plugins.aboutlibraries)
Expand Down Expand Up @@ -182,4 +182,4 @@ dependencies {
testImplementation(libs.mockito.core)
testImplementation(libs.mockito.junit.jupiter)
testImplementation(libs.mockito.kotlin)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ fun Preferences.toSettings(): Settings {
flowArticleListTonalElevation = FlowArticleListTonalElevationPreference.fromPreferences(this),

// Reading page
readingRenderer = ReadingRendererPreference.fromPreferences(this),
readingTheme = ReadingThemePreference.fromPreferences(this),
readingDarkTheme = ReadingDarkThemePreference.fromPreferences(this),
readingPageTonalElevation = ReadingPageTonalElevationPreference.fromPreferences(this),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package me.ash.reader.infrastructure.preference

import android.content.Context
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.compositionLocalOf
import androidx.datastore.preferences.core.Preferences
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import me.ash.reader.R
import me.ash.reader.ui.ext.DataStoreKey
import me.ash.reader.ui.ext.DataStoreKey.Companion.readingRenderer
import me.ash.reader.ui.ext.dataStore
import me.ash.reader.ui.ext.put

val LocalReadingRenderer =
compositionLocalOf<ReadingRendererPreference> { ReadingRendererPreference.default }
@Immutable
sealed class ReadingRendererPreference(val value: Int) : Preference() {
object WebView : ReadingRendererPreference(0)
object NativeComponent : ReadingRendererPreference(1)

override fun put(context: Context, scope: CoroutineScope) {
scope.launch {
context.dataStore.put(DataStoreKey.readingRenderer, value)
}
}

fun toDesc(context: Context): String =
when (this) {
WebView -> context.getString(R.string.web_view)
NativeComponent -> context.getString(R.string.native_component)
}

companion object {

val default = WebView
val values = listOf(WebView, NativeComponent)

fun fromPreferences(preferences: Preferences): ReadingRendererPreference =
when (preferences[DataStoreKey.keys[readingRenderer]?.key as Preferences.Key<Int>]) {
0 -> WebView
1 -> NativeComponent
else -> default
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ sealed class ReadingSubheadAlignPreference(val value: Int) : Preference() {
Justify -> TextAlign.Justify
}

fun toTextAlignCSS(): String =
when (this) {
Start -> "left"
End -> "right"
Center -> "center"
Justify -> "justify"
}

companion object {

val default = Start
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ sealed class ReadingTextAlignPreference(val value: Int) : Preference() {
Justify -> Alignment.Start
}

fun toTextAlignCSS(): String =
when (this) {
Start -> "left"
End -> "right"
Center -> "center"
Justify -> "justify"
}

companion object {

val default = Start
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ data class Settings(
val flowArticleListReadIndicator: FlowArticleReadIndicatorPreference = FlowArticleReadIndicatorPreference.default,

// Reading page
val readingRenderer: ReadingRendererPreference = ReadingRendererPreference.default,
val readingTheme: ReadingThemePreference = ReadingThemePreference.default,
val readingDarkTheme: ReadingDarkThemePreference = ReadingDarkThemePreference.default,
val readingPageTonalElevation: ReadingPageTonalElevationPreference = ReadingPageTonalElevationPreference.default,
Expand Down Expand Up @@ -140,6 +141,7 @@ fun SettingsProvider(
LocalFlowArticleListReadIndicator provides settings.flowArticleListReadIndicator,

// Reading page
LocalReadingRenderer provides settings.readingRenderer,
LocalReadingTheme provides settings.readingTheme,
LocalReadingDarkTheme provides settings.readingDarkTheme,
LocalReadingPageTonalElevation provides settings.readingPageTonalElevation,
Expand Down
Loading
Loading