pull/32/head
gedoor 6 years ago
parent 9d601074d5
commit 8fb9dbde38
  1. 17
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt
  2. 9
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt
  3. 34
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt
  4. 2
      app/src/main/res/layout/item_bookshelf_list.xml

@ -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<MainViewModel>(), BottomNavigationView.OnNavigationItemSelectedListener,
ViewPager.OnPageChangeListener {
@ -45,6 +48,7 @@ class MainActivity : BaseActivity<MainViewModel>(), 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<MainViewModel>(), BottomNavigationView.OnNavig
}
private fun importYueDu() {
PermissionsCompat.Builder(this)
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 { Restore.importYueDuData(this) }.request()
.onGranted { viewModel.restore() }
.request()
}
}
}
}
override fun onPageScrollStateChanged(state: Int) {

@ -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())
}
}
}

@ -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<Book, RecyclerView.ViewHolder>(DIFF_CALLBACK) {
class BookshelfAdapter : PagedListAdapter<Book, BookshelfAdapter.MyViewHolder>(DIFF_CALLBACK) {
companion object {
@JvmField
@ -33,43 +33,15 @@ class BookshelfAdapter : PagedListAdapter<Book, RecyclerView.ViewHolder>(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 -> {
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
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() }
}
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
init {

@ -40,7 +40,7 @@
android:includeFontPadding="false"
tools:ignore="RtlHardcoded" />
<com.victor.loading.rotate.RotateLoading
<io.legado.app.ui.widget.anima.RotateLoading
android:id="@+id/rl_loading"
android:layout_width="26dp"
android:layout_height="26dp"

Loading…
Cancel
Save