Add right-hand page gestures and managed Android imports
This commit is contained in:
1 parent
a2f417d147
commit
e6be02719d
11 files changed
+142
-52
No files matched your search
@@ -3,7 +3,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
|||||||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||||
|
|
||||||
val appVersionName = "1.1"
|
val appVersionName = "1.1"
|
||||||
val appVersionCode = 10
|
val appVersionCode = 12
|
||||||
val appVersionDisplay = "$appVersionName.$appVersionCode"
|
val appVersionDisplay = "$appVersionName.$appVersionCode"
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import net.sergeych.toread.storage.ContentAnchor
|
|||||||
import net.sergeych.toread.storage.LibraryFileRecord
|
import net.sergeych.toread.storage.LibraryFileRecord
|
||||||
import net.sergeych.toread.storage.ReadingStateRecord
|
import net.sergeych.toread.storage.ReadingStateRecord
|
||||||
import net.sergeych.toread.storage.jdbc.H2LibraryDatabase
|
import net.sergeych.toread.storage.jdbc.H2LibraryDatabase
|
||||||
|
import net.sergeych.toread.storage.jdbc.ManagedBookObjectStore
|
||||||
import net.sergeych.toread.storage.jdbc.LibraryScanner
|
import net.sergeych.toread.storage.jdbc.LibraryScanner
|
||||||
import net.sergeych.toread.storage.jdbc.LibraryScanSummary
|
import net.sergeych.toread.storage.jdbc.LibraryScanSummary
|
||||||
import java.io.File
|
import java.io.File
|
||||||
@@ -122,7 +123,7 @@ actual suspend fun loadPlatformOpenBookRequest(): PlatformOpenBookRequest? = wit
|
|||||||
val displayName = displayNameFor(uri)
|
val displayName = displayNameFor(uri)
|
||||||
val sizeBytes = sizeBytesFor(uri) ?: bytes.size.toLong()
|
val sizeBytes = sizeBytesFor(uri) ?: bytes.size.toLong()
|
||||||
val importResult = openLibraryDatabase().useLibrary { db ->
|
val importResult = openLibraryDatabase().useLibrary { db ->
|
||||||
LibraryScanner(db, ::appendLibraryLog).importExternalFileAndGetResult(
|
LibraryScanner(db, ::appendLibraryLog, ManagedBookObjectStore(File(appContext.filesDir, "library"))).importExternalFileAndGetResult(
|
||||||
bytes = bytes,
|
bytes = bytes,
|
||||||
displayName = displayName,
|
displayName = displayName,
|
||||||
storageUri = uri.toString(),
|
storageUri = uri.toString(),
|
||||||
@@ -221,7 +222,7 @@ actual suspend fun scanLibrarySubtree(
|
|||||||
error(strings.allFilesAccessRequired(path))
|
error(strings.allFilesAccessRequired(path))
|
||||||
}
|
}
|
||||||
openLibraryDatabase().useLibrary { db ->
|
openLibraryDatabase().useLibrary { db ->
|
||||||
val summary = LibraryScanner(db, ::appendLibraryLog).scanSubtree(File(path)) {
|
val summary = LibraryScanner(db, ::appendLibraryLog, ManagedBookObjectStore(File(appContext.filesDir, "library"))).scanSubtree(File(path)) {
|
||||||
emitProgress(it.toLibraryScanProgress())
|
emitProgress(it.toLibraryScanProgress())
|
||||||
}
|
}
|
||||||
LibraryScanReport(
|
LibraryScanReport(
|
||||||
@@ -497,6 +498,18 @@ actual suspend fun saveReaderPagingOnly(enabled: Boolean) = withContext(Dispatch
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
actual suspend fun loadReaderRightHand(): Boolean = withContext(Dispatchers.IO) {
|
||||||
|
openLibraryDatabase().useLibrary { db ->
|
||||||
|
db.getAppFlag(ReaderRightHandFlag)?.toBooleanStrictOrNull() ?: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
actual suspend fun saveReaderRightHand(enabled: Boolean) = withContext(Dispatchers.IO) {
|
||||||
|
openLibraryDatabase().useLibrary { db ->
|
||||||
|
db.setAppFlag(ReaderRightHandFlag, enabled.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
actual suspend fun loadScanDownloadsAutomatically(): Boolean = withContext(Dispatchers.IO) {
|
actual suspend fun loadScanDownloadsAutomatically(): Boolean = withContext(Dispatchers.IO) {
|
||||||
openLibraryDatabase().useLibrary { db ->
|
openLibraryDatabase().useLibrary { db ->
|
||||||
db.getAppFlag(ScanDownloadsAutomaticallyFlag)?.toBooleanStrictOrNull() ?: true
|
db.getAppFlag(ScanDownloadsAutomaticallyFlag)?.toBooleanStrictOrNull() ?: true
|
||||||
@@ -612,7 +625,7 @@ private fun scanLibraryContentTree(
|
|||||||
onProgress: (LibraryScanProgress) -> Unit,
|
onProgress: (LibraryScanProgress) -> Unit,
|
||||||
): LibraryScanReport =
|
): LibraryScanReport =
|
||||||
openLibraryDatabase().useLibrary { db ->
|
openLibraryDatabase().useLibrary { db ->
|
||||||
val scanner = LibraryScanner(db, ::appendLibraryLog)
|
val scanner = LibraryScanner(db, ::appendLibraryLog, ManagedBookObjectStore(File(appContext.filesDir, "library")))
|
||||||
var scanned = 0
|
var scanned = 0
|
||||||
var imported = 0
|
var imported = 0
|
||||||
var skipped = 0
|
var skipped = 0
|
||||||
@@ -917,6 +930,7 @@ private const val ThemeModeFlag = "theme_mode"
|
|||||||
private const val ReaderFontSettingsFlag = "reader_font_settings"
|
private const val ReaderFontSettingsFlag = "reader_font_settings"
|
||||||
private const val ReaderFullScreenFlag = "reader_full_screen"
|
private const val ReaderFullScreenFlag = "reader_full_screen"
|
||||||
private const val ReaderPagingOnlyFlag = "reader_paging_only"
|
private const val ReaderPagingOnlyFlag = "reader_paging_only"
|
||||||
|
private const val ReaderRightHandFlag = "reader_right_hand"
|
||||||
private const val ScanDownloadsAutomaticallyFlag = "scan_downloads_automatically"
|
private const val ScanDownloadsAutomaticallyFlag = "scan_downloads_automatically"
|
||||||
private const val LibraryFilterFlag = "library_filter"
|
private const val LibraryFilterFlag = "library_filter"
|
||||||
private const val AppLocaleTagFlag = "app_locale_tag"
|
private const val AppLocaleTagFlag = "app_locale_tag"
|
||||||
|
|||||||
@@ -175,6 +175,10 @@ expect suspend fun loadReaderPagingOnly(): Boolean
|
|||||||
|
|
||||||
expect suspend fun saveReaderPagingOnly(enabled: Boolean)
|
expect suspend fun saveReaderPagingOnly(enabled: Boolean)
|
||||||
|
|
||||||
|
expect suspend fun loadReaderRightHand(): Boolean
|
||||||
|
|
||||||
|
expect suspend fun saveReaderRightHand(enabled: Boolean)
|
||||||
|
|
||||||
expect suspend fun loadScanDownloadsAutomatically(): Boolean
|
expect suspend fun loadScanDownloadsAutomatically(): Boolean
|
||||||
|
|
||||||
expect suspend fun saveScanDownloadsAutomatically(enabled: Boolean)
|
expect suspend fun saveScanDownloadsAutomatically(enabled: Boolean)
|
||||||
|
|||||||
@@ -160,6 +160,8 @@ internal open class AppStrings {
|
|||||||
open val readerTypography = "Typography"
|
open val readerTypography = "Typography"
|
||||||
open val readerLayout = "Layout"
|
open val readerLayout = "Layout"
|
||||||
open val readerNavigation = "Navigation"
|
open val readerNavigation = "Navigation"
|
||||||
|
open val readerRightHand = "Right hand (reversed page gestures)"
|
||||||
|
open val readerLeftHand = "Left hand (default page gestures)"
|
||||||
open val readerPagingOnly = "Paging only (no free scroll)"
|
open val readerPagingOnly = "Paging only (no free scroll)"
|
||||||
open val readerProgress = "Progress"
|
open val readerProgress = "Progress"
|
||||||
open val closeReaderSettings = "Close reader settings"
|
open val closeReaderSettings = "Close reader settings"
|
||||||
@@ -389,6 +391,8 @@ internal object RussianStrings : AppStrings() {
|
|||||||
override val readerTypography = "Типографика"
|
override val readerTypography = "Типографика"
|
||||||
override val readerLayout = "Макет"
|
override val readerLayout = "Макет"
|
||||||
override val readerNavigation = "Навигация"
|
override val readerNavigation = "Навигация"
|
||||||
|
override val readerRightHand = "Правая рука (обратные жесты страниц)"
|
||||||
|
override val readerLeftHand = "Левая рука (обычные жесты страниц)"
|
||||||
override val readerPagingOnly = "Постраничный режим (без прокрутки)"
|
override val readerPagingOnly = "Постраничный режим (без прокрутки)"
|
||||||
override val readerProgress = "Прогресс"
|
override val readerProgress = "Прогресс"
|
||||||
override val closeReaderSettings = "Закрыть настройки чтения"
|
override val closeReaderSettings = "Закрыть настройки чтения"
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package net.sergeych.toread
|
|||||||
import androidx.compose.foundation.Image
|
import androidx.compose.foundation.Image
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
|
import androidx.compose.foundation.gestures.stopScroll
|
||||||
import androidx.compose.foundation.gestures.animateScrollBy
|
import androidx.compose.foundation.gestures.animateScrollBy
|
||||||
import androidx.compose.foundation.gestures.awaitEachGesture
|
import androidx.compose.foundation.gestures.awaitEachGesture
|
||||||
import androidx.compose.foundation.gestures.awaitFirstDown
|
import androidx.compose.foundation.gestures.awaitFirstDown
|
||||||
@@ -59,7 +60,6 @@ import androidx.compose.ui.graphics.ImageBitmap
|
|||||||
import androidx.compose.ui.graphics.StrokeCap
|
import androidx.compose.ui.graphics.StrokeCap
|
||||||
import androidx.compose.ui.graphics.graphicsLayer
|
import androidx.compose.ui.graphics.graphicsLayer
|
||||||
import androidx.compose.ui.input.pointer.PointerEventPass
|
import androidx.compose.ui.input.pointer.PointerEventPass
|
||||||
import androidx.compose.ui.input.pointer.PointerEventType
|
|
||||||
import androidx.compose.ui.input.pointer.PointerType
|
import androidx.compose.ui.input.pointer.PointerType
|
||||||
import androidx.compose.ui.input.pointer.pointerInput
|
import androidx.compose.ui.input.pointer.pointerInput
|
||||||
import androidx.compose.ui.layout.ContentScale
|
import androidx.compose.ui.layout.ContentScale
|
||||||
@@ -129,11 +129,15 @@ internal fun ContinuousBookReader(
|
|||||||
readerBackgroundColor: Color = MaterialTheme.colorScheme.surface,
|
readerBackgroundColor: Color = MaterialTheme.colorScheme.surface,
|
||||||
readerTextColor: Color = MaterialTheme.colorScheme.onSurface,
|
readerTextColor: Color = MaterialTheme.colorScheme.onSurface,
|
||||||
pagingOnly: Boolean = false,
|
pagingOnly: Boolean = false,
|
||||||
|
rightHand: Boolean = false,
|
||||||
onReaderFontZoom: (Int) -> Unit = {},
|
onReaderFontZoom: (Int) -> Unit = {},
|
||||||
onUserScroll: () -> Unit = {},
|
onUserScroll: () -> Unit = {},
|
||||||
onImageOpen: (ViewedBookImage) -> Unit = {},
|
onImageOpen: (ViewedBookImage) -> Unit = {},
|
||||||
onNoteOpen: (String) -> Unit = {},
|
onNoteOpen: (String) -> Unit = {},
|
||||||
) {
|
) {
|
||||||
|
LaunchedEffect(listState, pagingOnly) {
|
||||||
|
if (pagingOnly) listState.stopScroll()
|
||||||
|
}
|
||||||
val hyphenation = remember { HyphenationRegistry() }
|
val hyphenation = remember { HyphenationRegistry() }
|
||||||
val scope = rememberCoroutineScope()
|
val scope = rememberCoroutineScope()
|
||||||
val textLineMetricsByItem = remember(contentPlan, readerFontSettings) { mutableStateMapOf<Int, TextLineMetrics>() }
|
val textLineMetricsByItem = remember(contentPlan, readerFontSettings) { mutableStateMapOf<Int, TextLineMetrics>() }
|
||||||
@@ -155,9 +159,10 @@ internal fun ContinuousBookReader(
|
|||||||
modifier = modifier
|
modifier = modifier
|
||||||
.background(readerBackgroundColor)
|
.background(readerBackgroundColor)
|
||||||
.nestedScroll(userScrollConnection)
|
.nestedScroll(userScrollConnection)
|
||||||
.blockFreeScroll(pagingOnly)
|
|
||||||
.readerFontZoomGesture(onReaderFontZoom)
|
.readerFontZoomGesture(onReaderFontZoom)
|
||||||
.pageTurnOnTouchSwipe(
|
.pageTurnOnTouchSwipe(
|
||||||
|
enabled = pagingOnly,
|
||||||
|
rightHand = rightHand,
|
||||||
onPageDown = {
|
onPageDown = {
|
||||||
onUserScroll()
|
onUserScroll()
|
||||||
scope.launch {
|
scope.launch {
|
||||||
@@ -172,6 +177,8 @@ internal fun ContinuousBookReader(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
.pageTurnOnTouchTap(
|
.pageTurnOnTouchTap(
|
||||||
|
enabled = pagingOnly,
|
||||||
|
rightHand = rightHand,
|
||||||
onPageDown = {
|
onPageDown = {
|
||||||
onUserScroll()
|
onUserScroll()
|
||||||
scope.launch {
|
scope.launch {
|
||||||
@@ -185,6 +192,7 @@ internal fun ContinuousBookReader(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
userScrollEnabled = !pagingOnly,
|
||||||
contentPadding = contentPadding,
|
contentPadding = contentPadding,
|
||||||
verticalArrangement = Arrangement.spacedBy(10.dp),
|
verticalArrangement = Arrangement.spacedBy(10.dp),
|
||||||
) {
|
) {
|
||||||
@@ -334,47 +342,12 @@ private fun List<androidx.compose.ui.input.pointer.PointerInputChange>.firstTwoD
|
|||||||
return sqrt(dx * dx + dy * dy)
|
return sqrt(dx * dx + dy * dy)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Consumes vertical touch drags and mouse wheel/trackpad scroll so the lazy list
|
|
||||||
* cannot be scrolled freely ("paging only" mode). Taps and horizontal swipes are
|
|
||||||
* left untouched so page-turn gestures and clickable items keep working.
|
|
||||||
*/
|
|
||||||
private fun Modifier.blockFreeScroll(enabled: Boolean): Modifier =
|
|
||||||
if (!enabled) {
|
|
||||||
this
|
|
||||||
} else {
|
|
||||||
pointerInput(Unit) {
|
|
||||||
awaitEachGesture {
|
|
||||||
val down = awaitFirstDown(requireUnconsumed = false, pass = PointerEventPass.Initial)
|
|
||||||
if (down.isConsumed) return@awaitEachGesture
|
|
||||||
val touchSlop = viewConfiguration.touchSlop
|
|
||||||
val touchSlopSquared = touchSlop * touchSlop
|
|
||||||
var verticalDragLocked = false
|
|
||||||
while (true) {
|
|
||||||
val event = awaitPointerEvent(pass = PointerEventPass.Initial)
|
|
||||||
if (event.type == PointerEventType.Scroll) {
|
|
||||||
event.changes.forEach { it.consume() }
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
val change = event.changes.firstOrNull { it.id == down.id } ?: break
|
|
||||||
if (!change.pressed) break
|
|
||||||
if (!verticalDragLocked) {
|
|
||||||
val dx = change.position.x - down.position.x
|
|
||||||
val dy = change.position.y - down.position.y
|
|
||||||
if (dy * dy > touchSlopSquared && dy * dy > ReaderHorizontalSwipeDominance * dx * dx) {
|
|
||||||
verticalDragLocked = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (verticalDragLocked) change.consume()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun Modifier.pageTurnOnTouchSwipe(
|
private fun Modifier.pageTurnOnTouchSwipe(
|
||||||
|
enabled: Boolean,
|
||||||
|
rightHand: Boolean,
|
||||||
onPageDown: () -> Unit,
|
onPageDown: () -> Unit,
|
||||||
onPageUp: () -> Unit,
|
onPageUp: () -> Unit,
|
||||||
): Modifier = pointerInput(onPageDown, onPageUp) {
|
): Modifier = if (!enabled) this else pointerInput(rightHand, onPageDown, onPageUp) {
|
||||||
awaitEachGesture {
|
awaitEachGesture {
|
||||||
val down = awaitFirstDown(requireUnconsumed = false, pass = PointerEventPass.Initial)
|
val down = awaitFirstDown(requireUnconsumed = false, pass = PointerEventPass.Initial)
|
||||||
if (down.isConsumed) return@awaitEachGesture
|
if (down.isConsumed) return@awaitEachGesture
|
||||||
@@ -412,7 +385,7 @@ private fun Modifier.pageTurnOnTouchSwipe(
|
|||||||
movedBeyondTapSlop = true
|
movedBeyondTapSlop = true
|
||||||
}
|
}
|
||||||
if (absX >= swipeSlop && isHorizontalSwipeCandidate) {
|
if (absX >= swipeSlop && isHorizontalSwipeCandidate) {
|
||||||
pageScrollDirection = if (dx < 0f) 1 else -1
|
pageScrollDirection = (if (dx < 0f) 1 else -1) * (if (rightHand) -1 else 1)
|
||||||
change.consume()
|
change.consume()
|
||||||
} else if (movedBeyondTapSlop && !isHorizontalSwipeCandidate) {
|
} else if (movedBeyondTapSlop && !isHorizontalSwipeCandidate) {
|
||||||
cancelled = true
|
cancelled = true
|
||||||
@@ -450,9 +423,11 @@ private fun Modifier.pageTurnOnTouchSwipe(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun Modifier.pageTurnOnTouchTap(
|
private fun Modifier.pageTurnOnTouchTap(
|
||||||
|
enabled: Boolean,
|
||||||
|
rightHand: Boolean,
|
||||||
onPageDown: () -> Unit,
|
onPageDown: () -> Unit,
|
||||||
onPageUp: () -> Unit,
|
onPageUp: () -> Unit,
|
||||||
): Modifier = pointerInput(onPageDown, onPageUp) {
|
): Modifier = if (!enabled) this else pointerInput(rightHand, onPageDown, onPageUp) {
|
||||||
awaitEachGesture {
|
awaitEachGesture {
|
||||||
val down = awaitFirstDown(requireUnconsumed = false, pass = PointerEventPass.Final)
|
val down = awaitFirstDown(requireUnconsumed = false, pass = PointerEventPass.Final)
|
||||||
if (down.isConsumed) return@awaitEachGesture
|
if (down.isConsumed) return@awaitEachGesture
|
||||||
@@ -486,7 +461,7 @@ private fun Modifier.pageTurnOnTouchTap(
|
|||||||
|
|
||||||
if (cancelled) return@awaitEachGesture
|
if (cancelled) return@awaitEachGesture
|
||||||
|
|
||||||
if (down.position.x < size.width / 2f) {
|
if ((down.position.x < size.width / 2f) != rightHand) {
|
||||||
onPageDown()
|
onPageDown()
|
||||||
} else {
|
} else {
|
||||||
onPageUp()
|
onPageUp()
|
||||||
|
|||||||
@@ -153,6 +153,7 @@ internal fun BookView(
|
|||||||
var readerColorPickerTarget by remember(fileId) { mutableStateOf<ReaderColorPickerTarget?>(null) }
|
var readerColorPickerTarget by remember(fileId) { mutableStateOf<ReaderColorPickerTarget?>(null) }
|
||||||
var fullScreenReader by remember { mutableStateOf(false) }
|
var fullScreenReader by remember { mutableStateOf(false) }
|
||||||
var readerPagingOnly by remember { mutableStateOf(false) }
|
var readerPagingOnly by remember { mutableStateOf(false) }
|
||||||
|
var readerRightHand by remember { mutableStateOf(false) }
|
||||||
var tableOfContentsVisible by remember(fileId) { mutableStateOf(false) }
|
var tableOfContentsVisible by remember(fileId) { mutableStateOf(false) }
|
||||||
var tableOfContentsBackStack by remember(fileId) { mutableStateOf<List<ReadingPosition>>(emptyList()) }
|
var tableOfContentsBackStack by remember(fileId) { mutableStateOf<List<ReadingPosition>>(emptyList()) }
|
||||||
var tableOfContentsBackPosition by remember(fileId) { mutableStateOf<ReadingPosition?>(null) }
|
var tableOfContentsBackPosition by remember(fileId) { mutableStateOf<ReadingPosition?>(null) }
|
||||||
@@ -313,6 +314,7 @@ internal fun BookView(
|
|||||||
readerFontSettings = loadReaderFontSettings()
|
readerFontSettings = loadReaderFontSettings()
|
||||||
fullScreenReader = loadReaderFullScreen()
|
fullScreenReader = loadReaderFullScreen()
|
||||||
readerPagingOnly = loadReaderPagingOnly()
|
readerPagingOnly = loadReaderPagingOnly()
|
||||||
|
readerRightHand = loadReaderRightHand()
|
||||||
}
|
}
|
||||||
|
|
||||||
DisposableEffect(fileId) {
|
DisposableEffect(fileId) {
|
||||||
@@ -507,6 +509,7 @@ internal fun BookView(
|
|||||||
readerBackgroundColor = readerBackgroundColor,
|
readerBackgroundColor = readerBackgroundColor,
|
||||||
readerTextColor = readerTextColor,
|
readerTextColor = readerTextColor,
|
||||||
pagingOnly = readerPagingOnly,
|
pagingOnly = readerPagingOnly,
|
||||||
|
rightHand = readerRightHand,
|
||||||
onReaderFontZoom = { steps ->
|
onReaderFontZoom = { steps ->
|
||||||
updateReaderFontSettings { settings -> settings.adjustFontSize(steps) }
|
updateReaderFontSettings { settings -> settings.adjustFontSize(steps) }
|
||||||
},
|
},
|
||||||
@@ -619,6 +622,11 @@ internal fun BookView(
|
|||||||
},
|
},
|
||||||
pagingOnly = readerPagingOnly,
|
pagingOnly = readerPagingOnly,
|
||||||
onPagingOnlyChange = { pagingOnly -> updateReaderPagingOnly(pagingOnly) },
|
onPagingOnlyChange = { pagingOnly -> updateReaderPagingOnly(pagingOnly) },
|
||||||
|
rightHand = readerRightHand,
|
||||||
|
onRightHandChange = { enabled ->
|
||||||
|
readerRightHand = enabled
|
||||||
|
scope.launch { saveReaderRightHand(enabled) }
|
||||||
|
},
|
||||||
onDismiss = { readerExtraSettingsVisible = false },
|
onDismiss = { readerExtraSettingsVisible = false },
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -1409,6 +1417,8 @@ private fun ReaderExtraSettingsDialog(
|
|||||||
onRevertColorScheme: () -> Unit,
|
onRevertColorScheme: () -> Unit,
|
||||||
pagingOnly: Boolean,
|
pagingOnly: Boolean,
|
||||||
onPagingOnlyChange: (Boolean) -> Unit,
|
onPagingOnlyChange: (Boolean) -> Unit,
|
||||||
|
rightHand: Boolean,
|
||||||
|
onRightHandChange: (Boolean) -> Unit,
|
||||||
onDismiss: () -> Unit,
|
onDismiss: () -> Unit,
|
||||||
) {
|
) {
|
||||||
AlertDialog(
|
AlertDialog(
|
||||||
@@ -1466,6 +1476,21 @@ private fun ReaderExtraSettingsDialog(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
HorizontalDivider()
|
HorizontalDivider()
|
||||||
|
Row(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.clickable { onRightHandChange(!rightHand) }
|
||||||
|
.padding(horizontal = 4.dp, vertical = 8.dp),
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
text = if (rightHand) strings.readerRightHand else strings.readerLeftHand,
|
||||||
|
style = MaterialTheme.typography.bodyLarge,
|
||||||
|
modifier = Modifier.weight(1f),
|
||||||
|
)
|
||||||
|
Switch(checked = rightHand, onCheckedChange = onRightHandChange)
|
||||||
|
}
|
||||||
|
HorizontalDivider()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
confirmButton = {
|
confirmButton = {
|
||||||
|
|||||||
@@ -473,6 +473,18 @@ actual suspend fun saveReaderPagingOnly(enabled: Boolean) = withContext(Dispatch
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
actual suspend fun loadReaderRightHand(): Boolean = withContext(Dispatchers.IO) {
|
||||||
|
openLibraryDatabase().useLibrary { db ->
|
||||||
|
db.getAppFlag(ReaderRightHandFlag)?.toBooleanStrictOrNull() ?: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
actual suspend fun saveReaderRightHand(enabled: Boolean) = withContext(Dispatchers.IO) {
|
||||||
|
openLibraryDatabase().useLibrary { db ->
|
||||||
|
db.setAppFlag(ReaderRightHandFlag, enabled.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
actual suspend fun loadScanDownloadsAutomatically(): Boolean = withContext(Dispatchers.IO) {
|
actual suspend fun loadScanDownloadsAutomatically(): Boolean = withContext(Dispatchers.IO) {
|
||||||
openLibraryDatabase().useLibrary { db ->
|
openLibraryDatabase().useLibrary { db ->
|
||||||
db.getAppFlag(ScanDownloadsAutomaticallyFlag)?.toBooleanStrictOrNull() ?: true
|
db.getAppFlag(ScanDownloadsAutomaticallyFlag)?.toBooleanStrictOrNull() ?: true
|
||||||
@@ -764,6 +776,7 @@ private const val ThemeModeFlag = "theme_mode"
|
|||||||
private const val ReaderFontSettingsFlag = "reader_font_settings"
|
private const val ReaderFontSettingsFlag = "reader_font_settings"
|
||||||
private const val ReaderFullScreenFlag = "reader_full_screen"
|
private const val ReaderFullScreenFlag = "reader_full_screen"
|
||||||
private const val ReaderPagingOnlyFlag = "reader_paging_only"
|
private const val ReaderPagingOnlyFlag = "reader_paging_only"
|
||||||
|
private const val ReaderRightHandFlag = "reader_right_hand"
|
||||||
private const val ScanDownloadsAutomaticallyFlag = "scan_downloads_automatically"
|
private const val ScanDownloadsAutomaticallyFlag = "scan_downloads_automatically"
|
||||||
private const val LibraryFilterFlag = "library_filter"
|
private const val LibraryFilterFlag = "library_filter"
|
||||||
private const val AppLocaleTagFlag = "app_locale_tag"
|
private const val AppLocaleTagFlag = "app_locale_tag"
|
||||||
|
|||||||
@@ -97,6 +97,13 @@ actual suspend fun saveReaderPagingOnly(enabled: Boolean) {
|
|||||||
window.localStorage.setItem(ReaderPagingOnlyStorageKey, enabled.toString())
|
window.localStorage.setItem(ReaderPagingOnlyStorageKey, enabled.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
actual suspend fun loadReaderRightHand(): Boolean =
|
||||||
|
window.localStorage.getItem(ReaderRightHandStorageKey)?.toBooleanStrictOrNull() ?: false
|
||||||
|
|
||||||
|
actual suspend fun saveReaderRightHand(enabled: Boolean) {
|
||||||
|
window.localStorage.setItem(ReaderRightHandStorageKey, enabled.toString())
|
||||||
|
}
|
||||||
|
|
||||||
actual suspend fun loadScanDownloadsAutomatically(): Boolean = true
|
actual suspend fun loadScanDownloadsAutomatically(): Boolean = true
|
||||||
|
|
||||||
actual suspend fun saveScanDownloadsAutomatically(enabled: Boolean) = Unit
|
actual suspend fun saveScanDownloadsAutomatically(enabled: Boolean) = Unit
|
||||||
@@ -160,6 +167,7 @@ private const val LibraryFilterStorageKey = "toread.libraryFilter"
|
|||||||
private const val ReaderFontSettingsStorageKey = "toread.readerFontSettings"
|
private const val ReaderFontSettingsStorageKey = "toread.readerFontSettings"
|
||||||
private const val ReaderFullScreenStorageKey = "toread.readerFullScreen"
|
private const val ReaderFullScreenStorageKey = "toread.readerFullScreen"
|
||||||
private const val ReaderPagingOnlyStorageKey = "toread.readerPagingOnly"
|
private const val ReaderPagingOnlyStorageKey = "toread.readerPagingOnly"
|
||||||
|
private const val ReaderRightHandStorageKey = "toread.readerRightHand"
|
||||||
|
|
||||||
actual fun formatLibraryLastReadTime(millis: Long): String {
|
actual fun formatLibraryLastReadTime(millis: Long): String {
|
||||||
val totalMinutes = millis / 60_000L
|
val totalMinutes = millis / 60_000L
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ data class LibraryImportResult(
|
|||||||
class LibraryScanner(
|
class LibraryScanner(
|
||||||
private val database: H2LibraryDatabase,
|
private val database: H2LibraryDatabase,
|
||||||
private val log: (String) -> Unit = {},
|
private val log: (String) -> Unit = {},
|
||||||
|
private val objectStore: ManagedBookObjectStore? = null,
|
||||||
) {
|
) {
|
||||||
fun scanSubtree(root: File, onProgress: (LibraryScanSummary) -> Unit = {}): LibraryScanSummary {
|
fun scanSubtree(root: File, onProgress: (LibraryScanSummary) -> Unit = {}): LibraryScanSummary {
|
||||||
require(root.isDirectory) { "Scan root is not a directory: ${root.path}" }
|
require(root.isDirectory) { "Scan root is not a directory: ${root.path}" }
|
||||||
@@ -100,9 +101,15 @@ class LibraryScanner(
|
|||||||
sizeBytes: Long?,
|
sizeBytes: Long?,
|
||||||
lastModifiedMillis: Long?,
|
lastModifiedMillis: Long?,
|
||||||
): LibraryImportResult {
|
): LibraryImportResult {
|
||||||
|
val managed = objectStore?.importBytes(bytes)
|
||||||
|
val savedUri = managed?.let { objectStore.fileForObject(it.objectId).absolutePath } ?: storageUri
|
||||||
|
val storageKind = if (managed != null) BookFileStorageKind.MANAGED_FILE else BookFileStorageKind.EXTERNAL_URI
|
||||||
val rawSha256 = bytes.sha256Hex()
|
val rawSha256 = bytes.sha256Hex()
|
||||||
if (sizeBytes != null) {
|
if (sizeBytes != null) {
|
||||||
database.files.findByOriginalFilenameSizeAndRawSha256(displayName, sizeBytes, rawSha256).firstOrNull()?.let {
|
database.files.findByOriginalFilenameSizeAndRawSha256(displayName, sizeBytes, rawSha256).firstOrNull()?.let {
|
||||||
|
if (managed != null) database.files.upsert(it.copy(
|
||||||
|
storageKind = storageKind, storageUri = savedUri, updatedAt = System.currentTimeMillis(),
|
||||||
|
))
|
||||||
return LibraryImportResult(fileId = it.id, imported = false)
|
return LibraryImportResult(fileId = it.id, imported = false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -122,8 +129,8 @@ class LibraryScanner(
|
|||||||
mimeType = displayName.bookMimeType(),
|
mimeType = displayName.bookMimeType(),
|
||||||
sizeBytes = sizeBytes,
|
sizeBytes = sizeBytes,
|
||||||
originalFilename = displayName,
|
originalFilename = displayName,
|
||||||
storageKind = BookFileStorageKind.EXTERNAL_URI,
|
storageKind = storageKind,
|
||||||
storageUri = storageUri,
|
storageUri = savedUri,
|
||||||
lastModifiedMillis = lastModifiedMillis,
|
lastModifiedMillis = lastModifiedMillis,
|
||||||
lastSeenAt = now,
|
lastSeenAt = now,
|
||||||
importedAt = now,
|
importedAt = now,
|
||||||
@@ -205,8 +212,8 @@ class LibraryScanner(
|
|||||||
mimeType = displayName.bookMimeType(),
|
mimeType = displayName.bookMimeType(),
|
||||||
sizeBytes = sizeBytes,
|
sizeBytes = sizeBytes,
|
||||||
originalFilename = displayName,
|
originalFilename = displayName,
|
||||||
storageKind = BookFileStorageKind.EXTERNAL_URI,
|
storageKind = storageKind,
|
||||||
storageUri = storageUri,
|
storageUri = savedUri,
|
||||||
lastModifiedMillis = lastModifiedMillis,
|
lastModifiedMillis = lastModifiedMillis,
|
||||||
lastSeenAt = now,
|
lastSeenAt = now,
|
||||||
importedAt = now,
|
importedAt = now,
|
||||||
|
|||||||
+8
-1
@@ -1,6 +1,7 @@
|
|||||||
package net.sergeych.toread.storage.jdbc
|
package net.sergeych.toread.storage.jdbc
|
||||||
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.io.InputStream
|
||||||
import java.security.MessageDigest
|
import java.security.MessageDigest
|
||||||
|
|
||||||
data class ManagedBookObject(
|
data class ManagedBookObject(
|
||||||
@@ -16,6 +17,12 @@ class ManagedBookObjectStore(
|
|||||||
|
|
||||||
fun importFile(source: File): ManagedBookObject {
|
fun importFile(source: File): ManagedBookObject {
|
||||||
require(source.isFile) { "Book source is not a file: ${source.path}" }
|
require(source.isFile) { "Book source is not a file: ${source.path}" }
|
||||||
|
return source.inputStream().use(::importStream)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun importBytes(bytes: ByteArray): ManagedBookObject = bytes.inputStream().use(::importStream)
|
||||||
|
|
||||||
|
private fun importStream(input: InputStream): ManagedBookObject {
|
||||||
objectsDirectory.mkdirs()
|
objectsDirectory.mkdirs()
|
||||||
|
|
||||||
val tempFile = File.createTempFile("book-object-", ".tmp", objectsDirectory)
|
val tempFile = File.createTempFile("book-object-", ".tmp", objectsDirectory)
|
||||||
@@ -23,7 +30,7 @@ class ManagedBookObjectStore(
|
|||||||
var size = 0L
|
var size = 0L
|
||||||
|
|
||||||
try {
|
try {
|
||||||
source.inputStream().use { input ->
|
run {
|
||||||
tempFile.outputStream().use { output ->
|
tempFile.outputStream().use { output ->
|
||||||
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
|
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|||||||
@@ -9,6 +9,36 @@ import kotlin.test.assertNull
|
|||||||
import kotlin.test.assertTrue
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
class LibraryScannerTest {
|
class LibraryScannerTest {
|
||||||
|
@Test
|
||||||
|
fun managedImportSurvivesSourceDeletionAndRepairsExistingLink() {
|
||||||
|
val root = Files.createTempDirectory("yereed-managed-import").toFile()
|
||||||
|
val databasePath = File(root, "db/library").also { it.parentFile.mkdirs() }.absolutePath
|
||||||
|
val source = File(root, "source.fb2.zip")
|
||||||
|
source.writeBytes(File(findProjectRoot(), "test_books/Maraini_Zapiski-Terezy-Numy.G7vc8A.872381.fb2.zip").readBytes())
|
||||||
|
val bytes = source.readBytes()
|
||||||
|
val store = ManagedBookObjectStore(File(root, "library"))
|
||||||
|
try {
|
||||||
|
val id = H2LibraryDatabase.openFile(databasePath).useTestDatabase { db ->
|
||||||
|
val linked = LibraryScanner(db).importExternalFileAndGetResult(
|
||||||
|
bytes, source.name, "content://unavailable.provider/book", bytes.size.toLong(), null,
|
||||||
|
)
|
||||||
|
val copied = LibraryScanner(db, objectStore = store).importExternalFileAndGetResult(
|
||||||
|
bytes, source.name, source.absolutePath, bytes.size.toLong(), null,
|
||||||
|
)
|
||||||
|
assertEquals(linked.fileId, copied.fileId)
|
||||||
|
copied.fileId
|
||||||
|
}
|
||||||
|
source.delete()
|
||||||
|
H2LibraryDatabase.openFile(databasePath).useTestDatabase { db ->
|
||||||
|
val saved = assertNotNull(db.files.get(id))
|
||||||
|
assertEquals(net.sergeych.toread.storage.BookFileStorageKind.MANAGED_FILE, saved.storageKind)
|
||||||
|
assertTrue(bytes.contentEquals(File(assertNotNull(saved.storageUri)).readBytes()))
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
root.deleteRecursively()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun scansFb2ZipSubtreeIntoLinkedLibraryRecords() {
|
fun scansFb2ZipSubtreeIntoLinkedLibraryRecords() {
|
||||||
val root = findProjectRoot()
|
val root = findProjectRoot()
|
||||||
@@ -132,3 +162,6 @@ class LibraryScannerTest {
|
|||||||
error("Could not locate project root")
|
error("Could not locate project root")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun <T> H2LibraryDatabase.useTestDatabase(block: (H2LibraryDatabase) -> T): T =
|
||||||
|
try { block(this) } finally { close() }
|
||||||
Reference in new issue
Block a user