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
|
||||
|
||||
val appVersionName = "1.1"
|
||||
val appVersionCode = 10
|
||||
val appVersionCode = 12
|
||||
val appVersionDisplay = "$appVersionName.$appVersionCode"
|
||||
|
||||
plugins {
|
||||
|
||||
@@ -24,6 +24,7 @@ import net.sergeych.toread.storage.ContentAnchor
|
||||
import net.sergeych.toread.storage.LibraryFileRecord
|
||||
import net.sergeych.toread.storage.ReadingStateRecord
|
||||
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.LibraryScanSummary
|
||||
import java.io.File
|
||||
@@ -122,7 +123,7 @@ actual suspend fun loadPlatformOpenBookRequest(): PlatformOpenBookRequest? = wit
|
||||
val displayName = displayNameFor(uri)
|
||||
val sizeBytes = sizeBytesFor(uri) ?: bytes.size.toLong()
|
||||
val importResult = openLibraryDatabase().useLibrary { db ->
|
||||
LibraryScanner(db, ::appendLibraryLog).importExternalFileAndGetResult(
|
||||
LibraryScanner(db, ::appendLibraryLog, ManagedBookObjectStore(File(appContext.filesDir, "library"))).importExternalFileAndGetResult(
|
||||
bytes = bytes,
|
||||
displayName = displayName,
|
||||
storageUri = uri.toString(),
|
||||
@@ -221,7 +222,7 @@ actual suspend fun scanLibrarySubtree(
|
||||
error(strings.allFilesAccessRequired(path))
|
||||
}
|
||||
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())
|
||||
}
|
||||
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) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
db.getAppFlag(ScanDownloadsAutomaticallyFlag)?.toBooleanStrictOrNull() ?: true
|
||||
@@ -612,7 +625,7 @@ private fun scanLibraryContentTree(
|
||||
onProgress: (LibraryScanProgress) -> Unit,
|
||||
): LibraryScanReport =
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
val scanner = LibraryScanner(db, ::appendLibraryLog)
|
||||
val scanner = LibraryScanner(db, ::appendLibraryLog, ManagedBookObjectStore(File(appContext.filesDir, "library")))
|
||||
var scanned = 0
|
||||
var imported = 0
|
||||
var skipped = 0
|
||||
@@ -917,6 +930,7 @@ private const val ThemeModeFlag = "theme_mode"
|
||||
private const val ReaderFontSettingsFlag = "reader_font_settings"
|
||||
private const val ReaderFullScreenFlag = "reader_full_screen"
|
||||
private const val ReaderPagingOnlyFlag = "reader_paging_only"
|
||||
private const val ReaderRightHandFlag = "reader_right_hand"
|
||||
private const val ScanDownloadsAutomaticallyFlag = "scan_downloads_automatically"
|
||||
private const val LibraryFilterFlag = "library_filter"
|
||||
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 loadReaderRightHand(): Boolean
|
||||
|
||||
expect suspend fun saveReaderRightHand(enabled: Boolean)
|
||||
|
||||
expect suspend fun loadScanDownloadsAutomatically(): Boolean
|
||||
|
||||
expect suspend fun saveScanDownloadsAutomatically(enabled: Boolean)
|
||||
|
||||
@@ -160,6 +160,8 @@ internal open class AppStrings {
|
||||
open val readerTypography = "Typography"
|
||||
open val readerLayout = "Layout"
|
||||
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 readerProgress = "Progress"
|
||||
open val closeReaderSettings = "Close reader settings"
|
||||
@@ -389,6 +391,8 @@ internal object RussianStrings : AppStrings() {
|
||||
override val readerTypography = "Типографика"
|
||||
override val readerLayout = "Макет"
|
||||
override val readerNavigation = "Навигация"
|
||||
override val readerRightHand = "Правая рука (обратные жесты страниц)"
|
||||
override val readerLeftHand = "Левая рука (обычные жесты страниц)"
|
||||
override val readerPagingOnly = "Постраничный режим (без прокрутки)"
|
||||
override val readerProgress = "Прогресс"
|
||||
override val closeReaderSettings = "Закрыть настройки чтения"
|
||||
|
||||
@@ -3,6 +3,7 @@ package net.sergeych.toread
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.gestures.stopScroll
|
||||
import androidx.compose.foundation.gestures.animateScrollBy
|
||||
import androidx.compose.foundation.gestures.awaitEachGesture
|
||||
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.graphicsLayer
|
||||
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.pointerInput
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
@@ -129,11 +129,15 @@ internal fun ContinuousBookReader(
|
||||
readerBackgroundColor: Color = MaterialTheme.colorScheme.surface,
|
||||
readerTextColor: Color = MaterialTheme.colorScheme.onSurface,
|
||||
pagingOnly: Boolean = false,
|
||||
rightHand: Boolean = false,
|
||||
onReaderFontZoom: (Int) -> Unit = {},
|
||||
onUserScroll: () -> Unit = {},
|
||||
onImageOpen: (ViewedBookImage) -> Unit = {},
|
||||
onNoteOpen: (String) -> Unit = {},
|
||||
) {
|
||||
LaunchedEffect(listState, pagingOnly) {
|
||||
if (pagingOnly) listState.stopScroll()
|
||||
}
|
||||
val hyphenation = remember { HyphenationRegistry() }
|
||||
val scope = rememberCoroutineScope()
|
||||
val textLineMetricsByItem = remember(contentPlan, readerFontSettings) { mutableStateMapOf<Int, TextLineMetrics>() }
|
||||
@@ -155,9 +159,10 @@ internal fun ContinuousBookReader(
|
||||
modifier = modifier
|
||||
.background(readerBackgroundColor)
|
||||
.nestedScroll(userScrollConnection)
|
||||
.blockFreeScroll(pagingOnly)
|
||||
.readerFontZoomGesture(onReaderFontZoom)
|
||||
.pageTurnOnTouchSwipe(
|
||||
enabled = pagingOnly,
|
||||
rightHand = rightHand,
|
||||
onPageDown = {
|
||||
onUserScroll()
|
||||
scope.launch {
|
||||
@@ -172,6 +177,8 @@ internal fun ContinuousBookReader(
|
||||
},
|
||||
)
|
||||
.pageTurnOnTouchTap(
|
||||
enabled = pagingOnly,
|
||||
rightHand = rightHand,
|
||||
onPageDown = {
|
||||
onUserScroll()
|
||||
scope.launch {
|
||||
@@ -185,6 +192,7 @@ internal fun ContinuousBookReader(
|
||||
}
|
||||
},
|
||||
),
|
||||
userScrollEnabled = !pagingOnly,
|
||||
contentPadding = contentPadding,
|
||||
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)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(
|
||||
enabled: Boolean,
|
||||
rightHand: Boolean,
|
||||
onPageDown: () -> Unit,
|
||||
onPageUp: () -> Unit,
|
||||
): Modifier = pointerInput(onPageDown, onPageUp) {
|
||||
): Modifier = if (!enabled) this else pointerInput(rightHand, onPageDown, onPageUp) {
|
||||
awaitEachGesture {
|
||||
val down = awaitFirstDown(requireUnconsumed = false, pass = PointerEventPass.Initial)
|
||||
if (down.isConsumed) return@awaitEachGesture
|
||||
@@ -412,7 +385,7 @@ private fun Modifier.pageTurnOnTouchSwipe(
|
||||
movedBeyondTapSlop = true
|
||||
}
|
||||
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()
|
||||
} else if (movedBeyondTapSlop && !isHorizontalSwipeCandidate) {
|
||||
cancelled = true
|
||||
@@ -450,9 +423,11 @@ private fun Modifier.pageTurnOnTouchSwipe(
|
||||
}
|
||||
|
||||
private fun Modifier.pageTurnOnTouchTap(
|
||||
enabled: Boolean,
|
||||
rightHand: Boolean,
|
||||
onPageDown: () -> Unit,
|
||||
onPageUp: () -> Unit,
|
||||
): Modifier = pointerInput(onPageDown, onPageUp) {
|
||||
): Modifier = if (!enabled) this else pointerInput(rightHand, onPageDown, onPageUp) {
|
||||
awaitEachGesture {
|
||||
val down = awaitFirstDown(requireUnconsumed = false, pass = PointerEventPass.Final)
|
||||
if (down.isConsumed) return@awaitEachGesture
|
||||
@@ -486,7 +461,7 @@ private fun Modifier.pageTurnOnTouchTap(
|
||||
|
||||
if (cancelled) return@awaitEachGesture
|
||||
|
||||
if (down.position.x < size.width / 2f) {
|
||||
if ((down.position.x < size.width / 2f) != rightHand) {
|
||||
onPageDown()
|
||||
} else {
|
||||
onPageUp()
|
||||
|
||||
@@ -153,6 +153,7 @@ internal fun BookView(
|
||||
var readerColorPickerTarget by remember(fileId) { mutableStateOf<ReaderColorPickerTarget?>(null) }
|
||||
var fullScreenReader by remember { mutableStateOf(false) }
|
||||
var readerPagingOnly by remember { mutableStateOf(false) }
|
||||
var readerRightHand by remember { mutableStateOf(false) }
|
||||
var tableOfContentsVisible by remember(fileId) { mutableStateOf(false) }
|
||||
var tableOfContentsBackStack by remember(fileId) { mutableStateOf<List<ReadingPosition>>(emptyList()) }
|
||||
var tableOfContentsBackPosition by remember(fileId) { mutableStateOf<ReadingPosition?>(null) }
|
||||
@@ -313,6 +314,7 @@ internal fun BookView(
|
||||
readerFontSettings = loadReaderFontSettings()
|
||||
fullScreenReader = loadReaderFullScreen()
|
||||
readerPagingOnly = loadReaderPagingOnly()
|
||||
readerRightHand = loadReaderRightHand()
|
||||
}
|
||||
|
||||
DisposableEffect(fileId) {
|
||||
@@ -507,6 +509,7 @@ internal fun BookView(
|
||||
readerBackgroundColor = readerBackgroundColor,
|
||||
readerTextColor = readerTextColor,
|
||||
pagingOnly = readerPagingOnly,
|
||||
rightHand = readerRightHand,
|
||||
onReaderFontZoom = { steps ->
|
||||
updateReaderFontSettings { settings -> settings.adjustFontSize(steps) }
|
||||
},
|
||||
@@ -619,6 +622,11 @@ internal fun BookView(
|
||||
},
|
||||
pagingOnly = readerPagingOnly,
|
||||
onPagingOnlyChange = { pagingOnly -> updateReaderPagingOnly(pagingOnly) },
|
||||
rightHand = readerRightHand,
|
||||
onRightHandChange = { enabled ->
|
||||
readerRightHand = enabled
|
||||
scope.launch { saveReaderRightHand(enabled) }
|
||||
},
|
||||
onDismiss = { readerExtraSettingsVisible = false },
|
||||
)
|
||||
}
|
||||
@@ -1409,6 +1417,8 @@ private fun ReaderExtraSettingsDialog(
|
||||
onRevertColorScheme: () -> Unit,
|
||||
pagingOnly: Boolean,
|
||||
onPagingOnlyChange: (Boolean) -> Unit,
|
||||
rightHand: Boolean,
|
||||
onRightHandChange: (Boolean) -> Unit,
|
||||
onDismiss: () -> Unit,
|
||||
) {
|
||||
AlertDialog(
|
||||
@@ -1466,6 +1476,21 @@ private fun ReaderExtraSettingsDialog(
|
||||
)
|
||||
}
|
||||
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 = {
|
||||
|
||||
@@ -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) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
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 ReaderFullScreenFlag = "reader_full_screen"
|
||||
private const val ReaderPagingOnlyFlag = "reader_paging_only"
|
||||
private const val ReaderRightHandFlag = "reader_right_hand"
|
||||
private const val ScanDownloadsAutomaticallyFlag = "scan_downloads_automatically"
|
||||
private const val LibraryFilterFlag = "library_filter"
|
||||
private const val AppLocaleTagFlag = "app_locale_tag"
|
||||
|
||||
@@ -97,6 +97,13 @@ actual suspend fun saveReaderPagingOnly(enabled: Boolean) {
|
||||
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 saveScanDownloadsAutomatically(enabled: Boolean) = Unit
|
||||
@@ -160,6 +167,7 @@ private const val LibraryFilterStorageKey = "toread.libraryFilter"
|
||||
private const val ReaderFontSettingsStorageKey = "toread.readerFontSettings"
|
||||
private const val ReaderFullScreenStorageKey = "toread.readerFullScreen"
|
||||
private const val ReaderPagingOnlyStorageKey = "toread.readerPagingOnly"
|
||||
private const val ReaderRightHandStorageKey = "toread.readerRightHand"
|
||||
|
||||
actual fun formatLibraryLastReadTime(millis: Long): String {
|
||||
val totalMinutes = millis / 60_000L
|
||||
|
||||
@@ -34,6 +34,7 @@ data class LibraryImportResult(
|
||||
class LibraryScanner(
|
||||
private val database: H2LibraryDatabase,
|
||||
private val log: (String) -> Unit = {},
|
||||
private val objectStore: ManagedBookObjectStore? = null,
|
||||
) {
|
||||
fun scanSubtree(root: File, onProgress: (LibraryScanSummary) -> Unit = {}): LibraryScanSummary {
|
||||
require(root.isDirectory) { "Scan root is not a directory: ${root.path}" }
|
||||
@@ -100,9 +101,15 @@ class LibraryScanner(
|
||||
sizeBytes: Long?,
|
||||
lastModifiedMillis: Long?,
|
||||
): 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()
|
||||
if (sizeBytes != null) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -122,8 +129,8 @@ class LibraryScanner(
|
||||
mimeType = displayName.bookMimeType(),
|
||||
sizeBytes = sizeBytes,
|
||||
originalFilename = displayName,
|
||||
storageKind = BookFileStorageKind.EXTERNAL_URI,
|
||||
storageUri = storageUri,
|
||||
storageKind = storageKind,
|
||||
storageUri = savedUri,
|
||||
lastModifiedMillis = lastModifiedMillis,
|
||||
lastSeenAt = now,
|
||||
importedAt = now,
|
||||
@@ -205,8 +212,8 @@ class LibraryScanner(
|
||||
mimeType = displayName.bookMimeType(),
|
||||
sizeBytes = sizeBytes,
|
||||
originalFilename = displayName,
|
||||
storageKind = BookFileStorageKind.EXTERNAL_URI,
|
||||
storageUri = storageUri,
|
||||
storageKind = storageKind,
|
||||
storageUri = savedUri,
|
||||
lastModifiedMillis = lastModifiedMillis,
|
||||
lastSeenAt = now,
|
||||
importedAt = now,
|
||||
|
||||
+8
-1
@@ -1,6 +1,7 @@
|
||||
package net.sergeych.toread.storage.jdbc
|
||||
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
import java.security.MessageDigest
|
||||
|
||||
data class ManagedBookObject(
|
||||
@@ -16,6 +17,12 @@ class ManagedBookObjectStore(
|
||||
|
||||
fun importFile(source: File): ManagedBookObject {
|
||||
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()
|
||||
|
||||
val tempFile = File.createTempFile("book-object-", ".tmp", objectsDirectory)
|
||||
@@ -23,7 +30,7 @@ class ManagedBookObjectStore(
|
||||
var size = 0L
|
||||
|
||||
try {
|
||||
source.inputStream().use { input ->
|
||||
run {
|
||||
tempFile.outputStream().use { output ->
|
||||
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
|
||||
while (true) {
|
||||
|
||||
@@ -9,6 +9,36 @@ import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
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
|
||||
fun scansFb2ZipSubtreeIntoLinkedLibraryRecords() {
|
||||
val root = findProjectRoot()
|
||||
@@ -132,3 +162,6 @@ class LibraryScannerTest {
|
||||
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