commit
270751d3ce
@ -1,12 +1,42 @@ |
|||||||
package io.legado.app.ui.chapterlist |
package io.legado.app.ui.chapterlist |
||||||
|
|
||||||
|
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.LinearLayoutManager |
||||||
|
import io.legado.app.App |
||||||
import io.legado.app.R |
import io.legado.app.R |
||||||
import io.legado.app.base.VMBaseFragment |
import io.legado.app.base.VMBaseFragment |
||||||
|
import io.legado.app.data.entities.Bookmark |
||||||
|
import io.legado.app.lib.theme.ATH |
||||||
import io.legado.app.utils.getViewModelOfActivity |
import io.legado.app.utils.getViewModelOfActivity |
||||||
|
import kotlinx.android.synthetic.main.fragment_bookmark.* |
||||||
|
|
||||||
class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_bookmark) { |
class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_bookmark) { |
||||||
override val viewModel: ChapterListViewModel |
override val viewModel: ChapterListViewModel |
||||||
get() = getViewModelOfActivity(ChapterListViewModel::class.java) |
get() = getViewModelOfActivity(ChapterListViewModel::class.java) |
||||||
|
|
||||||
|
private lateinit var adapter: BookmarkAdapter |
||||||
|
private var bookmarkLiveData: LiveData<PagedList<Bookmark>>? = null |
||||||
|
|
||||||
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
||||||
|
super.onViewCreated(view, savedInstanceState) |
||||||
|
initRecyclerView() |
||||||
|
} |
||||||
|
|
||||||
|
private fun initRecyclerView() { |
||||||
|
ATH.applyEdgeEffectColor(recycler_view) |
||||||
|
adapter = BookmarkAdapter() |
||||||
|
recycler_view.layoutManager = LinearLayoutManager(requireContext()) |
||||||
|
recycler_view.adapter = adapter |
||||||
|
} |
||||||
|
|
||||||
|
private fun initData() { |
||||||
|
bookmarkLiveData?.removeObservers(viewLifecycleOwner) |
||||||
|
bookmarkLiveData = LivePagedListBuilder(App.db.bookmarkDao().observeByBook(""), 20).build() |
||||||
|
bookmarkLiveData?.observe(viewLifecycleOwner, Observer { adapter.submitList(it) }) |
||||||
|
} |
||||||
} |
} |
@ -1,2 +1,55 @@ |
|||||||
package io.legado.app.ui.chapterlist |
package io.legado.app.ui.chapterlist |
||||||
|
|
||||||
|
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.data.entities.BookChapter |
||||||
|
import kotlinx.android.synthetic.main.item_bookmark.view.* |
||||||
|
import org.jetbrains.anko.sdk27.listeners.onClick |
||||||
|
|
||||||
|
class ChapterListAdapter : PagedListAdapter<BookChapter, ChapterListAdapter.MyViewHolder>(DIFF_CALLBACK) { |
||||||
|
|
||||||
|
companion object { |
||||||
|
|
||||||
|
@JvmField |
||||||
|
val DIFF_CALLBACK = object : DiffUtil.ItemCallback<BookChapter>() { |
||||||
|
override fun areItemsTheSame(oldItem: BookChapter, newItem: BookChapter): Boolean = |
||||||
|
oldItem.bookUrl == newItem.bookUrl |
||||||
|
&& oldItem.url == newItem.url |
||||||
|
|
||||||
|
override fun areContentsTheSame(oldItem: BookChapter, newItem: BookChapter): Boolean = |
||||||
|
oldItem.title == newItem.title |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
var callback: Callback? = null |
||||||
|
|
||||||
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { |
||||||
|
return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_chapter_list, parent, false)) |
||||||
|
} |
||||||
|
|
||||||
|
override fun onBindViewHolder(holder: MyViewHolder, position: Int) { |
||||||
|
getItem(position)?.let { |
||||||
|
holder.bind(it, callback) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) { |
||||||
|
|
||||||
|
fun bind(bookChapter: BookChapter, callback: Callback?) = with(itemView) { |
||||||
|
tv_chapter_name.text = bookChapter.title |
||||||
|
|
||||||
|
itemView.onClick { |
||||||
|
callback?.openChapter() |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
interface Callback { |
||||||
|
fun openChapter() |
||||||
|
} |
||||||
|
} |
@ -1,13 +1,41 @@ |
|||||||
package io.legado.app.ui.chapterlist |
package io.legado.app.ui.chapterlist |
||||||
|
|
||||||
|
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.LinearLayoutManager |
||||||
|
import io.legado.app.App |
||||||
import io.legado.app.R |
import io.legado.app.R |
||||||
import io.legado.app.base.VMBaseFragment |
import io.legado.app.base.VMBaseFragment |
||||||
|
import io.legado.app.data.entities.BookChapter |
||||||
import io.legado.app.utils.getViewModelOfActivity |
import io.legado.app.utils.getViewModelOfActivity |
||||||
|
import kotlinx.android.synthetic.main.fragment_chapter_list.* |
||||||
|
|
||||||
class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_chapter_list) { |
class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_chapter_list) { |
||||||
override val viewModel: ChapterListViewModel |
override val viewModel: ChapterListViewModel |
||||||
get() = getViewModelOfActivity(ChapterListViewModel::class.java) |
get() = getViewModelOfActivity(ChapterListViewModel::class.java) |
||||||
|
|
||||||
|
lateinit var adapter: ChapterListAdapter |
||||||
|
var liveData: LiveData<PagedList<BookChapter>>? = null |
||||||
|
|
||||||
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
||||||
|
super.onViewCreated(view, savedInstanceState) |
||||||
|
initRecyclerView() |
||||||
|
initData() |
||||||
|
} |
||||||
|
|
||||||
|
private fun initRecyclerView() { |
||||||
|
adapter = ChapterListAdapter() |
||||||
|
recycler_view.layoutManager = LinearLayoutManager(requireContext()) |
||||||
|
recycler_view.adapter = adapter |
||||||
|
} |
||||||
|
|
||||||
|
private fun initData() { |
||||||
|
liveData?.removeObservers(viewLifecycleOwner) |
||||||
|
liveData = LivePagedListBuilder(App.db.bookChapterDao().observeByBook(""), 30).build() |
||||||
|
liveData?.observe(viewLifecycleOwner, Observer { adapter.submitList(it) }) |
||||||
|
} |
||||||
} |
} |
Loading…
Reference in new issue