Skip to content

Commit

Permalink
Refactoring. New structure of lib.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakhimulin Damir committed Feb 5, 2021
1 parent 940449b commit e2004c4
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 127 deletions.
16 changes: 16 additions & 0 deletions rekukler/src/main/java/com/rerekt/rekukler/RecyclerViewConfig.kt
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 rekukler/src/main/java/com/rerekt/rekukler/RecyclerViewDSL.kt

This file was deleted.

1 change: 1 addition & 0 deletions rekukler/src/main/java/com/rerekt/rekukler/ViewBinder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Holder<Type: Any, Binding: ViewBinding>(
fun getString(@StringRes resId: Int): String = itemView.context.getString(resId)
fun getString(@StringRes resId: Int, vararg formatArgs: Any): String = itemView.context.getString(resId, *formatArgs)
fun getDrawable(@DrawableRes resId: Int): Drawable? = ContextCompat.getDrawable(itemView.context, resId)
fun getColor(@ColorRes resId: Int): Int = itemView.context.getColor(resId)
fun getColorStateList(@ColorRes resId: Int): ColorStateList? = ContextCompat.getColorStateList(itemView.context, resId)
fun <V: View> findViewById(@IdRes resId: Int): V = itemView.findViewById(resId)
fun setOnClickListener(l: (View) -> Unit) {
Expand Down
34 changes: 34 additions & 0 deletions rekukler/src/main/java/com/rerekt/rekukler/dsl/ItemDecoration.kt
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 rekukler/src/main/java/com/rerekt/rekukler/dsl/ItemTouchHelper.kt
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 rekukler/src/main/java/com/rerekt/rekukler/dsl/LayoutManager.kt
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 rekukler/src/main/java/com/rerekt/rekukler/dsl/RecyclerViewDSL.kt
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>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rerekt.rekukler
package com.rerekt.rekukler.utils

import android.graphics.*
import androidx.recyclerview.widget.RecyclerView
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rerekt.rekukler
package com.rerekt.rekukler.utils

//Function for clip the borders
internal fun clip(size: Int, start: Int, end: Int, delta: Float): Float {
Expand Down
5 changes: 5 additions & 0 deletions sample/src/main/java/com/rerekt/sample/ui/ListFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import android.view.View
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import com.rerekt.rekukler.*
import com.rerekt.rekukler.dsl.configure
import com.rerekt.rekukler.dsl.itemDecoration
import com.rerekt.rekukler.dsl.itemTouchHelper
import com.rerekt.rekukler.dsl.linearLayout
import com.rerekt.rekukler.utils.MarginDividerItemDecoration
import com.rerekt.sample.R
import com.rerekt.sample.databinding.FragmentMainBinding
import com.rerekt.sample.ui.global.dip
Expand Down

0 comments on commit e2004c4

Please sign in to comment.