From ba6fc2e5d1db9dede747f96dab7b802acf28eee1 Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 11 Dec 2020 11:19:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 2 ++ app/src/main/assets/updateLog.md | 3 +++ app/src/main/java/io/legado/app/App.kt | 6 +++-- .../io/legado/app/help/http/HttpHelper.kt | 24 ++++++------------ .../io/legado/app/help/storage/BookWebDav.kt | 6 ++--- .../association/ImportBookSourceViewModel.kt | 7 ++---- .../association/ImportReplaceRuleViewModel.kt | 2 +- .../association/ImportRssSourceViewModel.kt | 4 +-- .../app/ui/book/read/ReadBookViewModel.kt | 25 +++++++------------ .../book/read/config/SpeakEngineViewModel.kt | 2 +- .../ui/book/read/config/TocRegexViewModel.kt | 2 +- 11 files changed, 35 insertions(+), 48 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index e606cb85c..f5baacd8e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -172,8 +172,10 @@ dependencies { implementation 'com.github.gedoor:rhino-android:1.4' //网络 + implementation 'com.ljx.rxhttp:rxhttp:2.4.4' implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.github.franmontiel:PersistentCookieJar:v1.0.1' + kapt 'com.ljx.rxhttp:rxhttp-compiler:2.4.4' //Glide implementation 'com.github.bumptech.glide:glide:4.11.0' diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index 4a97f0242..bfbb28280 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -3,6 +3,9 @@ * 关注合作公众号 **[小说拾遗]** 获取好看的小说。 * 旧版数据导入教程:先在旧版阅读(2.x)中进行备份,然后在新版阅读(3.x)【我的】->【备份与恢复】,选择【导入旧版本数据】。 +**2020/12/11** +* 修复因修改进度同步导致的bug + **2020/12/09** * 修复bug * 优化中文排序 diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index e43ce9ac7..7fdab716d 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -15,9 +15,11 @@ import io.legado.app.constant.AppConst.channelIdWeb import io.legado.app.constant.EventBus import io.legado.app.data.AppDatabase import io.legado.app.help.* +import io.legado.app.help.http.HttpHelper import io.legado.app.utils.LanguageUtils import io.legado.app.utils.postEvent import org.jetbrains.anko.defaultSharedPreferences +import rxhttp.wrapper.param.RxHttp @Suppress("DEPRECATION") class App : MultiDexApplication() { @@ -44,14 +46,14 @@ class App : MultiDexApplication() { CrashHandler(this) LanguageUtils.setConfiguration(this) db = AppDatabase.createDatabase(INSTANCE) + RxHttp.init(HttpHelper.client, BuildConfig.DEBUG) packageManager.getPackageInfo(packageName, 0)?.let { versionCode = it.versionCode versionName = it.versionName } createNotificationChannels() applyDayNight() - LiveEventBus - .config() + LiveEventBus.config() .supportBroadcast(this) .lifecycleObserverAlwaysActive(true) .autoClear(false) diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index c6568f50e..0498c6251 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -7,6 +7,8 @@ import io.legado.app.utils.NetworkUtils import kotlinx.coroutines.suspendCancellableCoroutine import okhttp3.* import retrofit2.Retrofit +import rxhttp.toStr +import rxhttp.wrapper.param.RxHttp import java.net.InetSocketAddress import java.net.Proxy import java.util.concurrent.TimeUnit @@ -39,23 +41,11 @@ object HttpHelper { builder.build() } - fun simpleGet(url: String, encode: String? = null): String? { - NetworkUtils.getBaseUrl(url)?.let { baseUrl -> - val response = getApiService(baseUrl, encode) - .get(url, mapOf(Pair(AppConst.UA_NAME, AppConfig.userAgent))) - .execute() - return response.body() - } - return null - } - - suspend fun simpleGetAsync(url: String, encode: String? = null): String? { - NetworkUtils.getBaseUrl(url)?.let { baseUrl -> - val response = getApiService(baseUrl, encode) - .getAsync(url, mapOf(Pair(AppConst.UA_NAME, AppConfig.userAgent))) - return response.body() - } - return null + suspend fun simpleGetAsync(url: String): String { + val str = RxHttp.get(url) + .addHeader(AppConst.UA_NAME, AppConfig.userAgent) + .toStr() + return str.await() } suspend fun simpleGetBytesAsync(url: String): ByteArray? { diff --git a/app/src/main/java/io/legado/app/help/storage/BookWebDav.kt b/app/src/main/java/io/legado/app/help/storage/BookWebDav.kt index c6facec28..f52bd9459 100644 --- a/app/src/main/java/io/legado/app/help/storage/BookWebDav.kt +++ b/app/src/main/java/io/legado/app/help/storage/BookWebDav.kt @@ -155,7 +155,7 @@ object BookWebDav { durChapterTitle = book.durChapterTitle ) val json = GSON.toJson(bookProgress) - val url = getUrl(book) + val url = geProtresstUrl(book) if (initWebDav()) { WebDav(url).upload(json.toByteArray()) } @@ -164,7 +164,7 @@ object BookWebDav { fun getBookProgress(book: Book): BookProgress? { if (initWebDav()) { - val url = getUrl(book) + val url = geProtresstUrl(book) WebDav(url).download()?.let { byteArray -> val json = String(byteArray) GSON.fromJsonObject(json)?.let { @@ -175,7 +175,7 @@ object BookWebDav { return null } - private fun getUrl(book: Book): String { + private fun geProtresstUrl(book: Book): String { return bookProgressUrl + book.name + "_" + book.author + ".json" } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt index 664513bc0..2b57d7c06 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt @@ -118,11 +118,8 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { } } - private fun importSourceUrl(url: String) { - HttpHelper.simpleGet(url, "UTF-8").let { body -> - if (body == null) { - throw Exception(context.getString(R.string.error_get_data)) - } + private suspend fun importSourceUrl(url: String) { + HttpHelper.simpleGetAsync(url).let { body -> val items: List> = Restore.jsonPath.parse(body).read("$") for (item in items) { val jsonItem = Restore.jsonPath.parse(item) diff --git a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt index bbd2cea78..28f90e553 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt @@ -48,7 +48,7 @@ class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) { fun import(text: String) { execute { if (text.isAbsUrl()) { - HttpHelper.simpleGet(text)?.let { + HttpHelper.simpleGetAsync(text).let { val rules = OldReplace.jsonToReplaceRules(it) allRules.addAll(rules) } diff --git a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt index 751390a93..8de7a33af 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt @@ -113,8 +113,8 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) { } } - private fun importSourceUrl(url: String) { - HttpHelper.simpleGet(url, "UTF-8")?.let { body -> + private suspend fun importSourceUrl(url: String) { + HttpHelper.simpleGetAsync(url).let { body -> val items: List> = Restore.jsonPath.parse(body).read("$") for (item in items) { val jsonItem = Restore.jsonPath.parse(item) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index 478175e3b..76137cfa7 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -47,7 +47,6 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { private fun initBook(book: Book) { if (ReadBook.book?.bookUrl != book.bookUrl) { - downloadProgress(book) ReadBook.resetData(book) isInitFinish = true if (!book.isLocalBook() && ReadBook.webBook == null) { @@ -67,6 +66,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } ReadBook.loadContent(resetPageOffset = true) } + syncBookProgress(book) } else { ReadBook.book = book if (ReadBook.durChapterIndex != book.durChapterIndex) { @@ -100,15 +100,6 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } } - private fun downloadProgress(book: Book) { - BookWebDav.getBookProgress(book)?.let { - book.durChapterIndex = it.durChapterIndex - book.durChapterPos = it.durChapterPos - book.durChapterTime = it.durChapterTime - book.durChapterTitle = it.durChapterTitle - } - } - private fun loadBookInfo( book: Book, changeDruChapterIndex: ((chapters: List) -> Unit)? = null, @@ -168,12 +159,14 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { fun syncBookProgress(book: Book) { execute { - downloadProgress(book) - ReadBook.durChapterIndex = book.durChapterIndex - ReadBook.durChapterPos = book.durChapterPos - ReadBook.prevTextChapter = null - ReadBook.clearTextChapter() - ReadBook.loadContent(resetPageOffset = true) + BookWebDav.getBookProgress(book)?.let { + book.durChapterIndex = it.durChapterIndex + book.durChapterPos = it.durChapterPos + book.durChapterTime = it.durChapterTime + book.durChapterTitle = it.durChapterTitle + ReadBook.clearTextChapter() + ReadBook.loadContent(resetPageOffset = true) + } } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt index c9680e4d4..e79ad4730 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt @@ -21,7 +21,7 @@ class SpeakEngineViewModel(application: Application) : BaseViewModel(application fun importOnLine(url: String, finally: (msg: String) -> Unit) { execute { - HttpHelper.simpleGetAsync(url)?.let { json -> + HttpHelper.simpleGetAsync(url).let { json -> GSON.fromJsonArray(json)?.let { App.db.httpTTSDao.insert(*it.toTypedArray()) } diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt index cf4793727..ae9ad00cb 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt @@ -28,7 +28,7 @@ class TocRegexViewModel(application: Application) : BaseViewModel(application) { fun importOnLine(url: String, finally: (msg: String) -> Unit) { execute { - HttpHelper.simpleGetAsync(url)?.let { json -> + HttpHelper.simpleGetAsync(url).let { json -> GSON.fromJsonArray(json)?.let { App.db.txtTocRule.insert(*it.toTypedArray()) }