Merge pull request #369 from 1552980358/master

Modify code style of package `io.legado.app.data`
pull/370/head
kunfei 4 years ago committed by GitHub
commit 7d3faace74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  2. 4
      app/src/main/java/io/legado/app/data/entities/Book.kt
  3. 11
      app/src/main/java/io/legado/app/data/entities/BookChapter.kt
  4. 101
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  5. 17
      app/src/main/java/io/legado/app/data/entities/RssArticle.kt
  6. 41
      app/src/main/java/io/legado/app/data/entities/RssSource.kt
  7. 4
      app/src/main/java/io/legado/app/data/entities/RssStar.kt
  8. 17
      app/src/main/java/io/legado/app/data/entities/SearchBook.kt

@ -25,8 +25,8 @@ abstract class AppDatabase : RoomDatabase() {
private const val DATABASE_NAME = "legado.db"
fun createDatabase(context: Context): AppDatabase {
return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME)
fun createDatabase(context: Context) =
Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME)
.fallbackToDestructiveMigration()
.addMigrations(
migration_10_11,
@ -41,7 +41,6 @@ abstract class AppDatabase : RoomDatabase() {
)
.allowMainThreadQueries()
.build()
}
private val migration_10_11 = object: Migration(10, 11) {
override fun migrate(database: SupportSQLiteDatabase) {

@ -120,8 +120,7 @@ data class Book(
return name.replace(AppPattern.fileNameRegex, "") + MD5Utils.md5Encode16(bookUrl)
}
fun toSearchBook(): SearchBook {
return SearchBook(
fun toSearchBook() = SearchBook(
name = name,
author = author,
kind = kind,
@ -140,7 +139,6 @@ data class Book(
this.infoHtml = this@Book.infoHtml
this.tocHtml = this@Book.tocHtml
}
}
fun changeTo(newBook: Book) {
newBook.group = group

@ -52,16 +52,9 @@ data class BookChapter(
variable = GSON.toJson(variableMap)
}
override fun hashCode(): Int {
return url.hashCode()
}
override fun hashCode() = url.hashCode()
override fun equals(other: Any?): Boolean {
if (other is BookChapter) {
return other.url == url
}
return false
}
override fun equals(other: Any?) = if (other is BookChapter) other.url == url else false
}

@ -11,8 +11,8 @@ import io.legado.app.data.entities.rule.*
import io.legado.app.help.JsExtensions
import io.legado.app.utils.*
import kotlinx.android.parcel.Parcelize
import java.util.*
import javax.script.SimpleBindings
import kotlin.collections.HashMap
@Parcelize
@TypeConverters(BookSource.Converters::class)
@ -48,51 +48,35 @@ data class BookSource(
return bookSourceUrl.hashCode()
}
override fun equals(other: Any?): Boolean {
if (other is BookSource) {
return other.bookSourceUrl == bookSourceUrl
}
return false
}
override fun equals(other: Any?) = if (other is BookSource) other.bookSourceUrl == bookSourceUrl else false
@Throws(Exception::class)
fun getHeaderMap(): Map<String, String> {
val headerMap = HashMap<String, String>()
headerMap[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: userAgent
fun getHeaderMap() = (HashMap<String, String>().apply {
this[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: userAgent
header?.let {
val header1 = when {
GSON.fromJsonObject<Map<String, String>>(
when {
it.startsWith("@js:", true) ->
evalJS(it.substring(4)).toString()
it.startsWith("<js>", true) ->
evalJS(it.substring(4, it.lastIndexOf("<"))).toString()
else -> it
}
GSON.fromJsonObject<Map<String, String>>(header1)?.let { map ->
headerMap.putAll(map)
}
)?.let { map ->
putAll(map)
}
return headerMap
}
}) as Map<String, String>
fun getSearchRule(): SearchRule {
return ruleSearch ?: SearchRule()
}
fun getSearchRule() = ruleSearch ?: SearchRule()
fun getExploreRule(): ExploreRule {
return ruleExplore ?: ExploreRule()
}
fun getExploreRule() = ruleExplore ?: ExploreRule()
fun getBookInfoRule(): BookInfoRule {
return ruleBookInfo ?: BookInfoRule()
}
fun getBookInfoRule() = ruleBookInfo ?: BookInfoRule()
fun getTocRule(): TocRule {
return ruleToc ?: TocRule()
}
fun getTocRule() = ruleToc ?: TocRule()
fun getContentRule(): ContentRule {
return ruleContent ?: ContentRule()
}
fun getContentRule() = ruleContent ?: ContentRule()
fun addGroup(group: String) {
bookSourceGroup?.let {
@ -111,8 +95,7 @@ data class BookSource(
}
}
fun getExploreKinds(): ArrayList<ExploreKind>? {
val exploreKinds = arrayListOf<ExploreKind>()
fun getExploreKinds() = arrayListOf<ExploreKind>().apply {
exploreUrl?.let {
var a = it
if (a.isNotBlank()) {
@ -135,14 +118,13 @@ data class BookSource(
b.forEach { c ->
val d = c.split("::")
if (d.size > 1)
exploreKinds.add(ExploreKind(d[0], d[1]))
add(ExploreKind(d[0], d[1]))
}
} catch (e: Exception) {
exploreKinds.add(ExploreKind(e.localizedMessage ?: ""))
add(ExploreKind(e.localizedMessage ?: ""))
}
}
}
return exploreKinds
}
/**
@ -155,8 +137,8 @@ data class BookSource(
return AppConst.SCRIPT_ENGINE.eval(jsStr, bindings)
}
fun equal(source: BookSource): Boolean {
return equal(bookSourceName, source.bookSourceName)
fun equal(source: BookSource) =
equal(bookSourceName, source.bookSourceName)
&& equal(bookSourceUrl, source.bookSourceUrl)
&& equal(bookSourceGroup, source.bookSourceGroup)
&& bookSourceType == source.bookSourceType
@ -173,11 +155,8 @@ data class BookSource(
&& getBookInfoRule() == source.getBookInfoRule()
&& getTocRule() == source.getTocRule()
&& getContentRule() == source.getContentRule()
}
private fun equal(a: String?, b: String?): Boolean {
return a == b || (a.isNullOrEmpty() && b.isNullOrEmpty())
}
private fun equal(a: String?, b: String?) = a == b || (a.isNullOrEmpty() && b.isNullOrEmpty())
data class ExploreKind(
var title: String,
@ -186,54 +165,34 @@ data class BookSource(
class Converters {
@TypeConverter
fun exploreRuleToString(exploreRule: ExploreRule?): String? {
return GSON.toJson(exploreRule)
}
fun exploreRuleToString(exploreRule: ExploreRule?) = GSON.toJson(exploreRule)
@TypeConverter
fun stringToExploreRule(json: String?): ExploreRule? {
return GSON.fromJsonObject<ExploreRule>(json)
}
fun stringToExploreRule(json: String?) = GSON.fromJsonObject<ExploreRule>(json)
@TypeConverter
fun searchRuleToString(searchRule: SearchRule?): String? {
return GSON.toJson(searchRule)
}
fun searchRuleToString(searchRule: SearchRule?) = GSON.toJson(searchRule)
@TypeConverter
fun stringToSearchRule(json: String?): SearchRule? {
return GSON.fromJsonObject<SearchRule>(json)
}
fun stringToSearchRule(json: String?) = GSON.fromJsonObject<SearchRule>(json)
@TypeConverter
fun bookInfoRuleToString(bookInfoRule: BookInfoRule?): String? {
return GSON.toJson(bookInfoRule)
}
fun bookInfoRuleToString(bookInfoRule: BookInfoRule?) = GSON.toJson(bookInfoRule)
@TypeConverter
fun stringToBookInfoRule(json: String?): BookInfoRule? {
return GSON.fromJsonObject<BookInfoRule>(json)
}
fun stringToBookInfoRule(json: String?) = GSON.fromJsonObject<BookInfoRule>(json)
@TypeConverter
fun tocRuleToString(tocRule: TocRule?): String? {
return GSON.toJson(tocRule)
}
fun tocRuleToString(tocRule: TocRule?) = GSON.toJson(tocRule)
@TypeConverter
fun stringToTocRule(json: String?): TocRule? {
return GSON.fromJsonObject<TocRule>(json)
}
fun stringToTocRule(json: String?) = GSON.fromJsonObject<TocRule>(json)
@TypeConverter
fun contentRuleToString(contentRule: ContentRule?): String? {
return GSON.toJson(contentRule)
}
fun contentRuleToString(contentRule: ContentRule?) = GSON.toJson(contentRule)
@TypeConverter
fun stringToContentRule(json: String?): ContentRule? {
return GSON.fromJsonObject<ContentRule>(json)
}
fun stringToContentRule(json: String?) = GSON.fromJsonObject<ContentRule>(json)
}
}

@ -20,22 +20,14 @@ data class RssArticle(
var read: Boolean = false
) {
override fun hashCode(): Int {
return link.hashCode()
}
override fun hashCode() = link.hashCode()
override fun equals(other: Any?): Boolean {
if (other == null) {
return false
}
if (other is RssArticle) {
return origin == other.origin && link == other.link
}
return false
other ?: return false
return if (other is RssArticle) origin == other.origin && link == other.link else false
}
fun toStar(): RssStar {
return RssStar(
fun toStar() = RssStar(
origin = origin,
sort = sort,
title = title,
@ -47,4 +39,3 @@ data class RssArticle(
image = image
)
}
}

@ -43,45 +43,33 @@ data class RssSource(
var customOrder: Int = 0
): Parcelable, JsExtensions {
override fun equals(other: Any?): Boolean {
if (other is RssSource) {
return other.sourceUrl == sourceUrl
}
return false
}
override fun equals(other: Any?) = if (other is RssSource) other.sourceUrl == sourceUrl else false
override fun hashCode(): Int {
return sourceUrl.hashCode()
}
override fun hashCode() = sourceUrl.hashCode()
@Throws(Exception::class)
fun getHeaderMap(): Map<String, String> {
val headerMap = HashMap<String, String>()
headerMap[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: AppConst.userAgent
fun getHeaderMap() = HashMap<String, String>().apply {
this[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: AppConst.userAgent
header?.let {
val header1 = when {
GSON.fromJsonObject<Map<String, String>>(
when {
it.startsWith("@js:", true) ->
evalJS(it.substring(4)).toString()
it.startsWith("<js>", true) ->
evalJS(it.substring(4, it.lastIndexOf("<"))).toString()
else -> it
}
GSON.fromJsonObject<Map<String, String>>(header1)?.let { map ->
headerMap.putAll(map)
)?.let { map ->
putAll(map)
}
}
return headerMap
}
/**
* 执行JS
*/
@Throws(Exception::class)
private fun evalJS(jsStr: String): Any {
val bindings = SimpleBindings()
bindings["java"] = this
return AppConst.SCRIPT_ENGINE.eval(jsStr, bindings)
}
private fun evalJS(jsStr: String): Any = AppConst.SCRIPT_ENGINE.eval(jsStr, SimpleBindings().apply { this["java"] = this@RssSource })
fun equal(source: RssSource): Boolean {
return equal(sourceUrl, source.sourceUrl)
@ -103,16 +91,15 @@ data class RssSource(
return a == b || (a.isNullOrEmpty() && b.isNullOrEmpty())
}
fun sortUrls(): LinkedHashMap<String, String> {
val sortMap = linkedMapOf<String, String>()
fun sortUrls(): LinkedHashMap<String, String> =
linkedMapOf<String, String>().apply {
sortUrl?.split("(&&|\n)+".toRegex())?.forEach { c ->
val d = c.split("::")
if (d.size > 1)
sortMap[d[0]] = d[1]
this[d[0]] = d[1]
}
if (sortMap.isEmpty()) {
sortMap[""] = sourceUrl
if (isEmpty()) {
this[""] = sourceUrl
}
return sortMap
}
}

@ -18,8 +18,7 @@ data class RssStar(
var content: String? = null,
var image: String? = null
) {
fun toRssArticle(): RssArticle {
return RssArticle(
fun toRssArticle() = RssArticle(
origin = origin,
sort = sort,
title = title,
@ -30,4 +29,3 @@ data class RssStar(
image = image
)
}
}

@ -46,18 +46,9 @@ data class SearchBook(
@IgnoredOnParcel
override var tocHtml: String? = null
override fun equals(other: Any?): Boolean {
if (other is SearchBook) {
if (other.bookUrl == bookUrl) {
return true
}
}
return false
}
override fun equals(other: Any?) = other is SearchBook && other.bookUrl == bookUrl
override fun hashCode(): Int {
return bookUrl.hashCode()
}
override fun hashCode() = bookUrl.hashCode()
override fun compareTo(other: SearchBook): Int {
return other.originOrder - this.originOrder
@ -93,8 +84,7 @@ data class SearchBook(
return "无最新章节"
}
fun toBook(): Book {
return Book(
fun toBook() = Book(
name = name,
author = author,
kind = kind,
@ -114,4 +104,3 @@ data class SearchBook(
this.tocUrl = this@SearchBook.tocUrl
}
}
}
Loading…
Cancel
Save