Skip to content

Commit

Permalink
Merge branch 'LNReader:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Batorian authored Jun 27, 2024
2 parents 9dce568 + 9b4bdfe commit ba1b787
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.rajarsheechatterjee.EpubUtil

class ChapterEntry (val name: String, val href: String)
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class EpubUtil(context: ReactApplicationContext) : ReactContextBaseJavaModule(co
}

private fun cleanUrl(url: String): String {
return url.replaceFirst("#[^.]+?$", "")
return url.replaceFirst("#[^.]+?$".toRegex(), "")
}

@ReactMethod
Expand Down Expand Up @@ -69,24 +69,30 @@ class EpubUtil(context: ReactApplicationContext) : ReactContextBaseJavaModule(co
val chapters: WritableArray = WritableNativeArray()
val parser = initParse(file)
val refMap = HashMap<String, String>()
val tocMap = HashMap<String, String?>()
val entryList = mutableListOf<ChapterEntry>()
val tocFile = File(contentDir, "toc.ncx")
if (tocFile.exists()) {
val tocParser = initParse(tocFile)
var label: String? = null
var label = ""
while (tocParser.next() != XmlPullParser.END_DOCUMENT) {
val tag = tocParser.name
if (tag != null) {
if (tag == "text") {
label = readText(tocParser)
} else if (tag == "content") {
val href = cleanUrl(tocParser.getAttributeValue(null, "src"))
tocMap[href] = label
if(label.isNotBlank()){
entryList.add(ChapterEntry(name = label, href = href))
}
label = ""
}
}
}
}else{
throw Error("Table of content doesn't exist!")
}
var cover: String? = null
var entryIndex = 0;
while (parser.next() != XmlPullParser.END_DOCUMENT) {
val tag = parser.name
if (tag != null) {
Expand All @@ -98,16 +104,21 @@ class EpubUtil(context: ReactApplicationContext) : ReactContextBaseJavaModule(co
refMap[id] = href
}
}

"itemref" -> {
val idRef = parser.getAttributeValue(null, "idref")
val href = refMap[idRef]
if (href != null) {
val chapter: WritableMap = WritableNativeMap()
chapter.putString("path", "$contentDir/$href")
val name = tocMap[href]
chapter.putString("name", name ?: href)
chapters.pushMap(chapter)
val chapterFile = File("$contentDir/$href")
if (chapterFile.exists()) {
if (entryIndex == 0 || entryIndex < entryList.size && entryList[entryIndex].href == href) {
val newChapter = WritableNativeMap()
newChapter.putString("path", chapterFile.path)
newChapter.putString("name", entryList[entryIndex].name)
chapters.pushMap(newChapter)
entryIndex += 1
}else{
// merge to previous entry
File("$contentDir/${entryList[entryIndex - 1].href}").appendBytes(chapterFile.readBytes())
}
}
}

Expand All @@ -128,6 +139,19 @@ class EpubUtil(context: ReactApplicationContext) : ReactContextBaseJavaModule(co
if (cover != null) {
val coverPath = contentDir + "/" + refMap[cover]
novel.putString("cover", coverPath)
} else {
// try scanning Images dir if exists
val imageDir = File("$contentDir/Images")
if(imageDir.exists() && imageDir.isDirectory){
imageDir.listFiles()?.forEach { img ->
if (img.isFile && img.name.lowercase().contains("cover|illus".toRegex())){
cover = img.path
}
}
}
if (cover != null){
novel.putString("cover", cover)
}
}
novel.putArray("chapters", chapters)
return novel
Expand Down
15 changes: 8 additions & 7 deletions src/screens/reader/ReaderScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,16 @@ export const ChapterContent = ({
VolumeButtonListener.preventDefault();
emmiter.current.addListener('VolumeUp', () => {
webViewRef.current?.injectJavaScript(`(()=>{
window.scrollBy({top:${-Dimensions.get('window')
.height},behavior:'smooth',})
window.scrollBy({top: -${
Dimensions.get('window').height * 0.75
}, behavior: 'smooth'})
})()`);
});
emmiter.current.addListener('VolumeDown', () => {
webViewRef.current?.injectJavaScript(`(()=>{
window.scrollBy({top:${
Dimensions.get('window').height
},behavior:'smooth',})
window.scrollBy({top: ${
Dimensions.get('window').height * 0.75
}, behavior: 'smooth'})
})()`);
});
};
Expand Down Expand Up @@ -291,8 +292,8 @@ export const ChapterContent = ({

const openDrawer = useCallback(() => {
drawerRef.current?.openDrawer();
setHidden(true);
}, [drawerRef]);
hideHeader();
}, [drawerRef, hideHeader]);

if (loading) {
return <ChapterLoadingScreen />;
Expand Down

0 comments on commit ba1b787

Please sign in to comment.