#2154 generified adapter

This commit is contained in:
HissPirat 2020-06-26 14:46:14 +02:00
parent 453b6fe67e
commit ecb780bb36
17 changed files with 72 additions and 34 deletions

View File

@ -0,0 +1,26 @@
/*
* Kiwix Android
* Copyright (c) 2020 Kiwix <android.kiwix.org>
* 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 <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.core.page.adapter
interface OnItemClickListener {
fun onItemClick(page: Page)
fun onItemLongClick(page: Page): Boolean
}

View File

@ -16,7 +16,7 @@
* *
*/ */
package org.kiwix.kiwixmobile.core.page package org.kiwix.kiwixmobile.core.page.adapter
interface Page { interface Page {
val zimFilePath: String? val zimFilePath: String?

View File

@ -0,0 +1,21 @@
/*
* Kiwix Android
* Copyright (c) 2020 Kiwix <android.kiwix.org>
* 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 <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.core.page.adapter
class PageAdapter

View File

@ -20,10 +20,11 @@ import org.kiwix.kiwixmobile.core.base.BaseActivity
import org.kiwix.kiwixmobile.core.di.components.CoreComponent import org.kiwix.kiwixmobile.core.di.components.CoreComponent
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.coreActivityComponent import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.coreActivityComponent
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.viewModel 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.BookmarkDelegate.BookmarkItemDelegate
import org.kiwix.kiwixmobile.core.page.bookmark.adapter.BookmarkItem 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
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.BookmarkState
import org.kiwix.kiwixmobile.core.page.bookmark.viewmodel.BookmarkViewModel import org.kiwix.kiwixmobile.core.page.bookmark.viewmodel.BookmarkViewModel
import org.kiwix.kiwixmobile.core.page.viewmodel.Action import org.kiwix.kiwixmobile.core.page.viewmodel.Action
@ -112,7 +113,7 @@ class BookmarksActivity : OnItemClickListener, BaseActivity() {
return super.onOptionsItemSelected(item) return super.onOptionsItemSelected(item)
} }
fun render(state: BookmarkState) { private fun render(state: BookmarkState) {
val filteredBookmarks = state.filteredBookmarks val filteredBookmarks = state.filteredBookmarks
filteredBookmarks.let { bookmarksAdapter.items = it } filteredBookmarks.let { bookmarksAdapter.items = it }
toggleNoBookmarksText(filteredBookmarks) 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)) bookmarkViewModel.actions.offer(OnItemClick(bookmark))
} }
override fun onItemLongClick(bookmark: BookmarkItem): Boolean = override fun onItemLongClick(bookmark: Page): Boolean =
bookmarkViewModel.actions.offer(OnItemLongClick(bookmark)) bookmarkViewModel.actions.offer(OnItemLongClick(bookmark))
} }

View File

@ -3,8 +3,8 @@ package org.kiwix.kiwixmobile.core.page.bookmark.adapter
import android.view.ViewGroup import android.view.ViewGroup
import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.base.adapter.AbsDelegateAdapter 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.extensions.ViewGroupExtensions.inflate
import org.kiwix.kiwixmobile.core.page.adapter.OnItemClickListener
sealed class BookmarkDelegate : sealed class BookmarkDelegate :
AbsDelegateAdapter<BookmarkItem, BookmarkItem, BookmarkViewHolder> { AbsDelegateAdapter<BookmarkItem, BookmarkItem, BookmarkViewHolder> {
@ -16,6 +16,7 @@ sealed class BookmarkDelegate :
override fun createViewHolder(parent: ViewGroup) = override fun createViewHolder(parent: ViewGroup) =
BookmarkViewHolder( BookmarkViewHolder(
parent.inflate(R.layout.item_bookmark_history, false), itemClickListener) parent.inflate(R.layout.item_bookmark_history, false), itemClickListener
)
} }
} }

View File

@ -19,7 +19,7 @@
package org.kiwix.kiwixmobile.core.page.bookmark.adapter package org.kiwix.kiwixmobile.core.page.bookmark.adapter
import org.kiwix.kiwixmobile.core.dao.entities.BookmarkEntity 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 import org.kiwix.kiwixmobile.core.reader.ZimFileReader
data class BookmarkItem( data class BookmarkItem(

View File

@ -5,10 +5,10 @@ import kotlinx.android.synthetic.main.item_bookmark_history.favicon
import kotlinx.android.synthetic.main.item_bookmark_history.title import kotlinx.android.synthetic.main.item_bookmark_history.title
import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.base.adapter.BaseViewHolder 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.downloader.model.Base64String
import org.kiwix.kiwixmobile.core.extensions.setBitmap import org.kiwix.kiwixmobile.core.extensions.setBitmap
import org.kiwix.kiwixmobile.core.extensions.setImageDrawableCompat import org.kiwix.kiwixmobile.core.extensions.setImageDrawableCompat
import org.kiwix.kiwixmobile.core.page.adapter.OnItemClickListener
class BookmarkViewHolder( class BookmarkViewHolder(
override val containerView: View, override val containerView: View,

View File

@ -7,8 +7,4 @@ class BookmarksAdapter(
vararg delegates: AdapterDelegate<BookmarkItem> vararg delegates: AdapterDelegate<BookmarkItem>
) : BaseDelegateAdapter<BookmarkItem>(*delegates) { ) : BaseDelegateAdapter<BookmarkItem>(*delegates) {
override fun getIdFor(item: BookmarkItem): Long = item.databaseId override fun getIdFor(item: BookmarkItem): Long = item.databaseId
interface OnItemClickListener {
fun onItemClick(bookmark: BookmarkItem)
fun onItemLongClick(bookmark: BookmarkItem): Boolean
}
} }

View File

@ -5,7 +5,6 @@ import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View.GONE import android.view.View.GONE
import android.view.View.VISIBLE import android.view.View.VISIBLE
import android.widget.ImageView
import androidx.appcompat.view.ActionMode import androidx.appcompat.view.ActionMode
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.lifecycle.Observer 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.di.components.CoreComponent
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.coreActivityComponent import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.coreActivityComponent
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.viewModel 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
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.HistoryDateDelegate
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryDelegate.HistoryItemDelegate 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.HistoryState
import org.kiwix.kiwixmobile.core.page.history.viewmodel.HistoryViewModel import org.kiwix.kiwixmobile.core.page.history.viewmodel.HistoryViewModel
import org.kiwix.kiwixmobile.core.page.viewmodel.Action import org.kiwix.kiwixmobile.core.page.viewmodel.Action
@ -105,7 +104,7 @@ class HistoryActivity : OnItemClickListener, BaseActivity() {
return true return true
} }
fun render(state: HistoryState) { private fun render(state: HistoryState) {
historyAdapter.items = state.historyListItems historyAdapter.items = state.historyListItems
history_switch.isEnabled = !state.isInSelectionState history_switch.isEnabled = !state.isInSelectionState
no_history.visibility = if (state.historyListItems.isEmpty()) VISIBLE else GONE no_history.visibility = if (state.historyListItems.isEmpty()) VISIBLE else GONE
@ -133,10 +132,10 @@ class HistoryActivity : OnItemClickListener, BaseActivity() {
return super.onOptionsItemSelected(item) return super.onOptionsItemSelected(item)
} }
override fun onItemClick(favicon: ImageView, history: HistoryItem) { override fun onItemClick(history: Page) {
historyViewModel.actions.offer(OnItemClick(history)) historyViewModel.actions.offer(OnItemClick(history))
} }
override fun onItemLongClick(favicon: ImageView, history: HistoryItem): Boolean = override fun onItemLongClick(history: Page): Boolean =
historyViewModel.actions.offer(OnItemLongClick(history)) historyViewModel.actions.offer(OnItemLongClick(history))
} }

View File

@ -1,17 +1,10 @@
package org.kiwix.kiwixmobile.core.page.history.adapter 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.AdapterDelegate
import org.kiwix.kiwixmobile.core.base.adapter.BaseDelegateAdapter import org.kiwix.kiwixmobile.core.base.adapter.BaseDelegateAdapter
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem
class HistoryAdapter( class HistoryAdapter(
vararg delegates: AdapterDelegate<HistoryListItem> vararg delegates: AdapterDelegate<HistoryListItem>
) : BaseDelegateAdapter<HistoryListItem>(*delegates) { ) : BaseDelegateAdapter<HistoryListItem>(*delegates) {
override fun getIdFor(item: HistoryListItem): Long = item.id override fun getIdFor(item: HistoryListItem): Long = item.id
interface OnItemClickListener {
fun onItemClick(favicon: ImageView, history: HistoryItem)
fun onItemLongClick(favicon: ImageView, history: HistoryItem): Boolean
}
} }

View File

@ -4,7 +4,7 @@ import android.view.ViewGroup
import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.base.adapter.AbsDelegateAdapter import org.kiwix.kiwixmobile.core.base.adapter.AbsDelegateAdapter
import org.kiwix.kiwixmobile.core.extensions.ViewGroupExtensions.inflate 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.HistoryListItemViewHolder.HistoryItemViewHolder
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.DateItem import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.DateItem
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem

View File

@ -18,7 +18,7 @@
package org.kiwix.kiwixmobile.core.page.history.adapter package org.kiwix.kiwixmobile.core.page.history.adapter
import org.kiwix.kiwixmobile.core.dao.entities.HistoryEntity 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 import org.kiwix.kiwixmobile.core.reader.ZimFileReader
sealed class HistoryListItem { sealed class HistoryListItem {

View File

@ -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.downloader.model.Base64String
import org.kiwix.kiwixmobile.core.extensions.setBitmap import org.kiwix.kiwixmobile.core.extensions.setBitmap
import org.kiwix.kiwixmobile.core.extensions.setImageDrawableCompat 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.DateItem
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
@ -30,8 +30,8 @@ sealed class HistoryListItemViewHolder<in T : HistoryListItem>(containerView: Vi
} else { } else {
favicon.setBitmap(Base64String(item.favicon)) favicon.setBitmap(Base64String(item.favicon))
} }
itemView.setOnClickListener { itemClickListener.onItemClick(favicon, item) } itemView.setOnClickListener { itemClickListener.onItemClick(item) }
itemView.setOnLongClickListener { itemClickListener.onItemLongClick(favicon, item) } itemView.setOnLongClickListener { itemClickListener.onItemLongClick(item) }
} }
} }

View File

@ -1,6 +1,6 @@
package org.kiwix.kiwixmobile.core.page.viewmodel 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 { sealed class Action {
object Exit : Action() object Exit : Action()

View File

@ -18,7 +18,7 @@
package org.kiwix.kiwixmobile.core.page.viewmodel package org.kiwix.kiwixmobile.core.page.viewmodel
import org.kiwix.kiwixmobile.core.page.Page import org.kiwix.kiwixmobile.core.page.adapter.Page
interface PageState { interface PageState {
val pageItems: List<Page> val pageItems: List<Page>

View File

@ -22,7 +22,7 @@ import android.app.Activity
import android.content.Intent import android.content.Intent
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import org.kiwix.kiwixmobile.core.base.SideEffect 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.reader.ZimReaderContainer
import org.kiwix.kiwixmobile.core.utils.EXTRA_CHOSE_X_FILE import org.kiwix.kiwixmobile.core.utils.EXTRA_CHOSE_X_FILE
import org.kiwix.kiwixmobile.core.utils.EXTRA_CHOSE_X_URL import org.kiwix.kiwixmobile.core.utils.EXTRA_CHOSE_X_URL

View File

@ -18,6 +18,7 @@
package org.kiwix.kiwixmobile.core.page 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.adapter.BookmarkItem
import org.kiwix.kiwixmobile.core.page.bookmark.viewmodel.BookmarkState import org.kiwix.kiwixmobile.core.page.bookmark.viewmodel.BookmarkState
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem