From ecb780bb36d80d5e9d98a22a35d915028fa1d454 Mon Sep 17 00:00:00 2001 From: HissPirat Date: Fri, 26 Jun 2020 14:46:14 +0200 Subject: [PATCH] #2154 generified adapter --- .../core/page/adapter/OnItemClickListener.kt | 26 +++++++++++++++++++ .../core/page/{ => adapter}/Page.kt | 2 +- .../core/page/adapter/PageAdapter.kt | 21 +++++++++++++++ .../core/page/bookmark/BookmarksActivity.kt | 9 ++++--- .../page/bookmark/adapter/BookmarkDelegate.kt | 5 ++-- .../page/bookmark/adapter/BookmarkItem.kt | 2 +- .../bookmark/adapter/BookmarkViewHolder.kt | 2 +- .../page/bookmark/adapter/BookmarksAdapter.kt | 4 --- .../core/page/history/HistoryActivity.kt | 11 ++++---- .../page/history/adapter/HistoryAdapter.kt | 7 ----- .../page/history/adapter/HistoryDelegate.kt | 2 +- .../page/history/adapter/HistoryListItem.kt | 2 +- .../adapter/HistoryListItemViewHolder.kt | 6 ++--- .../kiwixmobile/core/page/viewmodel/Action.kt | 2 +- .../core/page/viewmodel/PageState.kt | 2 +- .../core/page/viewmodel/effects/OpenPage.kt | 2 +- .../kiwixmobile/core/page/PageTestHelpers.kt | 1 + 17 files changed, 72 insertions(+), 34 deletions(-) create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/page/adapter/OnItemClickListener.kt rename core/src/main/java/org/kiwix/kiwixmobile/core/page/{ => adapter}/Page.kt (94%) create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/page/adapter/PageAdapter.kt diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/adapter/OnItemClickListener.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/adapter/OnItemClickListener.kt new file mode 100644 index 000000000..f9a582d15 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/adapter/OnItemClickListener.kt @@ -0,0 +1,26 @@ +/* + * Kiwix Android + * Copyright (c) 2020 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package org.kiwix.kiwixmobile.core.page.adapter + +interface OnItemClickListener { + + fun onItemClick(page: Page) + + fun onItemLongClick(page: Page): Boolean +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/Page.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/adapter/Page.kt similarity index 94% rename from core/src/main/java/org/kiwix/kiwixmobile/core/page/Page.kt rename to core/src/main/java/org/kiwix/kiwixmobile/core/page/adapter/Page.kt index 4e674fff3..f02675f62 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/Page.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/adapter/Page.kt @@ -16,7 +16,7 @@ * */ -package org.kiwix.kiwixmobile.core.page +package org.kiwix.kiwixmobile.core.page.adapter interface Page { val zimFilePath: String? diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/adapter/PageAdapter.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/adapter/PageAdapter.kt new file mode 100644 index 000000000..e76581f63 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/adapter/PageAdapter.kt @@ -0,0 +1,21 @@ +/* + * Kiwix Android + * Copyright (c) 2020 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package org.kiwix.kiwixmobile.core.page.adapter + +class PageAdapter diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksActivity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksActivity.kt index 92c0d1708..0b3273fb6 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksActivity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksActivity.kt @@ -20,10 +20,11 @@ import org.kiwix.kiwixmobile.core.base.BaseActivity import org.kiwix.kiwixmobile.core.di.components.CoreComponent import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.coreActivityComponent import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.viewModel +import org.kiwix.kiwixmobile.core.page.adapter.OnItemClickListener +import org.kiwix.kiwixmobile.core.page.adapter.Page import org.kiwix.kiwixmobile.core.page.bookmark.adapter.BookmarkDelegate.BookmarkItemDelegate import org.kiwix.kiwixmobile.core.page.bookmark.adapter.BookmarkItem import org.kiwix.kiwixmobile.core.page.bookmark.adapter.BookmarksAdapter -import org.kiwix.kiwixmobile.core.page.bookmark.adapter.BookmarksAdapter.OnItemClickListener import org.kiwix.kiwixmobile.core.page.bookmark.viewmodel.BookmarkState import org.kiwix.kiwixmobile.core.page.bookmark.viewmodel.BookmarkViewModel import org.kiwix.kiwixmobile.core.page.viewmodel.Action @@ -112,7 +113,7 @@ class BookmarksActivity : OnItemClickListener, BaseActivity() { return super.onOptionsItemSelected(item) } - fun render(state: BookmarkState) { + private fun render(state: BookmarkState) { val filteredBookmarks = state.filteredBookmarks filteredBookmarks.let { bookmarksAdapter.items = it } toggleNoBookmarksText(filteredBookmarks) @@ -140,10 +141,10 @@ class BookmarksActivity : OnItemClickListener, BaseActivity() { } } - override fun onItemClick(bookmark: BookmarkItem) { + override fun onItemClick(bookmark: Page) { bookmarkViewModel.actions.offer(OnItemClick(bookmark)) } - override fun onItemLongClick(bookmark: BookmarkItem): Boolean = + override fun onItemLongClick(bookmark: Page): Boolean = bookmarkViewModel.actions.offer(OnItemLongClick(bookmark)) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/adapter/BookmarkDelegate.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/adapter/BookmarkDelegate.kt index 8185b5b4e..306051078 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/adapter/BookmarkDelegate.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/adapter/BookmarkDelegate.kt @@ -3,8 +3,8 @@ package org.kiwix.kiwixmobile.core.page.bookmark.adapter import android.view.ViewGroup import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.base.adapter.AbsDelegateAdapter -import org.kiwix.kiwixmobile.core.page.bookmark.adapter.BookmarksAdapter.OnItemClickListener import org.kiwix.kiwixmobile.core.extensions.ViewGroupExtensions.inflate +import org.kiwix.kiwixmobile.core.page.adapter.OnItemClickListener sealed class BookmarkDelegate : AbsDelegateAdapter { @@ -16,6 +16,7 @@ sealed class BookmarkDelegate : override fun createViewHolder(parent: ViewGroup) = BookmarkViewHolder( - parent.inflate(R.layout.item_bookmark_history, false), itemClickListener) + parent.inflate(R.layout.item_bookmark_history, false), itemClickListener + ) } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/adapter/BookmarkItem.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/adapter/BookmarkItem.kt index b946398d1..a3865382e 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/adapter/BookmarkItem.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/adapter/BookmarkItem.kt @@ -19,7 +19,7 @@ package org.kiwix.kiwixmobile.core.page.bookmark.adapter import org.kiwix.kiwixmobile.core.dao.entities.BookmarkEntity -import org.kiwix.kiwixmobile.core.page.Page +import org.kiwix.kiwixmobile.core.page.adapter.Page import org.kiwix.kiwixmobile.core.reader.ZimFileReader data class BookmarkItem( diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/adapter/BookmarkViewHolder.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/adapter/BookmarkViewHolder.kt index 020528d3a..b39387da8 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/adapter/BookmarkViewHolder.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/adapter/BookmarkViewHolder.kt @@ -5,10 +5,10 @@ import kotlinx.android.synthetic.main.item_bookmark_history.favicon import kotlinx.android.synthetic.main.item_bookmark_history.title import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.base.adapter.BaseViewHolder -import org.kiwix.kiwixmobile.core.page.bookmark.adapter.BookmarksAdapter.OnItemClickListener import org.kiwix.kiwixmobile.core.downloader.model.Base64String import org.kiwix.kiwixmobile.core.extensions.setBitmap import org.kiwix.kiwixmobile.core.extensions.setImageDrawableCompat +import org.kiwix.kiwixmobile.core.page.adapter.OnItemClickListener class BookmarkViewHolder( override val containerView: View, diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/adapter/BookmarksAdapter.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/adapter/BookmarksAdapter.kt index 1105b29f4..cf0835c80 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/adapter/BookmarksAdapter.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/adapter/BookmarksAdapter.kt @@ -7,8 +7,4 @@ class BookmarksAdapter( vararg delegates: AdapterDelegate ) : BaseDelegateAdapter(*delegates) { override fun getIdFor(item: BookmarkItem): Long = item.databaseId - interface OnItemClickListener { - fun onItemClick(bookmark: BookmarkItem) - fun onItemLongClick(bookmark: BookmarkItem): Boolean - } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryActivity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryActivity.kt index 404e45937..c8cd5e8b1 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryActivity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryActivity.kt @@ -5,7 +5,6 @@ import android.view.Menu import android.view.MenuItem import android.view.View.GONE import android.view.View.VISIBLE -import android.widget.ImageView import androidx.appcompat.view.ActionMode import androidx.appcompat.widget.SearchView import androidx.lifecycle.Observer @@ -22,11 +21,11 @@ import org.kiwix.kiwixmobile.core.base.BaseActivity import org.kiwix.kiwixmobile.core.di.components.CoreComponent import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.coreActivityComponent import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.viewModel +import org.kiwix.kiwixmobile.core.page.adapter.OnItemClickListener +import org.kiwix.kiwixmobile.core.page.adapter.Page import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryAdapter -import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryAdapter.OnItemClickListener import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryDelegate.HistoryDateDelegate import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryDelegate.HistoryItemDelegate -import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem import org.kiwix.kiwixmobile.core.page.history.viewmodel.HistoryState import org.kiwix.kiwixmobile.core.page.history.viewmodel.HistoryViewModel import org.kiwix.kiwixmobile.core.page.viewmodel.Action @@ -105,7 +104,7 @@ class HistoryActivity : OnItemClickListener, BaseActivity() { return true } - fun render(state: HistoryState) { + private fun render(state: HistoryState) { historyAdapter.items = state.historyListItems history_switch.isEnabled = !state.isInSelectionState no_history.visibility = if (state.historyListItems.isEmpty()) VISIBLE else GONE @@ -133,10 +132,10 @@ class HistoryActivity : OnItemClickListener, BaseActivity() { return super.onOptionsItemSelected(item) } - override fun onItemClick(favicon: ImageView, history: HistoryItem) { + override fun onItemClick(history: Page) { historyViewModel.actions.offer(OnItemClick(history)) } - override fun onItemLongClick(favicon: ImageView, history: HistoryItem): Boolean = + override fun onItemLongClick(history: Page): Boolean = historyViewModel.actions.offer(OnItemLongClick(history)) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryAdapter.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryAdapter.kt index d92a09fbf..aa74710a0 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryAdapter.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryAdapter.kt @@ -1,17 +1,10 @@ package org.kiwix.kiwixmobile.core.page.history.adapter -import android.widget.ImageView import org.kiwix.kiwixmobile.core.base.adapter.AdapterDelegate import org.kiwix.kiwixmobile.core.base.adapter.BaseDelegateAdapter -import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem class HistoryAdapter( vararg delegates: AdapterDelegate ) : BaseDelegateAdapter(*delegates) { override fun getIdFor(item: HistoryListItem): Long = item.id - interface OnItemClickListener { - fun onItemClick(favicon: ImageView, history: HistoryItem) - - fun onItemLongClick(favicon: ImageView, history: HistoryItem): Boolean - } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryDelegate.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryDelegate.kt index 60203bf50..9b10c8d89 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryDelegate.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryDelegate.kt @@ -4,7 +4,7 @@ import android.view.ViewGroup import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.base.adapter.AbsDelegateAdapter import org.kiwix.kiwixmobile.core.extensions.ViewGroupExtensions.inflate -import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryAdapter.OnItemClickListener +import org.kiwix.kiwixmobile.core.page.adapter.OnItemClickListener import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItemViewHolder.HistoryItemViewHolder import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.DateItem import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryListItem.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryListItem.kt index 8ef029fc4..03dac447a 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryListItem.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryListItem.kt @@ -18,7 +18,7 @@ package org.kiwix.kiwixmobile.core.page.history.adapter import org.kiwix.kiwixmobile.core.dao.entities.HistoryEntity -import org.kiwix.kiwixmobile.core.page.Page +import org.kiwix.kiwixmobile.core.page.adapter.Page import org.kiwix.kiwixmobile.core.reader.ZimFileReader sealed class HistoryListItem { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryListItemViewHolder.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryListItemViewHolder.kt index 910856bb0..1b26047d2 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryListItemViewHolder.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/adapter/HistoryListItemViewHolder.kt @@ -9,7 +9,7 @@ import org.kiwix.kiwixmobile.core.base.adapter.BaseViewHolder import org.kiwix.kiwixmobile.core.downloader.model.Base64String import org.kiwix.kiwixmobile.core.extensions.setBitmap import org.kiwix.kiwixmobile.core.extensions.setImageDrawableCompat -import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryAdapter.OnItemClickListener +import org.kiwix.kiwixmobile.core.page.adapter.OnItemClickListener import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.DateItem import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem import org.threeten.bp.LocalDate @@ -30,8 +30,8 @@ sealed class HistoryListItemViewHolder(containerView: Vi } else { favicon.setBitmap(Base64String(item.favicon)) } - itemView.setOnClickListener { itemClickListener.onItemClick(favicon, item) } - itemView.setOnLongClickListener { itemClickListener.onItemLongClick(favicon, item) } + itemView.setOnClickListener { itemClickListener.onItemClick(item) } + itemView.setOnLongClickListener { itemClickListener.onItemLongClick(item) } } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/Action.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/Action.kt index e0defd335..428d02275 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/Action.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/Action.kt @@ -1,6 +1,6 @@ package org.kiwix.kiwixmobile.core.page.viewmodel -import org.kiwix.kiwixmobile.core.page.Page +import org.kiwix.kiwixmobile.core.page.adapter.Page sealed class Action { object Exit : Action() diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageState.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageState.kt index 713221158..23ed0ea04 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageState.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageState.kt @@ -18,7 +18,7 @@ package org.kiwix.kiwixmobile.core.page.viewmodel -import org.kiwix.kiwixmobile.core.page.Page +import org.kiwix.kiwixmobile.core.page.adapter.Page interface PageState { val pageItems: List diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenPage.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenPage.kt index 51e374aab..7a3988276 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenPage.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenPage.kt @@ -22,7 +22,7 @@ import android.app.Activity import android.content.Intent import androidx.appcompat.app.AppCompatActivity import org.kiwix.kiwixmobile.core.base.SideEffect -import org.kiwix.kiwixmobile.core.page.Page +import org.kiwix.kiwixmobile.core.page.adapter.Page import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer import org.kiwix.kiwixmobile.core.utils.EXTRA_CHOSE_X_FILE import org.kiwix.kiwixmobile.core.utils.EXTRA_CHOSE_X_URL diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/page/PageTestHelpers.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/page/PageTestHelpers.kt index 1d4097d2c..96617da5c 100644 --- a/core/src/test/java/org/kiwix/kiwixmobile/core/page/PageTestHelpers.kt +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/page/PageTestHelpers.kt @@ -18,6 +18,7 @@ package org.kiwix.kiwixmobile.core.page +import org.kiwix.kiwixmobile.core.page.adapter.Page import org.kiwix.kiwixmobile.core.page.bookmark.adapter.BookmarkItem import org.kiwix.kiwixmobile.core.page.bookmark.viewmodel.BookmarkState import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem