From 3efd9363574750d960fb78b902e1514b064e0d6a Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 25 Sep 2019 20:08:32 +0800 Subject: [PATCH] up --- .../io/legado/app/data/entities/BookSource.kt | 4 +-- .../app/ui/main/explore/ExploreAdapter.kt | 2 +- .../main/java/io/legado/app/utils/ACache.kt | 29 ++++++++++--------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/BookSource.kt b/app/src/main/java/io/legado/app/data/entities/BookSource.kt index a8a88e4cb..d2a61e330 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookSource.kt @@ -126,7 +126,7 @@ data class BookSource( try { if (it.startsWith("", false)) { val aCache = ACache.get(App.INSTANCE, "explore") - a = aCache.getAsString(bookSourceUrl) ?: "" + a = aCache?.getAsString(bookSourceUrl) ?: "" if (a.isBlank()) { val bindings = SimpleBindings() bindings["baseUrl"] = bookSourceUrl @@ -135,7 +135,7 @@ data class BookSource( it.substring(4, it.lastIndexOf("<")), bindings ).toString() - aCache.put(bookSourceUrl, a) + aCache?.put(bookSourceUrl, a) } } val b = a.split("(&&|\n)+".toRegex()) diff --git a/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt b/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt index 0bc6c0891..136d7d4bf 100644 --- a/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt @@ -48,7 +48,7 @@ class ExploreAdapter(context: Context, private val scope: CoroutineScope, val ca R.id.menu_edit -> callBack.editSource(item.bookSourceUrl) R.id.menu_top -> callBack.toTop(item) R.id.menu_refresh -> { - ACache.get(context, "explore").remove(item.bookSourceUrl) + ACache.get(context, "explore")?.remove(item.bookSourceUrl) notifyItemChanged(holder.layoutPosition) } } diff --git a/app/src/main/java/io/legado/app/utils/ACache.kt b/app/src/main/java/io/legado/app/utils/ACache.kt index 049b623da..c9b2fda3d 100644 --- a/app/src/main/java/io/legado/app/utils/ACache.kt +++ b/app/src/main/java/io/legado/app/utils/ACache.kt @@ -336,7 +336,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) * @param value 保存的bitmap数据 */ fun put(key: String, value: Bitmap) { - put(key, Utils.Bitmap2Bytes(value)) + put(key, Utils.bitmap2Bytes(value)) } /** @@ -347,7 +347,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) * @param saveTime 保存的时间,单位:秒 */ fun put(key: String, value: Bitmap, saveTime: Int) { - put(key, Utils.Bitmap2Bytes(value), saveTime) + put(key, Utils.bitmap2Bytes(value), saveTime) } /** @@ -358,7 +358,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) fun getAsBitmap(key: String): Bitmap? { return if (getAsBinary(key) == null) { null - } else Utils.Bytes2Bimap(getAsBinary(key)!!) + } else Utils.bytes2Bitmap(getAsBinary(key)!!) } // ======================================= @@ -395,7 +395,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) return if (getAsBinary(key) == null) { null } else Utils.bitmap2Drawable( - Utils.Bytes2Bimap( + Utils.bytes2Bitmap( getAsBinary(key)!! ) ) @@ -486,16 +486,17 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) fun newByteArrayWithDateInfo(second: Int, data2: ByteArray): ByteArray { val data1 = createDateInfo(second).toByteArray() - val retdata = ByteArray(data1.size + data2.size) - System.arraycopy(data1, 0, retdata, 0, data1.size) - System.arraycopy(data2, 0, retdata, data1.size, data2.size) - return retdata + val retData = ByteArray(data1.size + data2.size) + System.arraycopy(data1, 0, retData, 0, data1.size) + System.arraycopy(data2, 0, retData, data1.size, data2.size) + return retData } fun clearDateInfo(strInfo: String?): String? { - var strInfo = strInfo - if (strInfo != null && hasDateInfo(strInfo.toByteArray())) { - strInfo = strInfo.substring(strInfo.indexOf(mSeparator) + 1) + strInfo?.let { + if (hasDateInfo(strInfo.toByteArray())) { + return strInfo.substring(strInfo.indexOf(mSeparator) + 1) + } } return strInfo } @@ -559,7 +560,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) /* * Bitmap → byte[] */ - fun Bitmap2Bytes(bm: Bitmap): ByteArray { + fun bitmap2Bytes(bm: Bitmap): ByteArray { val baos = ByteArrayOutputStream() bm.compress(Bitmap.CompressFormat.PNG, 100, baos) return baos.toByteArray() @@ -568,8 +569,8 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) /* * byte[] → Bitmap */ - fun Bytes2Bimap(b: ByteArray): Bitmap? { - return if (b.size == 0) { + fun bytes2Bitmap(b: ByteArray): Bitmap? { + return if (b.isEmpty()) { null } else BitmapFactory.decodeByteArray(b, 0, b.size) }