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

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

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

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

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

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

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

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