From 0ff5f960d566891b51429637287aef25ac3b1a6e Mon Sep 17 00:00:00 2001 From: Invinciblelee <1760316362@qq.com> Date: Tue, 18 Jun 2019 14:15:19 +0800 Subject: [PATCH] update --- .../java/io/legado/app/base/BaseActivity.kt | 8 ++++- .../app/ui/bookshelf/BookshelfAdapter.kt | 22 ++++-------- .../io/legado/app/ui/main/MainActivity.kt | 7 ++-- .../io/legado/app/ui/main/MainViewModel.kt | 8 ++--- .../app/ui/main/bookshelf/BookshelfAdapter.kt | 35 +++++++------------ .../legado/app/ui/search/SearchViewModel.kt | 2 +- .../main/res/layout/item_bookshelf_list.xml | 2 +- 7 files changed, 35 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/io/legado/app/base/BaseActivity.kt b/app/src/main/java/io/legado/app/base/BaseActivity.kt index 8d86f5ccc..3f45f019b 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -15,9 +15,15 @@ import io.legado.app.utils.disableAutoFill import io.legado.app.utils.getCompatColor import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.setIconColor +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlin.coroutines.CoroutineContext -abstract class BaseActivity : AppCompatActivity() { +abstract class BaseActivity : AppCompatActivity(), CoroutineScope { + + override val coroutineContext: CoroutineContext + get() = Dispatchers.IO protected abstract val viewModel: VM diff --git a/app/src/main/java/io/legado/app/ui/bookshelf/BookshelfAdapter.kt b/app/src/main/java/io/legado/app/ui/bookshelf/BookshelfAdapter.kt index b7fdf7b8d..20e02a425 100644 --- a/app/src/main/java/io/legado/app/ui/bookshelf/BookshelfAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/bookshelf/BookshelfAdapter.kt @@ -11,6 +11,7 @@ import com.bumptech.glide.Glide import com.bumptech.glide.load.engine.DiskCacheStrategy import io.legado.app.R import io.legado.app.data.entities.Book +import io.legado.app.help.ImageLoader import io.legado.app.lib.theme.ThemeStore import kotlinx.android.synthetic.main.item_bookshelf_list.view.* import kotlinx.android.synthetic.main.item_relace_rule.view.tv_name @@ -60,21 +61,12 @@ class BookshelfAdapter : PagedListAdapter(D tv_author.text = book.author tv_read.text = book.durChapterTitle tv_last.text = book.latestChapterTitle - val cover = if (isEmpty(book.customCoverUrl)) book.coverUrl else book.customCoverUrl - cover?.let { - if (it.startsWith("http")) { - Glide.with(itemView).load(it) - .placeholder(R.drawable.img_cover_default) - .centerCrop() - .diskCacheStrategy(DiskCacheStrategy.RESOURCE) - .into(iv_cover) - } else { - Glide.with(itemView).load(File(it)) - .placeholder(R.drawable.img_cover_default) - .centerCrop() - .diskCacheStrategy(DiskCacheStrategy.RESOURCE) - .into(iv_cover) - } + book.getDisplayCover()?.let { + ImageLoader.load(context, it)//Glide自动识别http://和file:/// + .placeholder(R.drawable.img_cover_default) + .error(R.drawable.img_cover_default) + .centerCrop() + .setAsDrawable(iv_cover) } itemView.setOnClickListener { callBack?.open(book) } } diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index 55020c1db..ac197d9db 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -24,8 +24,7 @@ import io.legado.app.ui.main.myconfig.MyConfigFragment import io.legado.app.utils.getCompatColor import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.activity_main.* -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.launch class MainActivity : BaseActivity(), BottomNavigationView.OnNavigationItemSelectedListener, @@ -62,9 +61,9 @@ class MainActivity : BaseActivity(), BottomNavigationView.OnNavig } private fun importYueDu() { - GlobalScope.launch { + launch { if (App.db.bookDao().allBookCount == 0) { - GlobalScope.launch(Dispatchers.Main) { + launch(Main) { PermissionsCompat.Builder(this@MainActivity) .addPermissions(*Permissions.Group.STORAGE) .rationale(R.string.tip_perm_request_storage) diff --git a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt index 4285ae62c..95890e830 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt @@ -1,15 +1,15 @@ package io.legado.app.ui.main import android.app.Application -import androidx.lifecycle.AndroidViewModel +import io.legado.app.base.BaseViewModel import io.legado.app.help.storage.Restore -import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch -class MainViewModel(application: Application) : AndroidViewModel(application) { +class MainViewModel(application: Application) : BaseViewModel(application) { fun restore() { - GlobalScope.launch { + launch(IO) { Restore.importYueDuData(getApplication()) } } diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt index 87bcaf63b..c0b9e2293 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt @@ -1,20 +1,17 @@ package io.legado.app.ui.main.bookshelf -import android.text.TextUtils.isEmpty import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.paging.PagedListAdapter import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.RecyclerView -import com.bumptech.glide.Glide -import com.bumptech.glide.load.engine.DiskCacheStrategy import io.legado.app.R import io.legado.app.data.entities.Book +import io.legado.app.help.ImageLoader import io.legado.app.lib.theme.ThemeStore import kotlinx.android.synthetic.main.item_bookshelf_list.view.* import kotlinx.android.synthetic.main.item_relace_rule.view.tv_name -import java.io.File class BookshelfAdapter : PagedListAdapter(DIFF_CALLBACK) { @@ -22,12 +19,12 @@ class BookshelfAdapter : PagedListAdapter(D @JvmField val DIFF_CALLBACK = object : DiffUtil.ItemCallback() { override fun areItemsTheSame(oldItem: Book, newItem: Book): Boolean = - oldItem.descUrl == newItem.descUrl + oldItem.descUrl == newItem.descUrl override fun areContentsTheSame(oldItem: Book, newItem: Book): Boolean = - oldItem.descUrl == newItem.descUrl - && oldItem.durChapterTitle == newItem.durChapterTitle - && oldItem.latestChapterTitle == newItem.latestChapterTitle + oldItem.descUrl == newItem.descUrl + && oldItem.durChapterTitle == newItem.durChapterTitle + && oldItem.latestChapterTitle == newItem.latestChapterTitle } } @@ -42,6 +39,7 @@ class BookshelfAdapter : PagedListAdapter(D holder.bind(it, callBack) } } + class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) { init { @@ -53,21 +51,12 @@ class BookshelfAdapter : PagedListAdapter(D tv_author.text = book.author tv_read.text = book.durChapterTitle tv_last.text = book.latestChapterTitle - val cover = if (isEmpty(book.customCoverUrl)) book.coverUrl else book.customCoverUrl - cover?.let { - if (it.startsWith("http")) { - Glide.with(itemView).load(it) - .placeholder(R.drawable.img_cover_default) - .centerCrop() - .diskCacheStrategy(DiskCacheStrategy.RESOURCE) - .into(iv_cover) - } else { - Glide.with(itemView).load(File(it)) - .placeholder(R.drawable.img_cover_default) - .centerCrop() - .diskCacheStrategy(DiskCacheStrategy.RESOURCE) - .into(iv_cover) - } + book.getDisplayCover()?.let { + ImageLoader.load(context, it)//Glide自动识别http://和file:// + .placeholder(R.drawable.img_cover_default) + .error(R.drawable.img_cover_default) + .centerCrop() + .setAsDrawable(iv_cover) } itemView.setOnClickListener { callBack?.open(book) } } diff --git a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt index 594672d74..a1ea4039d 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt @@ -22,7 +22,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { val searchResponse = withContext(IO) { HttpHelper.getApiService( "http:www.baidu.com" - ).get("", mutableMapOf()) + ).post("", mutableMapOf(Pair("key", "value"))) } val result = searchResponse.await() diff --git a/app/src/main/res/layout/item_bookshelf_list.xml b/app/src/main/res/layout/item_bookshelf_list.xml index aaed990db..671a0ed23 100644 --- a/app/src/main/res/layout/item_bookshelf_list.xml +++ b/app/src/main/res/layout/item_bookshelf_list.xml @@ -40,7 +40,7 @@ android:includeFontPadding="false" tools:ignore="RtlHardcoded" /> -