From a6585cc60c0003363dd98e6d45d20758d2faf62e Mon Sep 17 00:00:00 2001 From: GKF Date: Wed, 19 Jun 2019 11:55:21 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/ui/main/MainActivity.kt | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) 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 ac197d9db..13b1f29b0 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,9 @@ 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.Main +import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext class MainActivity : BaseActivity(), BottomNavigationView.OnNavigationItemSelectedListener, ViewPager.OnPageChangeListener { @@ -62,14 +63,12 @@ class MainActivity : BaseActivity(), BottomNavigationView.OnNavig private fun importYueDu() { launch { - if (App.db.bookDao().allBookCount == 0) { - launch(Main) { - PermissionsCompat.Builder(this@MainActivity) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { viewModel.restore() } - .request() - } + if (withContext(IO) { App.db.bookDao().allBookCount == 0 }) { + PermissionsCompat.Builder(this@MainActivity) + .addPermissions(*Permissions.Group.STORAGE) + .rationale(R.string.tip_perm_request_storage) + .onGranted { viewModel.restore() } + .request() } } } From 4b60fa7f10f4b3788911f4123e662f55dffec044 Mon Sep 17 00:00:00 2001 From: GKF Date: Wed, 19 Jun 2019 12:28:27 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/ui/main/bookshelf/BookshelfFragment.kt | 4 ++-- .../app/ui/main/booksource/BookSourceFragment.kt | 8 ++++---- .../io/legado/app/ui/sourceedit/SourceEditActivity.kt | 10 +++++----- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt index da5fe6bf0..e9b9c8653 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt @@ -21,7 +21,7 @@ import io.legado.app.ui.bookshelf.BookshelfActivity import io.legado.app.utils.disableAutoFill import kotlinx.android.synthetic.main.fragment_bookshelf.* import kotlinx.android.synthetic.main.view_title_bar.* -import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch import org.jetbrains.anko.startActivity import org.jetbrains.anko.textColor @@ -88,7 +88,7 @@ class BookshelfFragment : BaseFragment(R.layout.fragment_bookshelf), BookGroupAd title(text = "新建分组") input(hint = "分组名称") { _, charSequence -> run { - GlobalScope.launch { + launch(IO) { App.db.bookGroupDao().insert( BookGroup( App.db.bookGroupDao().maxId + 1, diff --git a/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt b/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt index 90b75c4d4..b07dc48d9 100644 --- a/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt @@ -21,7 +21,7 @@ import io.legado.app.ui.qrcode.QrCodeActivity import io.legado.app.ui.sourceedit.SourceEditActivity import kotlinx.android.synthetic.main.fragment_book_source.* import kotlinx.android.synthetic.main.view_title_bar.* -import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch import org.jetbrains.anko.startActivity @@ -99,15 +99,15 @@ class BookSourceFragment : BaseFragment(R.layout.fragment_book_source), BookSour } override fun del(bookSource: BookSource) { - GlobalScope.launch { App.db.bookSourceDao().delete(bookSource) } + launch(IO) { App.db.bookSourceDao().delete(bookSource) } } override fun update(bookSource: BookSource) { - GlobalScope.launch { App.db.bookSourceDao().update(bookSource) } + launch(IO) { App.db.bookSourceDao().update(bookSource) } } override fun update(vararg bookSource: BookSource) { - GlobalScope.launch { App.db.bookSourceDao().update(*bookSource) } + launch(IO) { App.db.bookSourceDao().update(*bookSource) } } override fun edit(bookSource: BookSource) { diff --git a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt index 68a0d71df..bc7aabeec 100644 --- a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt @@ -10,9 +10,9 @@ import io.legado.app.base.BaseActivity import io.legado.app.data.entities.BookSource import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.activity_source_edit.* -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import org.jetbrains.anko.toast class SourceEditActivity : BaseActivity() { @@ -53,9 +53,9 @@ class SourceEditActivity : BaseActivity() { if (bookSource == null) { toast("书源名称和URL不能为空") } else { - GlobalScope.launch { - viewModel.save(bookSource) - GlobalScope.launch(Dispatchers.Main) { finish() } + launch { + withContext(IO) { viewModel.save(bookSource) } + finish() } } }