From 8fb9dbde388375445ec1a2e2f1d36f31e89a4539 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 13 Jun 2019 20:13:23 +0800 Subject: [PATCH] up --- .../io/legado/app/ui/main/MainActivity.kt | 21 +++++++--- .../io/legado/app/ui/main/MainViewModel.kt | 9 ++++- .../app/ui/main/bookshelf/BookshelfAdapter.kt | 38 +++---------------- .../main/res/layout/item_bookshelf_list.xml | 2 +- 4 files changed, 30 insertions(+), 40 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 c2a1b2f77..55020c1db 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 @@ -9,12 +9,12 @@ import androidx.lifecycle.Observer import androidx.viewpager.widget.ViewPager import com.google.android.material.bottomnavigation.BottomNavigationView import com.jeremyliao.liveeventbus.LiveEventBus +import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseActivity import io.legado.app.constant.Bus import io.legado.app.help.permission.Permissions import io.legado.app.help.permission.PermissionsCompat -import io.legado.app.help.storage.Restore import io.legado.app.lib.theme.Selector import io.legado.app.lib.theme.ThemeStore import io.legado.app.ui.main.bookshelf.BookshelfFragment @@ -24,6 +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 +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch class MainActivity : BaseActivity(), BottomNavigationView.OnNavigationItemSelectedListener, ViewPager.OnPageChangeListener { @@ -45,6 +48,7 @@ class MainActivity : BaseActivity(), BottomNavigationView.OnNavig view_pager_main.adapter = TabFragmentPageAdapter(supportFragmentManager) view_pager_main.addOnPageChangeListener(this) bottom_navigation_view.setOnNavigationItemSelectedListener(this) + importYueDu() } override fun onNavigationItemSelected(item: MenuItem): Boolean { @@ -58,10 +62,17 @@ class MainActivity : BaseActivity(), BottomNavigationView.OnNavig } private fun importYueDu() { - PermissionsCompat.Builder(this) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { Restore.importYueDuData(this) }.request() + GlobalScope.launch { + if (App.db.bookDao().allBookCount == 0) { + GlobalScope.launch(Dispatchers.Main) { + PermissionsCompat.Builder(this@MainActivity) + .addPermissions(*Permissions.Group.STORAGE) + .rationale(R.string.tip_perm_request_storage) + .onGranted { viewModel.restore() } + .request() + } + } + } } override fun onPageScrollStateChanged(state: Int) { 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 82a9146c5..4285ae62c 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 @@ -2,8 +2,15 @@ package io.legado.app.ui.main import android.app.Application import androidx.lifecycle.AndroidViewModel +import io.legado.app.help.storage.Restore +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch class MainViewModel(application: Application) : AndroidViewModel(application) { - + fun restore() { + GlobalScope.launch { + Restore.importYueDuData(getApplication()) + } + } } \ No newline at end of file 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 eae2f0aec..87bcaf63b 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 @@ -16,7 +16,7 @@ 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) { +class BookshelfAdapter : PagedListAdapter(DIFF_CALLBACK) { companion object { @JvmField @@ -33,43 +33,15 @@ class BookshelfAdapter : PagedListAdapter(DIFF_CA var callBack: CallBack? = null - override fun getItemViewType(position: Int): Int { - if (position == itemCount - 1) { - return 1 - } - return super.getItemViewType(position) - } - - override fun getItemCount(): Int { - return super.getItemCount() + 1 - } - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { - if (viewType == 1) { - return AddViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_bookshelf_list_add, parent, false)) - } + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_bookshelf_list, parent, false)) } - override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { - when (holder) { - is MyViewHolder -> { - currentList?.get(position)?.let { - holder.bind(it, callBack) - } - } - is AddViewHolder -> holder.bind(callBack) - } - - } - - class AddViewHolder(view: View) : RecyclerView.ViewHolder(view) { - - fun bind(callBack: CallBack?) = with(itemView) { - setOnClickListener { callBack?.search() } + override fun onBindViewHolder(holder: MyViewHolder, position: Int) { + currentList?.get(position)?.let { + holder.bind(it, callBack) } } - class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) { init { diff --git a/app/src/main/res/layout/item_bookshelf_list.xml b/app/src/main/res/layout/item_bookshelf_list.xml index 03845fa18..aaed990db 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" /> -