feat: 优化代码

pull/103/head
kunfei 5 years ago
parent f1f9bb787b
commit 8f8fc54d77
  1. 4
      app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt
  2. 9
      app/src/main/java/io/legado/app/base/adapter/ItemViewDelegate.kt
  3. 8
      app/src/main/java/io/legado/app/base/adapter/SimpleRecyclerAdapter.kt
  4. 2
      app/src/main/java/io/legado/app/help/coroutine/CompositeCoroutine.kt
  5. 13
      app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt
  6. 10
      app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt
  7. 13
      app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt
  8. 20
      app/src/main/java/io/legado/app/ui/book/info/ChapterListAdapter.kt
  9. 10
      app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt
  10. 11
      app/src/main/java/io/legado/app/ui/book/search/BookAdapter.kt
  11. 13
      app/src/main/java/io/legado/app/ui/book/search/HistoryKeyAdapter.kt
  12. 14
      app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt
  13. 4
      app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugAdapter.kt
  14. 47
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt
  15. 13
      app/src/main/java/io/legado/app/ui/book/source/manage/GroupManageDialog.kt
  16. 9
      app/src/main/java/io/legado/app/ui/changecover/CoverAdapter.kt
  17. 9
      app/src/main/java/io/legado/app/ui/changesource/ChangeSourceAdapter.kt
  18. 11
      app/src/main/java/io/legado/app/ui/chapterlist/ChapterListAdapter.kt
  19. 8
      app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt
  20. 14
      app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt
  21. 7
      app/src/main/java/io/legado/app/ui/filechooser/adapter/FileAdapter.kt
  22. 9
      app/src/main/java/io/legado/app/ui/filechooser/adapter/PathAdapter.kt
  23. 28
      app/src/main/java/io/legado/app/ui/importbook/ImportBookAdapter.kt
  24. 21
      app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterGrid.kt
  25. 21
      app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterList.kt
  26. 29
      app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt
  27. 9
      app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt
  28. 14
      app/src/main/java/io/legado/app/ui/replacerule/GroupManageDialog.kt
  29. 47
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt
  30. 11
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt
  31. 11
      app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesAdapter.kt
  32. 4
      app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugAdapter.kt
  33. 18
      app/src/main/java/io/legado/app/ui/rss/source/manage/GroupManageDialog.kt
  34. 19
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceAdapter.kt
  35. 11
      app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt
  36. 11
      app/src/main/java/io/legado/app/ui/widget/font/FontAdapter.kt
  37. 8
      app/src/main/java/io/legado/app/ui/widget/prefs/IconListPreference.kt

@ -374,6 +374,10 @@ abstract class CommonRecyclerAdapter<ITEM>(protected val context: Context) :
itemDelegates.getValue(getItemViewType(holder.layoutPosition)) itemDelegates.getValue(getItemViewType(holder.layoutPosition))
.convert(holder, it, payloads) .convert(holder, it, payloads)
} }
if (payloads.isEmpty()) {
itemDelegates.getValue(getItemViewType(holder.layoutPosition))
.registerListener(holder, position)
}
} }
} }

@ -11,8 +11,15 @@ abstract class ItemViewDelegate<ITEM>(protected val context: Context, val layout
/** /**
* 如果使用了事件回调,回调里不要直接使用item,会出现不更新的问题, * 如果使用了事件回调,回调里不要直接使用item,会出现不更新的问题,
* 使用getItem(holder.layoutPosition)来获取item * 使用getItem(holder.layoutPosition)来获取item,
* 或者使用registerListener(holder: ItemViewHolder, position: Int)
*/ */
abstract fun convert(holder: ItemViewHolder, item: ITEM, payloads: MutableList<Any>) abstract fun convert(holder: ItemViewHolder, item: ITEM, payloads: MutableList<Any>)
/**
* 注册事件
*/
abstract fun registerListener(holder: ItemViewHolder, position: Int)
} }

@ -15,6 +15,9 @@ abstract class SimpleRecyclerAdapter<ITEM>(context: Context, private val layoutI
this@SimpleRecyclerAdapter.convert(holder, item, payloads) 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<ITEM>(context: Context, private val layoutI
* 使用getItem(holder.layoutPosition)来获取item * 使用getItem(holder.layoutPosition)来获取item
*/ */
abstract fun convert(holder: ItemViewHolder, item: ITEM, payloads: MutableList<Any>) abstract fun convert(holder: ItemViewHolder, item: ITEM, payloads: MutableList<Any>)
/**
* 注册事件
*/
abstract fun registerListener(holder: ItemViewHolder, position: Int)
} }

@ -76,7 +76,7 @@ class CompositeCoroutine : CoroutineContainer {
resources = null resources = null
} }
set?.forEachIndexed { index, coroutine -> set?.forEachIndexed { _, coroutine ->
coroutine.cancel() coroutine.cancel()
} }
} }

@ -49,8 +49,13 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) :
} }
tv_author.text = getGroupName(item.group) tv_author.text = getGroupName(item.group)
checkbox.isChecked = selectedBooks.contains(item) checkbox.isChecked = selectedBooks.contains(item)
}
}
override fun registerListener(holder: ItemViewHolder, position: Int) {
holder.itemView.apply {
checkbox.setOnCheckedChangeListener { buttonView, isChecked -> checkbox.setOnCheckedChangeListener { buttonView, isChecked ->
getItem(holder.layoutPosition)?.let { getItem(position)?.let {
if (buttonView.isPressed) { if (buttonView.isPressed) {
if (isChecked) { if (isChecked) {
selectedBooks.add(it) selectedBooks.add(it)
@ -63,7 +68,7 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) :
} }
} }
onClick { onClick {
getItem(holder.layoutPosition)?.let { getItem(position)?.let {
checkbox.isChecked = !checkbox.isChecked checkbox.isChecked = !checkbox.isChecked
if (checkbox.isChecked) { if (checkbox.isChecked) {
selectedBooks.add(it) selectedBooks.add(it)
@ -74,12 +79,12 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) :
} }
} }
tv_delete.onClick { tv_delete.onClick {
getItem(holder.layoutPosition)?.let { getItem(position)?.let {
callBack.deleteBook(it) callBack.deleteBook(it)
} }
} }
tv_group.onClick { tv_group.onClick {
getItem(holder.layoutPosition)?.let { getItem(position)?.let {
actionItem = it actionItem = it
callBack.selectGroup(it.group, groupRequestCode) callBack.selectGroup(it.group, groupRequestCode)
} }

@ -182,8 +182,14 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
override fun convert(holder: ItemViewHolder, item: BookGroup, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: BookGroup, payloads: MutableList<Any>) {
holder.itemView.apply { holder.itemView.apply {
tv_group.text = item.groupName 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) } }
} }
} }

@ -176,16 +176,23 @@ class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
holder.itemView.apply { holder.itemView.apply {
cb_group.text = item.groupName cb_group.text = item.groupName
cb_group.isChecked = (groupId and item.groupId) > 0 cb_group.isChecked = (groupId and item.groupId) > 0
}
}
override fun registerListener(holder: ItemViewHolder, position: Int) {
holder.itemView.apply {
cb_group.setOnCheckedChangeListener { buttonView, isChecked -> cb_group.setOnCheckedChangeListener { buttonView, isChecked ->
getItem(position)?.let {
if (buttonView.isPressed) { if (buttonView.isPressed) {
groupId = if (isChecked) { groupId = if (isChecked) {
groupId + item.groupId groupId + it.groupId
} else { } else {
groupId - item.groupId groupId - it.groupId
}
} }
} }
} }
tv_edit.onClick { editGroup(item) } tv_edit.onClick { getItem(position)?.let { editGroup(it) } }
} }
} }

@ -13,22 +13,24 @@ import org.jetbrains.anko.textColorResource
class ChapterListAdapter(context: Context, var callBack: CallBack) : class ChapterListAdapter(context: Context, var callBack: CallBack) :
SimpleRecyclerAdapter<BookChapter>(context, R.layout.item_chapter_list) { SimpleRecyclerAdapter<BookChapter>(context, R.layout.item_chapter_list) {
var reorder: Boolean = false; // 是否倒序
override fun convert(holder: ItemViewHolder, item: BookChapter, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: BookChapter, payloads: MutableList<Any>) {
holder.itemView.apply { holder.itemView.apply {
var _item: BookChapter = item; tv_chapter_name.text = item.title
if (reorder) { if (item.index == callBack.durChapterIndex()) {
_item = getItems().get(getItems().size - item.index - 1);
}
tv_chapter_name.text = _item.title
if (_item.index == callBack.durChapterIndex()) {
tv_chapter_name.setTextColor(context.accentColor) tv_chapter_name.setTextColor(context.accentColor)
} else { } else {
tv_chapter_name.textColorResource = R.color.tv_text_secondary tv_chapter_name.textColorResource = R.color.tv_text_secondary
} }
}
}
override fun registerListener(holder: ItemViewHolder, position: Int) {
holder.itemView.apply {
this.onClick { this.onClick {
callBack.openChapter(_item) getItem(position)?.let {
callBack.openChapter(it)
}
} }
} }
} }

@ -149,14 +149,20 @@ class BgTextConfigDialog : DialogFragment() {
.centerCrop() .centerCrop()
.into(iv_bg) .into(iv_bg)
tv_name.text = item.substringBeforeLast(".") tv_name.text = item.substringBeforeLast(".")
}
}
override fun registerListener(holder: ItemViewHolder, position: Int) {
holder.itemView.apply {
this.onClick { this.onClick {
ReadBookConfig.getConfig().setBg(1, item) getItem(position)?.let {
ReadBookConfig.getConfig().setBg(1, it)
ReadBookConfig.upBg() ReadBookConfig.upBg()
postEvent(EventBus.UP_CONFIG, false) postEvent(EventBus.UP_CONFIG, false)
} }
} }
} }
}
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

@ -14,7 +14,16 @@ class BookAdapter(context: Context, val callBack: CallBack) :
override fun convert(holder: ItemViewHolder, item: Book, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: Book, payloads: MutableList<Any>) {
with(holder.itemView) { with(holder.itemView) {
text_view.text = item.name 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)
}
}
} }
} }

@ -22,15 +22,24 @@ class HistoryKeyAdapter(activity: SearchActivity, val callBack: CallBack) :
override fun convert(holder: ItemViewHolder, item: SearchKeyword, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: SearchKeyword, payloads: MutableList<Any>) {
with(holder.itemView) { with(holder.itemView) {
text_view.text = item.word text_view.text = item.word
}
}
override fun registerListener(holder: ItemViewHolder, position: Int) {
holder.itemView.apply {
onClick { onClick {
callBack.searchHistory(item.word) getItem(position)?.let {
callBack.searchHistory(it.word)
}
} }
onLongClick { onLongClick {
it?.let { it?.let {
explosionField.explode(it, true) explosionField.explode(it, true)
} }
getItem(position)?.let {
GlobalScope.launch(IO) { GlobalScope.launch(IO) {
App.db.searchKeywordDao().delete(item) App.db.searchKeywordDao().delete(it)
}
} }
true true
} }

@ -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) { private fun bind(itemView: View, searchBook: SearchBook) {
with(itemView) { with(itemView) {
tv_name.text = searchBook.name 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) tv_introduce.text = context.getString(R.string.intro_show, searchBook.intro)
upKind(itemView, searchBook.getKindList()) upKind(itemView, searchBook.getKindList())
iv_cover.load(searchBook.coverUrl, searchBook.name, searchBook.author) iv_cover.load(searchBook.coverUrl, searchBook.name, searchBook.author)
onClick {
callBack.showBookInfo(searchBook.name, searchBook.author)
}
} }
} }

@ -26,4 +26,8 @@ class BookSourceDebugAdapter(context: Context) :
text_view.text = item text_view.text = item
} }
} }
override fun registerListener(holder: ItemViewHolder, position: Int) {
//nothing
}
} }

@ -68,17 +68,38 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) :
String.format("%s (%s)", item.bookSourceName, item.bookSourceGroup) String.format("%s (%s)", item.bookSourceName, item.bookSourceGroup)
} }
swt_enabled.isChecked = item.enabled swt_enabled.isChecked = item.enabled
cb_book_source.isChecked = selected.contains(item)
upShowExplore(iv_explore, item)
} else {
payload.keySet().map {
when (it) {
"selected" -> cb_book_source.isChecked = selected.contains(item)
"name", "group" -> if (item.bookSourceGroup.isNullOrEmpty()) {
cb_book_source.text = item.bookSourceName
} else {
cb_book_source.text =
String.format("%s (%s)", item.bookSourceName, item.bookSourceGroup)
}
"enabled" -> swt_enabled.isChecked = payload.getBoolean(it)
"showExplore" -> upShowExplore(iv_explore, item)
}
}
}
}
}
override fun registerListener(holder: ItemViewHolder, position: Int) {
holder.itemView.apply {
swt_enabled.setOnCheckedChangeListener { view, checked -> swt_enabled.setOnCheckedChangeListener { view, checked ->
getItem(holder.layoutPosition)?.let { getItem(position)?.let {
if (view.isPressed) { if (view.isPressed) {
it.enabled = checked it.enabled = checked
callBack.update(it) callBack.update(it)
} }
} }
} }
cb_book_source.isChecked = selected.contains(item)
cb_book_source.setOnCheckedChangeListener { view, checked -> cb_book_source.setOnCheckedChangeListener { view, checked ->
getItem(holder.layoutPosition)?.let { getItem(position)?.let {
if (view.isPressed) { if (view.isPressed) {
if (checked) { if (checked) {
selected.add(it) selected.add(it)
@ -90,28 +111,12 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) :
} }
} }
iv_edit.onClick { iv_edit.onClick {
getItem(holder.layoutPosition)?.let { getItem(position)?.let {
callBack.edit(it) callBack.edit(it)
} }
} }
iv_menu_more.onClick { iv_menu_more.onClick {
showMenu(iv_menu_more, holder.layoutPosition) showMenu(iv_menu_more, position)
}
upShowExplore(iv_explore, item)
} else {
payload.keySet().map {
when (it) {
"selected" -> cb_book_source.isChecked = selected.contains(item)
"name", "group" -> if (item.bookSourceGroup.isNullOrEmpty()) {
cb_book_source.text = item.bookSourceName
} else {
cb_book_source.text =
String.format("%s (%s)", item.bookSourceName, item.bookSourceGroup)
}
"enabled" -> swt_enabled.isChecked = payload.getBoolean(it)
"showExplore" -> upShowExplore(iv_explore, item)
}
}
} }
} }
} }

@ -125,8 +125,17 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList<Any>) {
with(holder.itemView) { with(holder.itemView) {
tv_group.text = item 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) } }
} }
} }
} }

@ -15,8 +15,15 @@ class CoverAdapter(context: Context, val callBack: CallBack) :
with(holder.itemView) { with(holder.itemView) {
iv_cover.load(item.coverUrl, item.name, item.author) iv_cover.load(item.coverUrl, item.name, item.author)
tv_source.text = item.originName tv_source.text = item.originName
}
}
override fun registerListener(holder: ItemViewHolder, position: Int) {
holder.itemView.apply {
onClick { onClick {
callBack.changeTo(item.coverUrl!!) getItem(position)?.let {
callBack.changeTo(it.coverUrl ?: "")
}
} }
} }
} }

@ -19,7 +19,6 @@ class ChangeSourceAdapter(context: Context, val callBack: CallBack) :
val bundle = payloads.getOrNull(0) as? Bundle val bundle = payloads.getOrNull(0) as? Bundle
holder.itemView.apply { holder.itemView.apply {
if (bundle == null) { if (bundle == null) {
this.onClick { callBack.changeTo(item) }
tv_origin.text = item.originName tv_origin.text = item.originName
tv_last.text = item.getDisplayLastChapterTitle() tv_last.text = item.getDisplayLastChapterTitle()
if (callBack.bookUrl == item.bookUrl) { 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 { interface CallBack {
val bookUrl: String? val bookUrl: String?
fun changeTo(searchBook: SearchBook) fun changeTo(searchBook: SearchBook)

@ -32,9 +32,6 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
tv_tag.text = item.tag tv_tag.text = item.tag
tv_tag.visible() tv_tag.visible()
} }
this.onClick {
callback.openChapter(item)
}
upHasCache( upHasCache(
tv_chapter_name, tv_chapter_name,
cacheFileNames.contains(BookHelp.formatChapterName(item)) 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) { private fun upHasCache(textView: TextView, contains: Boolean) {
textView.paint.isFakeBoldText = contains textView.paint.isFakeBoldText = contains
} }

@ -26,9 +26,6 @@ class DownloadAdapter(context: Context) :
tv_download.text = tv_download.text =
context.getString(R.string.download_count, cs.size, item.totalChapterNum) context.getString(R.string.download_count, cs.size, item.totalChapterNum)
} }
tv_export.onClick {
}
} else { } else {
val cacheSize = cacheChapters[item.bookUrl]?.size ?: 0 val cacheSize = cacheChapters[item.bookUrl]?.size ?: 0
tv_download.text = tv_download.text =
@ -37,4 +34,9 @@ class DownloadAdapter(context: Context) :
} }
} }
override fun registerListener(holder: ItemViewHolder, position: Int) {
holder.itemView.tv_export.onClick {
}
}
} }

@ -16,8 +16,8 @@ import org.jetbrains.anko.sdk27.listeners.onClick
class ExploreShowAdapter(context: Context, val callBack: CallBack) : class ExploreShowAdapter(context: Context, val callBack: CallBack) :
SimpleRecyclerAdapter<SearchBook>(context, R.layout.item_search) { SimpleRecyclerAdapter<SearchBook>(context, R.layout.item_search) {
override fun convert(holder: ItemViewHolder, item: SearchBook, payloads: MutableList<Any>) = override fun convert(holder: ItemViewHolder, item: SearchBook, payloads: MutableList<Any>) {
with(holder.itemView) { holder.itemView.apply {
tv_name.text = item.name tv_name.text = item.name
tv_author.text = context.getString(R.string.author_show, item.author) tv_author.text = context.getString(R.string.author_show, item.author)
if (item.latestChapterTitle.isNullOrEmpty()) { if (item.latestChapterTitle.isNullOrEmpty()) {
@ -35,8 +35,14 @@ class ExploreShowAdapter(context: Context, val callBack: CallBack) :
ll_kind.setLabels(kinds) ll_kind.setLabels(kinds)
} }
iv_cover.load(item.coverUrl, item.name, item.author) 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())
}
} }
} }

@ -110,10 +110,13 @@ class FileAdapter(context: Context, val callBack: CallBack) :
holder.itemView.apply { holder.itemView.apply {
image_view.setImageDrawable(item.icon) image_view.setImageDrawable(item.icon)
text_view.text = item.name text_view.text = item.name
onClick {
callBack.onFileClick(holder.layoutPosition)
} }
} }
override fun registerListener(holder: ItemViewHolder, position: Int) {
holder.itemView.onClick {
callBack.onFileClick(position)
}
} }
interface CallBack { interface CallBack {

@ -50,10 +50,13 @@ class PathAdapter(context: Context, val callBack: CallBack) :
holder.itemView.apply { holder.itemView.apply {
text_view.text = item text_view.text = item
image_view.setImageDrawable(arrowIcon) image_view.setImageDrawable(arrowIcon)
onClick {
callBack.onPathClick(holder.layoutPosition)
} }
} }
override fun registerListener(holder: ItemViewHolder, position: Int) {
holder.itemView.onClick {
callBack.onPathClick(position)
}
} }
interface CallBack { interface CallBack {
@ -61,6 +64,6 @@ class PathAdapter(context: Context, val callBack: CallBack) :
} }
companion object { companion object {
private val ROOT_HINT = "SD" private const val ROOT_HINT = "SD"
} }
} }

@ -68,6 +68,7 @@ class ImportBookAdapter(context: Context, val callBack: CallBack) :
override fun convert(holder: ItemViewHolder, item: DocItem, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: DocItem, payloads: MutableList<Any>) {
holder.itemView.apply { holder.itemView.apply {
if (payloads.isEmpty()) {
if (item.isDir) { if (item.isDir) {
iv_icon.setImageResource(R.drawable.ic_folder) iv_icon.setImageResource(R.drawable.ic_folder)
iv_icon.visible() iv_icon.visible()
@ -90,19 +91,32 @@ class ImportBookAdapter(context: Context, val callBack: CallBack) :
cb_select.isChecked = selectedUris.contains(item.uri.toString()) cb_select.isChecked = selectedUris.contains(item.uri.toString())
} }
tv_name.text = item.name tv_name.text = item.name
} else {
cb_select.isChecked = selectedUris.contains(item.uri.toString())
}
onClick { 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 { } else {
selectedUris.remove(item.uri.toString()) selectedUris.remove(it.uri.toString())
} }
callBack.upCountView() callBack.upCountView()
} }
} }
notifyItemChanged(position, true)
callBack.upCountView()
} }
} }

@ -23,11 +23,6 @@ class BooksAdapterGrid(context: Context, private val callBack: CallBack) :
tv_name.text = item.name tv_name.text = item.name
bv_author.text = item.author bv_author.text = item.author
iv_cover.load(item.getDisplayCover(), item.name, 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)) { if (item.origin != BookType.local && callBack.isUpdate(item.bookUrl)) {
bv_unread.invisible() bv_unread.invisible()
rl_loading.show() 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
}
}
}
} }

@ -25,11 +25,6 @@ class BooksAdapterList(context: Context, private val callBack: CallBack) :
tv_read.text = item.durChapterTitle tv_read.text = item.durChapterTitle
tv_last.text = item.latestChapterTitle tv_last.text = item.latestChapterTitle
iv_cover.load(item.getDisplayCover(), item.name, 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)) { if (item.origin != BookType.local && callBack.isUpdate(item.bookUrl)) {
bv_unread.invisible() bv_unread.invisible()
rl_loading.show() 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
}
}
}
} }

@ -30,18 +30,6 @@ class ExploreAdapter(context: Context, private val scope: CoroutineScope, val ca
with(holder.itemView) { with(holder.itemView) {
if (payloads.isEmpty()) { if (payloads.isEmpty()) {
tv_name.text = item.bookSourceName 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) { if (exIndex == holder.layoutPosition) {
iv_status.setImageResource(R.drawable.ic_remove) 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 { private fun showMenu(view: View, position: Int): Boolean {
val source = getItem(position) ?: return true val source = getItem(position) ?: return true
val popupMenu = PopupMenu(context, view) val popupMenu = PopupMenu(context, view)

@ -20,7 +20,14 @@ class RssAdapter(context: Context, val callBack: CallBack) :
.placeholder(R.drawable.image_rss) .placeholder(R.drawable.image_rss)
.error(R.drawable.image_rss) .error(R.drawable.image_rss)
.into(iv_icon) .into(iv_icon)
onClick { callBack.openRss(item) } }
}
override fun registerListener(holder: ItemViewHolder, position: Int) {
holder.itemView.onClick {
getItem(position)?.let {
callBack.openRss(it)
}
} }
} }

@ -125,8 +125,18 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList<Any>) {
with(holder.itemView) { with(holder.itemView) {
tv_group.text = item 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) } }
} }
} }
} }

@ -65,25 +65,7 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
String.format("%s (%s)", item.name, item.group) String.format("%s (%s)", item.name, item.group)
} }
swt_enabled.isChecked = item.isEnabled 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.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 { } else {
bundle.keySet().map { bundle.keySet().map {
when (it) { 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) { private fun showMenu(view: View, position: Int) {
val item = getItem(position) ?: return val item = getItem(position) ?: return
val popupMenu = PopupMenu(context, view) val popupMenu = PopupMenu(context, view)

@ -25,9 +25,6 @@ class RssArticlesAdapter(context: Context, val callBack: CallBack) :
with(holder.itemView) { with(holder.itemView) {
tv_title.text = item.title tv_title.text = item.title
tv_pub_date.text = item.pubDate tv_pub_date.text = item.pubDate
onClick {
callBack.readRss(item)
}
if (item.image.isNullOrBlank()) { if (item.image.isNullOrBlank()) {
image_view.gone() image_view.gone()
} else { } 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 { interface CallBack {
fun readRss(rssArticle: RssArticle) fun readRss(rssArticle: RssArticle)
} }

@ -23,9 +23,6 @@ class RssFavoritesAdapter(context: Context, val callBack: CallBack) :
with(holder.itemView) { with(holder.itemView) {
tv_title.text = item.title tv_title.text = item.title
tv_pub_date.text = item.pubDate tv_pub_date.text = item.pubDate
onClick {
callBack.readRss(item)
}
if (item.image.isNullOrBlank()) { if (item.image.isNullOrBlank()) {
image_view.gone() image_view.gone()
} else { } 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 { interface CallBack {
fun readRss(rssStar: RssStar) fun readRss(rssStar: RssStar)
} }

@ -26,4 +26,8 @@ class RssSourceDebugAdapter(context: Context) :
text_view.text = item text_view.text = item
} }
} }
override fun registerListener(holder: ItemViewHolder, position: Int) {
//nothing
}
} }

@ -125,8 +125,22 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList<Any>) {
with(holder.itemView) { with(holder.itemView) {
tv_group.text = item 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)
}
}
} }
} }
} }

@ -61,6 +61,18 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) :
String.format("%s (%s)", item.sourceName, item.sourceGroup) String.format("%s (%s)", item.sourceName, item.sourceGroup)
} }
swt_enabled.isChecked = item.enabled swt_enabled.isChecked = item.enabled
cb_source.isChecked = selected.contains(item)
} else {
when (payloads[0]) {
1 -> cb_source.isChecked = selected.contains(item)
2 -> swt_enabled.isChecked = item.enabled
}
}
}
}
override fun registerListener(holder: ItemViewHolder, position: Int) {
holder.itemView.apply {
swt_enabled.setOnCheckedChangeListener { view, checked -> swt_enabled.setOnCheckedChangeListener { view, checked ->
getItem(holder.layoutPosition)?.let { getItem(holder.layoutPosition)?.let {
if (view.isPressed) { if (view.isPressed) {
@ -69,7 +81,6 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) :
} }
} }
} }
cb_source.isChecked = selected.contains(item)
cb_source.setOnCheckedChangeListener { view, checked -> cb_source.setOnCheckedChangeListener { view, checked ->
getItem(holder.layoutPosition)?.let { getItem(holder.layoutPosition)?.let {
if (view.isPressed) { if (view.isPressed) {
@ -90,12 +101,6 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) :
iv_menu_more.onClick { iv_menu_more.onClick {
showMenu(iv_menu_more, holder.layoutPosition) showMenu(iv_menu_more, holder.layoutPosition)
} }
} else {
when (payloads[0]) {
1 -> cb_source.isChecked = selected.contains(item)
2 -> swt_enabled.isChecked = item.enabled
}
}
} }
} }

@ -45,7 +45,16 @@ class KeyboardToolPop(
override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList<Any>) {
with(holder.itemView) { with(holder.itemView) {
text_view.text = item 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)
}
}
} }
} }
} }

@ -14,7 +14,7 @@ import java.io.File
class FontAdapter(context: Context, val callBack: CallBack) : class FontAdapter(context: Context, val callBack: CallBack) :
SimpleRecyclerAdapter<File>(context, R.layout.item_font) { SimpleRecyclerAdapter<File>(context, R.layout.item_font) {
override fun convert(holder: ItemViewHolder, item: File, payloads: MutableList<Any>) = override fun convert(holder: ItemViewHolder, item: File, payloads: MutableList<Any>) {
with(holder.itemView) { with(holder.itemView) {
val typeface = Typeface.createFromFile(item) val typeface = Typeface.createFromFile(item)
tv_font.typeface = typeface tv_font.typeface = typeface
@ -26,6 +26,15 @@ class FontAdapter(context: Context, val callBack: CallBack) :
iv_checked.invisible() iv_checked.invisible()
} }
} }
}
override fun registerListener(holder: ItemViewHolder, position: Int) {
holder.itemView.onClick {
getItem(position)?.let {
callBack.onClick(it)
}
}
}
interface CallBack { interface CallBack {
fun onClick(file: File) fun onClick(file: File)

@ -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 { private fun findIndexOfValue(value: String?): Int {
dialogEntryValues?.let { values -> dialogEntryValues?.let { values ->
for (i in values.indices.reversed()) { for (i in values.indices.reversed()) {

Loading…
Cancel
Save