From 8f8fc54d77d78251f642ba86e987e1256abec8d1 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 16 Feb 2020 12:31:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/base/adapter/CommonRecyclerAdapter.kt | 4 ++ .../app/base/adapter/ItemViewDelegate.kt | 9 ++- .../app/base/adapter/SimpleRecyclerAdapter.kt | 8 +++ .../app/help/coroutine/CompositeCoroutine.kt | 2 +- .../app/ui/book/arrange/ArrangeBookAdapter.kt | 13 ++-- .../app/ui/book/group/GroupManageDialog.kt | 10 ++- .../app/ui/book/group/GroupSelectDialog.kt | 19 ++++-- .../app/ui/book/info/ChapterListAdapter.kt | 20 +++--- .../ui/book/read/config/BgTextConfigDialog.kt | 14 +++-- .../legado/app/ui/book/search/BookAdapter.kt | 11 +++- .../app/ui/book/search/HistoryKeyAdapter.kt | 15 ++++- .../app/ui/book/search/SearchAdapter.kt | 14 ++++- .../source/debug/BookSourceDebugAdapter.kt | 4 ++ .../book/source/manage/BookSourceAdapter.kt | 61 ++++++++++--------- .../book/source/manage/GroupManageDialog.kt | 13 +++- .../legado/app/ui/changecover/CoverAdapter.kt | 9 ++- .../ui/changesource/ChangeSourceAdapter.kt | 9 ++- .../app/ui/chapterlist/ChapterListAdapter.kt | 11 +++- .../legado/app/ui/download/DownloadAdapter.kt | 8 ++- .../app/ui/explore/ExploreShowAdapter.kt | 14 +++-- .../app/ui/filechooser/adapter/FileAdapter.kt | 9 ++- .../app/ui/filechooser/adapter/PathAdapter.kt | 11 ++-- .../app/ui/importbook/ImportBookAdapter.kt | 60 +++++++++++------- .../main/bookshelf/books/BooksAdapterGrid.kt | 21 +++++-- .../main/bookshelf/books/BooksAdapterList.kt | 21 +++++-- .../app/ui/main/explore/ExploreAdapter.kt | 29 +++++---- .../io/legado/app/ui/main/rss/RssAdapter.kt | 9 ++- .../app/ui/replacerule/GroupManageDialog.kt | 14 ++++- .../app/ui/replacerule/ReplaceRuleAdapter.kt | 47 ++++++++------ .../app/ui/rss/article/RssArticlesAdapter.kt | 11 +++- .../ui/rss/favorites/RssFavoritesAdapter.kt | 11 +++- .../rss/source/debug/RssSourceDebugAdapter.kt | 4 ++ .../ui/rss/source/manage/GroupManageDialog.kt | 18 +++++- .../ui/rss/source/manage/RssSourceAdapter.kt | 61 ++++++++++--------- .../legado/app/ui/widget/KeyboardToolPop.kt | 11 +++- .../legado/app/ui/widget/font/FontAdapter.kt | 11 +++- .../app/ui/widget/prefs/IconListPreference.kt | 8 +++ 37 files changed, 437 insertions(+), 187 deletions(-) diff --git a/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt b/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt index c8f85889b..5560c4cbd 100644 --- a/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt +++ b/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt @@ -374,6 +374,10 @@ abstract class CommonRecyclerAdapter(protected val context: Context) : itemDelegates.getValue(getItemViewType(holder.layoutPosition)) .convert(holder, it, payloads) } + if (payloads.isEmpty()) { + itemDelegates.getValue(getItemViewType(holder.layoutPosition)) + .registerListener(holder, position) + } } } diff --git a/app/src/main/java/io/legado/app/base/adapter/ItemViewDelegate.kt b/app/src/main/java/io/legado/app/base/adapter/ItemViewDelegate.kt index d5e92e236..aefe1f3d8 100644 --- a/app/src/main/java/io/legado/app/base/adapter/ItemViewDelegate.kt +++ b/app/src/main/java/io/legado/app/base/adapter/ItemViewDelegate.kt @@ -11,8 +11,15 @@ abstract class ItemViewDelegate(protected val context: Context, val layout /** * 如果使用了事件回调,回调里不要直接使用item,会出现不更新的问题, - * 使用getItem(holder.layoutPosition)来获取item + * 使用getItem(holder.layoutPosition)来获取item, + * 或者使用registerListener(holder: ItemViewHolder, position: Int) */ abstract fun convert(holder: ItemViewHolder, item: ITEM, payloads: MutableList) + + /** + * 注册事件 + */ + abstract fun registerListener(holder: ItemViewHolder, position: Int) + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/base/adapter/SimpleRecyclerAdapter.kt b/app/src/main/java/io/legado/app/base/adapter/SimpleRecyclerAdapter.kt index baf40e487..47289b477 100644 --- a/app/src/main/java/io/legado/app/base/adapter/SimpleRecyclerAdapter.kt +++ b/app/src/main/java/io/legado/app/base/adapter/SimpleRecyclerAdapter.kt @@ -15,6 +15,9 @@ abstract class SimpleRecyclerAdapter(context: Context, private val layoutI this@SimpleRecyclerAdapter.convert(holder, item, payloads) } + override fun registerListener(holder: ItemViewHolder, position: Int) { + this@SimpleRecyclerAdapter.registerListener(holder, position) + } }) } @@ -23,4 +26,9 @@ abstract class SimpleRecyclerAdapter(context: Context, private val layoutI * 使用getItem(holder.layoutPosition)来获取item */ abstract fun convert(holder: ItemViewHolder, item: ITEM, payloads: MutableList) + + /** + * 注册事件 + */ + abstract fun registerListener(holder: ItemViewHolder, position: Int) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/coroutine/CompositeCoroutine.kt b/app/src/main/java/io/legado/app/help/coroutine/CompositeCoroutine.kt index 3f76acbe6..1eb1102ba 100644 --- a/app/src/main/java/io/legado/app/help/coroutine/CompositeCoroutine.kt +++ b/app/src/main/java/io/legado/app/help/coroutine/CompositeCoroutine.kt @@ -76,7 +76,7 @@ class CompositeCoroutine : CoroutineContainer { resources = null } - set?.forEachIndexed { index, coroutine -> + set?.forEachIndexed { _, coroutine -> coroutine.cancel() } } diff --git a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt index 540084d8f..1c8457073 100644 --- a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt @@ -49,8 +49,13 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) : } tv_author.text = getGroupName(item.group) checkbox.isChecked = selectedBooks.contains(item) + } + } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.apply { checkbox.setOnCheckedChangeListener { buttonView, isChecked -> - getItem(holder.layoutPosition)?.let { + getItem(position)?.let { if (buttonView.isPressed) { if (isChecked) { selectedBooks.add(it) @@ -63,7 +68,7 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) : } } onClick { - getItem(holder.layoutPosition)?.let { + getItem(position)?.let { checkbox.isChecked = !checkbox.isChecked if (checkbox.isChecked) { selectedBooks.add(it) @@ -74,12 +79,12 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) : } } tv_delete.onClick { - getItem(holder.layoutPosition)?.let { + getItem(position)?.let { callBack.deleteBook(it) } } tv_group.onClick { - getItem(holder.layoutPosition)?.let { + getItem(position)?.let { actionItem = it callBack.selectGroup(it.group, groupRequestCode) } diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt index 33ad0aa46..3c321f14e 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt @@ -182,8 +182,14 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { override fun convert(holder: ItemViewHolder, item: BookGroup, payloads: MutableList) { holder.itemView.apply { tv_group.text = item.groupName - tv_edit.onClick { editGroup(item) } - tv_del.onClick { viewModel.delGroup(item) } + + } + } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.apply { + tv_edit.onClick { getItem(position)?.let { editGroup(it) } } + tv_del.onClick { getItem(position)?.let { viewModel.delGroup(it) } } } } diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt index ebd8b6a23..7a39980fb 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt @@ -176,16 +176,23 @@ class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { holder.itemView.apply { cb_group.text = item.groupName cb_group.isChecked = (groupId and item.groupId) > 0 + } + } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.apply { cb_group.setOnCheckedChangeListener { buttonView, isChecked -> - if (buttonView.isPressed) { - groupId = if (isChecked) { - groupId + item.groupId - } else { - groupId - item.groupId + getItem(position)?.let { + if (buttonView.isPressed) { + groupId = if (isChecked) { + groupId + it.groupId + } else { + groupId - it.groupId + } } } } - tv_edit.onClick { editGroup(item) } + tv_edit.onClick { getItem(position)?.let { editGroup(it) } } } } diff --git a/app/src/main/java/io/legado/app/ui/book/info/ChapterListAdapter.kt b/app/src/main/java/io/legado/app/ui/book/info/ChapterListAdapter.kt index bfe98f4d8..f37410765 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/ChapterListAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/ChapterListAdapter.kt @@ -13,22 +13,24 @@ import org.jetbrains.anko.textColorResource class ChapterListAdapter(context: Context, var callBack: CallBack) : SimpleRecyclerAdapter(context, R.layout.item_chapter_list) { - var reorder: Boolean = false; // 是否倒序 - override fun convert(holder: ItemViewHolder, item: BookChapter, payloads: MutableList) { holder.itemView.apply { - var _item: BookChapter = item; - if (reorder) { - _item = getItems().get(getItems().size - item.index - 1); - } - tv_chapter_name.text = _item.title - if (_item.index == callBack.durChapterIndex()) { + tv_chapter_name.text = item.title + if (item.index == callBack.durChapterIndex()) { tv_chapter_name.setTextColor(context.accentColor) } else { tv_chapter_name.textColorResource = R.color.tv_text_secondary } + + } + } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.apply { this.onClick { - callBack.openChapter(_item) + getItem(position)?.let { + callBack.openChapter(it) + } } } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt index 3e1661eb6..04abaa4a7 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt @@ -149,14 +149,20 @@ class BgTextConfigDialog : DialogFragment() { .centerCrop() .into(iv_bg) tv_name.text = item.substringBeforeLast(".") + } + } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.apply { this.onClick { - ReadBookConfig.getConfig().setBg(1, item) - ReadBookConfig.upBg() - postEvent(EventBus.UP_CONFIG, false) + getItem(position)?.let { + ReadBookConfig.getConfig().setBg(1, it) + ReadBookConfig.upBg() + postEvent(EventBus.UP_CONFIG, false) + } } } } - } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { diff --git a/app/src/main/java/io/legado/app/ui/book/search/BookAdapter.kt b/app/src/main/java/io/legado/app/ui/book/search/BookAdapter.kt index 4165e5c23..1e835d3a5 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/BookAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/BookAdapter.kt @@ -14,7 +14,16 @@ class BookAdapter(context: Context, val callBack: CallBack) : override fun convert(holder: ItemViewHolder, item: Book, payloads: MutableList) { with(holder.itemView) { text_view.text = item.name - onClick { callBack.showBookInfo(item) } + } + } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.apply { + onClick { + getItem(position)?.let { + callBack.showBookInfo(it) + } + } } } diff --git a/app/src/main/java/io/legado/app/ui/book/search/HistoryKeyAdapter.kt b/app/src/main/java/io/legado/app/ui/book/search/HistoryKeyAdapter.kt index 2be9e6409..2a27aa695 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/HistoryKeyAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/HistoryKeyAdapter.kt @@ -22,15 +22,24 @@ class HistoryKeyAdapter(activity: SearchActivity, val callBack: CallBack) : override fun convert(holder: ItemViewHolder, item: SearchKeyword, payloads: MutableList) { with(holder.itemView) { text_view.text = item.word + } + } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.apply { onClick { - callBack.searchHistory(item.word) + getItem(position)?.let { + callBack.searchHistory(it.word) + } } onLongClick { it?.let { explosionField.explode(it, true) } - GlobalScope.launch(IO) { - App.db.searchKeywordDao().delete(item) + getItem(position)?.let { + GlobalScope.launch(IO) { + App.db.searchKeywordDao().delete(it) + } } true } diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt index f5af25507..348a583ae 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt @@ -26,6 +26,16 @@ class SearchAdapter(context: Context, val callBack: CallBack) : } } + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.apply { + onClick { + getItem(position)?.let { + callBack.showBookInfo(it.name, it.author) + } + } + } + } + private fun bind(itemView: View, searchBook: SearchBook) { with(itemView) { tv_name.text = searchBook.name @@ -35,9 +45,7 @@ class SearchAdapter(context: Context, val callBack: CallBack) : tv_introduce.text = context.getString(R.string.intro_show, searchBook.intro) upKind(itemView, searchBook.getKindList()) iv_cover.load(searchBook.coverUrl, searchBook.name, searchBook.author) - onClick { - callBack.showBookInfo(searchBook.name, searchBook.author) - } + } } diff --git a/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugAdapter.kt b/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugAdapter.kt index 7fd07fbda..77e496191 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugAdapter.kt @@ -26,4 +26,8 @@ class BookSourceDebugAdapter(context: Context) : text_view.text = item } } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + //nothing + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt index 2d0fefc39..78adb2ad2 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt @@ -68,35 +68,7 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : String.format("%s (%s)", item.bookSourceName, item.bookSourceGroup) } swt_enabled.isChecked = item.enabled - swt_enabled.setOnCheckedChangeListener { view, checked -> - getItem(holder.layoutPosition)?.let { - if (view.isPressed) { - it.enabled = checked - callBack.update(it) - } - } - } cb_book_source.isChecked = selected.contains(item) - cb_book_source.setOnCheckedChangeListener { view, checked -> - getItem(holder.layoutPosition)?.let { - if (view.isPressed) { - if (checked) { - selected.add(it) - } else { - selected.remove(it) - } - callBack.upCountView() - } - } - } - iv_edit.onClick { - getItem(holder.layoutPosition)?.let { - callBack.edit(it) - } - } - iv_menu_more.onClick { - showMenu(iv_menu_more, holder.layoutPosition) - } upShowExplore(iv_explore, item) } else { payload.keySet().map { @@ -116,6 +88,39 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : } } + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.apply { + swt_enabled.setOnCheckedChangeListener { view, checked -> + getItem(position)?.let { + if (view.isPressed) { + it.enabled = checked + callBack.update(it) + } + } + } + cb_book_source.setOnCheckedChangeListener { view, checked -> + getItem(position)?.let { + if (view.isPressed) { + if (checked) { + selected.add(it) + } else { + selected.remove(it) + } + callBack.upCountView() + } + } + } + iv_edit.onClick { + getItem(position)?.let { + callBack.edit(it) + } + } + iv_menu_more.onClick { + showMenu(iv_menu_more, position) + } + } + } + private fun showMenu(view: View, position: Int) { val source = getItem(position) ?: return val popupMenu = PopupMenu(context, view) diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/GroupManageDialog.kt index 55cbe6ae0..265afced0 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/GroupManageDialog.kt @@ -125,8 +125,17 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList) { with(holder.itemView) { tv_group.text = item - tv_edit.onClick { editGroup(item) } - tv_del.onClick { viewModel.delGroup(item) } + } + } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.apply { + tv_edit.onClick { + getItem(position)?.let { + editGroup(it) + } + } + tv_del.onClick { getItem(position)?.let { viewModel.delGroup(it) } } } } } diff --git a/app/src/main/java/io/legado/app/ui/changecover/CoverAdapter.kt b/app/src/main/java/io/legado/app/ui/changecover/CoverAdapter.kt index a26c21f57..9d4864f41 100644 --- a/app/src/main/java/io/legado/app/ui/changecover/CoverAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/changecover/CoverAdapter.kt @@ -15,8 +15,15 @@ class CoverAdapter(context: Context, val callBack: CallBack) : with(holder.itemView) { iv_cover.load(item.coverUrl, item.name, item.author) tv_source.text = item.originName + } + } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.apply { onClick { - callBack.changeTo(item.coverUrl!!) + getItem(position)?.let { + callBack.changeTo(it.coverUrl ?: "") + } } } } diff --git a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceAdapter.kt b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceAdapter.kt index 4ce482e96..0f0b67f86 100644 --- a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceAdapter.kt @@ -19,7 +19,6 @@ class ChangeSourceAdapter(context: Context, val callBack: CallBack) : val bundle = payloads.getOrNull(0) as? Bundle holder.itemView.apply { if (bundle == null) { - this.onClick { callBack.changeTo(item) } tv_origin.text = item.originName tv_last.text = item.getDisplayLastChapterTitle() if (callBack.bookUrl == item.bookUrl) { @@ -38,6 +37,14 @@ class ChangeSourceAdapter(context: Context, val callBack: CallBack) : } } + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.onClick { + getItem(position)?.let { + callBack.changeTo(it) + } + } + } + interface CallBack { val bookUrl: String? fun changeTo(searchBook: SearchBook) 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 b7453a09e..da18ff7a7 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 @@ -32,9 +32,6 @@ class ChapterListAdapter(context: Context, val callback: Callback) : tv_tag.text = item.tag tv_tag.visible() } - this.onClick { - callback.openChapter(item) - } upHasCache( tv_chapter_name, cacheFileNames.contains(BookHelp.formatChapterName(item)) @@ -48,6 +45,14 @@ class ChapterListAdapter(context: Context, val callback: Callback) : } } + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.onClick { + getItem(position)?.let { + callback.openChapter(it) + } + } + } + private fun upHasCache(textView: TextView, contains: Boolean) { textView.paint.isFakeBoldText = contains } diff --git a/app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt b/app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt index 6c67d3353..7f3ddeab8 100644 --- a/app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt @@ -26,9 +26,6 @@ class DownloadAdapter(context: Context) : tv_download.text = context.getString(R.string.download_count, cs.size, item.totalChapterNum) } - tv_export.onClick { - - } } else { val cacheSize = cacheChapters[item.bookUrl]?.size ?: 0 tv_download.text = @@ -37,4 +34,9 @@ class DownloadAdapter(context: Context) : } } + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.tv_export.onClick { + + } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt b/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt index a30d966f7..7bf4ef06c 100644 --- a/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt @@ -16,8 +16,8 @@ import org.jetbrains.anko.sdk27.listeners.onClick class ExploreShowAdapter(context: Context, val callBack: CallBack) : SimpleRecyclerAdapter(context, R.layout.item_search) { - override fun convert(holder: ItemViewHolder, item: SearchBook, payloads: MutableList) = - with(holder.itemView) { + override fun convert(holder: ItemViewHolder, item: SearchBook, payloads: MutableList) { + holder.itemView.apply { tv_name.text = item.name tv_author.text = context.getString(R.string.author_show, item.author) if (item.latestChapterTitle.isNullOrEmpty()) { @@ -35,10 +35,16 @@ class ExploreShowAdapter(context: Context, val callBack: CallBack) : ll_kind.setLabels(kinds) } iv_cover.load(item.coverUrl, item.name, item.author) - onClick { - callBack.showBookInfo(item.toBook()) + } + } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.onClick { + getItem(position)?.let { + callBack.showBookInfo(it.toBook()) } } + } interface CallBack { fun showBookInfo(book: Book) diff --git a/app/src/main/java/io/legado/app/ui/filechooser/adapter/FileAdapter.kt b/app/src/main/java/io/legado/app/ui/filechooser/adapter/FileAdapter.kt index da58d501a..d40d43368 100644 --- a/app/src/main/java/io/legado/app/ui/filechooser/adapter/FileAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/filechooser/adapter/FileAdapter.kt @@ -110,9 +110,12 @@ class FileAdapter(context: Context, val callBack: CallBack) : holder.itemView.apply { image_view.setImageDrawable(item.icon) text_view.text = item.name - onClick { - callBack.onFileClick(holder.layoutPosition) - } + } + } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.onClick { + callBack.onFileClick(position) } } diff --git a/app/src/main/java/io/legado/app/ui/filechooser/adapter/PathAdapter.kt b/app/src/main/java/io/legado/app/ui/filechooser/adapter/PathAdapter.kt index d0e7ec481..1697b8a45 100644 --- a/app/src/main/java/io/legado/app/ui/filechooser/adapter/PathAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/filechooser/adapter/PathAdapter.kt @@ -50,9 +50,12 @@ class PathAdapter(context: Context, val callBack: CallBack) : holder.itemView.apply { text_view.text = item image_view.setImageDrawable(arrowIcon) - onClick { - callBack.onPathClick(holder.layoutPosition) - } + } + } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.onClick { + callBack.onPathClick(position) } } @@ -61,6 +64,6 @@ class PathAdapter(context: Context, val callBack: CallBack) : } companion object { - private val ROOT_HINT = "SD" + private const val ROOT_HINT = "SD" } } diff --git a/app/src/main/java/io/legado/app/ui/importbook/ImportBookAdapter.kt b/app/src/main/java/io/legado/app/ui/importbook/ImportBookAdapter.kt index 23e1c8e17..a3e9ccee1 100644 --- a/app/src/main/java/io/legado/app/ui/importbook/ImportBookAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/importbook/ImportBookAdapter.kt @@ -68,41 +68,55 @@ class ImportBookAdapter(context: Context, val callBack: CallBack) : override fun convert(holder: ItemViewHolder, item: DocItem, payloads: MutableList) { holder.itemView.apply { - if (item.isDir) { - iv_icon.setImageResource(R.drawable.ic_folder) - iv_icon.visible() - cb_select.invisible() - ll_brief.gone() - cb_select.isChecked = false - } else { - if (bookshelf.contains(item.uri.toString())) { - iv_icon.setImageResource(R.drawable.ic_book_has) + if (payloads.isEmpty()) { + if (item.isDir) { + iv_icon.setImageResource(R.drawable.ic_folder) iv_icon.visible() cb_select.invisible() + ll_brief.gone() + cb_select.isChecked = false } else { - iv_icon.invisible() - cb_select.visible() + if (bookshelf.contains(item.uri.toString())) { + iv_icon.setImageResource(R.drawable.ic_book_has) + iv_icon.visible() + cb_select.invisible() + } else { + iv_icon.invisible() + cb_select.visible() + } + ll_brief.visible() + tv_tag.text = item.name.substringAfterLast(".") + tv_size.text = StringUtils.toSize(item.size) + tv_date.text = AppConst.DATE_FORMAT.format(item.date) + cb_select.isChecked = selectedUris.contains(item.uri.toString()) } - ll_brief.visible() - tv_tag.text = item.name.substringAfterLast(".") - tv_size.text = StringUtils.toSize(item.size) - tv_date.text = AppConst.DATE_FORMAT.format(item.date) + tv_name.text = item.name + } else { cb_select.isChecked = selectedUris.contains(item.uri.toString()) } - tv_name.text = item.name + onClick { - if (item.isDir) { - callBack.nextDoc(DocumentFile.fromSingleUri(context, item.uri)!!) - } else if (!bookshelf.contains(item.uri.toString())) { - cb_select.isChecked = !cb_select.isChecked - if (cb_select.isChecked) { - selectedUris.add(item.uri.toString()) + + } + } + } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.onClick { + getItem(position)?.let { + if (it.isDir) { + callBack.nextDoc(DocumentFile.fromSingleUri(context, it.uri)!!) + } else if (!bookshelf.contains(it.uri.toString())) { + if (!selectedUris.contains(it.uri.toString())) { + selectedUris.add(it.uri.toString()) } else { - selectedUris.remove(item.uri.toString()) + selectedUris.remove(it.uri.toString()) } callBack.upCountView() } } + notifyItemChanged(position, true) + callBack.upCountView() } } diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterGrid.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterGrid.kt index 3752b2452..fa031ae8a 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterGrid.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterGrid.kt @@ -23,11 +23,6 @@ class BooksAdapterGrid(context: Context, private val callBack: CallBack) : tv_name.text = item.name bv_author.text = item.author iv_cover.load(item.getDisplayCover(), item.name, item.author) - onClick { callBack.open(item) } - onLongClick { - callBack.openBookInfo(item) - true - } if (item.origin != BookType.local && callBack.isUpdate(item.bookUrl)) { bv_unread.invisible() rl_loading.show() @@ -56,4 +51,20 @@ class BooksAdapterGrid(context: Context, private val callBack: CallBack) : } } + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.apply { + onClick { + getItem(position)?.let { + callBack.open(it) + } + } + + onLongClick { + getItem(position)?.let { + callBack.openBookInfo(it) + } + true + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterList.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterList.kt index 1c500a786..2b4af6b7d 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterList.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterList.kt @@ -25,11 +25,6 @@ class BooksAdapterList(context: Context, private val callBack: CallBack) : tv_read.text = item.durChapterTitle tv_last.text = item.latestChapterTitle iv_cover.load(item.getDisplayCover(), item.name, item.author) - onClick { callBack.open(item) } - onLongClick { - callBack.openBookInfo(item) - true - } if (item.origin != BookType.local && callBack.isUpdate(item.bookUrl)) { bv_unread.invisible() rl_loading.show() @@ -60,4 +55,20 @@ class BooksAdapterList(context: Context, private val callBack: CallBack) : } } + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.apply { + onClick { + getItem(position)?.let { + callBack.open(it) + } + } + + onLongClick { + getItem(position)?.let { + callBack.openBookInfo(it) + } + true + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt b/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt index 49dfcc737..a1bce6b2b 100644 --- a/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt @@ -30,18 +30,6 @@ class ExploreAdapter(context: Context, private val scope: CoroutineScope, val ca with(holder.itemView) { if (payloads.isEmpty()) { tv_name.text = item.bookSourceName - ll_title.onClick { - val oldEx = exIndex - exIndex = if (exIndex == holder.layoutPosition) -1 else holder.layoutPosition - notifyItemChanged(oldEx, false) - if (exIndex != -1) { - notifyItemChanged(holder.layoutPosition, false) - } - callBack.scrollTo(holder.layoutPosition) - } - ll_title.onLongClick { - showMenu(ll_title, holder.layoutPosition) - } } if (exIndex == holder.layoutPosition) { iv_status.setImageResource(R.drawable.ic_remove) @@ -80,6 +68,23 @@ class ExploreAdapter(context: Context, private val scope: CoroutineScope, val ca } } + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.apply { + ll_title.onClick { + val oldEx = exIndex + exIndex = if (exIndex == position) -1 else position + notifyItemChanged(oldEx, false) + if (exIndex != -1) { + notifyItemChanged(position, false) + } + callBack.scrollTo(position) + } + ll_title.onLongClick { + showMenu(ll_title, position) + } + } + } + private fun showMenu(view: View, position: Int): Boolean { val source = getItem(position) ?: return true val popupMenu = PopupMenu(context, view) diff --git a/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt b/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt index 63bec82e2..38a6b5509 100644 --- a/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt @@ -20,7 +20,14 @@ class RssAdapter(context: Context, val callBack: CallBack) : .placeholder(R.drawable.image_rss) .error(R.drawable.image_rss) .into(iv_icon) - onClick { callBack.openRss(item) } + } + } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.onClick { + getItem(position)?.let { + callBack.openRss(it) + } } } diff --git a/app/src/main/java/io/legado/app/ui/replacerule/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/replacerule/GroupManageDialog.kt index b7b836896..ca7322ba4 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/GroupManageDialog.kt @@ -125,8 +125,18 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList) { with(holder.itemView) { tv_group.text = item - tv_edit.onClick { editGroup(item) } - tv_del.onClick { viewModel.delGroup(item) } + } + } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.apply { + tv_edit.onClick { + getItem(position)?.let { + editGroup(it) + } + } + + tv_del.onClick { getItem(position)?.let { viewModel.delGroup(it) } } } } } diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt index ee95ffd5a..1c911b5f5 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt @@ -65,25 +65,7 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) : String.format("%s (%s)", item.name, item.group) } swt_enabled.isChecked = item.isEnabled - swt_enabled.onClick { - item.isEnabled = swt_enabled.isChecked - callBack.update(item) - } - iv_edit.onClick { - callBack.edit(item) - } cb_name.isChecked = selected.contains(item) - cb_name.onClick { - if (cb_name.isChecked) { - selected.add(item) - } else { - selected.remove(item) - } - callBack.upCountView() - } - iv_menu_more.onClick { - showMenu(iv_menu_more, holder.layoutPosition) - } } else { bundle.keySet().map { when (it) { @@ -102,6 +84,35 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) : } } + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.apply { + swt_enabled.setOnCheckedChangeListener { _, isChecked -> + getItem(position)?.let { + it.isEnabled = isChecked + callBack.update(it) + } + } + iv_edit.onClick { + getItem(position)?.let { + callBack.edit(it) + } + } + cb_name.onClick { + getItem(position)?.let { + if (cb_name.isChecked) { + selected.add(it) + } else { + selected.remove(it) + } + } + callBack.upCountView() + } + iv_menu_more.onClick { + showMenu(iv_menu_more, holder.layoutPosition) + } + } + } + private fun showMenu(view: View, position: Int) { val item = getItem(position) ?: return val popupMenu = PopupMenu(context, view) diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt index 1c6e144fd..b26291b02 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt @@ -25,9 +25,6 @@ class RssArticlesAdapter(context: Context, val callBack: CallBack) : with(holder.itemView) { tv_title.text = item.title tv_pub_date.text = item.pubDate - onClick { - callBack.readRss(item) - } if (item.image.isNullOrBlank()) { image_view.gone() } else { @@ -65,6 +62,14 @@ class RssArticlesAdapter(context: Context, val callBack: CallBack) : } } + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.onClick { + getItem(position)?.let { + callBack.readRss(it) + } + } + } + interface CallBack { fun readRss(rssArticle: RssArticle) } diff --git a/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesAdapter.kt b/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesAdapter.kt index 2cb607df5..8263571cf 100644 --- a/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesAdapter.kt @@ -23,9 +23,6 @@ class RssFavoritesAdapter(context: Context, val callBack: CallBack) : with(holder.itemView) { tv_title.text = item.title tv_pub_date.text = item.pubDate - onClick { - callBack.readRss(item) - } if (item.image.isNullOrBlank()) { image_view.gone() } else { @@ -58,6 +55,14 @@ class RssFavoritesAdapter(context: Context, val callBack: CallBack) : } } + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.onClick { + getItem(position)?.let { + callBack.readRss(it) + } + } + } + interface CallBack { fun readRss(rssStar: RssStar) } diff --git a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugAdapter.kt b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugAdapter.kt index a5e4eecbf..95001a035 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugAdapter.kt @@ -26,4 +26,8 @@ class RssSourceDebugAdapter(context: Context) : text_view.text = item } } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + //nothing + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/GroupManageDialog.kt index 7645bebe3..3b7500fad 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/GroupManageDialog.kt @@ -125,8 +125,22 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList) { with(holder.itemView) { tv_group.text = item - tv_edit.onClick { editGroup(item) } - tv_del.onClick { viewModel.delGroup(item) } + } + } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.apply { + tv_edit.onClick { + getItem(position)?.let { + editGroup(it) + } + } + + tv_del.onClick { + getItem(position)?.let { + viewModel.delGroup(it) + } + } } } } diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceAdapter.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceAdapter.kt index a4e2d5671..9ef43925f 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceAdapter.kt @@ -61,35 +61,7 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) : String.format("%s (%s)", item.sourceName, item.sourceGroup) } swt_enabled.isChecked = item.enabled - swt_enabled.setOnCheckedChangeListener { view, checked -> - getItem(holder.layoutPosition)?.let { - if (view.isPressed) { - it.enabled = checked - callBack.update(it) - } - } - } cb_source.isChecked = selected.contains(item) - cb_source.setOnCheckedChangeListener { view, checked -> - getItem(holder.layoutPosition)?.let { - if (view.isPressed) { - if (checked) { - selected.add(it) - } else { - selected.remove(it) - } - callBack.upCountView() - } - } - } - iv_edit.onClick { - getItem(holder.layoutPosition)?.let { - callBack.edit(it) - } - } - iv_menu_more.onClick { - showMenu(iv_menu_more, holder.layoutPosition) - } } else { when (payloads[0]) { 1 -> cb_source.isChecked = selected.contains(item) @@ -99,6 +71,39 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) : } } + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.apply { + swt_enabled.setOnCheckedChangeListener { view, checked -> + getItem(holder.layoutPosition)?.let { + if (view.isPressed) { + it.enabled = checked + callBack.update(it) + } + } + } + cb_source.setOnCheckedChangeListener { view, checked -> + getItem(holder.layoutPosition)?.let { + if (view.isPressed) { + if (checked) { + selected.add(it) + } else { + selected.remove(it) + } + callBack.upCountView() + } + } + } + iv_edit.onClick { + getItem(holder.layoutPosition)?.let { + callBack.edit(it) + } + } + iv_menu_more.onClick { + showMenu(iv_menu_more, holder.layoutPosition) + } + } + } + private fun showMenu(view: View, position: Int) { val source = getItem(position) ?: return val popupMenu = PopupMenu(context, view) diff --git a/app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt b/app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt index 40e321b87..ce47e007d 100644 --- a/app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt +++ b/app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt @@ -45,7 +45,16 @@ class KeyboardToolPop( override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList) { with(holder.itemView) { text_view.text = item - onClick { callBack?.sendText(item) } + } + } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.apply { + onClick { + getItem(position)?.let { + callBack?.sendText(it) + } + } } } } diff --git a/app/src/main/java/io/legado/app/ui/widget/font/FontAdapter.kt b/app/src/main/java/io/legado/app/ui/widget/font/FontAdapter.kt index 902a5bfae..e376277cc 100644 --- a/app/src/main/java/io/legado/app/ui/widget/font/FontAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/widget/font/FontAdapter.kt @@ -14,7 +14,7 @@ import java.io.File class FontAdapter(context: Context, val callBack: CallBack) : SimpleRecyclerAdapter(context, R.layout.item_font) { - override fun convert(holder: ItemViewHolder, item: File, payloads: MutableList) = + override fun convert(holder: ItemViewHolder, item: File, payloads: MutableList) { with(holder.itemView) { val typeface = Typeface.createFromFile(item) tv_font.typeface = typeface @@ -26,6 +26,15 @@ class FontAdapter(context: Context, val callBack: CallBack) : iv_checked.invisible() } } + } + + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.onClick { + getItem(position)?.let { + callBack.onClick(it) + } + } + } interface CallBack { fun onClick(file: File) diff --git a/app/src/main/java/io/legado/app/ui/widget/prefs/IconListPreference.kt b/app/src/main/java/io/legado/app/ui/widget/prefs/IconListPreference.kt index 5b015150e..292a02fbd 100644 --- a/app/src/main/java/io/legado/app/ui/widget/prefs/IconListPreference.kt +++ b/app/src/main/java/io/legado/app/ui/widget/prefs/IconListPreference.kt @@ -175,6 +175,14 @@ class IconListPreference(context: Context, attrs: AttributeSet) : ListPreference } } + override fun registerListener(holder: ItemViewHolder, position: Int) { + holder.itemView.onClick { + getItem(position)?.let { + onChanged?.invoke(it.toString()) + } + } + } + private fun findIndexOfValue(value: String?): Int { dialogEntryValues?.let { values -> for (i in values.indices.reversed()) {