diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/BookmarkAdapter.kt b/app/src/main/java/io/legado/app/ui/chapterlist/BookmarkAdapter.kt index ef07eb323..6eff6e223 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/BookmarkAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/BookmarkAdapter.kt @@ -8,6 +8,8 @@ import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.RecyclerView import io.legado.app.R import io.legado.app.data.entities.Bookmark +import kotlinx.android.synthetic.main.item_bookmark.view.* +import org.jetbrains.anko.sdk27.listeners.onClick class BookmarkAdapter : PagedListAdapter(DIFF_CALLBACK) { @@ -27,20 +29,30 @@ class BookmarkAdapter : PagedListAdapter } } + var callback: Callback? = null + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_bookmark, parent, false)) } override fun onBindViewHolder(holder: MyViewHolder, position: Int) { getItem(position)?.let { - holder.bind(it) + holder.bind(it, callback) } } class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) { - fun bind(bookmark: Bookmark) { - + fun bind(bookmark: Bookmark, callback: Callback?) = with(itemView) { + tv_chapter_name.text = bookmark.chapterName + tv_content.text = bookmark.content + itemView.onClick { + callback?.open(bookmark) + } } } + + interface Callback { + fun open(bookmark: Bookmark) + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/BookmarkFragment.kt b/app/src/main/java/io/legado/app/ui/chapterlist/BookmarkFragment.kt index b62fa6f49..ecb17d9cb 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/BookmarkFragment.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/BookmarkFragment.kt @@ -1,12 +1,42 @@ 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.base.VMBaseFragment +import io.legado.app.data.entities.Bookmark +import io.legado.app.lib.theme.ATH import io.legado.app.utils.getViewModelOfActivity +import kotlinx.android.synthetic.main.fragment_bookmark.* class BookmarkFragment : VMBaseFragment(R.layout.fragment_bookmark) { override val viewModel: ChapterListViewModel get() = getViewModelOfActivity(ChapterListViewModel::class.java) + private lateinit var adapter: BookmarkAdapter + private var bookmarkLiveData: LiveData>? = 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) }) + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListAdapter.kt b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListAdapter.kt index 8fe0ecbe6..5dc8d27f2 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListAdapter.kt @@ -1,2 +1,55 @@ 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(DIFF_CALLBACK) { + + companion object { + + @JvmField + val DIFF_CALLBACK = object : DiffUtil.ItemCallback() { + 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() + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt index f88f0dbb6..ec6418a73 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt @@ -1,13 +1,41 @@ 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.base.VMBaseFragment +import io.legado.app.data.entities.BookChapter import io.legado.app.utils.getViewModelOfActivity +import kotlinx.android.synthetic.main.fragment_chapter_list.* class ChapterListFragment : VMBaseFragment(R.layout.fragment_chapter_list) { override val viewModel: ChapterListViewModel get() = getViewModelOfActivity(ChapterListViewModel::class.java) + lateinit var adapter: ChapterListAdapter + var liveData: LiveData>? = 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) }) + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt index 3b41d0a73..a76b6386d 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt @@ -5,4 +5,6 @@ import io.legado.app.base.BaseViewModel class ChapterListViewModel : BaseViewModel(App.INSTANCE) { + var bookUrl: String? = null + } \ No newline at end of file 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 06ebcdc77..5efb1801a 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 @@ -26,7 +26,10 @@ import io.legado.app.lib.theme.accentColor import io.legado.app.ui.bookshelf.BookshelfActivity import io.legado.app.ui.read.ReadActivity import io.legado.app.ui.search.SearchActivity -import io.legado.app.utils.* +import io.legado.app.utils.applyTint +import io.legado.app.utils.getViewModel +import io.legado.app.utils.requestInputMethod +import io.legado.app.utils.startActivity import kotlinx.android.synthetic.main.dialog_edittext.view.* import kotlinx.android.synthetic.main.fragment_bookshelf.* import kotlinx.android.synthetic.main.view_title_bar.* @@ -48,7 +51,6 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b override fun onViewCreated(view: View, savedInstanceState: Bundle?) { setSupportToolbar(toolbar) -// initSearchView() initRecyclerView() initBookGroupData() initBookshelfData() @@ -65,14 +67,6 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b } } - private fun initSearchView() { -// search_view.onActionViewExpanded() -// search_view.isSubmitButtonEnabled = true -// search_view.queryHint = getString(R.string.search_book_key) -// search_view.clearFocus() -// search_view.setOnQueryTextListener(this) - } - private fun initRecyclerView() { ATH.applyEdgeEffectColor(rv_bookshelf) refresh_layout.setColorSchemeColors(accentColor) diff --git a/app/src/main/res/layout/activity_read.xml b/app/src/main/res/layout/activity_read.xml index 1a16d9005..ec5acad1b 100644 --- a/app/src/main/res/layout/activity_read.xml +++ b/app/src/main/res/layout/activity_read.xml @@ -1,11 +1,34 @@ - + - \ No newline at end of file + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_bookmark.xml b/app/src/main/res/layout/fragment_bookmark.xml index e9b393f1e..fab36a60b 100644 --- a/app/src/main/res/layout/fragment_bookmark.xml +++ b/app/src/main/res/layout/fragment_bookmark.xml @@ -5,7 +5,7 @@ android:descendantFocusability="blocksDescendants"> diff --git a/app/src/main/res/layout/fragment_chapter_list.xml b/app/src/main/res/layout/fragment_chapter_list.xml index 5fa516994..40633c3d8 100644 --- a/app/src/main/res/layout/fragment_chapter_list.xml +++ b/app/src/main/res/layout/fragment_chapter_list.xml @@ -6,7 +6,7 @@ android:descendantFocusability="blocksDescendants">