pull/35/head
parent
866289bae2
commit
33bcc77103
@ -1,76 +0,0 @@ |
||||
package io.legado.app.ui.bookshelf |
||||
|
||||
import android.os.Bundle |
||||
import android.widget.LinearLayout |
||||
import androidx.lifecycle.LiveData |
||||
import androidx.lifecycle.Observer |
||||
import androidx.paging.LivePagedListBuilder |
||||
import androidx.paging.PagedList |
||||
import androidx.recyclerview.widget.DividerItemDecoration |
||||
import androidx.recyclerview.widget.LinearLayoutManager |
||||
import io.legado.app.App |
||||
import io.legado.app.R |
||||
import io.legado.app.base.VMBaseActivity |
||||
import io.legado.app.data.entities.Book |
||||
import io.legado.app.lib.theme.ATH |
||||
import io.legado.app.utils.getViewModel |
||||
import kotlinx.android.synthetic.main.activity_bookshelf.* |
||||
|
||||
class BookshelfActivity : VMBaseActivity<BookshelfViewModel>(R.layout.activity_bookshelf) { |
||||
override val viewModel: BookshelfViewModel |
||||
get() = getViewModel(BookshelfViewModel::class.java) |
||||
|
||||
private lateinit var bookshelfAdapter: BookshelfAdapter |
||||
private var bookshelfLiveData: LiveData<PagedList<Book>>? = null |
||||
|
||||
override fun onActivityCreated(savedInstanceState: Bundle?) { |
||||
if (viewModel.bookGroup == null) { |
||||
viewModel.bookGroup = intent.getParcelableExtra("data") |
||||
} |
||||
viewModel.bookGroup?.let { |
||||
title_bar.title = it.groupName |
||||
} |
||||
initRecyclerView() |
||||
upRecyclerData() |
||||
} |
||||
|
||||
private fun initRecyclerView() { |
||||
ATH.applyEdgeEffectColor(rv_bookshelf) |
||||
rv_bookshelf.layoutManager = LinearLayoutManager(this) |
||||
rv_bookshelf.addItemDecoration(DividerItemDecoration(this, LinearLayout.VERTICAL)) |
||||
bookshelfAdapter = BookshelfAdapter() |
||||
rv_bookshelf.adapter = bookshelfAdapter |
||||
} |
||||
|
||||
private fun upRecyclerData() { |
||||
viewModel.bookGroup?.let { |
||||
bookshelfLiveData?.removeObservers(this) |
||||
when (it.groupId) { |
||||
-1 -> { |
||||
|
||||
bookshelfLiveData = |
||||
LivePagedListBuilder(App.db.bookDao().observeAll(), 10).build() |
||||
} |
||||
-2 -> { |
||||
bookshelfLiveData = |
||||
LivePagedListBuilder(App.db.bookDao().observeLocal(), 10).build() |
||||
} |
||||
-3 -> { |
||||
bookshelfLiveData = |
||||
LivePagedListBuilder(App.db.bookDao().observeAudio(), 10).build() |
||||
} |
||||
else -> { |
||||
bookshelfLiveData = |
||||
LivePagedListBuilder( |
||||
App.db.bookDao().observeByGroup(it.groupId), |
||||
10 |
||||
).build() |
||||
} |
||||
} |
||||
bookshelfLiveData?.observe( |
||||
this, |
||||
Observer { pageList -> bookshelfAdapter.submitList(pageList) }) |
||||
} |
||||
} |
||||
|
||||
} |
@ -1,11 +0,0 @@ |
||||
package io.legado.app.ui.bookshelf |
||||
|
||||
import android.app.Application |
||||
import io.legado.app.base.BaseViewModel |
||||
import io.legado.app.data.entities.BookGroup |
||||
|
||||
class BookshelfViewModel(application: Application) : BaseViewModel(application) { |
||||
|
||||
var bookGroup: BookGroup? = null |
||||
|
||||
} |
@ -1,16 +1,56 @@ |
||||
package io.legado.app.ui.main |
||||
|
||||
import android.app.Application |
||||
import io.legado.app.App |
||||
import io.legado.app.base.BaseViewModel |
||||
import io.legado.app.constant.BookType |
||||
import io.legado.app.constant.Bus |
||||
import io.legado.app.help.storage.Restore |
||||
import io.legado.app.model.WebBook |
||||
import io.legado.app.utils.postEvent |
||||
import kotlinx.coroutines.Dispatchers.IO |
||||
import kotlinx.coroutines.delay |
||||
import kotlinx.coroutines.launch |
||||
|
||||
class MainViewModel(application: Application) : BaseViewModel(application) { |
||||
val updateList = arrayListOf<String>() |
||||
|
||||
fun restore() { |
||||
launch(IO) { |
||||
Restore.importYueDuData(getApplication()) |
||||
} |
||||
} |
||||
|
||||
fun upChapterList() { |
||||
execute { |
||||
App.db.bookDao().getRecentRead().forEach { book -> |
||||
if (book.origin != BookType.local) { |
||||
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> |
||||
synchronized(this) { |
||||
updateList.add(book.bookUrl) |
||||
postEvent(Bus.UP_BOOK, book.bookUrl) |
||||
} |
||||
WebBook(bookSource).getChapterList(book) |
||||
.onSuccess(IO) { |
||||
it?.let { |
||||
App.db.bookDao().update(book) |
||||
App.db.bookChapterDao().delByBook(book.bookUrl) |
||||
App.db.bookChapterDao().insert(*it.toTypedArray()) |
||||
} |
||||
} |
||||
.onError { |
||||
it.printStackTrace() |
||||
} |
||||
.onFinally { |
||||
synchronized(this) { |
||||
updateList.remove(book.bookUrl) |
||||
postEvent(Bus.UP_BOOK, book.bookUrl) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
delay(50) |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,102 @@ |
||||
package io.legado.app.ui.main.bookshelf |
||||
|
||||
import android.os.Bundle |
||||
import android.view.View |
||||
import androidx.lifecycle.LiveData |
||||
import androidx.lifecycle.Observer |
||||
import androidx.paging.LivePagedListBuilder |
||||
import androidx.paging.PagedList |
||||
import androidx.recyclerview.widget.DividerItemDecoration |
||||
import androidx.recyclerview.widget.LinearLayoutManager |
||||
import io.legado.app.App |
||||
import io.legado.app.R |
||||
import io.legado.app.base.VMBaseFragment |
||||
import io.legado.app.data.entities.Book |
||||
import io.legado.app.lib.theme.ATH |
||||
import io.legado.app.lib.theme.accentColor |
||||
import io.legado.app.ui.book.info.BookInfoActivity |
||||
import io.legado.app.ui.book.read.ReadBookActivity |
||||
import io.legado.app.ui.main.MainViewModel |
||||
import io.legado.app.utils.getViewModel |
||||
import io.legado.app.utils.getViewModelOfActivity |
||||
import kotlinx.android.synthetic.main.fragment_books.* |
||||
import org.jetbrains.anko.startActivity |
||||
|
||||
|
||||
class BooksFragment : VMBaseFragment<BooksViewModel>(R.layout.fragment_books), |
||||
BooksAdapter.CallBack { |
||||
override val viewModel: BooksViewModel |
||||
get() = getViewModel(BooksViewModel::class.java) |
||||
|
||||
private lateinit var activityViewModel: MainViewModel |
||||
private lateinit var booksAdapter: BooksAdapter |
||||
private var bookshelfLiveData: LiveData<PagedList<Book>>? = null |
||||
private val groupId = -1 |
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
||||
activityViewModel = getViewModelOfActivity(MainViewModel::class.java) |
||||
initRecyclerView() |
||||
upRecyclerData() |
||||
} |
||||
|
||||
private fun initRecyclerView() { |
||||
ATH.applyEdgeEffectColor(rv_bookshelf) |
||||
refresh_layout.setColorSchemeColors(accentColor) |
||||
refresh_layout.setOnRefreshListener { |
||||
refresh_layout.isRefreshing = false |
||||
activityViewModel.upChapterList() |
||||
} |
||||
rv_bookshelf.layoutManager = LinearLayoutManager(context) |
||||
rv_bookshelf.addItemDecoration( |
||||
DividerItemDecoration( |
||||
rv_bookshelf.context, |
||||
LinearLayoutManager.VERTICAL |
||||
) |
||||
) |
||||
booksAdapter = BooksAdapter(this) |
||||
rv_bookshelf.adapter = booksAdapter |
||||
|
||||
} |
||||
|
||||
private fun upRecyclerData() { |
||||
bookshelfLiveData?.removeObservers(this) |
||||
when (groupId) { |
||||
-1 -> { |
||||
|
||||
bookshelfLiveData = |
||||
LivePagedListBuilder(App.db.bookDao().observeAll(), 10).build() |
||||
} |
||||
-2 -> { |
||||
bookshelfLiveData = |
||||
LivePagedListBuilder(App.db.bookDao().observeLocal(), 10).build() |
||||
} |
||||
-3 -> { |
||||
bookshelfLiveData = |
||||
LivePagedListBuilder(App.db.bookDao().observeAudio(), 10).build() |
||||
} |
||||
else -> { |
||||
bookshelfLiveData = |
||||
LivePagedListBuilder( |
||||
App.db.bookDao().observeByGroup(groupId), |
||||
10 |
||||
).build() |
||||
} |
||||
} |
||||
bookshelfLiveData?.observe( |
||||
this, |
||||
Observer { pageList -> booksAdapter.submitList(pageList) }) |
||||
} |
||||
|
||||
override fun open(book: Book) { |
||||
context?.startActivity<ReadBookActivity>(Pair("bookUrl", book.bookUrl)) |
||||
} |
||||
|
||||
override fun openBookInfo(book: Book) { |
||||
context?.startActivity<BookInfoActivity>(Pair("bookUrl", book.bookUrl)) |
||||
} |
||||
|
||||
override fun isUpdate(bookUrl: String): Boolean { |
||||
return bookUrl in activityViewModel.updateList |
||||
} |
||||
|
||||
} |
@ -0,0 +1,9 @@ |
||||
package io.legado.app.ui.main.bookshelf |
||||
|
||||
import android.app.Application |
||||
import io.legado.app.base.BaseViewModel |
||||
|
||||
|
||||
class BooksViewModel(application: Application) : BaseViewModel(application) { |
||||
|
||||
} |
@ -1,101 +1,17 @@ |
||||
package io.legado.app.ui.main.bookshelf |
||||
|
||||
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 io.legado.app.R |
||||
import io.legado.app.constant.BookType |
||||
import io.legado.app.data.entities.Book |
||||
import io.legado.app.help.ImageLoader |
||||
import io.legado.app.lib.theme.ThemeStore |
||||
import io.legado.app.utils.getCompatColor |
||||
import io.legado.app.utils.invisible |
||||
import kotlinx.android.synthetic.main.item_bookshelf_list.view.* |
||||
import org.jetbrains.anko.sdk27.listeners.onClick |
||||
import org.jetbrains.anko.sdk27.listeners.onLongClick |
||||
import androidx.fragment.app.Fragment |
||||
import androidx.viewpager2.adapter.FragmentStateAdapter |
||||
|
||||
class BookshelfAdapter(private val callBack: CallBack) : |
||||
PagedListAdapter<Book, BookshelfAdapter.MyViewHolder>(DIFF_CALLBACK) { |
||||
|
||||
companion object { |
||||
@JvmField |
||||
val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Book>() { |
||||
override fun areItemsTheSame(oldItem: Book, newItem: Book): Boolean = |
||||
oldItem.bookUrl == newItem.bookUrl |
||||
class BookshelfAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) { |
||||
|
||||
override fun areContentsTheSame(oldItem: Book, newItem: Book): Boolean = |
||||
oldItem.durChapterTitle == newItem.durChapterTitle |
||||
&& oldItem.latestChapterTitle == newItem.latestChapterTitle |
||||
&& oldItem.durChapterTime == newItem.durChapterTime |
||||
&& oldItem.lastCheckTime == newItem.lastCheckTime |
||||
} |
||||
} |
||||
|
||||
fun notification(bookUrl: String) { |
||||
for (i in 0..itemCount) { |
||||
getItem(i)?.let { |
||||
if (it.bookUrl == bookUrl) { |
||||
notifyItemChanged(i) |
||||
return |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { |
||||
return MyViewHolder( |
||||
LayoutInflater.from(parent.context) |
||||
.inflate(R.layout.item_bookshelf_list, parent, false) |
||||
) |
||||
override fun getItemCount(): Int { |
||||
return 3 |
||||
} |
||||
|
||||
override fun onBindViewHolder(holder: MyViewHolder, position: Int) { |
||||
currentList?.get(position)?.let { |
||||
holder.bind(it, callBack) |
||||
override fun createFragment(position: Int): Fragment { |
||||
return BooksFragment() |
||||
} |
||||
} |
||||
|
||||
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) { |
||||
|
||||
init { |
||||
itemView.setBackgroundColor(ThemeStore.backgroundColor(itemView.context)) |
||||
} |
||||
|
||||
fun bind(book: Book, callBack: CallBack) = with(itemView) { |
||||
this.setBackgroundColor(context.getCompatColor(R.color.background)) |
||||
tv_name.text = book.name |
||||
tv_author.text = book.author |
||||
tv_read.text = book.durChapterTitle |
||||
tv_last.text = book.latestChapterTitle |
||||
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.onClick { callBack.open(book) } |
||||
itemView.onLongClick { |
||||
callBack.openBookInfo(book) |
||||
true |
||||
} |
||||
if (book.origin != BookType.local && callBack.isUpdate(book.bookUrl)) { |
||||
bv_unread.invisible() |
||||
rl_loading.show() |
||||
} else { |
||||
rl_loading.hide() |
||||
bv_unread.setBadgeCount(book.getUnreadChapterNum()) |
||||
bv_unread.setHighlight(book.lastCheckCount > 0) |
||||
} |
||||
} |
||||
} |
||||
|
||||
interface CallBack { |
||||
fun open(book: Book) |
||||
fun openBookInfo(book: Book) |
||||
fun isUpdate(bookUrl: String): Boolean |
||||
} |
||||
} |
@ -1,17 +1,12 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:orientation="vertical" |
||||
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:id="@+id/refresh_layout" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent"> |
||||
|
||||
<io.legado.app.ui.widget.TitleBar |
||||
android:id="@+id/title_bar" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" /> |
||||
|
||||
<androidx.recyclerview.widget.RecyclerView |
||||
android:id="@+id/rv_bookshelf" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" /> |
||||
|
||||
</LinearLayout> |
||||
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout> |
Loading…
Reference in new issue