Skip to content

Commit

Permalink
Merge branch 'release/v4.1.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
mikepenz committed Dec 23, 2019
2 parents e2a84f7 + b7c64f9 commit 179871e
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 23 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ You can try it out here [Google Play](https://play.google.com/store/apps/details
# Include in your project
## Latest releases

- Kotlin | [v4.1.1](https://github.com/mikepenz/FastAdapter/tree/v4.1.1)
- Kotlin | [v4.1.2](https://github.com/mikepenz/FastAdapter/tree/v4.1.2)
- Java && AndroidX | [v3.3.1](https://github.com/mikepenz/FastAdapter/tree/v3.3.1)
- Java && AppCompat | [v3.2.9](https://github.com/mikepenz/FastAdapter/tree/v3.2.9)

Expand Down Expand Up @@ -165,20 +165,22 @@ For further details scroll down to the `ExpandableItems` (under advanced usage)
### 3. Click listener
```kotlin
fastAdapter.onClickListener = { view, adapter, item, position ->
// Handle click here
// Handle click here
false
}
}
```

### 4. Click listeners for views inside your item
```kotlin
//just add an `EventHook` to your `FastAdapter` by implementing either a `ClickEventHook`, `LongClickEventHook`, `TouchEventHook`, `CustomEventHook`
// just add an `EventHook` to your `FastAdapter` by implementing either a `ClickEventHook`, `LongClickEventHook`, `TouchEventHook`, `CustomEventHook`
fastAdapter.addEventHook(object : ClickEventHook<SimpleImageItem>() {
override fun onBind(viewHolder: RecyclerView.ViewHolder): View? {
//return the views on which you want to bind this event
return if (viewHolder is SimpleImageItem.ViewHolder) {
viewHolder.viewWhichReactsOnClick
} else null
} else {
null
}
}

override fun onClick(v: View, position: Int, fastAdapter: FastAdapter<SimpleImageItem>, item: SimpleImageItem) {
Expand All @@ -193,8 +195,8 @@ fastAdapter.addEventHook(object : ClickEventHook<SimpleImageItem>() {
// Call this in onQueryTextSubmit() & onQueryTextChange() when using SearchView
itemAdapter.filter("yourSearchTerm")
itemAdapter.itemFilter.filterPredicate = { item: SimpleItem, constraint: CharSequence? ->
item.name?.text.toString().toLowerCase().contains(constraint.toString().toLowerCase())
}
item.name?.text.toString().contains(constraint.toString(), ignoreCase = true)
}
```
`filter()` should return true for items to be retained and false for items to be removed.

Expand All @@ -216,7 +218,7 @@ Implement `ItemTouchCallback` interface in your Activity, and override the `item
override fun itemTouchOnMove(oldPosition: Int, newPosition: Int): Boolean {
DragDropUtil.onMove(fastItemAdapter.itemAdapter, oldPosition, newPosition) // change position
return true
}
}
```

### 7. Using different ViewHolders (like HeaderView)
Expand Down Expand Up @@ -256,7 +258,7 @@ recyclerView.addOnScrollListener(object : EndlessRecyclerOnScrollListener(footer
footerAdapter.clear()
footerAdapter.add(ProgressItem())

// Load your items here and add it to FastAdapter
// Load your items here and add it to FastAdapter
itemAdapter.add(NEWITEMS)
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PagedActivity : AppCompatActivity() {
}).build()

//create our ItemAdapter which will host our items
mItemAdapter = PagedModelAdapter<DemoEntity, SimpleImageItem>(asyncDifferConfig) {
mItemAdapter = PagedModelAdapter<DemoEntity, SimpleImageItem>(asyncDifferConfig, { arg: Int -> SimpleImageItem().setPlaceholder() }) {
SimpleImageItem().withName(it.data1 ?: "").withDescription(it.data2 ?: "").apply {
identifier = it.identifier.toLong()
isSelectable = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class SimpleImageItem : AbstractItem<SimpleImageItem.ViewHolder>() {
private var mImageUrl: String? = null
private var mName: String? = null
private var mDescription: String? = null
private var isPlaceholder: Boolean = false // True when used as placeholderInterceptor by PagedModelAdapter

/**
* defines the type defining this item. must be unique. preferably an id
Expand Down Expand Up @@ -57,6 +58,11 @@ class SimpleImageItem : AbstractItem<SimpleImageItem.ViewHolder>() {
return this
}

fun setPlaceholder(): SimpleImageItem {
this.isPlaceholder = true
return this
}

/**
* binds the data of this item onto the viewHolder
*
Expand All @@ -68,6 +74,11 @@ class SimpleImageItem : AbstractItem<SimpleImageItem.ViewHolder>() {
//get the context
val ctx = holder.itemView.context

if (isPlaceholder) { // Nothing to display at all; may also display placeholder resources
holder.view.visibility = View.GONE
return
} else holder.view.visibility = View.VISIBLE

//define our data for the view
holder.imageName.text = mName
holder.imageDescription.text = mDescription
Expand Down
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
buildscript {
ext {
release = [
versionName: "4.1.1",
versionCode: 4101
versionName: "4.1.2",
versionCode: 4102
]

setup = [
Expand All @@ -16,12 +16,12 @@ buildscript {
versions = [
androidX : '1.0.0',
recyclerView : '1.1.0-beta05',
material : '1.1.0-beta02',
material : '1.1.0-rc01',
appcompat : '1.1.0',
drawerlayout : '1.1.0-alpha03',
constraintLayout: '2.0.0-beta3',
cardview : '1.0.0',
kotlin : '1.3.50',
kotlin : '1.3.61',
materialize : '1.2.1',
iconics : '4.0.1',
materialdrawer : '7.0.0-rc08',
Expand All @@ -42,7 +42,7 @@ buildscript {
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.1'
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ interface IItemList<Item : GenericItem> {

fun addAll(position: Int, items: List<Item>, preItemCount: Int)

operator fun get(position: Int): Item
operator fun get(position: Int): Item?
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ open class ModelAdapter<Model, Item : GenericItem>(
* @return the item inside this adapter
*/
override fun getAdapterItem(position: Int): Item {
return itemList[position]
return itemList[position] ?: throw java.lang.RuntimeException("A normal ModelAdapter does not allow null items.")
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mikepenz.fastadapter.paged

import android.util.Log
import androidx.paging.AsyncPagedListDiffer
import androidx.paging.PagedList
import androidx.recyclerview.widget.AsyncDifferConfig
Expand All @@ -16,9 +17,9 @@ import com.mikepenz.fastadapter.utils.DefaultItemList
open class PagedItemListImpl<Model, Item : GenericItem> @JvmOverloads constructor(
listUpdateCallback: ListUpdateCallback,
differConfig: AsyncDifferConfig<Model>,
var placeholderInterceptor: (position: Int) -> Item = getDefaultPlaceholderInterceptor(),
var interceptor: (element: Model) -> Item?
) : DefaultItemList<Item>() {

val differ: AsyncPagedListDiffer<Model> = AsyncPagedListDiffer<Model>(listUpdateCallback, differConfig)

var idDistributor: IIdDistributor<Item> = IIdDistributor.DEFAULT as IIdDistributor<Item>
Expand All @@ -37,7 +38,10 @@ open class PagedItemListImpl<Model, Item : GenericItem> @JvmOverloads constructo
get() = differ.currentList?.isEmpty() == true

override fun get(position: Int): Item {
return differ.getItem(position)?.let { getItem(it) } ?: throw RuntimeException("No item found at position")
return differ.getItem(position)?.let { getItem(it) } ?: run {
Log.w(TAG, "Position currently contains a placeholder")
placeholderInterceptor.invoke(position)
}
}

private fun getItem(model: Model): Item? {
Expand Down Expand Up @@ -194,4 +198,17 @@ open class PagedItemListImpl<Model, Item : GenericItem> @JvmOverloads constructo
fun addPagedListListener(listener: AsyncPagedListDiffer.PagedListListener<Model>) {
differ.addPagedListListener(listener)
}

companion object {
private const val TAG = "PagedItemListImpl"

/**
* Returns the default placeholder interceptor
*
* Note if your PagedItemList should contain placeholder, you have to provide a logic what the adapter should show while in placeholding state
*/
internal fun <Item : GenericItem> getDefaultPlaceholderInterceptor(): (Int) -> Item {
return { throw RuntimeException("No item found at position") }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.recyclerview.widget.AsyncDifferConfig
import androidx.recyclerview.widget.ListUpdateCallback
import com.mikepenz.fastadapter.*
import com.mikepenz.fastadapter.dsl.FastAdapterDsl
import com.mikepenz.fastadapter.paged.PagedItemListImpl.Companion.getDefaultPlaceholderInterceptor
import com.mikepenz.fastadapter.utils.DefaultItemList

// Notify user that the DSL is currently experimental
Expand All @@ -25,9 +26,10 @@ typealias GenericPagedModelAdapter<Model> = PagedModelAdapter<Model, GenericItem
@FastAdapterDsl
open class PagedModelAdapter<Model, Item : GenericItem>(
asyncDifferConfig: AsyncDifferConfig<Model>,
placeholderInterceptor: (position: Int) -> Item = getDefaultPlaceholderInterceptor(),
var interceptor: (element: Model) -> Item?
) : AbstractAdapter<Item>(), IItemAdapter<Model, Item>, ListUpdateCallback {
val itemList: PagedItemListImpl<Model, Item> = PagedItemListImpl(this, asyncDifferConfig, interceptor)
val itemList: PagedItemListImpl<Model, Item> = PagedItemListImpl(this, asyncDifferConfig, placeholderInterceptor, interceptor)

override var idDistributor: IIdDistributor<Item> = IIdDistributor.DEFAULT as IIdDistributor<Item>

Expand Down Expand Up @@ -298,7 +300,7 @@ open class PagedModelAdapter<Model, Item : GenericItem>(
*
* @param pagedList The new list to be displayed.
*/
fun submitList(pagedList: PagedList<Model>) {
fun submitList(pagedList: PagedList<Model>?) {
itemList.submitList(pagedList)
}

Expand Down Expand Up @@ -358,8 +360,8 @@ open class PagedModelAdapter<Model, Item : GenericItem>(
* @return a new ItemAdapter
*/
@JvmStatic
fun <Model, Item : GenericItem> models(asyncDifferConfig: AsyncDifferConfig<Model>, interceptor: (element: Model) -> Item?): PagedModelAdapter<Model, Item> {
return PagedModelAdapter(asyncDifferConfig, interceptor)
fun <Model, Item : GenericItem> models(asyncDifferConfig: AsyncDifferConfig<Model>, placeholderInterceptor: (position: Int) -> Item, interceptor: (element: Model) -> Item?): PagedModelAdapter<Model, Item> {
return PagedModelAdapter(asyncDifferConfig, placeholderInterceptor, interceptor)
}
}
}

0 comments on commit 179871e

Please sign in to comment.