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. 19
      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. 14
      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. 15
      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. 61
      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. 9
      app/src/main/java/io/legado/app/ui/filechooser/adapter/FileAdapter.kt
  22. 11
      app/src/main/java/io/legado/app/ui/filechooser/adapter/PathAdapter.kt
  23. 60
      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. 61
      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))
.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,会出现不更新的问题,
* 使用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 registerListener(holder: ItemViewHolder, position: Int)
}

@ -15,6 +15,9 @@ abstract class SimpleRecyclerAdapter<ITEM>(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<ITEM>(context: Context, private val layoutI
* 使用getItem(holder.layoutPosition)来获取item
*/
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
}
set?.forEachIndexed { index, coroutine ->
set?.forEachIndexed { _, coroutine ->
coroutine.cancel()
}
}

@ -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)
}

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

@ -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) } }
}
}

@ -13,22 +13,24 @@ import org.jetbrains.anko.textColorResource
class ChapterListAdapter(context: Context, var callBack: CallBack) :
SimpleRecyclerAdapter<BookChapter>(context, R.layout.item_chapter_list) {
var reorder: Boolean = false; // 是否倒序
override fun convert(holder: ItemViewHolder, item: BookChapter, payloads: MutableList<Any>) {
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)
}
}
}
}

@ -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?) {

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

@ -22,15 +22,24 @@ class HistoryKeyAdapter(activity: SearchActivity, val callBack: CallBack) :
override fun convert(holder: ItemViewHolder, item: SearchKeyword, payloads: MutableList<Any>) {
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
}

@ -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)
}
}
}

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

@ -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)

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

@ -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 ?: "")
}
}
}
}

@ -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)

@ -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
}

@ -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 {
}
}
}

@ -16,8 +16,8 @@ import org.jetbrains.anko.sdk27.listeners.onClick
class ExploreShowAdapter(context: Context, val callBack: CallBack) :
SimpleRecyclerAdapter<SearchBook>(context, R.layout.item_search) {
override fun convert(holder: ItemViewHolder, item: SearchBook, payloads: MutableList<Any>) =
with(holder.itemView) {
override fun convert(holder: ItemViewHolder, item: SearchBook, payloads: MutableList<Any>) {
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)

@ -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)
}
}

@ -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"
}
}

@ -68,41 +68,55 @@ class ImportBookAdapter(context: Context, val callBack: CallBack) :
override fun convert(holder: ItemViewHolder, item: DocItem, payloads: MutableList<Any>) {
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()
}
}

@ -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
}
}
}
}

@ -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
}
}
}
}

@ -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)

@ -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)
}
}
}

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

@ -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)

@ -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)
}

@ -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)
}

@ -26,4 +26,8 @@ class RssSourceDebugAdapter(context: Context) :
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>) {
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)
}
}
}
}
}

@ -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)

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

@ -14,7 +14,7 @@ import java.io.File
class FontAdapter(context: Context, val callBack: CallBack) :
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) {
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)

@ -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()) {

Loading…
Cancel
Save