Skip to content

Commit fdb891c

Browse files
committed
Add reading time
1 parent f363453 commit fdb891c

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

app/src/main/java/com/capyreader/app/ui/accounts/LoginModule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.capyreader.app.ui.accounts
22

3-
import org.koin.androidx.viewmodel.dsl.viewModel
3+
import org.koin.core.module.dsl.viewModel
44
import org.koin.dsl.module
55

66
val loginModule = module {

app/src/main/java/com/capyreader/app/ui/articles/ArticlesModule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.capyreader.app.ui.articles
33
import com.capyreader.app.preferences.AppPreferences
44
import com.capyreader.app.ui.articles.feeds.edit.EditFeedViewModel
55
import com.jocmp.capy.articles.ArticleRenderer
6-
import org.koin.androidx.viewmodel.dsl.viewModel
6+
import org.koin.core.module.dsl.viewModel
77
import org.koin.dsl.module
88

99
internal val articlesModule = module {

app/src/main/java/com/capyreader/app/ui/settings/SettingsModule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.capyreader.app.ui.settings.panels.DisplaySettingsViewModel
66
import com.capyreader.app.ui.settings.panels.GeneralSettingsViewModel
77
import com.capyreader.app.ui.settings.panels.GesturesSettingsViewModel
88
import com.capyreader.app.ui.settings.panels.SettingsViewModel
9-
import org.koin.androidx.viewmodel.dsl.viewModel
9+
import org.koin.core.module.dsl.viewModel
1010
import org.koin.androidx.workmanager.dsl.worker
1111
import org.koin.dsl.module
1212

capy/src/main/java/com/jocmp/capy/articles/ArticleRenderer.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class ArticleRenderer(
4242
article.feedName
4343
}
4444

45+
val characters = article.content.length;
46+
val charactersPerMinute = if (isCJK(article.content)) 265 else 500
47+
val readingTime = (characters + charactersPerMinute - 1) / charactersPerMinute; // 向上取整分钟
48+
4549
val substitutions = colors + mapOf(
4650
"external_link" to article.externalLink(),
4751
"title" to title,
@@ -52,6 +56,7 @@ class ArticleRenderer(
5256
"font_preload" to fontPreload(fontFamily),
5357
"top_margin" to topMargin(),
5458
"pre_white_space" to preWhiteSpace(),
59+
"reading_time" to "$readingTime min read"
5560
)
5661

5762
val html = MacroProcessor(
@@ -106,6 +111,23 @@ class ArticleRenderer(
106111
""".trimIndent()
107112
}
108113
}
114+
115+
fun isCJK(text: String): Boolean {
116+
for (ch in text) {
117+
val code = ch.code
118+
if (
119+
(code in 0x4E00..0x9FFF) || // 汉字
120+
(code in 0x3400..0x4DBF) || // 扩展A
121+
(code in 0x20000..0x2A6DF) || // 扩展B
122+
(code in 0x3040..0x309F) || // 日文平假名
123+
(code in 0x30A0..0x30FF) || // 日文片假名
124+
(code in 0xAC00..0xD7AF) // 韩文音节
125+
) {
126+
return true
127+
}
128+
}
129+
return false
130+
}
109131
}
110132

111133
private val Document.content

capy/src/main/res/raw/template.html

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)