-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rakhimulin Damir
committed
Feb 5, 2021
1 parent
940449b
commit e2004c4
Showing
10 changed files
with
148 additions
and
127 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
rekukler/src/main/java/com/rerekt/rekukler/RecyclerViewConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.rerekt.rekukler | ||
|
||
import android.content.Context | ||
import androidx.recyclerview.widget.ItemTouchHelper | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import androidx.recyclerview.widget.RecyclerView | ||
|
||
class RecyclerViewConfig( | ||
internal val context: Context | ||
) { | ||
|
||
internal var itemDecorations: MutableList<RecyclerView.ItemDecoration> = mutableListOf() | ||
internal var layoutManager: RecyclerView.LayoutManager = LinearLayoutManager(context) | ||
internal var itemTouchHelper: ItemTouchHelper? = null | ||
|
||
} |
125 changes: 0 additions & 125 deletions
125
rekukler/src/main/java/com/rerekt/rekukler/RecyclerViewDSL.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
rekukler/src/main/java/com/rerekt/rekukler/dsl/ItemDecoration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.rerekt.rekukler.dsl | ||
|
||
import android.graphics.Bitmap | ||
import androidx.core.graphics.drawable.toDrawable | ||
import androidx.recyclerview.widget.DividerItemDecoration | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.rerekt.rekukler.RecyclerViewConfig | ||
|
||
fun RecyclerViewConfig.dividerItemDecoration( | ||
orientation: Int = DividerItemDecoration.VERTICAL, | ||
block: DividerItemDecoration.() -> Unit | ||
) { | ||
itemDecoration( | ||
DividerItemDecoration(context, orientation).apply(block) | ||
) | ||
} | ||
|
||
fun RecyclerViewConfig.dividerItemDecoration( | ||
size: Int, | ||
orientation: Int = DividerItemDecoration.VERTICAL | ||
) { | ||
itemDecoration( | ||
DividerItemDecoration(context, orientation).apply { | ||
setDrawable( | ||
Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888) | ||
.toDrawable(context.resources) | ||
) | ||
} | ||
) | ||
} | ||
|
||
fun RecyclerViewConfig.itemDecoration(decoration: RecyclerView.ItemDecoration) { | ||
itemDecorations.add(decoration) | ||
} |
46 changes: 46 additions & 0 deletions
46
rekukler/src/main/java/com/rerekt/rekukler/dsl/ItemTouchHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.rerekt.rekukler.dsl | ||
|
||
import android.graphics.Canvas | ||
import androidx.recyclerview.widget.ItemTouchHelper | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.rerekt.rekukler.MultiBindingAdapter | ||
import com.rerekt.rekukler.RecyclerViewConfig | ||
import com.rerekt.rekukler.utils.clip | ||
|
||
fun RecyclerViewConfig.itemTouchHelper( | ||
dragFlags: Int = ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.RIGHT or ItemTouchHelper.LEFT, | ||
swipeFlags: Int = 0, | ||
onSwiped: (RecyclerView.ViewHolder, direction: Int) -> Unit = { _, _ -> }, | ||
onClearView: () -> Unit = {}, | ||
isCanBeOutOfBounds: Boolean = false, | ||
onMove: ((RecyclerView.ViewHolder, RecyclerView.ViewHolder) -> Boolean)? = null | ||
) = ItemTouchHelper( | ||
object : ItemTouchHelper.Callback() { | ||
override fun getMovementFlags( | ||
recyclerView: RecyclerView, | ||
viewHolder: RecyclerView.ViewHolder | ||
): Int = makeMovementFlags(dragFlags, swipeFlags) | ||
override fun onSwiped( | ||
viewHolder: RecyclerView.ViewHolder, | ||
direction: Int | ||
) = onSwiped.invoke(viewHolder, direction) | ||
override fun clearView( | ||
recyclerView: RecyclerView, | ||
viewHolder: RecyclerView.ViewHolder | ||
) = onClearView.invoke() | ||
override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) { | ||
if (isCanBeOutOfBounds) { | ||
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive) | ||
} else { | ||
val clippedDx = clip(recyclerView.width, viewHolder.itemView.left, viewHolder.itemView.right, dX) | ||
val clippedDy = clip(recyclerView.height, viewHolder.itemView.top, viewHolder.itemView.bottom, dY) | ||
super.onChildDraw(c, recyclerView, viewHolder, clippedDx, clippedDy, actionState, isCurrentlyActive) | ||
} | ||
} | ||
override fun onMove( | ||
recyclerView: RecyclerView, | ||
viewHolder: RecyclerView.ViewHolder, | ||
target: RecyclerView.ViewHolder | ||
): Boolean = onMove?.invoke(viewHolder, target) ?: (recyclerView.adapter as? MultiBindingAdapter)?.moveItem(viewHolder.adapterPosition, target.adapterPosition) ?: false | ||
} | ||
).apply { itemTouchHelper = this } |
23 changes: 23 additions & 0 deletions
23
rekukler/src/main/java/com/rerekt/rekukler/dsl/LayoutManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.rerekt.rekukler.dsl | ||
|
||
import androidx.recyclerview.widget.GridLayoutManager | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import androidx.recyclerview.widget.StaggeredGridLayoutManager | ||
import com.rerekt.rekukler.RecyclerViewConfig | ||
|
||
fun RecyclerViewConfig.linearLayout( | ||
block: LinearLayoutManager.() -> Unit = {} | ||
) { layoutManager = LinearLayoutManager(context).apply(block) } | ||
|
||
fun RecyclerViewConfig.gridLayout( | ||
spansCount: Int = 1, | ||
block: GridLayoutManager.() -> Unit = {} | ||
) { layoutManager = GridLayoutManager(context, spansCount).apply(block) } | ||
|
||
fun RecyclerViewConfig.staggeredGridLayout( | ||
spansCount: Int = 1, | ||
orientation: Int = StaggeredGridLayoutManager.VERTICAL, | ||
block: StaggeredGridLayoutManager.() -> Unit = {} | ||
) { | ||
layoutManager = StaggeredGridLayoutManager(spansCount, orientation).apply(block) | ||
} |
21 changes: 21 additions & 0 deletions
21
rekukler/src/main/java/com/rerekt/rekukler/dsl/RecyclerViewDSL.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.rerekt.rekukler.dsl | ||
|
||
import androidx.recyclerview.widget.* | ||
import com.rerekt.rekukler.MultiBindingAdapter | ||
import com.rerekt.rekukler.RecyclerViewConfig | ||
|
||
fun RecyclerView.configure( | ||
adapter: MultiBindingAdapter, | ||
block: RecyclerViewConfig.() -> Unit = {} | ||
) { | ||
RecyclerViewConfig(context).also { | ||
block(it) | ||
layoutManager = it.layoutManager | ||
this.adapter = adapter | ||
it.itemDecorations.forEach { addItemDecoration(it) } | ||
it.itemTouchHelper?.attachToRecyclerView(this) | ||
} | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun <T> RecyclerView.getItems() = (adapter as? MultiBindingAdapter)?.items as? List<T> |
2 changes: 1 addition & 1 deletion
2
...t/rekukler/MarginDividerItemDecoration.kt → ...kler/utils/MarginDividerItemDecoration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...java/com/rerekt/rekukler/RecyclerUtils.kt → ...om/rerekt/rekukler/utils/RekuklerUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters