From 22be80f7f67d8839061c784d4120a9b0d7d4c4cc Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 9 Aug 2019 09:36:02 +0800 Subject: [PATCH 1/4] up --- .../app/ui/chapterlist/BookmarkAdapter.kt | 18 +++++++++-- .../app/ui/chapterlist/BookmarkFragment.kt | 30 +++++++++++++++++++ .../ui/chapterlist/ChapterListViewModel.kt | 2 ++ .../ui/main/bookshelf/BookshelfFragment.kt | 14 +++------ 4 files changed, 51 insertions(+), 13 deletions(-) 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..5133474bf 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(rv_list) + adapter = BookmarkAdapter() + rv_list.layoutManager = LinearLayoutManager(requireContext()) + rv_list.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/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) From d11e58315b90696ac0f14bb2c1c8ee0e45808012 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 9 Aug 2019 11:30:49 +0800 Subject: [PATCH 2/4] up --- .../app/ui/chapterlist/ChapterListAdapter.kt | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) 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 From 83cfc65455ae2c396f37e6e4372491cdda68414b Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 9 Aug 2019 12:16:12 +0800 Subject: [PATCH 3/4] up --- .../app/ui/chapterlist/BookmarkFragment.kt | 6 ++-- .../app/ui/chapterlist/ChapterListFragment.kt | 28 +++++++++++++++++++ app/src/main/res/layout/fragment_bookmark.xml | 2 +- .../main/res/layout/fragment_chapter_list.xml | 2 +- 4 files changed, 33 insertions(+), 5 deletions(-) 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 5133474bf..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 @@ -28,10 +28,10 @@ class BookmarkFragment : VMBaseFragment(R.layout.fragment_ } private fun initRecyclerView() { - ATH.applyEdgeEffectColor(rv_list) + ATH.applyEdgeEffectColor(recycler_view) adapter = BookmarkAdapter() - rv_list.layoutManager = LinearLayoutManager(requireContext()) - rv_list.adapter = adapter + recycler_view.layoutManager = LinearLayoutManager(requireContext()) + recycler_view.adapter = adapter } private fun initData() { 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/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"> Date: Fri, 9 Aug 2019 12:30:46 +0800 Subject: [PATCH 4/4] up --- app/src/main/res/layout/activity_read.xml | 33 +++++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) 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