From 795623d1ff6c507c6333982558757378a41d3f07 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 2 Jan 2021 09:24:31 +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/src/main/java/io/legado/app/api/ReaderProvider.kt | 1 - .../io/legado/app/{web/utils => api}/ReturnData.kt | 11 +++-------- .../legado/app/api/controller/BookshelfController.kt | 2 +- .../io/legado/app/api/controller/SourceController.kt | 9 +++++---- app/src/main/java/io/legado/app/web/HttpServer.kt | 2 +- .../app/web/{utils => }/SourceDebugWebSocket.kt | 5 ++--- .../main/java/io/legado/app/web/WebSocketServer.kt | 1 - 7 files changed, 12 insertions(+), 19 deletions(-) rename app/src/main/java/io/legado/app/{web/utils => api}/ReturnData.kt (63%) rename app/src/main/java/io/legado/app/web/{utils => }/SourceDebugWebSocket.kt (96%) diff --git a/app/src/main/java/io/legado/app/api/ReaderProvider.kt b/app/src/main/java/io/legado/app/api/ReaderProvider.kt index b45d2f5c0..897b0d757 100644 --- a/app/src/main/java/io/legado/app/api/ReaderProvider.kt +++ b/app/src/main/java/io/legado/app/api/ReaderProvider.kt @@ -12,7 +12,6 @@ import android.net.Uri import com.google.gson.Gson import io.legado.app.api.controller.BookshelfController import io.legado.app.api.controller.SourceController -import io.legado.app.web.utils.ReturnData import java.util.* /** diff --git a/app/src/main/java/io/legado/app/web/utils/ReturnData.kt b/app/src/main/java/io/legado/app/api/ReturnData.kt similarity index 63% rename from app/src/main/java/io/legado/app/web/utils/ReturnData.kt rename to app/src/main/java/io/legado/app/api/ReturnData.kt index 7c460e5e2..f6d6aefdc 100644 --- a/app/src/main/java/io/legado/app/web/utils/ReturnData.kt +++ b/app/src/main/java/io/legado/app/api/ReturnData.kt @@ -1,4 +1,4 @@ -package io.legado.app.web.utils +package io.legado.app.api class ReturnData { @@ -6,18 +6,13 @@ class ReturnData { var isSuccess: Boolean = false private set - var errorMsg: String? = null + var errorMsg: String = "未知错误,请联系开发者!" private set var data: Any? = null private set - init { - this.isSuccess = false - this.errorMsg = "未知错误,请联系开发者!" - } - - fun setErrorMsg(errorMsg: String?): ReturnData { + fun setErrorMsg(errorMsg: String): ReturnData { this.isSuccess = false this.errorMsg = errorMsg return this diff --git a/app/src/main/java/io/legado/app/api/controller/BookshelfController.kt b/app/src/main/java/io/legado/app/api/controller/BookshelfController.kt index df3c96d1c..c8e878f0f 100644 --- a/app/src/main/java/io/legado/app/api/controller/BookshelfController.kt +++ b/app/src/main/java/io/legado/app/api/controller/BookshelfController.kt @@ -1,6 +1,7 @@ package io.legado.app.api.controller import io.legado.app.App +import io.legado.app.api.ReturnData import io.legado.app.constant.PreferKey import io.legado.app.data.entities.Book import io.legado.app.help.BookHelp @@ -10,7 +11,6 @@ import io.legado.app.utils.GSON import io.legado.app.utils.cnCompare import io.legado.app.utils.fromJsonObject import io.legado.app.utils.getPrefInt -import io.legado.app.web.utils.ReturnData import kotlinx.coroutines.runBlocking object BookshelfController { diff --git a/app/src/main/java/io/legado/app/api/controller/SourceController.kt b/app/src/main/java/io/legado/app/api/controller/SourceController.kt index 69d609931..6dc142804 100644 --- a/app/src/main/java/io/legado/app/api/controller/SourceController.kt +++ b/app/src/main/java/io/legado/app/api/controller/SourceController.kt @@ -3,11 +3,12 @@ package io.legado.app.api.controller import android.text.TextUtils import io.legado.app.App +import io.legado.app.api.ReturnData import io.legado.app.data.entities.BookSource import io.legado.app.utils.GSON import io.legado.app.utils.fromJsonArray import io.legado.app.utils.fromJsonObject -import io.legado.app.web.utils.ReturnData +import io.legado.app.utils.msg object SourceController { @@ -22,7 +23,7 @@ object SourceController { fun saveSource(postData: String?): ReturnData { val returnData = ReturnData() - try { + kotlin.runCatching { val bookSource = GSON.fromJsonObject(postData) if (bookSource != null) { if (TextUtils.isEmpty(bookSource.bookSourceName) || TextUtils.isEmpty(bookSource.bookSourceUrl)) { @@ -34,8 +35,8 @@ object SourceController { } else { returnData.setErrorMsg("转换书源失败") } - } catch (e: Exception) { - returnData.setErrorMsg(e.localizedMessage) + }.onFailure { + returnData.setErrorMsg(it.msg) } return returnData } diff --git a/app/src/main/java/io/legado/app/web/HttpServer.kt b/app/src/main/java/io/legado/app/web/HttpServer.kt index da7c03400..1b8b47eed 100644 --- a/app/src/main/java/io/legado/app/web/HttpServer.kt +++ b/app/src/main/java/io/legado/app/web/HttpServer.kt @@ -2,10 +2,10 @@ package io.legado.app.web import com.google.gson.Gson import fi.iki.elonen.NanoHTTPD +import io.legado.app.api.ReturnData import io.legado.app.api.controller.BookshelfController import io.legado.app.api.controller.SourceController import io.legado.app.web.utils.AssetsWeb -import io.legado.app.web.utils.ReturnData import java.util.* class HttpServer(port: Int) : NanoHTTPD(port) { diff --git a/app/src/main/java/io/legado/app/web/utils/SourceDebugWebSocket.kt b/app/src/main/java/io/legado/app/web/SourceDebugWebSocket.kt similarity index 96% rename from app/src/main/java/io/legado/app/web/utils/SourceDebugWebSocket.kt rename to app/src/main/java/io/legado/app/web/SourceDebugWebSocket.kt index 136035a80..8b18d1179 100644 --- a/app/src/main/java/io/legado/app/web/utils/SourceDebugWebSocket.kt +++ b/app/src/main/java/io/legado/app/web/SourceDebugWebSocket.kt @@ -1,4 +1,4 @@ -package io.legado.app.web.utils +package io.legado.app.web import fi.iki.elonen.NanoHTTPD @@ -20,13 +20,12 @@ class SourceDebugWebSocket(handshakeRequest: NanoHTTPD.IHTTPSession) : CoroutineScope by MainScope(), Debug.Callback { - override fun onOpen() { launch(IO) { do { delay(30000) runCatching { - ping(byteArrayOf("ping".toByte())) + ping("ping".toByteArray()) } } while (isOpen) } diff --git a/app/src/main/java/io/legado/app/web/WebSocketServer.kt b/app/src/main/java/io/legado/app/web/WebSocketServer.kt index a32b5f2a3..d6a259687 100644 --- a/app/src/main/java/io/legado/app/web/WebSocketServer.kt +++ b/app/src/main/java/io/legado/app/web/WebSocketServer.kt @@ -1,7 +1,6 @@ package io.legado.app.web import fi.iki.elonen.NanoWSD -import io.legado.app.web.utils.SourceDebugWebSocket class WebSocketServer(port: Int) : NanoWSD(port) {